Skip to content

Fix #7748: Don't allow case classes to be defined in universal traits #7772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions tests/neg/i7748.scala
Original file line number Diff line number Diff line change
@@ -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
}
}