@@ -3,30 +3,62 @@ package dotty.tools
3
3
import dotc .ast .tpd
4
4
import dotc .core .Names ._
5
5
import 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
7
9
import dotc .core .Decorators ._
8
10
import dotc .core .Types .Type
9
11
12
+ import scala .util .CommandLineParser .FromString
13
+
10
14
/** Pass a string representing a Scala source file,
11
15
* and then some type signatures referencing prior definitions.
12
16
*
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.
15
18
*
16
- * @param kind the kind of type we are inspecting [`rhs`, `method`, `class`, `type`]
17
19
* @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`]
18
21
* @param typeStrings Scala type signatures, e.g. `"O#X"`
19
22
*
20
23
* @syntax markdown
21
24
*/
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* )
25
27
tpes.foreach(t => println(s " $t [ ${t.getClass}] " ))
26
28
}
27
29
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
+
28
53
object DottyTypeStealer extends DottyTest {
29
54
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
+
30
62
enum Kind :
31
63
case `rhs`, `method`, `class`, `type`
32
64
@@ -36,24 +68,19 @@ object DottyTypeStealer extends DottyTest {
36
68
case `class` => s " class $name $arg"
37
69
case `type` => s " type $name $arg"
38
70
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
-
50
71
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 ]) = {
51
78
val dummyName = " x_x_x"
52
79
val vals = typeStrings.zipWithIndex.map{case (s, x) => kind.format(dummyName + x, s) }.mkString(" \n " )
53
80
val gatheredSource = s " ${source}\n object A $dummyName { $vals} "
54
81
var scontext : Context = null
55
- var tp : List [Type ] = null
56
- checkCompile(" typer " , gatheredSource) {
82
+ var members : List [Symbol ] = null
83
+ checkCompile(lastPhase , gatheredSource) {
57
84
(tree, context) =>
58
85
given Context = context
59
86
val findMemberDef : (List [MemberDef ], tpd.Tree ) => List [MemberDef ] =
@@ -64,9 +91,9 @@ object DottyTypeStealer extends DottyTest {
64
91
case _ => acc
65
92
}
66
93
val d = new DeepFolder [List [MemberDef ]](findMemberDef).foldOver(Nil , tree)
67
- tp = d.map(_.symbol.info ).reverse
94
+ members = d.map(_.symbol).reverse
68
95
scontext = context
69
96
}
70
- (scontext, tp )
97
+ (scontext, members )
71
98
}
72
99
}
0 commit comments