Permalink
Browse files

SI-8451 quasiquotes now handle quirks of secondary constructors

Apparently even though the rhs of a secondary constructor looks like an expr,
it always gets wrapped in a block by the parser. This works just fine with
the typer, but crashes in uncurry. This commit brings quasiquotes in line with the parser.
  • Loading branch information...
xeno-by committed Mar 27, 2014
1 parent c765537 commit a0c3bbdb3a4b1055834f3ff5f729dc3af1867696
@@ -523,11 +523,21 @@ trait ReificationSupport { self: SymbolTable =>
object SyntacticDefDef extends SyntacticDefDefExtractor {
def apply(mods: Modifiers, name: TermName, tparams: List[Tree],
vparamss: List[List[Tree]], tpt: Tree, rhs: Tree): DefDef = {
+ val tparams0 = mkTparams(tparams)
val vparamss0 = mkParam(vparamss, PARAM)
- DefDef(mods, name, mkTparams(tparams), vparamss0, tpt, rhs)
+ val rhs0 = {
+ if (name != nme.CONSTRUCTOR) rhs
+ else rhs match {
+ case Block(_, _) => rhs
+ case _ => Block(List(rhs), gen.mkSyntheticUnit)
+ }
+ }
+ DefDef(mods, name, tparams0, vparamss0, tpt, rhs0)
}
def unapply(tree: Tree): Option[(Modifiers, TermName, List[TypeDef], List[List[ValDef]], Tree, Tree)] = tree match {
+ case DefDef(mods, nme.CONSTRUCTOR, tparams, vparamss, tpt, Block(List(expr), Literal(Constant(())))) =>
+ Some((mods, nme.CONSTRUCTOR, tparams, vparamss, tpt, expr))
case DefDef(mods, name, tparams, vparamss, tpt, rhs) =>
Some((mods, name, tparams, vparamss, tpt, rhs))
case _ => None
@@ -90,6 +90,15 @@ trait ClassConstruction { self: QuasiquoteProperties =>
val args = q"val a: Int; val b: Int"
assertEqAst(q"class C(implicit ..$args)", "class C(implicit val a: Int, val b: Int)")
}
+
+ property("SI-8451: inline secondary constructors") = test {
+ assertEqAst(q"class C(x: Int) { def this() = this(0) }", "class C(x: Int) { def this() = this(0) }")
+ }
+
+ property("SI-8451: unquoted secondary constructors") = test {
+ val secondaryCtor = q"def this() = this(0)"
+ assertEqAst(q"class C(x: Int) { $secondaryCtor }", "class C(x: Int) { def this() = this(0) }")
+ }
}
trait TraitConstruction { self: QuasiquoteProperties =>
@@ -229,6 +229,12 @@ trait DefDeconstruction { self: QuasiquoteProperties =>
val q"def foo(...$argss)(implicit ..$impl)" = q"def foo(x: Int)"
assert(impl.isEmpty)
}
+
+ property("SI-8451") = test {
+ val q"def this(..$params) = this(..$args)" = q"def this(x: Int) = this(0)"
+ assert(params ≈ List(q"${Modifiers(PARAM)} val x: Int"))
+ assert(args ≈ List(q"0"))
+ }
}
trait ImportDeconstruction { self: QuasiquoteProperties =>
@@ -195,6 +195,19 @@ object ErrorProps extends QuasiquoteProperties("errors") {
q"f(..$l)"
""")
+ property("SI-8451 construction: disallow everything except for constructor calls in secondary constructor bodies") = fails(
+ "'this' expected but unquotee found",
+ """
+ val rhs1 = q"this(0)"
+ val ctor1 = q"def this(x: Int) = $rhs1"
+ """)
+
+ property("SI-8451 deconstruction: disallow everything except for constructor calls in secondary constructor bodies") = fails(
+ "'this' expected but unquotee found",
+ """
+ val q"def this(..$params) = $rhs2" = q"def this(x: Int) = this(0)"
+ """)
+
// // Make sure a nice error is reported in this case
// { import Flag._; val mods = NoMods; q"lazy $mods val x: Int" }
}

0 comments on commit a0c3bbd

Please sign in to comment.