Skip to content

Commit c266bc9

Browse files
oderskynicolasstucki
authored andcommitted
Handle splices in quoted patterns
1 parent a8d7615 commit c266bc9

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,8 +1180,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
11801180

11811181
/** An extractor for typed splices */
11821182
object Splice {
1183-
def apply(tree: Tree)(implicit ctx: Context): Tree =
1184-
ref(defn.InternalQuoted_exprSplice).appliedTo(tree)
1183+
def apply(tree: Tree)(implicit ctx: Context): Tree = {
1184+
val argType = tree.tpe.baseType(defn.QuotedExprClass).argTypesHi.head
1185+
ref(defn.InternalQuoted_exprSplice).appliedToType(argType).appliedTo(tree)
1186+
}
11851187
def unapply(tree: Tree)(implicit ctx: Context): Option[Tree] = tree match {
11861188
case Apply(fn, arg :: Nil) if fn.symbol == defn.InternalQuoted_exprSplice => Some(arg)
11871189
case _ => None

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1963,7 +1963,7 @@ class Typer extends Namer
19631963
val exprTpt = ref(defn.QuotedExprType).appliedToTypeTrees(tpt :: Nil)
19641964
transform(Splice(Typed(pat, exprTpt)))
19651965
case Splice(pat) =>
1966-
try tasty.TreePickler.Hole(patBuf.length, Nil)
1966+
try holeForSplice(tree)
19671967
finally patBuf += pat
19681968
case _ =>
19691969
super.transform(tree)
@@ -1973,6 +1973,13 @@ class Typer extends Namer
19731973
(result, splitter.patBuf.toList)
19741974
}
19751975

1976+
// TODO: Currently, a hole is expressed as interal.quoted.ExprSplice[T](???)
1977+
// Settle on a different representation and apply Stagin
1978+
def holeForSplice(splice: Tree)(implicit ctx: Context): Tree = {
1979+
val Apply(fn, arg) = splice
1980+
tpd.cpy.Apply(splice)(fn, ref(defn.Predef_undefined) :: Nil)
1981+
}
1982+
19761983
def givenReflection(implicit ctx: Context): Tree = Literal(Constant(null)) // FIXME: fill in
19771984

19781985
/** Translate `${ t: Expr[T] }` into expression `t.splice` while tracking the quotation level in the context */

tests/pos/quotedPatterns.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ object Test {
22

33
val x = '{1 + 2}
44

5+
def f(x: Int) = x
6+
57
x match {
6-
case '{1 + 2} =>
7-
case _ =>
8+
case '{1 + 2} => 0
9+
case '{f($x)} => x
810
}
911
}

0 commit comments

Comments
 (0)