Skip to content

Commit 681e17e

Browse files
smarterlrytzbbarker
committed
Kotlin interop: Find nested class if InnerClass entry is missing
This is a port of scala/scala#5822 which works around a bug in Kotlin (https://youtrack.jetbrains.com/issue/KT-27936). Fixes #12086. Co-Authored-By: Lukas Rytz <[email protected]> Co-Authored-By: Brandon Barker <[email protected]>
1 parent 064c213 commit 681e17e

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

+33-4
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,39 @@ class ClassfileParser(
111111
}
112112

113113
/** Return the class symbol of the given name. */
114-
def classNameToSymbol(name: Name)(using Context): Symbol = innerClasses.get(name.toString) match {
115-
case Some(entry) => innerClasses.classSymbol(entry)
116-
case None => requiredClass(name)
117-
}
114+
def classNameToSymbol(name: Name)(using Context): Symbol =
115+
val nameStr = name.toString
116+
innerClasses.get(nameStr) match
117+
case Some(entry) => innerClasses.classSymbol(entry)
118+
case None =>
119+
def lookupTopLevel(): Symbol = requiredClass(name)
120+
// For inner classes we usually don't get to this branch: `innerClasses.classSymbol` already returns the symbol
121+
// of the inner class based on the InnerClass table. However, if the classfile is missing the
122+
// InnerClass entry for `name`, it might still be that there exists an inner symbol (because
123+
// some other classfile _does_ have an InnerClass entry for `name`). In this case, we want to
124+
// return the actual inner symbol (C.D, with owner C), not the top-level symbol C$D. This is
125+
// what the logic below is for (see scala/bug#9937 / lampepfl/dotty#12086).
126+
val split = nameStr.lastIndexOf('$')
127+
if split < 0 || split >= nameStr.length - 1 then
128+
lookupTopLevel()
129+
else
130+
val outerNameStr = nameStr.substring(0, split)
131+
val innerNameStr = nameStr.substring(split + 1, nameStr.length)
132+
val outerSym = classNameToSymbol(outerNameStr.toTypeName)
133+
outerSym.denot.infoOrCompleter match
134+
case _: StubInfo =>
135+
// If the outer class C cannot be found, look for a top-level class C$D
136+
lookupTopLevel()
137+
case _ =>
138+
// We have a java-defined class name C$D and look for a member D of C. But we don't know if
139+
// D is declared static or not, so we have to search both in class C and its companion.
140+
val innerName = innerNameStr.toTypeName
141+
val r =
142+
if outerSym eq classRoot.symbol then
143+
instanceScope.lookup(innerName).orElse(staticScope.lookup(innerName))
144+
else
145+
outerSym.info.member(innerName).orElse(outerSym.asClass.companionModule.info.member(innerName)).symbol
146+
r.orElse(lookupTopLevel())
118147

119148
var sawPrivateConstructor: Boolean = false
120149

tests/run/i12086/Test_1.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class C$D { public int i() { return 1; } }
2+
class C$E { public int i() { return 1; } }
3+
class C$F$G { public int i() { return 1; } }
4+
5+
// Test1 has a reference to C$D, which is a top-level class in this case,
6+
// so there's no INNERCLASS attribute in Test1
7+
class Test_1 {
8+
static C$D mD(C$D cd) { return cd; }
9+
static C$E mE(C$E ce) { return ce; }
10+
static C$F$G mG(C$F$G cg ) { return cg; }
11+
}

tests/run/i12086/Test_2.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class C {
2+
class D { public int i() { return 2; } }
3+
static class E { public int i() { return 2; } }
4+
static class F { static class G { public int i() { return 2; } } }
5+
}
6+
7+
// Test2 has an INNERCLASS attribute for C$D
8+
class Test_2 {
9+
public static int acceptD(C.D cd) { return cd.i(); }
10+
public static int acceptE(C.E ce) { return ce.i(); }
11+
public static int acceptG(C.F.G cg ) { return cg.i(); }
12+
}

tests/run/i12086/Test_3.scala

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
val c = new C
4+
assert(Test_2.acceptD(Test_1.mD(new c.D)) == 2)
5+
assert(Test_2.acceptE(Test_1.mE(new C.E)) == 2)
6+
assert(Test_2.acceptG(Test_1.mG(new C.F.G)) == 2)
7+
}
8+
}

0 commit comments

Comments
 (0)