-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Top-level methods with the same name in the same package overwrite each other #22721
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
Comments
For a moment, I believed.
That is separate compilation and on the REPL classpath of 3.7. |
I do see the same issue with latest nightly and with 3.6.4-RC2. |
@tgodzik How to reproduce? I get the same result with |
@tgodzik Thanks! I don't normally use (Same result with regular joint compilation. The private within is not a repl artifact.) TIL it's intended that top-level defs from different files can be overloaded. This is not an overload: I remember previous discussion on a ticket about the semantic of top-level private, but I incorrectly assumed I correctly intuited what was intended. Now I think explicit |
FWIW, lookup in typer has an exclusion for opaque types (where opaque scope really is the package object). I would expect lookup to prefer my local package object, but in the presence of overloads, what I really mean is that if there is a competing symbol, prefer my local package object as a tie-breaker. (That does not seem stranger as a special rule than relaxed imports that make extension methods more useful or tractable.) |
When compiling |
Oh, but even if we choose to use the more-local reference, the problem will still happen for other files which do not have their own local ref: package example
private def foobar: String = "foo"
object test1 { val x = foobar } package example
private def foobar: Int = 0
object test2 { val x = foobar } package example:
object test3:
def x = foobar // which one do we choose?
@main def main() =
println(example.test1.x)
println(example.test2.x)
println(example.test3.x) We could scope the private for file (instead of package), but that's a little arbitrary, especially that |
Ok, via reference (https://docs.scala-lang.org/scala3/reference/dropped-features/package-objects.html): So it should definitely error out, like it already does when two of the same names with |
Oh good. I didn't check the spec, I only misread a code comment in Typer. |
This remains true if the 2 first files are compiled separately, regardless of the access modifier used, unfortunately |
Because we are now "aware" of the package object due to opaque types, I might have preferred a spec change, with private meaning private to the package object, and qualified private for current behavior. (Note to self when I read this later.) |
Compiler version
3.3.5, 3.4.3, 3.5.2, 3.6.3
Minimized code
Output
In the console:
Expectation
Ideally the calls to
foobar
would be scoped properly in each file but I'm guessing that's hard, so at least a compiler warning or error would be nice.The text was updated successfully, but these errors were encountered: