Skip to content

Fix #2473: Check self for inner class redefinitions #2553

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
May 29, 2017
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
18 changes: 16 additions & 2 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1472,12 +1472,26 @@ object messages {
|"""
}

case class CannotHaveSameNameAs(sym: Symbol, cls: Symbol)(implicit ctx: Context)
case class CannotHaveSameNameAs(sym: Symbol, cls: Symbol, reason: CannotHaveSameNameAs.Reason)(implicit ctx: Context)
extends Message(CannotHaveSameNameAsID) {
val msg = hl"""$sym cannot have the same name as ${cls.showLocated} -- class definitions cannot be overridden"""
import CannotHaveSameNameAs._
def reasonMessage: String = reason match {
case CannotBeOverridden => "class definitions cannot be overridden"
case DefinedInSelf(self) =>
s"""cannot define ${sym.showKind} member with the same name as a ${cls.showKind} member in self reference ${self.name}.
|(Note: this can be resolved by using another name)
|""".stripMargin
}

val msg = hl"""$sym cannot have the same name as ${cls.showLocated} -- """ + reasonMessage
val kind = "Syntax"
val explanation = ""
}
object CannotHaveSameNameAs {
sealed trait Reason
case object CannotBeOverridden extends Reason
case class DefinedInSelf(self: tpd.ValDef) extends Reason
}

case class ValueClassesMayNotDefineInner(valueClass: Symbol, inner: Symbol)(implicit ctx: Context)
extends Message(ValueClassesMayNotDefineInnerID) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ object Checking {
checkNoConflict(Abstract, Override)
if (sym.isType && !sym.is(Deferred))
for (cls <- sym.allOverriddenSymbols.filter(_.isClass)) {
fail(CannotHaveSameNameAs(sym, cls))
fail(CannotHaveSameNameAs(sym, cls, CannotHaveSameNameAs.CannotBeOverridden))
sym.setFlag(Private) // break the overriding relationship by making sym Private
}
}
Expand Down
20 changes: 19 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1316,12 +1316,30 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
result
}

/** Checks if one of the decls is a type with the same name as class type member in selfType */
def classExistsOnSelf(decls: Scope, self: tpd.ValDef): Boolean = {
val selfType = self.tpt.tpe
if (!selfType.exists || (selfType.classSymbol eq cls)) false
else {
def memberInSelfButNotThis(decl: Symbol) =
selfType.member(decl.name).symbol.filter(other => other.isClass && other.owner != cls)
decls.iterator.filter(_.isType).foldLeft(false) { (foundRedef, decl) =>
val other = memberInSelfButNotThis(decl)
if (other.exists) {
val msg = CannotHaveSameNameAs(decl, other, CannotHaveSameNameAs.DefinedInSelf(self))
ctx.error(msg, decl.pos)
}
foundRedef || other.exists
}
}
}

completeAnnotations(cdef, cls)
val constr1 = typed(constr).asInstanceOf[DefDef]
val parentsWithClass = ensureFirstIsClass(parents mapconserve typedParent, cdef.namePos)
val parents1 = ensureConstrCall(cls, parentsWithClass)(superCtx)
val self1 = typed(self)(ctx.outer).asInstanceOf[ValDef] // outer context where class members are not visible
if (self1.tpt.tpe.isError) {
if (self1.tpt.tpe.isError || classExistsOnSelf(cls.unforcedDecls, self1)) {
// fail fast to avoid typing the body with an error type
cdef.withType(UnspecifiedErrorType)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ class ErrorMessagesTests extends ErrorMessagesTest {
.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val CannotHaveSameNameAs(symbol, cls) :: Nil = messages
val CannotHaveSameNameAs(symbol, cls, _) :: Nil = messages
assertEquals("class A", symbol.show)
assertEquals("class A", cls.show)
}
Expand Down
7 changes: 7 additions & 0 deletions tests/neg/i2473.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
trait Foo { trait Inner }
trait Bar { foo: Foo =>
type Inner <: foo.Inner // error
}
trait Baz { baz: Foo =>
class Inner // error
}