Skip to content

Commit 181c1aa

Browse files
committed
Package object members are not conflicting with inherited
1 parent 062d9ca commit 181c1aa

File tree

4 files changed

+88
-13
lines changed

4 files changed

+88
-13
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
418418
* the inherited member (has an overloaded alternative that) coincides with
419419
* (an overloaded alternative of) the definition x.
420420
*/
421-
def checkNoOuterDefs(denot: Denotation, last: Context, prevCtx: Context): Unit =
421+
def checkNoOuterDefs(denot: Denotation, ctx: Context, origCtx: Context): Unit =
422422
def sameTermOrType(d1: SingleDenotation, d2: Denotation) =
423423
d2.containsSym(d1.symbol) || d2.hasUniqueSym && {
424424
val sym1 = d1.symbol
@@ -430,19 +430,23 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
430430
else
431431
(sym1.isAliasType || sym2.isAliasType) && d1.info =:= d2.info
432432
}
433-
val outer = last.outer
434-
val owner = outer.owner
435-
if (owner eq last.owner) && (outer.scope eq last.scope) then
436-
checkNoOuterDefs(denot, outer, prevCtx)
437-
else if !owner.isRoot then
433+
val outerCtx = ctx.outer
434+
val outerOwner = outerCtx.owner
435+
if (outerOwner eq ctx.owner) && (outerCtx.scope eq ctx.scope) then
436+
checkNoOuterDefs(denot, outerCtx, origCtx)
437+
else if !outerOwner.isRoot then
438438
val found =
439-
if owner.is(Package) then
440-
owner.denot.asClass.membersNamed(name)
439+
if outerOwner.is(Package) then
440+
def notInPackageObject(sym: Symbol) =
441+
sym.owner == outerOwner || // sym.owner.isPackageObject is false if sym is defined in a parent of the package object
442+
sym.owner.isPackageObject && sym.owner.name.endsWith(str.TOPLEVEL_SUFFIX) // top-level definitions
443+
outerOwner.denot.asClass.membersNamed(name)
441444
.filterWithPredicate(d => !d.symbol.is(Package)
445+
&& notInPackageObject(d.symbol)
442446
&& d.symbol.source.exists
443447
&& isDefinedInCurrentUnit(d))
444448
else
445-
val scope = if owner.isClass then owner.info.decls else outer.scope
449+
val scope = if outerOwner.isClass then outerOwner.info.decls else outerCtx.scope
446450
scope.denotsNamed(name)
447451
val competing = found.filterWithFlags(required, excluded | Synthetic)
448452
if competing.exists then
@@ -451,14 +455,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
451455
.exists
452456
if !symsMatch && !suppressErrors then
453457
report.errorOrMigrationWarning(
454-
AmbiguousReference(name, Definition, Inheritance, prevCtx)(using outer),
458+
AmbiguousReference(name, Definition, Inheritance, origCtx)(using outerCtx),
455459
pos, from = `3.0`)
456460
if migrateTo3 then
457461
patch(Span(pos.span.start),
458-
if prevCtx.owner == refctx.owner.enclosingClass then "this."
459-
else s"${prevCtx.owner.name}.this.")
462+
if origCtx.owner == refctx.owner.enclosingClass then "this."
463+
else s"${origCtx.owner.name}.this.")
460464
else
461-
checkNoOuterDefs(denot, outer, prevCtx)
465+
checkNoOuterDefs(denot, outerCtx, origCtx)
462466

463467
if isNewDefScope then
464468
val defDenot = ctx.denotNamed(name, required, excluded)

tests/neg/t12186.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package object p extends p.U {
2+
def b: Int = 0
3+
trait Y
4+
}
5+
6+
package p {
7+
trait U {
8+
def a: Int = 0
9+
trait X
10+
}
11+
12+
object c
13+
def c1 = 0 // top-level def
14+
trait Z
15+
trait T {
16+
def a = 1
17+
def b = 1
18+
def c = 1
19+
def c1 = 1
20+
21+
trait X
22+
trait Y
23+
trait Z
24+
}
25+
26+
trait RR extends T {
27+
def m1 = a // ok
28+
def m2 = b // ok
29+
def m3 = c // error
30+
def m4 = c1 // error
31+
32+
def n1: X // ok
33+
def n2: Y // ok
34+
def n3: Z // error
35+
}
36+
}

tests/neg/t12186b/A.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package object p extends p.U {
2+
def b: Int = 0
3+
trait Y
4+
}

tests/neg/t12186b/B.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package p {
2+
trait U {
3+
def a: Int = 0
4+
trait X
5+
}
6+
7+
object c
8+
def c1 = 0 // top-level def
9+
trait Z
10+
trait T {
11+
def a = 1
12+
def b = 1
13+
def c = 1
14+
def c1 = 1
15+
16+
trait X
17+
trait Y
18+
trait Z
19+
}
20+
21+
trait RR extends T {
22+
def m1 = a // ok
23+
def m2 = b // ok
24+
def m3 = c // error
25+
def m4 = c1 // error
26+
27+
def n1: X // ok
28+
def n2: Y // ok
29+
def n3: Z // error
30+
}
31+
}

0 commit comments

Comments
 (0)