@@ -380,11 +380,41 @@ abstract class ClassfileParser {
380380 }
381381
382382 private def lookupClass (name : Name ) = try {
383- if (name containsChar '.' )
384- rootMirror getClassByName name
385- else
383+ def lookupTopLevel = {
384+ if (name containsChar '.' )
385+ rootMirror getClassByName name
386+ else
386387 // FIXME - we shouldn't be doing ad hoc lookups in the empty package, getClassByName should return the class
387- definitions.getMember(rootMirror.EmptyPackageClass , name.toTypeName)
388+ definitions.getMember(rootMirror.EmptyPackageClass , name.toTypeName)
389+ }
390+
391+ // For inner classes we usually don't get here: `classNameToSymbol` already returns the symbol
392+ // of the inner class based on the InnerClass table. However, if the classfile is missing the
393+ // InnerClass entry for `name`, it might still be that there exists an inner symbol (because
394+ // some other classfile _does_ have an InnerClass entry for `name`). In this case, we want to
395+ // return the actual inner symbol (C.D, with owner C), not the top-level symbol C$D. This is
396+ // what the logic below is for (see PR #5822 / scala/bug#9937).
397+ val split = if (isScalaRaw) - 1 else name.lastIndexOf('$' )
398+ if (split > 0 && split < name.length) {
399+ val outerName = name.subName(0 , split)
400+ val innerName = name.subName(split + 1 , name.length).toTypeName
401+ val outerSym = classNameToSymbol(outerName)
402+
403+ // If the outer class C cannot be found, look for a top-level class C$D
404+ if (outerSym.isInstanceOf [StubSymbol ]) lookupTopLevel
405+ else {
406+ // We have a java-defined class name C$D and look for a member D of C. But we don't know if
407+ // D is declared static or not, so we have to search both in class C and its companion.
408+ val r = if (outerSym == clazz)
409+ staticScope.lookup(innerName) orElse
410+ instanceScope.lookup(innerName)
411+ else
412+ lookupMemberAtTyperPhaseIfPossible(outerSym, innerName) orElse
413+ lookupMemberAtTyperPhaseIfPossible(outerSym.companionModule, innerName)
414+ r orElse lookupTopLevel
415+ }
416+ } else
417+ lookupTopLevel
388418 } catch {
389419 // The handler
390420 // - prevents crashes with deficient InnerClassAttributes (scala/bug#2464, 0ce0ad5)
0 commit comments