Commit 7f90796
committed
Handle TypeError during Erasure phase
Fixes #20491
Wrap the `transform` method in Erasure with a try-catch to handle `TypeError` (including `MissingType`).
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 unhandled `MissingType` exception during the Erasure phase instead of reporting an error.
`MissingType` is thrown from `TypeErasure.sigName` or `checkedSuperType`, which are called from `TypeErasure.eraseXXX`.
These errors are usually caught by `Typer.handleTypeError` or `TreePickler.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.transform` with a try-catch to intercept the `TypeError` and call `report.error`.
We can test the change manually:
```
.
|-- cp
| `-- ox
| `-- Ox.class
`-- test.scala
```
`Ox.class` is compiled from:
```scala
//> using jvm 21
package ox
import java.util.concurrent.StructuredTaskScope
trait Ox {
def scope: StructuredTaskScope[Any]
}
```
and `test.scala`:
```scala
//> using jvm 17
given ox.Ox = ???
```
`$ scala-cli compile test.scala -S 3.8.1-RC1-bin-SNAPSHOT --extra-jars ./cp`
now it should compile fails, instead of crash.1 parent 4d4f543 commit 7f90796
1 file changed
+6
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
52 | | - | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| |||
130 | 131 | | |
131 | 132 | | |
132 | 133 | | |
133 | | - | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
134 | 138 | | |
135 | 139 | | |
136 | 140 | | |
| |||
0 commit comments