scala / bug

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

Non-visible symbol in wildcard import taking precedence over visible symbol in current package #12736

Closed armanbilge closed 1 year ago

armanbilge commented 1 year ago

Reproduction steps

Scala version: 2.13.10

https://github.com/armanbilge/sandbox/commit/0c6533cff815c823c13dd7397289ebc49edf0526

Problem

sbt:sandbox> bar/compile
[info] compiling 1 Scala source to /workspace/sandbox/foo/target/scala-2.13/classes ...
[info] compiling 2 Scala sources to /workspace/sandbox/bar/target/scala-2.13/classes ...
[error] /workspace/sandbox/bar/src/main/scala/bar.scala:7:3: object Foo in package foo cannot be accessed as a member of package foo from object Bar in package baz
[error]   Foo
[error]   ^
[error] one error found
[error] (bar / Compile / compileIncremental) Compilation failed
[error] Total time: 0 s, completed Feb 22, 2023, 7:15:12 AM
sbt:sandbox> 

Meanwhile, there is a visible Foo in the current package, which is the one we want.

som-snytt commented 1 year ago
➜  t12736 ls
bar.scala  baz.scala  foo.scala
➜  t12736 cat foo.scala

package foo

private object Foo
➜  t12736 cat bar.scala

package bar

private object Foo
➜  t12736 cat baz.scala

package bar
package baz

import foo._

object Bar {
  def test() = println(Foo)
}
➜  t12736 scalac -d /tmp *.scala
➜  t12736 scala -cp /tmp
Welcome to Scala 2.13.10 (OpenJDK 64-Bit Server VM, Java 19).
Type in expressions for evaluation. Or try :help.

scala> bar.baz.Bar.test()
bar.Foo$@619f2afc

scala>

Importable requires accessible.

Maybe try clean; compile.

armanbilge commented 1 year ago
sbt:sandbox> clean; compile
[success] Total time: 0 s, completed Feb 22, 2023, 7:43:41 AM
[info] compiling 1 Scala source to /workspace/sandbox/foo/target/scala-2.13/classes ...
[info] compiling 2 Scala sources to /workspace/sandbox/bar/target/scala-2.13/classes ...
[error] /workspace/sandbox/bar/src/main/scala/bar.scala:7:3: object Foo in package foo cannot be accessed as a member of package foo from object Bar in package baz
[error]   Foo
[error]   ^
[error] one error found
[error] (bar / Compile / compileIncremental) Compilation failed
[error] Total time: 1 s, completed Feb 22, 2023, 7:43:41 AM
sbt:sandbox> 
som-snytt commented 1 year ago

I see the error after cloning the repo. Thanks!

som-snytt commented 1 year ago

It's the separate compilation bug where it doesn't do "package private" correctly.

➜  t12736 scalac -d /tmp foo.scala
➜  t12736 scalac -d /tmp -cp /tmp b*.scala
baz.scala:8: error: object Foo in package foo cannot be accessed as a member of package foo from object Bar in package baz
  def test() = println(Foo)
                       ^
1 error

Not sure I knew it manifested this way, but it rings a bell.

The related issue was https://github.com/scala/bug/issues/8121

(Checking sym.exists around importedSelectedSymbol seems close. The fix for the related ticket was for checking the import, as opposed to performing the lookup.)