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

Fix #13523: Survive missing Java inner annotation classfiles #15094

Merged
merged 1 commit into from
May 3, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,16 @@ class ClassfileParser(
val outerName = entry.strippedOuter
val innerName = entry.originalName
val owner = classNameToSymbol(outerName)
val result = atPhase(typerPhase)(getMember(owner, innerName.toTypeName))
val result = owner.denot.infoOrCompleter match
case _: StubInfo if hasAnnotation(entry.jflags) =>
requiredClass(innerName.toTypeName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is not obvious, could you add a comment explaining why this is needed and pointing the reader to the test case?

// It's okay for the classfiles of Java annotations to be missing
// from the classpath. If an annotation is defined as an inner class
// we need to avoid forcing the outer class symbol here, and instead
// return a new stub symbol for the inner class. This is tested by
// `surviveMissingInnerClassAnnot` in AnnotationsTests.scala
case _ =>
atPhase(typerPhase)(getMember(owner, innerName.toTypeName))
assert(result ne NoSymbol,
i"""failure to resolve inner class:
|externalName = ${entry.externalName},
Expand Down
23 changes: 23 additions & 0 deletions compiler/test/dotty/tools/AnnotationsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,26 @@ class AnnotationsTest:
s"A missing annotation while parsing a Java class should be silently ignored but: ${ctx.reporter.summary}")
}
}

@Test def surviveMissingInnerClassAnnot: Unit =
withJavaCompiled(
VirtualJavaSource("Outer.java",
"""|package a.b;
|public @interface Outer { public @interface Value { @interface Immutable {} } }
|""".stripMargin),
VirtualJavaSource("Baz.java",
"""|package a.b;
|@Outer.Value.Immutable abstract class Baz {}""".stripMargin)
) { javaOutputDir =>
Files.delete(javaOutputDir.resolve("a/b/Outer.class"))
Files.delete(javaOutputDir.resolve("a/b/Outer$Value.class"))
Files.delete(javaOutputDir.resolve("a/b/Outer$Value$Immutable.class"))
inCompilerContext(javaOutputDir.toString + File.pathSeparator + TestConfiguration.basicClasspath) {
val cls = requiredClass("a.b.Baz")
val annots = cls.annotations.map(_.tree)
assert(annots == Nil,
s"class Baz should have no visible annotations since Outer.Value.Immutable is not on the classpath, but found: $annots")
assert(!ctx.reporter.hasErrors && !ctx.reporter.hasWarnings,
s"A missing annotation while parsing a Java class should be silently ignored but: ${ctx.reporter.summary}")
}
}