Skip to content

Commit e517c8f

Browse files
committed
Demonstrate that signature caching is broken
The test currently fails with: Wrong cached signature at phase elimErasedValueType for (Foo.this.value : (): scala.runtime.BoxedUnit). Actual denotation signature: Signature(List(),scala.runtime.BoxedUnit) Cached ref signature: Signature(List(),), The test passes if I tweak NamedType to not cache signatures.
1 parent 755a81f commit e517c8f

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dotty.tools
2+
3+
import vulpix.TestConfiguration
4+
5+
import org.junit.Test
6+
7+
import dotc.ast.Trees._
8+
import dotc.core.Decorators._
9+
import dotc.core.Contexts._
10+
import dotc.core.Types._
11+
12+
import java.io.File
13+
import java.nio.file._
14+
15+
class SignaturesTest:
16+
@Test def signatureCaching: Unit =
17+
inCompilerContext(TestConfiguration.basicClasspath, "case class Foo(value: Unit)") {
18+
val defn = ctx.definitions
19+
val leftCls = ctx.requiredClass("Foo")
20+
val ref = leftCls.requiredMethod("value").termRef
21+
22+
def checkSig()(using Context): Unit =
23+
val denot = ref.denot
24+
assert(ref.signature == denot.signature, i"Wrong cached signature at phase ${ctx.phase} for $ref.\nActual denotation signature: ${denot.signature}\nCached ref signature: ${ref.signature}")
25+
26+
checkSig()
27+
checkSig()(using ctx.withPhase(ctx.erasurePhase.next))
28+
}

compiler/test/dotty/tools/compilerSupport.scala

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dotty.tools
22

33
import javax.tools._
4-
import java.io.File
4+
import java.io._
55
import java.nio.file._
66
import java.net.URI
77
import scala.jdk.CollectionConverters._
@@ -10,13 +10,25 @@ import core._
1010
import core.Contexts._
1111
import dotc.core.Comments.{ContextDoc, ContextDocstrings}
1212

13+
// TODO: refactor, copy-pasted from Run#compileFromStrings
14+
private def sourceFile(source: String, isJava: Boolean): util.SourceFile = {
15+
val uuid = java.util.UUID.randomUUID().toString
16+
val ext = if (isJava) ".java" else ".scala"
17+
val virtualFile = new io.VirtualFile(s"compileFromString-$uuid.$ext")
18+
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, "UTF-8")) // buffering is still advised by javadoc
19+
writer.write(source)
20+
writer.close()
21+
new util.SourceFile(virtualFile, scala.io.Codec.UTF8)
22+
}
23+
1324
/** Initialize a compiler context with the given classpath and
1425
* pass it to `op`.
1526
*/
16-
def inCompilerContext[T](classpath: String)(op: Context ?=> T): T =
27+
def inCompilerContext[T](classpath: String, scalaSources: String*)(op: Context ?=> T): T =
1728
val compiler = Compiler()
1829
val run = compiler.newRun(initCtx(classpath))
19-
run.compileUnits(Nil) // Initialize phases
30+
run.compileUnits(scalaSources.toList.map(s =>
31+
CompilationUnit(sourceFile(s, isJava = false))(using run.runContext)))
2032
op(using run.runContext)
2133

2234
private def initCtx(classpath: String): Context =

0 commit comments

Comments
 (0)