Skip to content

Commit

Permalink
fix(#17255): cannot find Scala companion module from Java (#19773)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bishabosha committed Feb 28, 2024
2 parents 93d8b1a + 96c91da commit de25aa3
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 4 deletions.
20 changes: 16 additions & 4 deletions compiler/src/dotty/tools/dotc/core/ContextOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ object ContextOps:
else pre.findMember(name, pre, required, excluded)
}
else // we are in the outermost context belonging to a class; self is invisible here. See inClassContext.
ctx.owner.findMember(name, ctx.owner.thisType, required, excluded)
if ctx.isJava then
javaFindMember(name, ctx.owner.thisType, lookInCompanion = true,required, excluded)
else
ctx.owner.findMember(name, ctx.owner.thisType, required, excluded)
else
ctx.scope.denotsNamed(name).filterWithFlags(required, excluded).toDenot(NoPrefix)
}
Expand All @@ -55,11 +58,20 @@ object ContextOps:
final def javaFindMember(name: Name, pre: Type, lookInCompanion: Boolean, required: FlagSet = EmptyFlags, excluded: FlagSet = EmptyFlags): Denotation =
assert(ctx.isJava)
inContext(ctx) {

import dotty.tools.dotc.core.NameOps.*
val preSym = pre.typeSymbol

// 1. Try to search in current type and parents.
val directSearch = pre.findMember(name, pre, required, excluded)
val directSearch =
def asModule =
if name.isTypeName && name.endsWith(StdNames.str.MODULE_SUFFIX) then
pre.findMember(name.stripModuleClassSuffix.moduleClassName, pre, required, excluded) match
case NoDenotation => NoDenotation
case symDenot: SymDenotation =>
symDenot.companionModule.denot
else NoDenotation
pre.findMember(name, pre, required, excluded) match
case NoDenotation => asModule
case denot => denot

// 2. Try to search in companion class if current is an object.
def searchCompanionClass = if lookInCompanion && preSym.is(Flags.Module) then
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotc/pos-test-pickling.blacklist
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ i16649-irrefutable.scala
strict-pattern-bindings-3.0-migration.scala
i17186b.scala
i11982a.scala
i17255

# Tree is huge and blows stack for printing Text
i7034.scala
Expand Down
2 changes: 2 additions & 0 deletions compiler/test/dotc/run-test-pickling.blacklist
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ t6138
t6138-2
i12656.scala
trait-static-forwarder
i17255

6 changes: 6 additions & 0 deletions tests/pos/i17255/Bar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package example;


public class Bar {
private static final example.Foo$ MOD = example.Foo$.MODULE$;
}
7 changes: 7 additions & 0 deletions tests/pos/i17255/Baz.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package example;

import example.Foo$;

public class Baz {
private static final Foo$ MOD = Foo$.MODULE$;
}
5 changes: 5 additions & 0 deletions tests/pos/i17255/Foo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package example

case class Foo(i: Int)

object Foo
16 changes: 16 additions & 0 deletions tests/run/i17255/J.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package p;

public class J {
public static J j = new J();
public static p.J f() {
return p.J.j;
}
public static Module$ module2() {
return p.Module$.MODULE$;
}
public static p.Module$ module() {
return p.Module$.MODULE$;
}

public String toString() { return "J"; }
}
13 changes: 13 additions & 0 deletions tests/run/i17255/Module.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// scalajs: --skip

package p {
object Module {
override def toString = "Module"
}
}

object Test extends App {
assert(p.J.f().toString == "J")
assert(p.J.module().toString == "Module")
assert(p.J.module2().toString == "Module")
}

0 comments on commit de25aa3

Please sign in to comment.