Skip to content

Commit 41f6215

Browse files
committed
Load tasty from .tasty file if compiled with -YemmitTasty
1 parent ed61847 commit 41f6215

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,23 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter
224224
if (claszSymbol.isClass) // @DarkDimius is this test needed here?
225225
for (binary <- ctx.compilationUnit.pickled.get(claszSymbol.asClass)) {
226226
val store = if (mirrorC ne null) mirrorC else plainC
227-
if (ctx.settings.emitTasty.value) {
228-
val outTastyFile = getFileForClassfile(outF, store.name, ".tasty")
229-
val outstream = new DataOutputStream(outTastyFile.bufferedOutput)
230-
231-
try outstream.write(binary)
232-
finally outstream.close()
233-
} else {
234-
val dataAttr = new CustomAttr(nme.TASTYATTR.mangledString, binary)
235-
store.visitAttribute(dataAttr)
236-
// Create an empty file to signal that a tasty section exist in the corresponding .class
237-
// This is much cheaper and simpler to check than doing classfile parsing
238-
getFileForClassfile(outF, store.name, ".hasTasty")
239-
}
227+
val tasty =
228+
if (ctx.settings.emitTasty.value) {
229+
val outTastyFile = getFileForClassfile(outF, store.name, ".tasty")
230+
val outstream = new DataOutputStream(outTastyFile.bufferedOutput)
231+
try outstream.write(binary)
232+
finally outstream.close()
233+
// TASTY attribute is created but 0 bytes are stored in it.
234+
// A TASTY attribute has length 0 if and only if the .tasty file exists.
235+
Array.empty[Byte]
236+
} else {
237+
// Create an empty file to signal that a tasty section exist in the corresponding .class
238+
// This is much cheaper and simpler to check than doing classfile parsing
239+
getFileForClassfile(outF, store.name, ".hasTasty")
240+
binary
241+
}
242+
val dataAttr = new CustomAttr(nme.TASTYATTR.mangledString, tasty)
243+
store.visitAttribute(dataAttr)
240244
}
241245

242246
// -------------- bean info class, if needed --------------

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import scala.collection.{ mutable, immutable }
1313
import scala.collection.mutable.{ ListBuffer, ArrayBuffer }
1414
import scala.annotation.switch
1515
import typer.Checking.checkNonCyclic
16-
import io.AbstractFile
16+
import io.{AbstractFile, PlainFile}
1717
import scala.util.control.NonFatal
1818

1919
object ClassfileParser {
@@ -781,7 +781,13 @@ class ClassfileParser(
781781

782782
if (scan(tpnme.TASTYATTR)) {
783783
val attrLen = in.nextInt
784-
return unpickleTASTY(in.nextBytes(attrLen))
784+
if (attrLen == 0) {
785+
// A tasty attribute implies the existence of the .tasty file
786+
val file = new PlainFile(io.File(classfile.jpath).changeExtension("tasty"))
787+
if (file.exists) return unpickleTASTY(new AbstractFileReader(file).nextBytes(file.sizeOption.get))
788+
else ctx.error("Could not find " + file)
789+
}
790+
else return unpickleTASTY(in.nextBytes(attrLen))
785791
}
786792

787793
if (scan(tpnme.ScalaATTR) && !scalaUnpickleWhitelist.contains(classRoot.name)) {

0 commit comments

Comments
 (0)