Skip to content

Commit 752f1eb

Browse files
committed
deprecates resetAllAttrs and resetLocalAttrs in favor of the new API
We now have c.untypecheck, which is supposed to be a counterpart of c.typecheck in the sense that it goes back from typed trees to untyped ones: http://stackoverflow.com/questions/20936509/scala-macros-what-is-the-difference-between-typed-aka-typechecked-an-untyped. Let’s hope that c.untypecheck will soon be able to solve our problems with partially/incorrectly attributed trees emitted by macros: https://groups.google.com/forum/#!topic/scala-internals/TtCTPlj_qcQ.
1 parent 97286d7 commit 752f1eb

File tree

12 files changed

+41
-20
lines changed

12 files changed

+41
-20
lines changed

src/compiler/scala/reflect/macros/contexts/Typers.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ trait Typers {
4949
def resetAllAttrs(tree: Tree): Tree = universe.resetAllAttrs(universe.duplicateAndKeepPositions(tree))
5050

5151
def resetLocalAttrs(tree: Tree): Tree = universe.resetLocalAttrs(universe.duplicateAndKeepPositions(tree))
52+
53+
def untypecheck(tree: Tree): Tree = universe.resetLocalAttrs(universe.duplicateAndKeepPositions(tree))
5254
}

src/compiler/scala/tools/reflect/ToolBox.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,22 @@ trait ToolBox[U <: scala.reflect.api.Universe] {
7272
def inferImplicitView(tree: u.Tree, from: u.Type, to: u.Type, silent: Boolean = true, withMacrosDisabled: Boolean = false, pos: u.Position = u.NoPosition): u.Tree
7373

7474
/** Recursively resets symbols and types in a given tree.
75-
*
76-
* Note that this does not revert the tree to its pre-typer shape.
77-
* For more info, read up https://issues.scala-lang.org/browse/SI-5464.
75+
* WARNING: Don't use this API, go for [[untypecheck]] instead.
7876
*/
77+
@deprecated("Use `tb.untypecheck` instead", "2.11.0")
7978
def resetAllAttrs(tree: u.Tree): u.Tree
8079

8180
/** Recursively resets locally defined symbols and types in a given tree.
82-
*
83-
* Note that this does not revert the tree to its pre-typer shape.
84-
* For more info, read up https://issues.scala-lang.org/browse/SI-5464.
81+
* WARNING: Don't use this API, go for [[untypecheck]] instead.
8582
*/
83+
@deprecated("Use `tb.untypecheck` instead", "2.11.0")
8684
def resetLocalAttrs(tree: u.Tree): u.Tree
8785

86+
/**
87+
* @see [[scala.reflect.macros.Typers.untypecheck]]
88+
*/
89+
def untypecheck(tree: u.Tree): u.Tree
90+
8891
/** .. */
8992
def parse(code: String): u.Tree
9093

src/compiler/scala/tools/reflect/ToolBoxFactory.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf =>
401401
uttree
402402
}
403403

