Handle TypeError during Erasure phase #24817
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #20491
Wrap the
transformmethod in Erasure with a try-catch to handleTypeError(includingMissingType). These errors can occur during TypeErasure when accessing types from dependencies compiled with newer JDK versions that use APIs unavailable in the current runtime.For example, when a library compiled with JDK 21 (using
java.util.concurrent.StructuredTaskScope) is used in a project running on JDK 17, the compiler currently crashes with an unhandledMissingTypeexception during the Erasure phase instead of reporting an error.MissingTypeis thrown fromTypeErasure.sigNameorcheckedSuperType, which are called fromTypeErasure.eraseXXX. These errors are usually caught byTyper.handleTypeErrororTreePickler.pickleType, but those handlers do not cover exceptions thrown during the Erasure. (It seems that during typer, the compiler trusts the information from the classfile or tasty without verifying that all referenced types actually exist on the classpath).This commit encloses
Erasure.transformwith a try-catch to intercept theTypeErrorand callreport.error.We can test the change manually:
Ox.classis compiled from:and
test.scala:$ scala-cli compile test.scala -S 3.8.1-RC1-bin-SNAPSHOT --extra-jars ./cpnow it should compile fails, instead of crash.
I'm wondering how do I test on CI.Is it okay to put a classfile on tests/neg/i20491/?Can we test it only for specific JVM version?