Skip to content

Commit 4aa7f90

Browse files
authored
Merge pull request #12889 from dotty-staging/refine-type-stealer-workflow
update type stealer workflow doc
2 parents 50d6967 + 4816a73 commit 4aa7f90

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

compiler/test/dotty/tools/DottyTypeStealer.scala

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@ import dotc.core.Contexts.Context
77
import dotc.core.Decorators._
88
import dotc.core.Types.Type
99

10-
@main def steal() = {
11-
val s = DottyTypeStealer.stealType("class O { type X }", "O#X")
12-
val t = s._2(0)
13-
println(t)
10+
/**Pass a string representing a Scala source file,
11+
* and then some type signatures referencing prior definitions.
12+
*
13+
* The type signatures will then be printed (singleton types
14+
* are widened.)
15+
*
16+
* @param source top level Scala definitions, e.g. `"class O { type X }"`
17+
* @param typeStrings Scala type signatures, e.g. `"O#X"`
18+
*
19+
* @syntax markdown
20+
*/
21+
@main def printTypes(source: String, typeStrings: String*) = {
22+
val (_, tpes) = DottyTypeStealer.stealType(source, typeStrings*)
23+
tpes.foreach(println)
1424
}
1525

1626
object DottyTypeStealer extends DottyTest {

docs/docs/contributing/workflow.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,43 @@ Here are some useful debugging `<OPTIONS>`:
3737
can be enabled through the `dotty.tools.dotc.config.Printers` object. Change any of the desired printer from `noPrinter` to
3838
`default` and this will give you the full logging capability of the compiler.
3939

40-
## Inspecting Trees with Type Stealer ##
40+
## Inspecting Types with Type Stealer ##
4141

42-
You can inspect types with the type stealer, open `compiler/test/dotty/tools/DottyTypeStealer.scala` and you'll see:
42+
You can inspect types with the main method `dotty.tools.printTypes` from the sbt shell,
43+
passing at least two arguments. The first argument is a string that introduces some
44+
Scala definitions, the following arguments are type signatures, (i.e. the return type
45+
of a definition) that are allowed to reference definitions from the first argument.
4346

44-
```scala
45-
@main def steal() = {
46-
val s = DottyTypeStealer.stealType("class O { type X }", "O#X")
47-
val t = s._2(0)
48-
println(t)
49-
}
47+
The type signatures will then be printed, displaying their internal structure, using
48+
the same representation that can later be used in pattern matching to decompose the type.
49+
50+
Here, we inspect a refinement of a class `Box`:
51+
```bash
52+
$ sbt
53+
> scala3-compiler-bootstrapped/Test/runMain dotty.tools.printTypes "class Box { def x: Any }" "Box { def x: Int }"
54+
RefinedType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),class Box),x,ExprType(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),class Int)))
5055
```
5156

57+
You can also pass the empty string as the first
58+
argument, e.g. to inspect a standard library type:
5259
```bash
5360
$ sbt
54-
> scala3-compiler-bootstrapped/Test/runMain dotty.tools.steal
55-
TypeRef(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),class O),type X)
61+
> scala3-compiler-bootstrapped/Test/runMain dotty.tools.printTypes "" "1 *: EmptyTuple"
62+
AppliedType(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),class *:),List(ConstantType(Constant(1)), TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class scala)),object Tuple$package),type EmptyTuple)))
5663
```
5764

58-
You can inspect other value types by editing the arguments of `stealType`.
65+
If you want to further inspect the types, and not just print them, the object `dotty.tools.DottyTypeStealer` has a
66+
method `stealType`. It takes the same arguments as `printTypes`, but returns both a `Context` containing the
67+
definitions passed, along with the list of types:
68+
```scala
69+
// compiler/test/dotty/tools/DottyTypeStealer.scala
70+
object DottyTypeStealer extends DottyTest {
71+
def stealType(source: String, typeStrings: String*): (Context, List[Type]) = {
72+
...
73+
}
74+
}
75+
```
76+
Any test source within `compiler/test` can then call `stealType` for custom purposes.
5977

6078
## Pretty-printing ##
6179
Many objects in the scalac compiler implement a `Showable` trait (e.g. `Tree`,

0 commit comments

Comments
 (0)