Skip to content

Commit 6f5ca20

Browse files
authored
Merge pull request scala#10339 from lrytz/ambiguous-top-level
2 parents 418f76d + b8c58b1 commit 6f5ca20

File tree

10 files changed

+38
-19
lines changed

10 files changed

+38
-19
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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,16 +1377,17 @@ trait Contexts { self: Analyzer =>
13771377
sm"""|it is both defined in the enclosing ${outer1.owner} and inherited in the enclosing $classDesc as $inherited1 (defined in ${inherited1.ownsString}$inherit)
13781378
|In Scala 2, symbols inherited from a superclass shadow symbols defined in an outer scope.
13791379
|Such references are ambiguous in Scala 3. To continue using the inherited symbol, write `this.${outer1.name}`."""
1380-
if (currentRun.isScala3)
1381-
Some(LookupAmbiguous(message))
1382-
else {
1380+
// For now (2.13.11), warn under Xsource:3. We'll consider warning by default (and erring in Xsource:3) in 2.13.12
1381+
if (currentRun.isScala3) {
13831382
// passing the message to `typedIdent` as attachment, we don't have the position here to report the warning
13841383
inherited.updateAttachment(LookupAmbiguityWarning(
13851384
sm"""|reference to ${outer1.name} is ambiguous;
13861385
|$message
13871386
|Or use `-Wconf:msg=legacy-binding:s` to silence this warning."""))
1387+
// Some(LookupAmbiguous(message)) // to make it an error in 2.13.12
1388+
None
1389+
} else
13881390
None
1389-
}
13901391
}
13911392
} else
13921393
Some(LookupAmbiguous(s"it is both defined in ${outer.owner} and available as ${inherited.fullLocationString}"))
@@ -1612,7 +1613,7 @@ trait Contexts { self: Analyzer =>
16121613
val wasFoundInSuper = foundInSuper
16131614
val foundCompetingSymbol: () => Boolean =
16141615
if (foreignDefined) () => !foreignDefined
1615-
else () => !defSym.isTopLevel && !defSym.owner.isPackageObjectOrClass && !foundInSuper && !foreignDefined
1616+
else () => !defSym.owner.isPackageObjectOrClass && !foundInSuper && !foreignDefined
16161617
while ((cx ne NoContext) && cx.depth >= symbolDepth) cx = cx.outer
16171618
if (wasFoundInSuper)
16181619
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/t11921-alias.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// scalac: -Werror
1+
// scalac: -Werror -Xsource:3
22

33
object t1 {
44
class C[T] { type TT = T }

test/files/neg/t11921.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
// scalac: -Xsource:3
22

33
class C {
44
def lazyMap[A, B](coll: Iterable[A], f: A => B) =

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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// scalac: -Werror
1+
// scalac: -Werror -Xsource:3
22

33
object test1 {
44

@@ -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+
}

test/files/neg/t11921c.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// scalac: -Wconf:msg=legacy-binding:s
1+
// scalac: -Wconf:msg=legacy-binding:s -Xsource:3
22

33
class C {
44
def lazyMap[A, B](coll: Iterable[A], f: A => B) =

test/files/pos/t11921b.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// scalac: -Werror -Wconf:msg=legacy-binding:s
1+
// scalac: -Werror -Wconf:msg=legacy-binding:s -Xsource:3
22

33
object test1 {
44

test/files/pos/t11921c.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// scalac: -Xsource:3
12

23
// test/scaladoc/resources/t5784.scala
34

0 commit comments

Comments
 (0)