From d36989d1ec1de3b5b75de41415c852b087974bc7 Mon Sep 17 00:00:00 2001 From: Den Shabalin Date: Fri, 27 Sep 2013 17:22:03 +0200 Subject: advanced fresh name reification During parsing some names are generated artificially using freshTermName & freshTypeName (e.g. `x$1`). Such names should be reified in a different way because they are assumed to be always fresh and non-overlapping with the environment. So `x$1` should reify down to equivalent of `freshTermName("x$")` rather than `TermName("x$1")`. But this is not enough. One name can be used more than once in a tree. E.g. `q"_ + 1"` desugars into `q"x$1 => x$1 + 1"`. So we need to ensure that every place where `x$1` is used gets the same fresh name. Hence the need for `withFreshTermName` that lets q"_ + 1" quasiquote desugare into equivalent of `withFreshTermName("x$") { freshx => q"$freshx => $freshx + 1" }`. For pattern quasiquotes it's a bit different. Due to the fact that end-result must be a pattern we need to represent fresh names as patterns too. A natural way to express that something is fresh is to represent it as a free variable (e.g. any name will do in that place). But due to possible use of the same name in multiple places we need to make sure that all such places have the same values by adding a sequence of guards to the pattern. Previously such names were reified naively and it could have caused name collision problems and inability to properly much on trees that contain such names. --- .../quasiquotes/TermConstructionProps.scala | 10 ++++++++- .../quasiquotes/TermDeconstructionProps.scala | 24 +++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/files/scalacheck/quasiquotes/TermConstructionProps.scala b/test/files/scalacheck/quasiquotes/TermConstructionProps.scala index 753ad1aa59..9284903623 100644 --- a/test/files/scalacheck/quasiquotes/TermConstructionProps.scala +++ b/test/files/scalacheck/quasiquotes/TermConstructionProps.scala @@ -7,7 +7,6 @@ import scala.reflect.runtime.universe._ import Flag._ object TermConstructionProps extends QuasiquoteProperties("term construction") { - property("splice single tree return tree itself") = forAll { (t: Tree) => q"$t" ≈ t } @@ -191,4 +190,13 @@ object TermConstructionProps extends QuasiquoteProperties("term construction") { val assignx = q"x = 1" assertEqAst(q"f($assignx)", "f(x = 1)") } + + property("fresh names are regenerated at each evaluation") = test { + def plusOne = q"{ _ + 1 }" + assert(!(plusOne ≈ plusOne)) + def whileTrue = q"while(true) false" + assert(!(whileTrue ≈ whileTrue)) + def withEvidence = q"def foo[T: X]" + assert(!(withEvidence ≈ withEvidence)) + } } diff --git a/test/files/scalacheck/quasiquotes/TermDeconstructionProps.scala b/test/files/scalacheck/quasiquotes/TermDeconstructionProps.scala index 22d4b1ce4f..ff105f7fba 100644 --- a/test/files/scalacheck/quasiquotes/TermDeconstructionProps.scala +++ b/test/files/scalacheck/quasiquotes/TermDeconstructionProps.scala @@ -88,7 +88,7 @@ object TermDeconstructionProps extends QuasiquoteProperties("term deconstruction matches("new foo with bar") matches("new foo with bar { body }") matches("new { anonymous }") - matches("new { val early = 1} with Parent[Int] { body }") + matches("new { val early = 1 } with Parent[Int] { body }") matches("new Foo { selfie => }") } @@ -111,4 +111,26 @@ object TermDeconstructionProps extends QuasiquoteProperties("term deconstruction assert(left ≈ q"foo(bar)") assert(value ≈ q"baz") } + + property("deconstruct while loop") = test { + val q"while($cond) $body" = parse("while(cond) body") + assert(cond ≈ q"cond") + assert(body ≈ q"body") + } + + property("deconstruct do while loop") = test { + val q"do $body while($cond)" = parse("do body while(cond)") + assert(cond ≈ q"cond") + assert(body ≈ q"body") + } + + property("deconstruct anonymous function with placeholders") = test { + val q"{ $f(_) }" = q"{ foo(_) }" + assert(f ≈ q"foo") + val q"{ _.$member }" = q"{ _.foo }" + assert(member ≈ TermName("foo")) + val q"{ _ + $x }" = q"{ _ + x }" + assert(x ≈ q"x") + val q"{ _ * _ }" = q"{ _ * _ }" + } } -- cgit v1.2.3