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

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

Closed
i10416 opened this issue Apr 13, 2023 · 4 comments · Fixed by #19773
Closed

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

i10416 opened this issue Apr 13, 2023 · 4 comments · Fixed by #19773

Comments

@i10416
Copy link
Contributor

i10416 commented Apr 13, 2023

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 i10416 added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 13, 2023
@anatoliykmetyuk anatoliykmetyuk added compat:java area:backend and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 17, 2023
@i10416 i10416 mentioned this issue Jun 4, 2023
7 tasks
@SethTisue
Copy link
Member

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

@i10416
Copy link
Contributor Author

i10416 commented Feb 19, 2024

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

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

@i10416
Copy link
Contributor Author

i10416 commented Feb 19, 2024

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

@som-snytt
Copy link
Contributor

som-snytt commented Feb 19, 2024

That is not forward-ported yet.

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

i10416 added a commit to i10416/dotty that referenced this issue Feb 24, 2024
To find Scala companion mudule from Java, we should strip module suffix
`$`.
This provides workaround for scala#17255, but it requires some refinment to fully fix it
because not-fully-qualified type like the following example still fails
to compile due to missing symbol.

```java
package example;

public class Bar {
    private static final Foo$ MOD = Foo$.MODULE;
}
```

This is because `pre` in `javaFindMember` for `Foo` in the case above is `<root>`,
not `example` and therefore `pre.findMember` looks for `<root>.Foo` instead
of `example.Foo`.

I'm not sure whether the qualifier is intentionally dropped.
i10416 added a commit to i10416/dotty that referenced this issue Feb 24, 2024
To find Scala companion mudule from Java, we should strip module suffix
`$`.
This provides workaround for scala#17255, but it requires some refinment to fully fix it
because not-fully-qualified type like the following example still fails
to compile due to missing symbol.

```java
package example;

public class Bar {
    private static final Foo$ MOD = Foo$.MODULE;
}
```

This is because `pre` in `javaFindMember` for `Foo` in the case above is `<root>`,
not `example` and therefore `pre.findMember` looks for `<root>.Foo` instead
of `example.Foo`.

I'm not sure whether the qualifier is intentionally dropped.
i10416 added a commit to i10416/dotty that referenced this issue Feb 24, 2024
To find Scala companion mudule from Java, we should strip module suffix
`$`.
This provides workaround for scala#17255, but it requires some refinment to fully fix it
because not-fully-qualified type like the following example still fails
to compile due to missing symbol.

```java
package example;

public class Bar {
    private static final Foo$ MOD = Foo$.MODULE;
}
```

This is because `pre` in `javaFindMember` for `Foo` in the case above is `<root>`,
not `example` and therefore `pre.findMember` looks for `<root>.Foo` instead
of `example.Foo`.

I'm not sure whether the qualifier is intentionally dropped.
i10416 added a commit to i10416/dotty that referenced this issue Feb 24, 2024
To find Scala companion mudule from Java in mixed sources,
we should strip module suffix `$`.
This provides workaround for scala#17255, but it requires some refinment to fix it
because not-fully-qualified type like the following example still fails
to compile due to missing symbol.

```java
package example;

public class Bar {
    private static final Foo$ MOD = Foo$.MODULE;
}
```

This is because `pre` in `javaFindMember` for `Foo` in the case above is `<root>`,
not `example` and therefore `pre.findMember` looks for `<root>.Foo` instead
of `example.Foo`.

I'm not sure whether the qualifier is intentionally dropped.

References
- scala#12884
- scala/scala#7671
bishabosha added a commit that referenced this issue Feb 28, 2024
To find Scala companion mudule from Java, we should strip module suffix
`$`.
This ~provides workaround~ fixes #17255 as well as forward-port
scala/scala#10644. ~, but it requires some
refinment to fix it because not-fully-qualified type like the following
example still fails to compile due to missing symbol.~

Related to scala/scala#10644
@Kordyjan Kordyjan added this to the 3.4.2 milestone Mar 28, 2024
@Kordyjan Kordyjan modified the milestones: 3.4.2, 3.5.0 May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants