Skip to content

Commit

Permalink
don't report type / term aliases as ambiguous
Browse files Browse the repository at this point in the history
  • Loading branch information
lrytz committed Nov 15, 2022
1 parent f0be08a commit 4a48082
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/compiler/scala/tools/nsc/typechecker/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,7 @@ trait Contexts { self: Analyzer =>
}

// cx.scope eq null arises during FixInvalidSyms in Duplicators
def nextDefinition(): Unit = {
def nextDefinition(lastDef: Symbol, lastPre: Type): Unit = {
var inPrefix = false
defSym = NoSymbol
while (defSym == NoSymbol && (cx ne NoContext) && (cx.scope ne null)) {
Expand All @@ -1466,10 +1466,14 @@ trait Contexts { self: Analyzer =>
}
if (!defSym.exists) cx = cx.outer // push further outward
}
if ((defSym.isAliasType || lastDef.isAliasType) && pre.memberType(defSym) =:= lastPre.memberType(lastDef))
defSym = NoSymbol
if (defSym.isStable && lastDef.isStable && singleType(pre, defSym) =:= singleType(lastPre, lastDef))
defSym = NoSymbol
foundInPrefix = inPrefix && defSym.exists
foundInSuper = foundInPrefix && defSym.owner != cx.owner
}
nextDefinition()
nextDefinition(NoSymbol, NoPrefix)

if (symbolDepth < 0)
symbolDepth = cx.depth
Expand Down Expand Up @@ -1581,7 +1585,7 @@ trait Contexts { self: Analyzer =>
while ((cx ne NoContext) && (cx.owner eq cx0.owner)) cx = cx.outer
var done = false
while (!done) {
nextDefinition()
nextDefinition(defSym0, pre0)
done = (cx eq NoContext) || defSym.exists && foundCompetingSymbol()
if (!done && (cx ne NoContext)) cx = cx.outer
}
Expand Down
15 changes: 15 additions & 0 deletions test/files/neg/t11921-alias.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
t11921-alias.scala:16: error: reference to TT is ambiguous;
it is both defined in object O and available as type TT in class C
Since 2.13.11, symbols inherited from a superclass no longer shadow symbols defined in an outer scope.
If shadowing was intended, write `this.TT`.
Or use `-Ylegacy-binding` to enable the previous behavior everywhere.
def n(x: TT) = x // ambiguous
^
t11921-alias.scala:36: error: reference to c is ambiguous;
it is both defined in class B and available as value c in class A
Since 2.13.11, symbols inherited from a superclass no longer shadow symbols defined in an outer scope.
If shadowing was intended, write `this.c`.
Or use `-Ylegacy-binding` to enable the previous behavior everywhere.
def n = c // ambiguous
^
2 errors
39 changes: 39 additions & 0 deletions test/files/neg/t11921-alias.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
object t1 {
class C[T] { type TT = T }
object O {
type TT = String
class D extends C[TT] {
def n(x: TT) = x // OK
}
}
}

object t2 {
class C[T] { type TT <: T }
object O {
type TT = String
class D extends C[TT] {
def n(x: TT) = x // ambiguous
}
}
}

object t3 {
trait Context
class A[C <: Context](val c: C)
class B(val c: Context) { b =>
val a = new A[c.type](c) {
def n = c // OK
}
}
}

object t4 {
trait Context
class A[C <: Context](val c: C)
class B(val c: Context) { b =>
val a = new A(c) {
def n = c // ambiguous
}
}
}

0 comments on commit 4a48082

Please sign in to comment.