Skip to content

Commit e17c97d

Browse files
committed
fix: cannot find Scala companion module from Java
To find Scala companion mudule from Java, we should strip module suffix `$`. This provides workaround for #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.
1 parent 8e2f92a commit e17c97d

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

compiler/src/dotty/tools/dotc/core/ContextOps.scala

+9-3
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,17 @@ object ContextOps:
5555
final def javaFindMember(name: Name, pre: Type, lookInCompanion: Boolean, required: FlagSet = EmptyFlags, excluded: FlagSet = EmptyFlags): Denotation =
5656
assert(ctx.isJava)
5757
inContext(ctx) {
58-
58+
import dotty.tools.dotc.core.NameOps.*
5959
val preSym = pre.typeSymbol
60-
6160
// 1. Try to search in current type and parents.
62-
val directSearch = pre.findMember(name, pre, required, excluded)
61+
val directSearch =
62+
if name.isTypeName && name.endsWith(StdNames.str.MODULE_SUFFIX) then
63+
pre.findMember(name.stripModuleClassSuffix, pre, required, excluded) match
64+
case NoDenotation => NoDenotation
65+
case symDenot: SymDenotation =>
66+
symDenot.companionModule.denot
67+
else
68+
pre.findMember(name, pre, required, excluded)
6369

6470
// 2. Try to search in companion class if current is an object.
6571
def searchCompanionClass = if lookInCompanion && preSym.is(Flags.Module) then

compiler/test/dotc/pos-test-pickling.blacklist

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ i16649-irrefutable.scala
2929
strict-pattern-bindings-3.0-migration.scala
3030
i17186b.scala
3131
i11982a.scala
32+
i17255
3233

3334
# Tree is huge and blows stack for printing Text
3435
i7034.scala

tests/pos/i17255/Bar.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package example;
2+
3+
4+
public class Bar {
5+
private static final example.Foo$ MOD = example.Foo$.MODULE$;
6+
}

tests/pos/i17255/Foo.scala

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package example
2+
3+
case class Foo(i: Int)
4+
5+
object Foo

0 commit comments

Comments
 (0)