Skip to content

Commit fe8d050

Browse files
Merge pull request #4467 from dotty-staging/always-emit-tasty
Default to emitting .tasty files (not .hasTasty)
2 parents 0db2b36 + de291c1 commit fe8d050

File tree

8 files changed

+51
-32
lines changed

8 files changed

+51
-32
lines changed

compiler/src/dotty/tools/backend/jvm/GenBCode.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter
216216
for (binary <- ctx.compilationUnit.pickled.get(claszSymbol.asClass)) {
217217
val store = if (mirrorC ne null) mirrorC else plainC
218218
val tasty =
219-
if (ctx.settings.YemitTasty.value) {
219+
if (!ctx.settings.YemitTastyInClass.value) {
220220
val outTastyFile = getFileForClassfile(outF, store.name, ".tasty")
221221
val outstream = new DataOutputStream(outTastyFile.bufferedOutput)
222222
try outstream.write(binary)

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class ScalaSettings extends Settings.SettingGroup {
9090
val YdebugNames = BooleanSetting("-Ydebug-names", "Show internal representation of names")
9191
val YtermConflict = ChoiceSetting("-Yresolve-term-conflict", "strategy", "Resolve term conflicts", List("package", "object", "error"), "error")
9292
val Ylog = PhasesSetting("-Ylog", "Log operations during")
93-
val YemitTasty = BooleanSetting("-Yemit-tasty", "Generate tasty in separate *.tasty file.")
93+
val YemitTastyInClass = BooleanSetting("-Yemit-tasty-in-class", "Generate tasty in the .class file and add an empty *.hasTasty file.")
9494
val YlogClasspath = BooleanSetting("-Ylog-classpath", "Output information about what classpath is being applied.")
9595
val YdisableFlatCpCaching = BooleanSetting("-YdisableFlatCpCaching", "Do not cache flat classpath representation of classpath elements from jars across compiler instances.")
9696

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import Contexts._, Symbols._, Types._, Names._, StdNames._, NameOps._, Scopes._,
77
import SymDenotations._, unpickleScala2.Scala2Unpickler._, Constants._, Annotations._, util.Positions._
88
import NameKinds.{ModuleClassName, DefaultGetterName}
99
import ast.tpd._
10-
import java.io.{ ByteArrayInputStream, DataInputStream, File, IOException }
10+
import java.io.{ ByteArrayInputStream, ByteArrayOutputStream, DataInputStream, File, IOException }
1111
import java.nio
1212
import java.lang.Integer.toHexString
13+
import java.net.URLClassLoader
14+
1315
import scala.collection.{ mutable, immutable }
1416
import scala.collection.mutable.{ ListBuffer, ArrayBuffer }
1517
import scala.annotation.switch
@@ -784,24 +786,35 @@ class ClassfileParser(
784786
if (scan(tpnme.TASTYATTR)) {
785787
val attrLen = in.nextInt
786788
if (attrLen == 0) { // A tasty attribute implies the existence of the .tasty file
787-
def readTastyForClass(jpath: nio.file.Path): Array[Byte] = {
788-
val plainFile = new PlainFile(io.File(jpath).changeExtension("tasty"))
789-
if (plainFile.exists) plainFile.toByteArray
790-
else {
791-
ctx.error("Could not find " + plainFile)
792-
Array.empty
793-
}
794-
}
795-
val tastyBytes = classfile.underlyingSource match { // TODO: simplify when #3552 is fixed
789+
val tastyBytes: Array[Byte] = classfile.underlyingSource match { // TODO: simplify when #3552 is fixed
796790
case None =>
797791
ctx.error("Could not load TASTY from .tasty for virtual file " + classfile)
798-
Array.empty[Byte]
792+
Array.empty
799793
case Some(jar: ZipArchive) => // We are in a jar
800-
val jarFile = JarArchive.open(io.File(jar.jpath))
801-
try readTastyForClass(jarFile.jpath.resolve(classfile.path))
802-
finally jarFile.close()
794+
val cl = new URLClassLoader(Array(jar.jpath.toUri.toURL))
795+
val path = classfile.path.stripSuffix(".class") + ".tasty"
796+
val stream = cl.getResourceAsStream(path)
797+
if (stream != null) {
798+
val tastyOutStream = new ByteArrayOutputStream()
799+
val buffer = new Array[Byte](1024)
800+
var read = stream.read(buffer, 0, buffer.length)
801+
while (read != -1) {
802+
tastyOutStream.write(buffer, 0, read)
803+
read = stream.read(buffer, 0, buffer.length)
804+
}
805+
tastyOutStream.flush()
806+
tastyOutStream.toByteArray
807+
} else {
808+
ctx.error(s"Could not find $path in $jar")
809+
Array.empty
810+
}
803811
case _ =>
804-
readTastyForClass(classfile.jpath)
812+
val plainFile = new PlainFile(io.File(classfile.jpath).changeExtension("tasty"))
813+
if (plainFile.exists) plainFile.toByteArray
814+
else {
815+
ctx.error("Could not find " + plainFile)
816+
Array.empty
817+
}
805818
}
806819
if (tastyBytes.nonEmpty)
807820
return unpickleTASTY(tastyBytes)

compiler/src/dotty/tools/io/JarArchive.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dotty.tools.io
22

3-
import java.nio.file.{Files, FileSystem, FileSystems}
3+
import java.nio.file.{FileSystemAlreadyExistsException, FileSystems}
44

55
import scala.collection.JavaConverters._
66

@@ -9,7 +9,7 @@ import scala.collection.JavaConverters._
99
* that be can used as the compiler's output directory.
1010
*/
1111
class JarArchive private (root: Directory) extends PlainDirectory(root) {
12-
def close() = jpath.getFileSystem().close()
12+
def close(): Unit = jpath.getFileSystem().close()
1313
}
1414

1515
object JarArchive {
@@ -28,8 +28,12 @@ object JarArchive {
2828
// https://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/zipfilesystemprovider.html
2929
val env = Map("create" -> create.toString).asJava
3030
val uri = java.net.URI.create("jar:file:" + path.toAbsolute.path)
31-
val fs = FileSystems.newFileSystem(uri, env)
32-
31+
val fs = {
32+
try FileSystems.newFileSystem(uri, env)
33+
catch {
34+
case _: FileSystemAlreadyExistsException => FileSystems.getFileSystem(uri)
35+
}
36+
}
3337
val root = fs.getRootDirectories().iterator.next()
3438
new JarArchive(Directory(root))
3539
}

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class CompilationTests extends ParallelTesting {
101101
compileFilesInDir("tests/pos", defaultOptions) +
102102
compileFilesInDir("tests/pos-deep-subtype", allowDeepSubtypes) +
103103
compileFilesInDir("tests/pos-kind-polymorphism", defaultOptions and "-Ykind-polymorphism") +
104-
compileDir("tests/pos/i1137-1", defaultOptions and "-Yemit-tasty") +
104+
compileDir("tests/pos/i1137-1", defaultOptions) +
105105
compileFile(
106106
// succeeds despite -Xfatal-warnings because of -nowarn
107107
"tests/neg-custom-args/fatal-warnings/xfatalWarnings.scala",
@@ -254,7 +254,7 @@ class CompilationTests extends ParallelTesting {
254254
defaultOutputDir + dotty1Group + "/dotty/:" +
255255
// and the other compiler dependecies:
256256
Jars.dottyInterfaces + ":" + Jars.jline,
257-
Array("-Ycheck-reentrant")
257+
Array("-Ycheck-reentrant", "-Yemit-tasty-in-class")
258258
)
259259

260260
val lib =

compiler/test/dotty/tools/dotc/IdempotencyTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class IdempotencyTests extends ParallelTesting {
2828
@Category(Array(classOf[SlowTests]))
2929
@Test def idempotency: Unit = {
3030
implicit val testGroup: TestGroup = TestGroup("idempotency")
31-
val opt = defaultOptions.and("-Yemit-tasty")
31+
val opt = defaultOptions
3232

3333
def sourcesFrom(dir: Path) = CompilationTests.sources(Files.walk(dir))
3434

compiler/test/dotty/tools/vulpix/ParallelTesting.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,11 @@ trait ParallelTesting extends RunnerOrchestration { self =>
404404
tastyOutput.mkdir()
405405
val flags = flags0 and ("-d", tastyOutput.getAbsolutePath) and "-from-tasty"
406406

407-
def hasTastyFileToClassName(f: JFile): String =
408-
targetDir.toPath.relativize(f.toPath).toString.dropRight(".hasTasty".length).replace('/', '.')
409-
val classes = flattenFiles(targetDir).filter(isHasTastyFile).map(hasTastyFileToClassName)
407+
def tastyFileToClassName(f: JFile): String = {
408+
val pathStr = targetDir.toPath.relativize(f.toPath).toString.replace('/', '.')
409+
pathStr.stripSuffix(".tasty").stripSuffix(".hasTasty")
410+
}
411+
val classes = flattenFiles(targetDir).filter(isTastyFile).map(tastyFileToClassName)
410412

411413
val reporter =
412414
TestReporter.reporter(realStdout, logLevel =
@@ -434,8 +436,8 @@ trait ParallelTesting extends RunnerOrchestration { self =>
434436
"-decompile" and "-pagewidth" and "80"
435437

436438
def hasTastyFileToClassName(f: JFile): String =
437-
targetDir0.toPath.relativize(f.toPath).toString.dropRight(".hasTasty".length).replace('/', '.')
438-
val classes = flattenFiles(targetDir0).filter(isHasTastyFile).map(hasTastyFileToClassName).sorted
439+
targetDir0.toPath.relativize(f.toPath).toString.stripSuffix(".hasTasty").stripSuffix(".tasty").replace('/', '.')
440+
val classes = flattenFiles(targetDir0).filter(isTastyFile).map(hasTastyFileToClassName).sorted
439441

440442
val reporter =
441443
TestReporter.reporter(realStdout, logLevel =
@@ -1368,6 +1370,6 @@ object ParallelTesting {
13681370
name.endsWith(".scala") || name.endsWith(".java")
13691371
}
13701372

1371-
def isHasTastyFile(f: JFile): Boolean =
1372-
f.getName.endsWith(".hasTasty")
1373+
def isTastyFile(f: JFile): Boolean =
1374+
f.getName.endsWith(".hasTasty") || f.getName.endsWith(".tasty")
13731375
}

project/scripts/cmdTests

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ grep -qe "$EXPECTED_OUTPUT" "$tmp"
4747

4848
echo "testing loading tasty from .tasty file in jar"
4949
clear_out "$OUT"
50-
"$SBT" ";dotc -d $OUT/out.jar -Yemit-tasty $SOURCE; dotc -decompile -classpath $OUT/out.jar -color:never $MAIN" > "$tmp"
50+
"$SBT" ";dotc -d $OUT/out.jar $SOURCE; dotc -decompile -classpath $OUT/out.jar -color:never $MAIN" > "$tmp"
5151
grep -qe "def main(args: scala.Array\[scala.Predef.String\]): scala.Unit =" "$tmp"
5252

5353
echo "testing scala.quoted.Expr.run from sbt dotr"

0 commit comments

Comments
 (0)