Skip to content

Commit 0d0c006

Browse files
authored
Merge pull request #7096 from dotty-staging/fix-#7086
Fix #7086: Don't export overriding symbols
2 parents 38e87b0 + 5e49b92 commit 0d0c006

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,12 @@ class Namer { typer: Typer =>
954954
else if (sym.isConstructor || sym.is(ModuleClass) || sym.is(Bridge)) SKIP
955955
else if (cls.derivesFrom(sym.owner) &&
956956
(sym.owner == cls || !sym.is(Deferred))) i"is already a member of $cls"
957+
else if (sym.is(Override))
958+
sym.allOverriddenSymbols.find(
959+
other => cls.derivesFrom(other.owner) && !other.is(Deferred)) match {
960+
case Some(other) => i"overrides ${other.showLocated}, which is already a member of $cls"
961+
case None => ""
962+
}
957963
else ""
958964
}
959965

docs/docs/reference/other-new-features/export.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ of one of the following forms:
5959
A member is _eligible_ if all of the following holds:
6060

6161
- its owner is not a base class of the class(*) containing the export clause,
62+
- the member does not override a concrete definition that has as owner
63+
a base class of the class containing the export clause.
6264
- it is accessible at the export clause,
6365
- it is not a constructor, nor the (synthetic) class part of an object,
6466
- it is a given instance (or an old-style `implicit` value)

tests/neg/i7086.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
object Test1 {
2+
class A {
3+
override def toString: String = "A"
4+
}
5+
class B(a: A) {
6+
export a.toString // error: no eligible member toString at B.this.a
7+
}
8+
}
9+
10+
object Test extends App {
11+
trait T {
12+
def foo: Int = 1
13+
def bar: Int
14+
}
15+
class A extends T {
16+
override def foo = 2
17+
override def bar = 2
18+
}
19+
class B(a: A) extends T {
20+
export a.foo // error: no eligible member foo at B.this.a
21+
export a.bar // OK
22+
}
23+
}

tests/run/i7086.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
object Test1 {
2+
class A {
3+
override def toString: String = "A"
4+
}
5+
class B(a: A) {
6+
export a._ // OK
7+
}
8+
}
9+
10+
object Test extends App {
11+
trait T {
12+
def foo: Int = 1
13+
def bar: Int
14+
}
15+
class A extends T {
16+
override def foo = 2
17+
override def bar = 2
18+
}
19+
class B(a: A) extends T {
20+
export a._
21+
}
22+
val b = B(A())
23+
assert(b.foo == 1)
24+
assert(b.bar == 2)
25+
}

0 commit comments

Comments
 (0)