Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shadowing rules inconsistent for packages and objects #8559

Closed
scabug opened this issue May 4, 2014 · 6 comments
Closed

Shadowing rules inconsistent for packages and objects #8559

scabug opened this issue May 4, 2014 · 6 comments
Assignees

Comments

@scabug
Copy link

scabug commented May 4, 2014

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
Copy link
Author

scabug commented May 4, 2014

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
Copy link
Author

scabug commented May 5, 2014

@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
Copy link
Author

scabug commented May 7, 2014

@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
Copy link
Author

scabug commented May 8, 2014

@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 scabug closed this as completed May 8, 2014
@scabug
Copy link
Author

scabug commented May 8, 2014

@paulp said:
Take that, alefas.

@scabug
Copy link
Author

scabug commented May 9, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants