Skip to content

Commit b119026

Browse files
committed
fix: cannot find Scala companion module from Java
To find Scala companion mudule from Java in mixed sources, we should strip module suffix `$`. This provides workaround for #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 - #12884 - scala/scala#7671 [Cherry-picked 22d98d6][modified]
1 parent 07680d2 commit b119026

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

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

Lines changed: 9 additions & 3 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ i9804.scala
2727
i13433.scala
2828
i16649-irrefutable.scala
2929
strict-pattern-bindings-3.0-migration.scala
30+
i17255
3031

3132
# Tree is huge and blows stack for printing Text
3233
i7034.scala

tests/pos/i17255/Bar.java

Lines changed: 6 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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)