scala / bug

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

Shadowing rules inconsistent for packages and objects #8559

Closed scabug closed 10 years ago

scabug commented 10 years ago

It has to be a bug? Oh well I also must have the record for "Not a Bug", like how home run records can be accompanied by strikeout and walk records.

package bob

// The expected behavior
object util {
  import scala.util

  class Random { override def toString = "bob's random "}

  object Test {
    def main(args: Array[String]): Unit = println(new util.Random)
  }
}
// a.scala:11: error: reference to util is ambiguous;
// it is both defined in package bob and imported subsequently by
// import scala.util
//       println(new util.Random)
//                   ^
// one error found

This one however compiles, and when run indicates that package util was successfully shadowed.

package bob

package util {
  import scala.util

  class Random { override def toString = "bob's random "}

  object Test {
    def main(args: Array[String]): Unit = println(new util.Random)
  }
}
scabug commented 10 years ago

Imported From: https://issues.scala-lang.org/browse/SI-8559?orig=1 Reporter: @paulp Affected Versions: 2.10.4, 2.11.0 See #2548

scabug commented 10 years ago

@som-snytt said: As with baseball stats, some must be discounted as assisted by various injections, such as Dr Odersky's run of Dummy Tickets (#77) or soc's meta-bug period.

I don't think twelve is a record.

extempore

In fact,

alefas

Of course, these numbers are skewed by under-reporting.

scabug commented 10 years ago

@paulp said: I should receive partial credit on some of his. Look at #8338 for instance. I reopened it after it was closed not-a-bug, and it was again closed not-a-bug. That has to be reflected in my stats or the whole thing is a sham.

scabug commented 10 years ago

@retronym said: We've got an imported symbol in the inner scope (s.util) , and a defined symbol (bob.util) in the outer scope. What are the binding precedences of each of these?

1. Definitions and declarations that are local, inherited, or made available by a package clause in the same compilation unit where the definition occurs have highest precedence.
2. Explicit imports have next highest precedence.
3. Wildcard imports have next highest precedence.
4. Definitions made available by a package clause not in the compilation unit where the definition occurs have lowest precedence.

Intuition suggests that bob.util should have precedence #1, as syntactically it is defined in the same source file as package bob. But because packages are open, it could also be defined an any number of other source files. As such, packages are considered to be source-file-less drifters, and that shunts the precedence of the defSym down to 4.

In this case, shadowing is as specced:

A binding in some inner scope shadows bindings of lower precedence in the same scope as well as bindings of the same or lower precedence in outer scopes.

See also #2458 / https://github.com/retronym/scala/tree/ticket/2458, in which I have a fix for the arbiter of this rule. I'll add a task to that ticket to add a clarification to the spec along these lines.

scabug commented 10 years ago

@paulp said: Take that, alefas.

scabug commented 8 years ago

@som-snytt said: I enshrined the example. I hope they accept https://github.com/scala/scala/pull/5151/files