Skip to content

Commit b5f14f4

Browse files
committed
Include top-level symbols from same file in outer ambiguity error
1 parent 44c6006 commit b5f14f4

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
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,9 @@ 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+
1. Definitions and declarations that are local, inherited, or made
21+
available by a package clause and also defined in the same compilation unit
22+
as the reference to them, have the highest precedence.
2523
1. Explicit imports have the next highest precedence.
2624
1. Wildcard imports have the next highest precedence.
2725
1. Bindings made available by a package clause,
@@ -39,6 +37,11 @@ in some inner scope _shadows_ bindings of lower precedence in the
3937
same scope as well as bindings of the same or lower precedence in outer
4038
scopes.
4139

40+
It is an error if an identifier x is available as an inherited member in an inner scope
41+
and the same name x is defined in an outer scope in the same compilation unit, unless
42+
the inherited member (has an overloaded alternative that) coincides with
43+
(an overloaded alternative of) the definition x.
44+
4245
Note that shadowing is only a partial order. In the following example,
4346
neither binding of `x` shadows the other. Consequently, the
4447
reference to `x` in the last line of the block is ambiguous.

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

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

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)