Skip to content

Commit 9c651d1

Browse files
committed
track JDK version only when runtime exception occurs
[Cherry-picked 2183bf9][modified]
1 parent 6f33c61 commit 9c651d1

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

compiler/src/dotty/tools/dotc/core/classfile/ClassfileConstants.scala

Lines changed: 0 additions & 1 deletion
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
*

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@ import io.{AbstractFile, ZipArchive}
2525
import scala.util.control.NonFatal
2626

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

3052
/** Marker trait for unpicklers that can be embedded in classfiles. */
@@ -53,7 +75,7 @@ object ClassfileParser {
5375
}
5476
}
5577

56-
private[classfile] def parseHeader(classfile: AbstractFile)(using in: DataReader): Unit = {
78+
private[classfile] def parseHeader(classfile: AbstractFile)(using in: DataReader): Header.Version = {
5779
val magic = in.nextInt
5880
if (magic != JAVA_MAGIC)
5981
throw new IOException(s"class file '${classfile}' has wrong magic number 0x${toHexString(magic)}, should be 0x${toHexString(JAVA_MAGIC)}")
@@ -64,9 +86,7 @@ object ClassfileParser {
6486
(minorVersion < JAVA_MINOR_VERSION)))
6587
throw new IOException(
6688
s"class file '${classfile}' has unknown version $majorVersion.$minorVersion, should be at least $JAVA_MAJOR_VERSION.$JAVA_MINOR_VERSION")
67-
if majorVersion > JAVA_LATEST_MAJOR_VERSION then
68-
throw new IOException(
69-
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.")
89+
Header.Version(majorVersion, minorVersion)
7090
}
7191

7292
}
@@ -89,6 +109,7 @@ class ClassfileParser(
89109
protected var classTParams: Map[Name, Symbol] = Map()
90110

91111
private var Scala2UnpicklingMode = Mode.Scala2Unpickling
112+
private var classfileVersion: Header.Version = Header.Version.Unknown
92113

93114
classRoot.info = NoLoader().withDecls(instanceScope)
94115
moduleRoot.info = NoLoader().withDecls(staticScope).withSourceModule(staticModule)
@@ -101,7 +122,7 @@ class ClassfileParser(
101122
def run()(using Context): Option[Embedded] = try ctx.base.reusableDataReader.withInstance { reader =>
102123
implicit val reader2 = reader.reset(classfile)
103124
report.debuglog("[class] >> " + classRoot.fullName)
104-
parseHeader(classfile)
125+
classfileVersion = parseHeader(classfile)
105126
this.pool = new ConstantPool
106127
val res = parseClass()
107128
this.pool = null
@@ -110,9 +131,11 @@ class ClassfileParser(
110131
catch {
111132
case e: RuntimeException =>
112133
if (ctx.debug) e.printStackTrace()
134+
val addendum = Header.Version.brokenVersionAddendum(classfileVersion)
113135
throw new IOException(
114-
i"""class file ${classfile.canonicalPath} is broken, reading aborted with ${e.getClass}
115-
|${Option(e.getMessage).getOrElse("")}""")
136+
i""" class file ${classfile.canonicalPath} is broken$addendum,
137+
| reading aborted with ${e.getClass}:
138+
| ${Option(e.getMessage).getOrElse("")}""")
116139
}
117140

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

0 commit comments

Comments
 (0)