Skip to content

Commit 9d9664f

Browse files
dwijnandWojciechMazur
authored andcommitted
Fail early & more clearly on shaded-broken classfiles
The shading utility uses for the coursier or coursierapi classes has removed the RuntimeVisibleAnnotation attribute after the ScalaSignature attribute. Parsing such a classfile caused the Scala 3 compiler to parse the file as a Java classfile. It seems there something about how we deal with packages and package objects (which don't exist in Java) when we are incorrectly sent down this code path. The Scala 2 compiler already correctly caught and failed on this early, with this detail, so look to do the same here. Automating the test for this isn't easy. Because relying on an external dependency would be a hazard for CI, long term. Perhaps we could try to recreate a classfile with a similar bytecode problem, but that would be quite involved as well. I guess we could commit some or all of the classfiles from the original, but it's generally advised not to commit binaries to the source repo. So instead I'm leaving instructions as to how I manually tested this, in preparing this change. I used coursier's `cs` binary to download the jars and produce a classpath string: cs fetch -p org.scalameta:scalafmt-dynamic_2.13:3.8.1 Then I invoked `scalac` in the sbt shell, passing that classpath string: scalac -classpath $classpathAbove i20405.scala scalac -classpath $classpathAbove i20555.scala Using the two minisations in the 20405 and 20555 issues. [Cherry-picked cecfa8c]
1 parent bccc81a commit 9d9664f

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Diff for: compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,10 @@ class ClassfileParser(
10391039
// attribute isn't, this classfile is a compilation artifact.
10401040
return Some(NoEmbedded)
10411041

1042-
if (scan(tpnme.ScalaSignatureATTR) && scan(tpnme.RuntimeVisibleAnnotationATTR)) {
1042+
if (scan(tpnme.ScalaSignatureATTR)) {
1043+
if !scan(tpnme.RuntimeVisibleAnnotationATTR) then
1044+
report.error(em"No RuntimeVisibleAnnotations in classfile with ScalaSignature attribute: ${classRoot.fullName}")
1045+
return None
10431046
val attrLen = in.nextInt
10441047
val nAnnots = in.nextChar
10451048
var i = 0

0 commit comments

Comments
 (0)