Skip to content

Commit

Permalink
Include top-level symbols from same file in outer ambiguity error
Browse files Browse the repository at this point in the history
  • Loading branch information
lrytz committed May 17, 2023
1 parent eeb52dc commit ca6b46f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
13 changes: 8 additions & 5 deletions spec/02-identifiers-names-and-scopes.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ which are collectively called _bindings_.
Bindings of each kind are assigned a precedence which determines
whether one binding can shadow another:

1. Definitions and declarations in lexical scope that are not [top-level](09-top-level-definitions.html)
have the highest precedence.
1. Definitions and declarations that are either inherited,
or made available by a package clause and also defined in the same compilation unit as the reference to them,
have the next highest precedence.
<!-- Not in the spec since Scala 2 only warns (scala/scala#10339)
1. Definitions and declarations that are local, or made available by a package clause and also
defined in the same compilation unit as the reference to them, have the highest precedence.
1. Definitions and declarations that are inherited have the next highest precedence.
-->
1. Definitions and declarations that are local, inherited, or made
available by a package clause and also defined in the same compilation unit
as the reference to them, have the highest precedence.
1. Explicit imports have the next highest precedence.
1. Wildcard imports have the next highest precedence.
1. Bindings made available by a package clause,
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/scala/tools/nsc/typechecker/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,7 @@ trait Contexts { self: Analyzer =>
val wasFoundInSuper = foundInSuper
val foundCompetingSymbol: () => Boolean =
if (foreignDefined) () => !foreignDefined
else () => !defSym.isTopLevel && !defSym.owner.isPackageObjectOrClass && !foundInSuper && !foreignDefined
else () => !defSym.owner.isPackageObjectOrClass && !foundInSuper && !foreignDefined
while ((cx ne NoContext) && cx.depth >= symbolDepth) cx = cx.outer
if (wasFoundInSuper)
while ((cx ne NoContext) && (cx.owner eq cx0.owner)) cx = cx.outer
Expand Down
4 changes: 2 additions & 2 deletions test/files/jvm/protectedacc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ package p {
abstract class PolyA[a] {
protected def m(x: a): Unit;

class B {
class BB {

trait Node {
def s: String = "";
Expand Down Expand Up @@ -134,7 +134,7 @@ package p {

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

trait Inner extends B {
trait Inner extends BB {
def self: T;
def self2: Node;
def getB: Inner;
Expand Down
9 changes: 8 additions & 1 deletion test/files/neg/t11921b.check
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ Such references are ambiguous in Scala 3. To continue using the inherited symbol
Or use `-Wconf:msg=legacy-binding:s` to silence this warning.
println(y) // error
^
t11921b.scala:65: warning: reference to global is ambiguous;
it is both defined in the enclosing package <empty> and inherited in the enclosing object D as value global (defined in class C)
In Scala 2, symbols inherited from a superclass shadow symbols defined in an outer scope.
Such references are ambiguous in Scala 3. To continue using the inherited symbol, write `this.global`.
Or use `-Wconf:msg=legacy-binding:s` to silence this warning.
println(global) // error
^
t11921b.scala:75: warning: reference to x is ambiguous;
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)
In Scala 2, symbols inherited from a superclass shadow symbols defined in an outer scope.
Expand All @@ -50,5 +57,5 @@ Such references are ambiguous in Scala 3. To continue using the inherited symbol
Or use `-Wconf:msg=legacy-binding:s` to silence this warning.
def v = t(lo) // error
^
7 warnings
8 warnings
1 error
9 changes: 8 additions & 1 deletion test/files/neg/t11921b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class C {
val global = 42
}
object D extends C {
println(global) // OK, since global is defined in package (https://github.com/scala/scala/pull/10220/files#r1109773904)
println(global) // error
}

object test5 {
Expand Down Expand Up @@ -136,3 +136,10 @@ object test10 {
def v = t(lo) // error
}
}

package scala {
trait P { trait Option[+A] }
class C extends P {
def t = new Option[String] {} // OK, competing scala.Option is not defined in the same compilation unit
}
}

0 comments on commit ca6b46f

Please sign in to comment.