scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Companion object cannot be found from Java in Scala 3, but it compiles in Scala 2.x.

Open i10416 opened this issue 2 years ago • 4 comments

Compiler version

3.0.2, 3.1.3, 3.2.2, 3.3.0-RC3

Minimized code

https://github.com/i10416/reproduce-missing-symbol-error-in-scala-3

The following code compiles in Scala 2.x, but won't compile in Scala 3.x for "Not found: type Foo".

src/main/java/Bar.java

package example;

import example.Foo$;

public class Bar {
	// this compiles in Scala 2.x, but won't compile in Scala 3
	private static final Foo$ MOD = Foo$.MODULE$;
}

src/main/scala/Foo.scala

package example

case class Foo(i: Int)

object Foo

Output

error] -- [E006] Not Found Error: /path/to/src/main/java/Bar.java:7:21 
[error] 7 |     private static final Foo$ MOD = Foo$.MODULE$;
[error]   |                          ^^^^
[error]   |                          Not found: type Foo
[error]   |

Expectation

It should compile. Or is this expected behavior?

Note

It compiles in Scala 3 when you explicitly specify compileOrder := CompileOrder.ScalaThenJava. However, if there are both Scala code depending on Java and Java code depending Scala, you have no choice but CompileOrder.Mixed.

Notably, even if there are both Scala code depending on Java and Java code depending Scala, Scala 2.x can compile them (in CompileOrder.Mixed mode).

i10416 avatar Apr 13 '23 19:04 i10416

seems similar to https://github.com/scala/scala/pull/10644 (by @som-snytt) — though that PR won't ship until 2.13.13. @i10416 did you test 2.13.12, or a nightly?

SethTisue avatar Feb 19 '24 15:02 SethTisue

I tested the following versions and with Scala 2.x, it compiles with CompileOrder.Mixed, while it does not with 3.x. I didn't tested Scala 2.13.12 nor 3.3.1.

  • 2.12.17
  • 2.13.10
  • 3.0.2
  • 3.1.3
  • 3.2.2
  • 3.3.0-RC3

https://github.com/i10416/reproduce-missing-symbol-error-in-scala-3/blob/ba5092138d487ee1ed221f7d55b7f3bb8f812e9e/build.sbt#L4

https://github.com/scala/scala/pull/10644 seems so similar to this issue that we could derive some hints from it. I'll look into the cnanges in 10644.

i10416 avatar Feb 19 '24 15:02 i10416

By the way, 10644 is forward ported to Scala 3?

i10416 avatar Feb 19 '24 15:02 i10416

That is not forward-ported yet.

The current difference may be that there is no reason to typecheck a private element in Java.

som-snytt avatar Feb 19 '24 15:02 som-snytt