|
| 1 | +package dotty.tools.tasty |
| 2 | + |
| 3 | +import java.util.UUID |
| 4 | + |
| 5 | +import TastyFormat.{MajorVersion, ScalaMajorVersion, ScalaMinorVersion, ExperimentalVersion, header} |
| 6 | + |
| 7 | +/** |
| 8 | + * The Tasty Header consists of four fields: |
| 9 | + * - uuid |
| 10 | + * - contains a hash of the sections of the tasty file |
| 11 | + * - majorVersion |
| 12 | + * - matching the TASTy format version that last broke backwards compatibility |
| 13 | + * - scalaMajorVersion |
| 14 | + * - matching Scala compiler that last broke forward compatibility |
| 15 | + * - scalaMinorVersion |
| 16 | + * - matching Scala compiler that last broke forward compatibility |
| 17 | + * - experimentalVersion |
| 18 | + * - 0 for final compiler version |
| 19 | + * - positive for between scala major/minor versions and forward compatibility |
| 20 | + * is broken since the previous stable version. |
| 21 | + */ |
| 22 | +sealed abstract case class TastyHeader( |
| 23 | + uuid: UUID, |
| 24 | + majorVersion: Int, |
| 25 | + scalaMajorVersion: Int, |
| 26 | + scalaMinorVersion: Int, |
| 27 | + experimentalVersion: Int |
| 28 | +) |
| 29 | + |
| 30 | +class TastyHeaderUnpickler(reader: TastyReader) { |
| 31 | + import reader._ |
| 32 | + |
| 33 | + def this(bytes: Array[Byte]) = this(new TastyReader(bytes)) |
| 34 | + |
| 35 | + /** reads and verifies the TASTy version, extracting the UUID */ |
| 36 | + def readHeader(): UUID = |
| 37 | + readFullHeader().uuid |
| 38 | + |
| 39 | + /** reads and verifies the TASTy version, extracting the whole header */ |
| 40 | + def readFullHeader(): TastyHeader = { |
| 41 | + |
| 42 | + def showScalaVersion(maj: Int, min: Int, exp: Int) = { |
| 43 | + val expStr = if (exp == 0) "" else s" [unstable release: $exp]" |
| 44 | + s"$maj.$min$expStr" |
| 45 | + } |
| 46 | + |
| 47 | + def expectedScala = showScalaVersion(ScalaMajorVersion, ScalaMinorVersion, ExperimentalVersion) |
| 48 | + |
| 49 | + def myScalaV = s"$ScalaMajorVersion.$ScalaMinorVersion.x" |
| 50 | + |
| 51 | + def myAddendum = { |
| 52 | + if (ExperimentalVersion > 0) |
| 53 | + "\nNote that you are currently using an unstable compiler version." |
| 54 | + else |
| 55 | + "" |
| 56 | + } |
| 57 | + |
| 58 | + for (i <- 0 until header.length) |
| 59 | + check(readByte() == header(i), "not a TASTy file") |
| 60 | + val majorVersion = readNat() |
| 61 | + if (majorVersion <= 27) { // old behavior before Scala 3.0.0-M4 |
| 62 | + val minorVersion = readNat() |
| 63 | + throw new UnpickleException( |
| 64 | + s"""TASTy signature is from a backwards incompatible release. |
| 65 | + | expected: {majorVersion: $MajorVersion, scalaVersion: $expectedScala} |
| 66 | + | found : {majorVersion: $majorVersion, minorVersion: $minorVersion} |
| 67 | + | |
| 68 | + |Please recompile this TASTy with at least Scala $myScalaV.$myAddendum""".stripMargin |
| 69 | + ) |
| 70 | + } |
| 71 | + else { |
| 72 | + check(majorVersion <= MajorVersion, // layout of header may have changed in future release |
| 73 | + s"""TASTy signature is from a future release of Scala with unknown TASTy format. |
| 74 | + | expected: {majorVersion: $MajorVersion, scalaVersion: $expectedScala} |
| 75 | + | found : {majorVersion: $majorVersion, ...} |
| 76 | + | |
| 77 | + |Please use TASTy compiled by Scala $myScalaV.$myAddendum""".stripMargin) |
| 78 | + val scalaMajorVersion = readNat() |
| 79 | + val scalaMinorVersion = readNat() |
| 80 | + val experimentalVersion = readNat() |
| 81 | + // |=============|==============|========| |
| 82 | + // | Our Version | Read Version | Valid | |
| 83 | + // |=============|==============|========| |
| 84 | + // | 3.0 @ 0 | 3.0 @ 0 | true | |
| 85 | + // | 3.0 @ 0 | 3.0 @ 1 | false | |
| 86 | + // | 3.0 @ 1 | 3.0 @ 0 | true | |
| 87 | + // | 3.0 | 3.1 | false | |
| 88 | + // | 3.1 | 3.0 | true | |
| 89 | + // | 4.0 | 3.0 | true | |
| 90 | + // | 4.0 | 3.1 | true | |
| 91 | + // | 4.1 | 3.0 | true | |
| 92 | + // | 3.0 | 4.0 | false | |
| 93 | + // | 3.0 | 4.1 | false | |
| 94 | + // | 3.1 | 4.0 | false | |
| 95 | + // |=============|==============|========| |
| 96 | + val validVersion = ( |
| 97 | + majorVersion == MajorVersion |
| 98 | + && scalaMajorVersion <= ScalaMajorVersion |
| 99 | + && !(scalaMajorVersion == ScalaMajorVersion && scalaMinorVersion > ScalaMinorVersion) |
| 100 | + && !( |
| 101 | + scalaMajorVersion == ScalaMajorVersion |
| 102 | + && scalaMinorVersion == ScalaMinorVersion |
| 103 | + && experimentalVersion > ExperimentalVersion |
| 104 | + ) |
| 105 | + ) |
| 106 | + check(validVersion, { |
| 107 | + val foundScala = showScalaVersion(scalaMajorVersion, scalaMinorVersion, experimentalVersion) |
| 108 | + val foundAddendum = { |
| 109 | + if (experimentalVersion > 0) |
| 110 | + "\nNote that this TASTy file was produced by an unstable compiler version." |
| 111 | + else |
| 112 | + "" |
| 113 | + } |
| 114 | + if (majorVersion < MajorVersion) { |
| 115 | + s"""TASTy signature is from a backwards incompatible release. |
| 116 | + | expected: {majorVersion: $MajorVersion, scalaVersion: $expectedScala} |
| 117 | + | found : {majorVersion: $majorVersion, scalaVersion: $foundScala} |
| 118 | + | |
| 119 | + |Please recompile this TASTy with at least Scala $myScalaV.$myAddendum$foundAddendum""".stripMargin |
| 120 | + } |
| 121 | + else { |
| 122 | + val foundScalaV = s"$scalaMajorVersion.$scalaMinorVersion.x" |
| 123 | + s"""TASTy signature is from a forwards incompatible release. |
| 124 | + | expected: {majorVersion: $MajorVersion, scalaVersion: $expectedScala} |
| 125 | + | found : {majorVersion: $majorVersion, scalaVersion: $foundScala} |
| 126 | + | |
| 127 | + |To read this TASTy file, please upgrade your Scala version to at least $foundScalaV.$myAddendum$foundAddendum""".stripMargin |
| 128 | + } |
| 129 | + } |
| 130 | + ) |
| 131 | + val uuid = new UUID(readUncompressedLong(), readUncompressedLong()) |
| 132 | + new TastyHeader(uuid, majorVersion, scalaMajorVersion, scalaMinorVersion, experimentalVersion) {} |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + def isAtEnd: Boolean = reader.isAtEnd |
| 137 | + |
| 138 | + private def check(cond: Boolean, msg: => String): Unit = { |
| 139 | + if (!cond) throw new UnpickleException(msg) |
| 140 | + } |
| 141 | +} |
0 commit comments