Skip to content

Commit d06beff

Browse files
Merge pull request #6093 from dotty-staging/cancel-quote-splice-early
Cancel quotes/splice early
2 parents 3c9935f + d06cf48 commit d06beff

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,18 +1933,27 @@ class Typer extends Namer
19331933
* while tracking the quotation level in the context.
19341934
*/
19351935
def typedQuote(tree: untpd.Quote, pt: Type)(implicit ctx: Context): Tree = track("typedQuote") {
1936-
val tree1 =
1937-
if (tree.t.isType)
1938-
typedTypeApply(untpd.TypeApply(untpd.ref(defn.InternalQuoted_typeQuoteR), List(tree.t)), pt)(quoteContext)
1939-
else
1940-
typedApply(untpd.Apply(untpd.ref(defn.InternalQuoted_exprQuoteR), tree.t), pt)(quoteContext)
1941-
tree1.withSpan(tree.span)
1936+
tree.t match {
1937+
case untpd.Splice(innerExpr) =>
1938+
ctx.warning("Canceled splice directly inside a quote. '{ ${ XYZ } } is equivalent to XYZ.", tree.sourcePos)
1939+
typed(innerExpr, pt)
1940+
case t if t.isType =>
1941+
typedTypeApply(untpd.TypeApply(untpd.ref(defn.InternalQuoted_typeQuoteR), List(tree.t)), pt)(quoteContext).withSpan(tree.span)
1942+
case t=>
1943+
typedApply(untpd.Apply(untpd.ref(defn.InternalQuoted_exprQuoteR), tree.t), pt)(quoteContext).withSpan(tree.span)
1944+
}
19421945
}
19431946

19441947
/** Translate `${ t: Expr[T] }` into expresiion `t.splice` while tracking the quotation level in the context */
19451948
def typedSplice(tree: untpd.Splice, pt: Type)(implicit ctx: Context): Tree = track("typedSplice") {
19461949
checkSpliceOutsideQuote(tree)
1947-
typedSelect(untpd.Select(tree.expr, nme.splice), pt)(spliceContext).withSpan(tree.span)
1950+
tree.expr match {
1951+
case untpd.Quote(innerExpr) =>
1952+
ctx.warning("Canceled quote directly inside a splice. ${ '{ XYZ } } is equivalent to XYZ.", tree.sourcePos)
1953+
typed(innerExpr, pt)
1954+
case expr =>
1955+
typedSelect(untpd.Select(expr, nme.splice), pt)(spliceContext).withSpan(tree.span)
1956+
}
19481957
}
19491958

19501959
/** Translate ${ t: Type[T] }` into type `t.splice` while tracking the quotation level in the context */
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Test {
2+
val x = '{0}
3+
val y = '{ // error: Canceled splice directly inside a quote. '{ ${ XYZ } } is equivalent to XYZ.
4+
$x
5+
}
6+
val z = '{
7+
val a = ${ // error: Canceled quote directly inside a splice. ${ '{ XYZ } } is equivalent to XYZ.
8+
'{
9+
$y
10+
}
11+
}
12+
}
13+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
val a: scala.quoted.Expr[scala.Int] = '{4}
3-
a
3+
4+
(a: scala.quoted.Expr[scala.Int])
45
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
val a: scala.quoted.Expr[scala.Int] = '{4}
3-
a
3+
4+
(a: scala.quoted.Expr[scala.Int])
45
}

0 commit comments

Comments
 (0)