@@ -3,30 +3,62 @@ package dotty.tools
33import dotc .ast .tpd
44import dotc .core .Names ._
55import dotc .ast .tpd ._
6- import dotc .core .Contexts .Context
6+ import dotc .core .Contexts .{Context , atPhase }
7+ import dotty .tools .dotc .core .Phases .{typerPhase , erasurePhase }
8+ import dotc .core .Symbols .Symbol
79import dotc .core .Decorators ._
810import dotc .core .Types .Type
911
12+ import scala .util .CommandLineParser .FromString
13+
1014/** Pass a string representing a Scala source file,
1115 * and then some type signatures referencing prior definitions.
1216 *
13- * The type signatures will then be printed (singleton types
14- * are widened.)
17+ * The type signatures will then be printed as raw data structures.
1518 *
16- * @param kind the kind of type we are inspecting [`rhs`, `method`, `class`, `type`]
1719 * @param source top level Scala definitions, e.g. `"class O { type X }"`
20+ * @param kind the kind of type we are inspecting [`rhs`, `method`, `class`, `type`]
1821 * @param typeStrings Scala type signatures, e.g. `"O#X"`
1922 *
2023 * @syntax markdown
2124 */
22- @ main def printTypes (source : String , kind : String , typeStrings : String * ) = {
23- val k = DottyTypeStealer .Kind .lookup(kind)
24- val (_, tpes) = DottyTypeStealer .stealType(source, k, typeStrings* )
25+ @ main def printTypes (source : String , kind : DottyTypeStealer .Kind , typeStrings : String * ) = {
26+ val (_, tpes) = DottyTypeStealer .stealType(source, kind, typeStrings* )
2527 tpes.foreach(t => println(s " $t [ ${t.getClass}] " ))
2628}
2729
30+ /** Pass a string representing a Scala source file,
31+ * and then some type signatures referencing prior definitions.
32+ *
33+ * The type signatures will then be printed comparing between phase
34+ * `typer` where types are as Scala understands them and phase `erasure`,
35+ * which models the JVM types.
36+ *
37+ * @param source top level Scala definitions, e.g. `"class O { type X }"`
38+ * @param kind the kind of type we are inspecting [`rhs`, `method`, `class`, `type`]
39+ * @param typeStrings Scala type signatures, e.g. `"O#X"`
40+ *
41+ * @syntax markdown
42+ */
43+ @ main def printTypesAndErasure (source : String , kind : DottyTypeStealer .Kind , typeStrings : String * ): Unit =
44+ val (ictx, vdefs) = DottyTypeStealer .stealMember(" erasure" , source, kind, typeStrings* )
45+
46+ given Context = ictx
47+
48+ for vdef <- vdefs do
49+ println(i " info @ typer => ${atPhase(typerPhase.next)(vdef.info)}" )
50+ println(i " info @ erasure => ${atPhase(erasurePhase.next)(vdef.info)}" )
51+ end printTypesAndErasure
52+
2853object DottyTypeStealer extends DottyTest {
2954
55+ given FromString [Kind ] = kind =>
56+ if kind == " " then
57+ println(s " assuming kind ` ${Kind .rhs}` " )
58+ Kind .rhs
59+ else
60+ Kind .valueOf(kind)
61+
3062 enum Kind :
3163 case `rhs`, `method`, `class`, `type`
3264
@@ -36,24 +68,19 @@ object DottyTypeStealer extends DottyTest {
3668 case `class` => s " class $name $arg"
3769 case `type` => s " type $name $arg"
3870
39- object Kind :
40-
41- def lookup (kind : String ): Kind =
42- values.find(_.productPrefix == kind).getOrElse {
43- println(s " unknown kind ` $kind`, assuming ` $rhs` " )
44- rhs
45- }
46-
47- end Kind
48-
49-
5071 def stealType (source : String , kind : Kind , typeStrings : String * ): (Context , List [Type ]) = {
72+ val (scontext, members) = stealMember(" typer" , source, kind, typeStrings* )
73+ given Context = scontext
74+ (scontext, members.map(_.info))
75+ }
76+
77+ def stealMember (lastPhase : String , source : String , kind : Kind , typeStrings : String * ): (Context , List [Symbol ]) = {
5178 val dummyName = " x_x_x"
5279 val vals = typeStrings.zipWithIndex.map{case (s, x) => kind.format(dummyName + x, s) }.mkString(" \n " )
5380 val gatheredSource = s " ${source}\n object A $dummyName { $vals} "
5481 var scontext : Context = null
55- var tp : List [Type ] = null
56- checkCompile(" typer " , gatheredSource) {
82+ var members : List [Symbol ] = null
83+ checkCompile(lastPhase , gatheredSource) {
5784 (tree, context) =>
5885 given Context = context
5986 val findMemberDef : (List [MemberDef ], tpd.Tree ) => List [MemberDef ] =
@@ -64,9 +91,9 @@ object DottyTypeStealer extends DottyTest {
6491 case _ => acc
6592 }
6693 val d = new DeepFolder [List [MemberDef ]](findMemberDef).foldOver(Nil , tree)
67- tp = d.map(_.symbol.info ).reverse
94+ members = d.map(_.symbol).reverse
6895 scontext = context
6996 }
70- (scontext, tp )
97+ (scontext, members )
7198 }
7299}
0 commit comments