diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index b795ab296467..50b973983c83 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -1810,6 +1810,13 @@ class Typer extends Namer // check value class constraints checkDerivedValueClass(cls, body1) + val effectiveOwner = cls.owner.skipWeakOwner + if !cls.isRefinementClass + && !cls.isAllOf(PrivateLocal) + && effectiveOwner.is(Trait) + && !effectiveOwner.derivesFrom(defn.ObjectClass) + ctx.error(i"$cls cannot be defined in universal $effectiveOwner", cdef.sourcePos) + // Temporarily set the typed class def as root tree so that we have at least some // information in the IDE in case we never reach `SetRootTree`. if (ctx.mode.is(Mode.Interactive) && ctx.settings.YretainTrees.value) diff --git a/tests/neg/i7748.scala b/tests/neg/i7748.scala new file mode 100644 index 000000000000..60574fbf6182 --- /dev/null +++ b/tests/neg/i7748.scala @@ -0,0 +1,11 @@ +trait A extends Any { + case class B() // error + val x = { + case class C() // error + 1 + } + def f = { + case class C() // ok + 1 + } +} \ No newline at end of file