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

In mixed compilation, allow Java sources to reference MODULE$ #10644

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/compiler/scala/tools/nsc/typechecker/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1316,8 +1316,20 @@ trait Contexts { self: Analyzer =>
}

final def javaFindMember(pre: Type, name: Name, qualifies: Symbol => Boolean): (Type, Symbol) = {
val sym = pre.member(name).filter(qualifies)
val preSym = pre.typeSymbol
val sym = {
def asModule =
if (name.isTypeName && nme.isModuleName(name))
pre.member(name.dropModule.toTermName) match {
case nope @ NoSymbol => nope
case member => member.filter(qualifies).moduleClass
}
else NoSymbol
pre.member(name) match {
case NoSymbol => asModule
case member => member.filter(qualifies)
}
}
if (sym.exists || preSym.isPackageClass || !preSym.isClass) (pre, sym)
else {
// In Java code, static inner classes, which we model as members of the companion object,
Expand Down
7 changes: 3 additions & 4 deletions src/compiler/scala/tools/nsc/typechecker/Typers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5356,9 +5356,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
// For Java, instance and static members are in the same scope, but we put the static ones in the companion object
// so, when we can't find a member in the class scope, check the companion
def inCompanionForJavaStatic(cls: Symbol, name: Name): Symbol =
if (!(context.unit.isJava && cls.isClass)) NoSymbol else {
context.javaFindMember(cls.typeOfThis, name, _.isStaticMember)._2
}
if (!(context.unit.isJava && cls.isClass)) NoSymbol
else context.javaFindMember(cls.typeOfThis, name, s => s.isStaticMember || s.isStaticModule)._2

/* Attribute a selection where `tree` is `qual.name`.
* `qual` is already attributed.
Expand All @@ -5379,7 +5378,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
if (!isPastTyper && isUniversalMember(result.symbol))
context.warning(tree.pos, s"dubious usage of ${result.symbol} with unit value", WarningCategory.LintUniversalMethods)

val sym = tree.symbol orElse member(qual.tpe, name) orElse inCompanionForJavaStatic(qual.tpe.typeSymbol, name)
val sym = tree.symbol orElse member(qualTp, name) orElse inCompanionForJavaStatic(qualTp.typeSymbol, name)
if ((sym eq NoSymbol) && name != nme.CONSTRUCTOR && mode.inAny(EXPRmode | PATTERNmode)) {
// symbol not found? --> try to convert implicitly to a type that does have the required
// member. Added `| PATTERNmode` to allow enrichment in patterns (so we can add e.g., an
Expand Down
2 changes: 1 addition & 1 deletion src/reflect/scala/reflect/internal/Names.scala
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ trait Names extends api.Names {

def dropLocal: TermName = toTermName stripSuffix NameTransformer.LOCAL_SUFFIX_STRING
def dropSetter: TermName = toTermName stripSuffix NameTransformer.SETTER_SUFFIX_STRING
def dropModule: ThisNameType = this stripSuffix NameTransformer.MODULE_SUFFIX_STRING
def dropModule: ThisNameType = stripSuffix(NameTransformer.MODULE_SUFFIX_STRING)
def localName: TermName = getterName append NameTransformer.LOCAL_SUFFIX_STRING
def setterName: TermName = getterName append NameTransformer.SETTER_SUFFIX_STRING
def getterName: TermName = dropTraitSetterSeparator.dropSetter.dropLocal
Expand Down
14 changes: 14 additions & 0 deletions test/files/run/t9714/J.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

package p;

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

public String toString() { return "J"; }
}
10 changes: 10 additions & 0 deletions test/files/run/t9714/Module.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package p {
object Module {
override def toString = "Module"
}
}

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