Skip to content

Commit 4e822d7

Browse files
adriaanmretronym
authored andcommitted
Javadoc: java static name resolution
[Jakob Odersky <jodersky@gmail.com>: remove obsolete comments and fix tests]
1 parent 4b77734 commit 4e822d7

File tree

5 files changed

+41
-22
lines changed

5 files changed

+41
-22
lines changed

src/compiler/scala/tools/nsc/javac/JavaParsers.scala

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -600,26 +600,8 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners {
600600
Import(Ident(cdef.name.toTermName), ImportSelector.wildList)
601601
}
602602

603-
// Importing the companion object members cannot be done uncritically: see
604-
// ticket #2377 wherein a class contains two static inner classes, each of which
605-
// has a static inner class called "Builder" - this results in an ambiguity error
606-
// when each performs the import in the enclosing class's scope.
607-
//
608-
// To address this I moved the import Companion._ inside the class, as the first
609-
// statement. This should work without compromising the enclosing scope, but may (?)
610-
// end up suffering from the same issues it does in scala - specifically that this
611-
// leaves auxiliary constructors unable to access members of the companion object
612-
// as unqualified identifiers.
613-
def addCompanionObject(statics: List[Tree], cdef: ClassDef): List[Tree] = {
614-
def implWithImport(importStmt: Tree) = deriveTemplate(cdef.impl)(importStmt :: _)
615-
// if there are no statics we can use the original cdef, but we always
616-
// create the companion so import A._ is not an error (see ticket #1700)
617-
val cdefNew =
618-
if (statics.isEmpty) cdef
619-
else deriveClassDef(cdef)(_ => implWithImport(importCompanionObject(cdef)))
620-
621-
List(makeCompanionObject(cdefNew, statics), cdefNew)
622-
}
603+
def addCompanionObject(statics: List[Tree], cdef: ClassDef): List[Tree] =
604+
List(makeCompanionObject(cdef, statics), cdef)
623605

624606
def importDecl(): List[Tree] = {
625607
accept(IMPORT)

src/compiler/scala/tools/nsc/typechecker/Contexts.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,16 @@ trait Contexts { self: Analyzer =>
10161016
|| unit.exists && s.sourceFile != unit.source.file
10171017
)
10181018
)
1019-
def lookupInPrefix(name: Name) = pre member name filter qualifies
1019+
def lookupInPrefix(name: Name) = {
1020+
val sym = pre.member(name).filter(qualifies)
1021+
def isNonPackageNoModuleClass(sym: Symbol) =
1022+
sym.isClass && !sym.isModuleClass && !sym.isPackageClass
1023+
if (!sym.exists && unit.isJava && isNonPackageNoModuleClass(pre.typeSymbol)) {
1024+
// TODO factor out duplication with Typer::inCompanionForJavaStatic
1025+
val pre1 = companionSymbolOf(pre.typeSymbol, this).typeOfThis
1026+
pre1.member(name).filter(qualifies).andAlso(_ => pre = pre1)
1027+
} else sym
1028+
}
10201029
def accessibleInPrefix(s: Symbol) = isAccessible(s, pre, superAccess = false)
10211030

10221031
def searchPrefix = {

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4719,6 +4719,16 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
47194719
if (isStableContext(tree, mode, pt)) tree setType clazz.thisType else tree
47204720
}
47214721

4722+
4723+
// For Java, instance and static members are in the same scope, but we put the static ones in the companion object
4724+
// so, when we can't find a member in the class scope, check the companion
4725+
def inCompanionForJavaStatic(pre: Type, cls: Symbol, name: Name): Symbol =
4726+
if (!(context.unit.isJava && cls.isClass && !cls.isModuleClass)) NoSymbol else {
4727+
val companion = companionSymbolOf(cls, context)
4728+
if (!companion.exists) NoSymbol
4729+
else member(gen.mkAttributedRef(pre, companion), name) // assert(res.isStatic, s"inCompanionJavaStatic($pre, $cls, $name) = $res ${res.debugFlagString}")
4730+
}
4731+
47224732
/* Attribute a selection where `tree` is `qual.name`.
47234733
* `qual` is already attributed.
47244734
*/
@@ -4745,7 +4755,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
47454755
dyna.wrapErrors(t, (_.typed1(t, mode, pt)))
47464756
}
47474757

4748-
val sym = tree.symbol orElse member(qual, name) orElse {
4758+
val sym = tree.symbol orElse member(qual, name) orElse inCompanionForJavaStatic(qual.tpe.prefix, qual.symbol, name) orElse {
47494759
// symbol not found? --> try to convert implicitly to a type that does have the required
47504760
// member. Added `| PATTERNmode` to allow enrichment in patterns (so we can add e.g., an
47514761
// xml member to StringContext, which in turn has an unapply[Seq] method)

test/files/pos/t2377b/Q.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Q {
2+
3+
public static class Builder {}
4+
5+
public static class Inner {
6+
public static class Builder { public void innerMethod() {} }
7+
public Builder foo() { return new Builder(); } // this line gives an error, that Builder is ambiguous
8+
9+
public Inner.Builder viaSelect() { return new Builder(); } // this line gives an error, that Builder is ambiguous
10+
}
11+
12+
}
13+

test/files/pos/t2377b/a.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test {
2+
(new Q.Inner).foo.innerMethod
3+
(new Q.Inner).viaSelect.innerMethod
4+
5+
}

0 commit comments

Comments
 (0)