Skip to content

Commit 1b98cb9

Browse files
committed
Signature caching test for this PR
This currently fails with: assertion failed: Before erasure, the signature of a TypeRef should be the signature of its denotation, but the cached signature of TermRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),class Foo)),method value) was: Signature(List(),scala.Unit), whereas its denotation signature at typer was: Signature(List(),)
1 parent ab40a6e commit 1b98cb9

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 SignatureTest:
16+
@Test def signatureCaching: Unit =
17+
inCompilerContext(TestConfiguration.basicClasspath, separateRun = true, "case class Foo(value: Unit)") {
18+
val (ref, refSig) = ctx.atPhase(ctx.erasurePhase.next) {
19+
val cls = ctx.requiredClass("Foo")
20+
val ref = cls.requiredMethod("value").termRef
21+
(ref, ref.signature)
22+
}
23+
ctx.atPhase(ctx.typerPhase) {
24+
assert(refSig == ref.signature,
25+
s"""The signature of a type should never change but the signature of $ref was:
26+
|${ref.signature} at typer, whereas it was:
27+
|${refSig} after erasure""".stripMargin)
28+
assert(ref.signature == ref.denot.signature,
29+
s"""Before erasure, the signature of a TypeRef should be the signature of its denotation,
30+
|but the cached signature of $ref was:
31+
|${ref.signature}, whereas its denotation signature at typer was:
32+
|${ref.denot.signature}""".stripMargin)
33+
}
34+
}

compiler/test/dotty/tools/compilerSupport.scala

Lines changed: 27 additions & 7 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,14 +10,34 @@ import core._
1010
import core.Contexts._
1111
import dotc.core.Comments.{ContextDoc, ContextDocstrings}
1212

13-
/** Initialize a compiler context with the given classpath and
14-
* pass it to `op`.
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+
24+
/** Initialize a compiler context with the given `classpath`, compile all
25+
* `scalaSources`, then run `op`.
26+
*
27+
* If `separateRun` is true , then `op` will be run in a new run different from the
28+
* one used to compile the sources, this makes it easier to test for potential
29+
* issues involving retrieving symbols defined in a previous run.
1530
*/
16-
def inCompilerContext[T](classpath: String)(op: Context ?=> T): T =
31+
def inCompilerContext[T](classpath: String, separateRun: Boolean = true, scalaSources: String*)(op: Context ?=> T): T =
1732
val compiler = Compiler()
18-
val run = compiler.newRun(initCtx(classpath))
19-
run.compileUnits(Nil) // Initialize phases
20-
op(using run.runContext)
33+
val rootCtx = initCtx(classpath)
34+
val firstRun = compiler.newRun(rootCtx)
35+
firstRun.compileUnits(scalaSources.toList.map(s =>
36+
CompilationUnit(sourceFile(s, isJava = false))(using firstRun.runContext)))
37+
val opRun = if separateRun
38+
then compiler.newRun(rootCtx)
39+
else firstRun
40+
op(using opRun.runContext)
2141

2242
private def initCtx(classpath: String): Context =
2343
val ctx0 = (new ContextBase).initialCtx.fresh

0 commit comments

Comments
 (0)