scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
230 stars 21 forks source link

Spurious "ambiguous reference inherited vs outer" warning #12850

Closed lrytz closed 10 months ago

lrytz commented 10 months ago
trait T {
  def pm(x: Int) = 1
  def pm(x: String) = 2
}

package object p extends T {
  def t = pm(1)
}

Gives a spurious warning

Test.scala:7: warning: reference to pm is ambiguous;
it is both defined in the enclosing trait T and inherited in the enclosing package object p as method pm (defined in trait T)
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.pm`.
Or use `-Wconf:msg=legacy-binding:s` to silence this warning. [quickfixable]
  def t = pm(1)
          ^
lrytz commented 10 months ago

Same underlying reason:

trait T {
  def pm(x: Int) = 1
  def pm(x: String) = 2
  object p extends T {
    def t = pm(1)
  }
}

We get the warning in the overloaded case, but not if there's only one def pm. Arguably we should warn when the prefixes don't match. Currently we just compare the symbols

https://github.com/scala/scala/blob/49d55077e8fba24003b58761468e83abcd0c0280/src/compiler/scala/tools/nsc/typechecker/Contexts.scala#L1640

        if (defSym.exists && (defSym ne defSym0)) {
          val ambiguity = ...
som-snytt commented 10 months ago

:heart: to Lukas. I thought both overloading and package object already arose as issues and addressed.

We could change "is ambiguous" to "may appear ambiguous"...