diff options
author | Den Shabalin <den.shabalin@gmail.com> | 2013-09-27 17:22:03 +0200 |
---|---|---|
committer | Den Shabalin <den.shabalin@gmail.com> | 2013-10-18 17:20:01 +0200 |
commit | d36989d1ec1de3b5b75de41415c852b087974bc7 (patch) | |
tree | 1b49ed9311ddf4b35924aa890ac271c000b74f0e /test/files/scalacheck/quasiquotes/TermDeconstructionProps.scala | |
parent | debdd2f316934c417c7d9865ea0f2ea503e51b63 (diff) | |
download | scala-d36989d1ec1de3b5b75de41415c852b087974bc7.tar.gz scala-d36989d1ec1de3b5b75de41415c852b087974bc7.tar.bz2 scala-d36989d1ec1de3b5b75de41415c852b087974bc7.zip |
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.
Diffstat (limited to 'test/files/scalacheck/quasiquotes/TermDeconstructionProps.scala')
-rw-r--r-- | test/files/scalacheck/quasiquotes/TermDeconstructionProps.scala | 24 |
1 files changed, 23 insertions, 1 deletions
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"{ _ * _ }" + } } |