Skip to content

Fix #7086: Don't export overriding symbols #7096

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 2 commits into from
Aug 25, 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
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,12 @@ class Namer { typer: Typer =>
else if (sym.isConstructor || sym.is(ModuleClass) || sym.is(Bridge)) SKIP
else if (cls.derivesFrom(sym.owner) &&
(sym.owner == cls || !sym.is(Deferred))) i"is already a member of $cls"
else if (sym.is(Override))
sym.allOverriddenSymbols.find(
other => cls.derivesFrom(other.owner) && !other.is(Deferred)) match {
case Some(other) => i"overrides ${other.showLocated}, which is already a member of $cls"
case None => ""
}
else ""
}

Expand Down
2 changes: 2 additions & 0 deletions docs/docs/reference/other-new-features/export.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ of one of the following forms:
A member is _eligible_ if all of the following holds:

- its owner is not a base class of the class(*) containing the export clause,
- the member does not override a concrete definition that has as owner
a base class of the class containing the export clause.
- it is accessible at the export clause,
- it is not a constructor, nor the (synthetic) class part of an object,
- it is a given instance (or an old-style `implicit` value)
Expand Down
23 changes: 23 additions & 0 deletions tests/neg/i7086.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
object Test1 {
class A {
override def toString: String = "A"
}
class B(a: A) {
export a.toString // error: no eligible member toString at B.this.a
}
}

object Test extends App {
trait T {
def foo: Int = 1
def bar: Int
}
class A extends T {
override def foo = 2
override def bar = 2
}
class B(a: A) extends T {
export a.foo // error: no eligible member foo at B.this.a
export a.bar // OK
}
}
25 changes: 25 additions & 0 deletions tests/run/i7086.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
object Test1 {
class A {
override def toString: String = "A"
}
class B(a: A) {
export a._ // OK
}
}

object Test extends App {
trait T {
def foo: Int = 1
def bar: Int
}
class A extends T {
override def foo = 2
override def bar = 2
}
class B(a: A) extends T {
export a._
}
val b = B(A())
assert(b.foo == 1)
assert(b.bar == 2)
}