Skip to content

Commit

Permalink
move scala3Compiler config to compiler module
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed Nov 2, 2023
1 parent 67351cb commit 7993b29
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 44 deletions.
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/backend/jvm/CodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import StdNames.nme
import java.io.DataOutputStream
import java.nio.channels.ClosedByInterruptException

import dotty.tools.tasty.{ TastyBuffer, TastyHeaderUnpickler, UnpicklerConfig }
import dotty.tools.tasty.{ TastyBuffer, TastyHeaderUnpickler }
import dotty.tools.dotc.core.tasty.TastyUnpickler

import scala.tools.asm
import scala.tools.asm.tree._
Expand Down Expand Up @@ -94,7 +95,7 @@ class CodeGen(val int: DottyBackendInterface, val primitives: DottyPrimitives)(
for (binary <- unit.pickled.get(claszSymbol.asClass)) {
generatedTasty += GeneratedTasty(store, binary)
val tasty =
val uuid = new TastyHeaderUnpickler(UnpicklerConfig.scala3Compiler, binary()).readHeader()
val uuid = new TastyHeaderUnpickler(TastyUnpickler.scala3CompilerConfig, binary()).readHeader()
val lo = uuid.getMostSignificantBits
val hi = uuid.getLeastSignificantBits

Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import ast.desugar
import parsing.JavaParsers.OutlineJavaParser
import parsing.Parsers.OutlineParser
import dotty.tools.tasty.{TastyHeaderUnpickler, UnpickleException, UnpicklerConfig}
import dotty.tools.dotc.core.tasty.TastyUnpickler


object SymbolLoaders {
Expand Down Expand Up @@ -447,7 +448,7 @@ class TastyLoader(val tastyFile: AbstractFile) extends SymbolLoader {
val className = tastyFile.name.stripSuffix(".tasty")
tastyFile.resolveSibling(className + ".class")
if classfile != null then
val tastyUUID = new TastyHeaderUnpickler(UnpicklerConfig.scala3Compiler, tastyBytes).readHeader()
val tastyUUID = new TastyHeaderUnpickler(TastyUnpickler.scala3CompilerConfig, tastyBytes).readHeader()
new ClassfileTastyUUIDParser(classfile)(ctx).checkTastyUUID(tastyUUID)
else
// This will be the case in any of our tests that compile with `-Youtput-only-tasty`
Expand Down
36 changes: 35 additions & 1 deletion compiler/src/dotty/tools/dotc/core/tasty/TastyUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package tasty
import scala.language.unsafeNulls

import dotty.tools.tasty.{TastyFormat, TastyBuffer, TastyReader, TastyHeaderUnpickler, UnpicklerConfig}
import TastyHeaderUnpickler.TastyVersion
import TastyFormat.NameTags._, TastyFormat.nameTagToString
import TastyBuffer.NameRef

Expand All @@ -24,6 +25,39 @@ object TastyUnpickler {
def apply(ref: NameRef): TermName = names(ref.index)
def contents: Iterable[TermName] = names
}

trait Scala3CompilerConfig extends UnpicklerConfig:
private def asScala3Compiler(version: TastyVersion): String =
if (version.major == 28) {
// scala 3.x.y series
if (version.experimental > 0)
// scenario here is someone using 3.4.0 to read 3.4.1-RC1-NIGHTLY, in this case, we should show 3.4 nightly.
s"the same nightly or snapshot Scala 3.${version.minor - 1} compiler"
else s"a Scala 3.${version.minor}.0 compiler or newer"
}
else if (version.experimental > 0) "the same Scala compiler" // unknown major version, just say same
else "a more recent Scala compiler" // unknown major version, just say later

/** The description of the upgraded scala compiler that can read the given TASTy version */
final def upgradedReaderTool(version: TastyVersion): String = asScala3Compiler(version)

/** The description of the upgraded scala compiler that can produce the given TASTy version */
final def upgradedProducerTool(version: TastyVersion): String = asScala3Compiler(version)

final def recompileAdditionalInfo: String = """
| Usually this means that the library dependency containing this file should be updated.""".stripMargin

final def upgradeAdditionalInfo(fileVersion: TastyVersion): String =
if (fileVersion.isExperimental && experimentalVersion == 0) {
"""
| Note that you are using a stable compiler, which can not read experimental TASTy.""".stripMargin
}
else ""
end Scala3CompilerConfig

/** A config for the TASTy reader of a scala 3 compiler */
val scala3CompilerConfig = new Scala3CompilerConfig with UnpicklerConfig.DefaultTastyVersion {}

}

import TastyUnpickler._
Expand Down Expand Up @@ -88,7 +122,7 @@ class TastyUnpickler(reader: TastyReader) {
result
}

new TastyHeaderUnpickler(UnpicklerConfig.scala3Compiler, reader).readHeader()
new TastyHeaderUnpickler(scala3CompilerConfig, reader).readHeader()

locally {
until(readEnd()) { nameAtRef.add(readNameContents()) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package dotty.tools.tasty
package dotty.tools.dotc.core.tasty

import org.junit.Assert._
import org.junit.{Test, Ignore}

import TastyFormat._
import TastyBuffer._
import TastyHeaderUnpickler.TastyVersion
import dotty.tools.tasty.TastyFormat._
import dotty.tools.tasty.TastyBuffer._
import dotty.tools.tasty.TastyBuffer
import dotty.tools.tasty.TastyReader
import dotty.tools.tasty.UnpickleException
import dotty.tools.tasty.TastyHeaderUnpickler
import dotty.tools.tasty.TastyHeaderUnpickler.TastyVersion
import dotty.tools.tasty.UnpicklerConfig

class TastyHeaderUnpicklerTest {

Expand Down Expand Up @@ -260,7 +265,7 @@ class TastyHeaderUnpicklerTest {
object TastyHeaderUnpicklerTest {

def fillHeader(maj: Int, min: Int, exp: Int, compiler: String): TastyBuffer = {
val compilerBytes = compiler.getBytes(java.nio.charset.StandardCharsets.UTF_8)
val compilerBytes = compiler.getBytes(java.nio.charset.StandardCharsets.UTF_8).nn
val buf = new TastyBuffer(header.length + 32 + compilerBytes.length)
for (ch <- header) buf.writeByte(ch.toByte)
buf.writeNat(maj)
Expand All @@ -273,7 +278,7 @@ object TastyHeaderUnpicklerTest {
buf
}

case class CustomScalaConfig(compilerVersion: TastyVersion) extends UnpicklerConfig.Scala3Compiler {
case class CustomScalaConfig(compilerVersion: TastyVersion) extends TastyUnpickler.Scala3CompilerConfig {
override def majorVersion: Int = compilerVersion.major
override def minorVersion: Int = compilerVersion.minor
override def experimentalVersion: Int = compilerVersion.experimental
Expand All @@ -299,7 +304,7 @@ object TastyHeaderUnpicklerTest {
fail()
}
catch {
case err: UnpickleException => assert(err.getMessage.contains(message))
case err: UnpickleException => assert(err.getMessage.nn.contains(message))
}
}

Expand Down
32 changes: 0 additions & 32 deletions tasty/src/dotty/tools/tasty/TastyHeaderUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,6 @@ object UnpicklerConfig {
override final def experimentalVersion: Int = ExperimentalVersion
}

trait Scala3Compiler extends UnpicklerConfig {
private def asScala3Compiler(version: TastyVersion): String =
if (version.major == 28) {
// scala 3.x.y series
if (version.experimental > 0)
// scenario here is someone using 3.4.0 to read 3.4.1-RC1-NIGHTLY, in this case, we should show 3.4 nightly.
s"the same nightly or snapshot Scala 3.${version.minor - 1} compiler"
else s"a Scala 3.${version.minor}.0 compiler or newer"
}
else if (version.experimental > 0) "the same Scala compiler" // unknown major version, just say same
else "a more recent Scala compiler" // unknown major version, just say later

/** The description of the upgraded scala compiler that can read the given TASTy version */
final def upgradedReaderTool(version: TastyVersion): String = asScala3Compiler(version)

/** The description of the upgraded scala compiler that can produce the given TASTy version */
final def upgradedProducerTool(version: TastyVersion): String = asScala3Compiler(version)

final def recompileAdditionalInfo: String = """
| Usually this means that the library dependency containing this file should be updated.""".stripMargin

final def upgradeAdditionalInfo(fileVersion: TastyVersion): String =
if (fileVersion.isExperimental && experimentalVersion == 0) {
"""
| Note that you are using a stable compiler, which can not read experimental TASTy.""".stripMargin
}
else ""
}

trait Generic extends UnpicklerConfig {
final def upgradedProducerTool(version: TastyVersion): String =
"a later version"
Expand All @@ -102,9 +73,6 @@ object UnpicklerConfig {
else ""
}

/** A config for the TASTy reader of a scala 3 compiler */
val scala3Compiler = new UnpicklerConfig with Scala3Compiler with DefaultTastyVersion {}

/** A config for the TASTy reader of a generic tool */
val generic = new UnpicklerConfig with Generic with DefaultTastyVersion {}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/pos-with-compiler-cc/backend/jvm/GenBCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import java.io.DataOutputStream
import java.nio.channels.ClosedByInterruptException

import dotty.tools.tasty.{ TastyBuffer, TastyHeaderUnpickler, UnpicklerConfig }
import dotty.tools.tasty.core.TastyUnpickler

import scala.tools.asm
import scala.tools.asm.Handle
Expand Down Expand Up @@ -285,7 +286,7 @@ class GenBCodePipeline(val int: DottyBackendInterface, val primitives: DottyPrim
throw ex
finally outstream.close()

val uuid = new TastyHeaderUnpickler(UnpicklerConfig.scala3Compiler, binary()).readHeader()
val uuid = new TastyHeaderUnpickler(TastyUnpickler.scala3CompilerConfig, binary()).readHeader()
val lo = uuid.getMostSignificantBits
val hi = uuid.getLeastSignificantBits

Expand Down

0 comments on commit 7993b29

Please sign in to comment.