@@ -312,6 +312,42 @@ object desugar {
312312 } else tree
313313 }
314314
315+ /** Add an explicit ascription to the `expectedTpt` to every tail splice.
316+ *
317+ * - `'{ x }` -> `'{ x }`
318+ * - `'{ $x }` -> `'{ $x: T }`
319+ * - `'{ if (...) $x else $y }` -> `'{ if (...) ($x: T) else ($y: T) }`
320+ *
321+ * Note that the splice `$t: T` will be typed as `${t: Expr[T]}`
322+ */
323+ def quotedPattern (tree : untpd.Tree , expectedTpt : untpd.Tree )(implicit ctx : Context ): untpd.Tree = {
324+ def adaptToExpectedTpt (tree : untpd.Tree ): untpd.Tree = tree match {
325+ // Add the expected type as an ascription
326+ case _ : untpd.Splice =>
327+ untpd.Typed (tree, expectedTpt).withSpan(tree.span)
328+ case Typed (expr : untpd.Splice , tpt) =>
329+ cpy.Typed (tree)(expr, untpd.makeAndType(tpt, expectedTpt).withSpan(tpt.span))
330+
331+ // Propagate down the expected type to the leafs of the expression
332+ case Block (stats, expr) =>
333+ cpy.Block (tree)(stats, adaptToExpectedTpt(expr))
334+ case If (cond, thenp, elsep) =>
335+ cpy.If (tree)(cond, adaptToExpectedTpt(thenp), adaptToExpectedTpt(elsep))
336+ case untpd.Parens (expr) =>
337+ cpy.Parens (tree)(adaptToExpectedTpt(expr))
338+ case Match (selector, cases) =>
339+ val newCases = cases.map(cdef => cpy.CaseDef (cdef)(body = adaptToExpectedTpt(cdef.body)))
340+ cpy.Match (tree)(selector, newCases)
341+ case untpd.ParsedTry (expr, handler, finalizer) =>
342+ cpy.ParsedTry (tree)(adaptToExpectedTpt(expr), adaptToExpectedTpt(handler), finalizer)
343+
344+ // Tree does not need to be ascribed
345+ case _ =>
346+ tree
347+ }
348+ adaptToExpectedTpt(tree)
349+ }
350+
315351 // Add all evidence parameters in `params` as implicit parameters to `meth` */
316352 private def addEvidenceParams (meth : DefDef , params : List [ValDef ])(implicit ctx : Context ): DefDef =
317353 params match {
0 commit comments