Skip to content

Commit 63fa5ad

Browse files
DarkDimiussmarter
authored andcommitted
Type stealer that allows to steal types after typer
Proposed to be used from repl: it gives you internal representation of types after frontend and a context to play with them
1 parent 778072d commit 63fa5ad

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

test/test/DottyTypeStealer.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package test
2+
3+
import dotty.tools.dotc.ast.tpd
4+
import dotty.tools.dotc.core.Names._
5+
import dotty.tools.dotc.ast.tpd._
6+
import dotty.tools.dotc.core.Contexts.Context
7+
import dotty.tools.dotc.core.Decorators._
8+
import dotty.tools.dotc.core.Types.Type
9+
10+
object DottyTypeStealer {
11+
def stealType(source: String, typeStrings: String*): (Context, List[Type]) = {
12+
val dummyName = "x_x_x"
13+
val vals = typeStrings.zipWithIndex.map{case (s, x)=> s"val ${dummyName}$x: $s = ???"}.mkString("\n")
14+
val gatheredSource = s" ${source}\n object A$dummyName {$vals}"
15+
var scontext : Context = null
16+
var tp: List[Type] = null
17+
new DottyTest().checkCompile("frontend",gatheredSource) {
18+
(tree, context) =>
19+
implicit val ctx = context
20+
val findValDef: (List[ValDef], tpd.Tree) => List[ValDef] =
21+
(acc , tree) => { tree match {
22+
case t: ValDef if t.name.startsWith(dummyName.toTermName) => t :: acc
23+
case _ => acc
24+
}
25+
}
26+
val d = new DeepFolder[List[ValDef]](findValDef).foldOver(Nil, tree)
27+
tp = d.map(_.tpe.widen)
28+
scontext = context
29+
}
30+
(scontext, tp)
31+
}
32+
}

0 commit comments

Comments
 (0)