Skip to content

Commit ca6b46f

Browse files
committed
Include top-level symbols from same file in outer ambiguity error
1 parent eeb52dc commit ca6b46f

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

spec/02-identifiers-names-and-scopes.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ which are collectively called _bindings_.
1717
Bindings of each kind are assigned a precedence which determines
1818
whether one binding can shadow another:
1919

20-
1. Definitions and declarations in lexical scope that are not [top-level](09-top-level-definitions.html)
21-
have the highest precedence.
22-
1. Definitions and declarations that are either inherited,
23-
or made available by a package clause and also defined in the same compilation unit as the reference to them,
24-
have the next highest precedence.
20+
<!-- Not in the spec since Scala 2 only warns (scala/scala#10339)
21+
1. Definitions and declarations that are local, or made available by a package clause and also
22+
defined in the same compilation unit as the reference to them, have the highest precedence.
23+
1. Definitions and declarations that are inherited have the next highest precedence.
24+
-->
25+
1. Definitions and declarations that are local, inherited, or made
26+
available by a package clause and also defined in the same compilation unit
27+
as the reference to them, have the highest precedence.
2528
1. Explicit imports have the next highest precedence.
2629
1. Wildcard imports have the next highest precedence.
2730
1. Bindings made available by a package clause,

src/compiler/scala/tools/nsc/typechecker/Contexts.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,7 @@ trait Contexts { self: Analyzer =>
16121612
val wasFoundInSuper = foundInSuper
16131613
val foundCompetingSymbol: () => Boolean =
16141614
if (foreignDefined) () => !foreignDefined
1615-
else () => !defSym.isTopLevel && !defSym.owner.isPackageObjectOrClass && !foundInSuper && !foreignDefined
1615+
else () => !defSym.owner.isPackageObjectOrClass && !foundInSuper && !foreignDefined
16161616
while ((cx ne NoContext) && cx.depth >= symbolDepth) cx = cx.outer
16171617
if (wasFoundInSuper)
16181618
while ((cx ne NoContext) && (cx.owner eq cx0.owner)) cx = cx.outer

test/files/jvm/protectedacc.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ package p {
4747
abstract class PolyA[a] {
4848
protected def m(x: a): Unit;
4949

50-
class B {
50+
class BB {
5151

5252
trait Node {
5353
def s: String = "";
@@ -134,7 +134,7 @@ package p {
134134

135135
abstract class X[T] extends PolyA[T] {
136136

137-
trait Inner extends B {
137+
trait Inner extends BB {
138138
def self: T;
139139
def self2: Node;
140140
def getB: Inner;

test/files/neg/t11921b.check

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ Such references are ambiguous in Scala 3. To continue using the inherited symbol
2929
Or use `-Wconf:msg=legacy-binding:s` to silence this warning.
3030
println(y) // error
3131
^
32+
t11921b.scala:65: warning: reference to global is ambiguous;
33+
it is both defined in the enclosing package <empty> and inherited in the enclosing object D as value global (defined in class C)
34+
In Scala 2, symbols inherited from a superclass shadow symbols defined in an outer scope.
35+
Such references are ambiguous in Scala 3. To continue using the inherited symbol, write `this.global`.
36+
Or use `-Wconf:msg=legacy-binding:s` to silence this warning.
37+
println(global) // error
38+
^
3239
t11921b.scala:75: warning: reference to x is ambiguous;
3340
it is both defined in the enclosing object Uhu and inherited in the enclosing class C as value x (defined in class A, inherited through parent class B)
3441
In Scala 2, symbols inherited from a superclass shadow symbols defined in an outer scope.
@@ -50,5 +57,5 @@ Such references are ambiguous in Scala 3. To continue using the inherited symbol
5057
Or use `-Wconf:msg=legacy-binding:s` to silence this warning.
5158
def v = t(lo) // error
5259
^
53-
7 warnings
60+
8 warnings
5461
1 error

test/files/neg/t11921b.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class C {
6262
val global = 42
6363
}
6464
object D extends C {
65-
println(global) // OK, since global is defined in package (https://github.com/scala/scala/pull/10220/files#r1109773904)
65+
println(global) // error
6666
}
6767

6868
object test5 {
@@ -136,3 +136,10 @@ object test10 {
136136
def v = t(lo) // error
137137
}
138138
}
139+
140+
package scala {
141+
trait P { trait Option[+A] }
142+
class C extends P {
143+
def t = new Option[String] {} // OK, competing scala.Option is not defined in the same compilation unit
144+
}
145+
}

0 commit comments

Comments
 (0)