Skip to content

Commit 9b2cbf0

Browse files
Backport "fix(#17255): cannot find Scala companion module from Java" to LTS (#20959)
Backports #19773 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents df7b0bd + acf286f commit 9b2cbf0

File tree

8 files changed

+66
-4
lines changed

8 files changed

+66
-4
lines changed

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

+16-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ object ContextOps:
4141
else pre.findMember(name, pre, required, excluded)
4242
}
4343
else // we are in the outermost context belonging to a class; self is invisible here. See inClassContext.
44-
ctx.owner.findMember(name, ctx.owner.thisType, required, excluded)
44+
if ctx.isJava then
45+
javaFindMember(name, ctx.owner.thisType, lookInCompanion = true,required, excluded)
46+
else
47+
ctx.owner.findMember(name, ctx.owner.thisType, required, excluded)
4548
else
4649
ctx.scope.denotsNamed(name).filterWithFlags(required, excluded).toDenot(NoPrefix)
4750
}
@@ -55,11 +58,20 @@ object ContextOps:
5558
final def javaFindMember(name: Name, pre: Type, lookInCompanion: Boolean, required: FlagSet = EmptyFlags, excluded: FlagSet = EmptyFlags): Denotation =
5659
assert(ctx.isJava)
5760
inContext(ctx) {
58-
61+
import dotty.tools.dotc.core.NameOps.*
5962
val preSym = pre.typeSymbol
60-
6163
// 1. Try to search in current type and parents.
62-
val directSearch = pre.findMember(name, pre, required, excluded)
64+
val directSearch =
65+
def asModule =
66+
if name.isTypeName && name.endsWith(StdNames.str.MODULE_SUFFIX) then
67+
pre.findMember(name.stripModuleClassSuffix.moduleClassName, pre, required, excluded) match
68+
case NoDenotation => NoDenotation
69+
case symDenot: SymDenotation =>
70+
symDenot.companionModule.denot
71+
else NoDenotation
72+
pre.findMember(name, pre, required, excluded) match
73+
case NoDenotation => asModule
74+
case denot => denot
6375

6476
// 2. Try to search in companion class if current is an object.
6577
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
@@ -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

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

+2
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ t6138
4444
t6138-2
4545
i12656.scala
4646
trait-static-forwarder
47+
i17255
48+

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/Baz.java

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

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

tests/run/i17255/J.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package p;
2+
3+
public class J {
4+
public static J j = new J();
5+
public static p.J f() {
6+
return p.J.j;
7+
}
8+
public static Module$ module2() {
9+
return p.Module$.MODULE$;
10+
}
11+
public static p.Module$ module() {
12+
return p.Module$.MODULE$;
13+
}
14+
15+
public String toString() { return "J"; }
16+
}

tests/run/i17255/Module.scala

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// scalajs: --skip
2+
3+
package p {
4+
object Module {
5+
override def toString = "Module"
6+
}
7+
}
8+
9+
object Test extends App {
10+
assert(p.J.f().toString == "J")
11+
assert(p.J.module().toString == "Module")
12+
assert(p.J.module2().toString == "Module")
13+
}

0 commit comments

Comments
 (0)