Skip to content

Commit 2183bf9

Browse files
committed
track JDK version only when runtime exception occurs
1 parent f430e44 commit 2183bf9

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ object ClassfileConstants {
1111
inline val JAVA_MINOR_VERSION = 3
1212

1313
inline val JAVA8_MAJOR_VERSION = 52
14-
inline val JAVA_LATEST_MAJOR_VERSION = 65
1514

1615
/** (see http://java.sun.com/docs/books/jvms/second_edition/jvms-clarify.html)
1716
*

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

+30-7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ import scala.compiletime.uninitialized
2929

3030
object ClassfileParser {
3131

32+
object Header:
33+
opaque type Version = Long
34+
35+
object Version:
36+
val Unknown: Version = -1L
37+
38+
def brokenVersionAddendum(classfileVersion: Version)(using Context): String =
39+
if classfileVersion.exists then
40+
val (maj, min) = (classfileVersion.majorVersion, classfileVersion.minorVersion)
41+
val scalaVersion = config.Properties.versionNumberString
42+
i""" (version $maj.$min),
43+
| please check the JDK compatibility of your Scala version ($scalaVersion)"""
44+
else
45+
""
46+
47+
def apply(major: Int, minor: Int): Version =
48+
(major.toLong << 32) | (minor.toLong & 0xFFFFFFFFL)
49+
extension (version: Version)
50+
def exists: Boolean = version != Unknown
51+
def majorVersion: Int = (version >> 32).toInt
52+
def minorVersion: Int = (version & 0xFFFFFFFFL).toInt
53+
3254
import ClassfileConstants._
3355

3456
/** Marker trait for unpicklers that can be embedded in classfiles. */
@@ -57,7 +79,7 @@ object ClassfileParser {
5779
}
5880
}
5981

60-
private[classfile] def parseHeader(classfile: AbstractFile)(using in: DataReader): Unit = {
82+
private[classfile] def parseHeader(classfile: AbstractFile)(using in: DataReader): Header.Version = {
6183
val magic = in.nextInt
6284
if (magic != JAVA_MAGIC)
6385
throw new IOException(s"class file '${classfile}' has wrong magic number 0x${toHexString(magic)}, should be 0x${toHexString(JAVA_MAGIC)}")
@@ -68,9 +90,7 @@ object ClassfileParser {
6890
(minorVersion < JAVA_MINOR_VERSION)))
6991
throw new IOException(
7092
s"class file '${classfile}' has unknown version $majorVersion.$minorVersion, should be at least $JAVA_MAJOR_VERSION.$JAVA_MINOR_VERSION")
71-
if majorVersion > JAVA_LATEST_MAJOR_VERSION then
72-
throw new IOException(
73-
s"class file '${classfile}' has unknown version $majorVersion.$minorVersion, and was compiled by a newer JDK than supported by this Scala version, please update to a newer Scala version.")
93+
Header.Version(majorVersion, minorVersion)
7494
}
7595

7696
abstract class AbstractConstantPool(using in: DataReader) {
@@ -263,6 +283,7 @@ class ClassfileParser(
263283
protected var classTParams: Map[Name, Symbol] = Map()
264284

265285
private var Scala2UnpicklingMode = Mode.Scala2Unpickling
286+
private var classfileVersion: Header.Version = Header.Version.Unknown
266287

267288
classRoot.info = NoLoader().withDecls(instanceScope)
268289
moduleRoot.info = NoLoader().withDecls(staticScope).withSourceModule(staticModule)
@@ -275,7 +296,7 @@ class ClassfileParser(
275296
def run()(using Context): Option[Embedded] = try ctx.base.reusableDataReader.withInstance { reader =>
276297
implicit val reader2 = reader.reset(classfile)
277298
report.debuglog("[class] >> " + classRoot.fullName)
278-
parseHeader(classfile)
299+
classfileVersion = parseHeader(classfile)
279300
this.pool = new ConstantPool
280301
val res = parseClass()
281302
this.pool = null
@@ -284,9 +305,11 @@ class ClassfileParser(
284305
catch {
285306
case e: RuntimeException =>
286307
if (ctx.debug) e.printStackTrace()
308+
val addendum = Header.Version.brokenVersionAddendum(classfileVersion)
287309
throw new IOException(
288-
i"""class file ${classfile.canonicalPath} is broken, reading aborted with ${e.getClass}
289-
|${Option(e.getMessage).getOrElse("")}""")
310+
i""" class file ${classfile.canonicalPath} is broken$addendum,
311+
| reading aborted with ${e.getClass}:
312+
| ${Option(e.getMessage).getOrElse("")}""")
290313
}
291314

292315
/** Return the class symbol of the given name. */

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import dotty.tools.dotc.util._
1414
import dotty.tools.io.AbstractFile
1515
import dotty.tools.tasty.TastyReader
1616

17+
import ClassfileParser.Header
18+
1719
import java.io.IOException
1820
import java.lang.Integer.toHexString
1921
import java.util.UUID
@@ -23,20 +25,23 @@ class ClassfileTastyUUIDParser(classfile: AbstractFile)(ictx: Context) {
2325
import ClassfileConstants._
2426

2527
private var pool: ConstantPool = uninitialized // the classfile's constant pool
28+
private var classfileVersion: Header.Version = Header.Version.Unknown
2629

2730
def checkTastyUUID(tastyUUID: UUID)(using Context): Unit = try ctx.base.reusableDataReader.withInstance { reader =>
2831
implicit val reader2 = reader.reset(classfile)
29-
ClassfileParser.parseHeader(classfile)
32+
this.classfileVersion = ClassfileParser.parseHeader(classfile)
3033
this.pool = new ConstantPool
3134
checkTastyAttr(tastyUUID)
3235
this.pool = null
3336
}
3437
catch {
3538
case e: RuntimeException =>
3639
if (ctx.debug) e.printStackTrace()
40+
val addendum = Header.Version.brokenVersionAddendum(classfileVersion)
3741
throw new IOException(
38-
i"""class file ${classfile.canonicalPath} is broken, reading aborted with ${e.getClass}
39-
|${Option(e.getMessage).getOrElse("")}""")
42+
i""" class file ${classfile.canonicalPath} is broken$addendum,
43+
| reading aborted with ${e.getClass}:
44+
| ${Option(e.getMessage).getOrElse("")}""")
4045
}
4146

4247
private def checkTastyAttr(tastyUUID: UUID)(using ctx: Context, in: DataReader): Unit = {

0 commit comments

Comments
 (0)