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 1 commit
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
17 changes: 16 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,27 @@ 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, selfType: Type): Boolean = {
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)
ctx.error(CannotHaveSameNameAs(decl, other), 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.tpt.tpe)) {
// fail fast to avoid typing the body with an error type
cdef.withType(UnspecifiedErrorType)
} else {
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: type Inner cannot have the same name as trait Inner in trait Foo -- class definitions cannot be overridden
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's not an override, it's a shadowing, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes. I reused the old error message. I will improve it to make the distinction.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I don't think that shadowing is the right term either, consider:

trait Foo {
  val x: Int = 1
}

trait Bar { foo: Foo =>
  val x: String = ""
}

In both scalac and dotty, this code compiles, but if you try to make an instance of Bar, it fails:

object Test {
  def main(args: Array[String]): Unit = {
    new Foo with Bar {}
  }
}
-- Error: try/i2473.scala:14:4 -------------------------------------------------
14 |    new Foo with Bar {}
   |    ^
   |   class $anon inherits conflicting members:
   |     value x in trait Foo of type Int  and
   |     value x in trait Bar of type String
   |   (Note: this can be resolved by declaring an override in class $anon.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the message to print

-- [E068] Syntax Error: tests/neg/i2473.scala:3:7 ------------------------------
3 |  type Inner <: foo.Inner // error
  |       ^
  |type Inner cannot have the same name as trait Inner in trait Foo -- cannot define type member with the same name as a trait member in self reference foo.
  |(Note: this can be resolved by using another name)
-- [E068] Syntax Error: tests/neg/i2473.scala:6:8 ------------------------------
6 |  class Inner // error
  |        ^
  |class Inner cannot have the same name as trait Inner in trait Foo -- cannot define class member with the same name as a trait member in self reference baz.
  |(Note: this can be resolved by using another name)

}
trait Baz { baz: Foo =>
class Inner // error: class Inner cannot have the same name as trait Inner in trait Foo -- class definitions cannot be overridden
}