404+
def untypecheck(tree: u.Tree): u.Tree = resetLocalAttrs(tree)
405+
404406
def parse(code: String): u.Tree = withCompilerApi { compilerApi =>
405407
import compilerApi._
406408
if (compiler.settings.verbose) println("parsing "+code)

src/reflect/scala/reflect/macros/Typers.scala

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,32 @@ trait Typers {
6969
def inferImplicitView(tree: Tree, from: Type, to: Type, silent: Boolean = true, withMacrosDisabled: Boolean = false, pos: Position = enclosingPosition): Tree
7070

7171
/** Recursively resets symbols and types in a given tree.
72-
*
73-
* Note that this does not revert the tree to its pre-typer shape.
74-
* For more info, read up https://issues.scala-lang.org/browse/SI-5464.
72+
* WARNING: Don't use this API, go for [[untypecheck]] instead.
7573
*/
74+
@deprecated("Use `c.untypecheck` instead", "2.11.0")
7675
def resetAllAttrs(tree: Tree): Tree
7776

7877
/** Recursively resets locally defined symbols and types in a given tree.
79-
*
80-
* Note that this does not revert the tree to its pre-typer shape.
81-
* For more info, read up https://issues.scala-lang.org/browse/SI-5464.
78+
* WARNING: Don't use this API, go for [[untypecheck]] instead.
8279
*/
80+
@deprecated("Use `c.untypecheck` instead", "2.11.0")
8381
def resetLocalAttrs(tree: Tree): Tree
82+
83+
/** In the current implementation of Scala's reflection API, untyped trees (also known as parser trees or unattributed trees)
84+
* are observationally different from typed trees (also known as typer trees, typechecked trees or attributed trees),
85+
*
86+
* Usually, if some compiler API takes a tree, then both untyped and typed trees will do. However in some cases,
87+
* only untyped or only typed trees are appropriate. For example, [[eval]] only accepts untyped trees and one can only splice
88+
* typed trees inside typed trees. Therefore in the current reflection API, there is a need in functions
89+
* that go back and forth between untyped and typed trees. For this we have [[typecheck]] and `untypecheck`.
90+
*
91+
* Note that `untypecheck` is currently afflicted by https://issues.scala-lang.org/browse/SI-5464,
92+
* which makes it sometimes corrupt trees so that they don't make sense anymore. Unfortunately, there's no workaround for that.
93+
* We plan to fix this issue soon, but for now please keep it in mind.
94+
*
95+
* @see [[http://stackoverflow.com/questions/20936509/scala-macros-what-is-the-difference-between-typed-aka-typechecked-an-untyped]]
96+
*/
97+
def untypecheck(tree: Tree): Tree
8498
}
8599

86100
/** Indicates an error during one of the methods in [[scala.reflect.macros.Typers]].

test/files/run/idempotency-case-classes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ object Test extends App {
1212
val tb = cm.mkToolBox()
1313
val tcasee = tb.typecheck(casee.tree)
1414
println(tcasee)
15-
val rtcasee = tb.resetAllAttrs(tcasee)
15+
val rtcasee = tb.untypecheck(tcasee)
1616
try {
1717
println(tb.eval(rtcasee))
1818
} catch {

test/files/run/idempotency-extractors.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ object Test extends App {
1212
val tb = cm.mkToolBox()
1313
val textractor = tb.typecheck(extractor.tree)
1414
println(textractor)
15-
val rtextractor = tb.resetAllAttrs(textractor)
15+
val rtextractor = tb.untypecheck(textractor)
1616
try {
1717
println(tb.eval(rtextractor))
1818
} catch {

test/files/run/idempotency-labels.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Test extends App {
1313
val tb = cm.mkToolBox()
1414
val tlabel = tb.typecheck(label.tree)
1515
println(tlabel)
16-
val rtlabel = tb.resetAllAttrs(tlabel)
16+
val rtlabel = tb.untypecheck(tlabel)
1717
try {
1818
println(tb.eval(rtlabel))
1919
} catch {

test/files/run/idempotency-lazy-vals.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object Test extends App {
1717
val tb = cm.mkToolBox()
1818
val tlazee = tb.typecheck(lazee.tree)
1919
println(tlazee)
20-
val rtlazee = tb.resetAllAttrs(tlazee)
20+
val rtlazee = tb.untypecheck(tlazee)
2121
try {
2222
println(tb.eval(rtlazee))
2323
} catch {

test/files/run/idempotency-this.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ object Test extends App {
1212
val tthiss = tb.typecheck(thiss.tree)
1313
println(tthiss)
1414
println(showRaw(tthiss))
15-
val rtthiss = tb.resetAllAttrs(tthiss)
15+
val rtthiss = tb.untypecheck(tthiss)
1616
try {
1717
println(tb.eval(rtthiss))
1818
} catch {

test/files/run/resetattrs-this.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ object Test extends App {
66
val tb = cm.mkToolBox()
77
val tree = Select(This(cm.staticPackage("scala").moduleClass), TermName("Predef"))
88
val ttree = tb.typecheck(tree)
9-
val rttree = tb.resetAllAttrs(ttree)
9+
val rttree = tb.untypecheck(ttree)
1010
println(tb.eval(rttree) == Predef)
1111
}

0 commit comments

Comments
 (0)