Skip to content

Commit da25dcd

Browse files
Merge pull request #3517 from dotty-staging/fix-#3515
Fix #3515: Do not save tasty in .class with -YemitTasty
2 parents 331bc47 + a667aa0 commit da25dcd

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,20 +223,24 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter
223223

224224
if (claszSymbol.isClass) // @DarkDimius is this test needed here?
225225
for (binary <- ctx.compilationUnit.pickled.get(claszSymbol.asClass)) {
226-
val dataAttr = new CustomAttr(nme.TASTYATTR.mangledString, binary)
227226
val store = if (mirrorC ne null) mirrorC else plainC
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)
228243
store.visitAttribute(dataAttr)
229-
if (ctx.settings.emitTasty.value) {
230-
val outTastyFile = getFileForClassfile(outF, store.name, ".tasty")
231-
val outstream = new DataOutputStream(outTastyFile.bufferedOutput)
232-
233-
try outstream.write(binary)
234-
finally outstream.close()
235-
} else {
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-
}
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)) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class CompilationTests extends ParallelTesting {
9696
compileFilesInDir("../tests/pos-scala2", scala2Mode) +
9797
compileFilesInDir("../tests/pos", defaultOptions) +
9898
compileFilesInDir("../tests/pos-deep-subtype", allowDeepSubtypes) +
99+
compileDir("../tests/pos/i1137-1", defaultOptions and "-YemitTasty") +
99100
compileFile(
100101
// succeeds despite -Xfatal-warnings because of -nowarn
101102
"../tests/neg/customArgs/xfatalWarnings.scala",

0 commit comments

Comments
 (0)