Skip to content

Cancel quotes/splice early #6093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1933,18 +1933,27 @@ class Typer extends Namer
* while tracking the quotation level in the context.
*/
def typedQuote(tree: untpd.Quote, pt: Type)(implicit ctx: Context): Tree = track("typedQuote") {
val tree1 =
if (tree.t.isType)
typedTypeApply(untpd.TypeApply(untpd.ref(defn.InternalQuoted_typeQuoteR), List(tree.t)), pt)(quoteContext)
else
typedApply(untpd.Apply(untpd.ref(defn.InternalQuoted_exprQuoteR), tree.t), pt)(quoteContext)
tree1.withSpan(tree.span)
tree.t match {
case untpd.Splice(innerExpr) =>
ctx.warning("Canceled splice directly inside a quote. '{ ${ XYZ } } is equivalent to XYZ.", tree.sourcePos)
typed(innerExpr, pt)
case t if t.isType =>
typedTypeApply(untpd.TypeApply(untpd.ref(defn.InternalQuoted_typeQuoteR), List(tree.t)), pt)(quoteContext).withSpan(tree.span)
case t=>
typedApply(untpd.Apply(untpd.ref(defn.InternalQuoted_exprQuoteR), tree.t), pt)(quoteContext).withSpan(tree.span)
}
}

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

/** Translate ${ t: Type[T] }` into type `t.splice` while tracking the quotation level in the context */
Expand Down
13 changes: 13 additions & 0 deletions tests/neg-custom-args/fatal-warnings/quote-simple-hole.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Test {
val x = '{0}
val y = '{ // error: Canceled splice directly inside a quote. '{ ${ XYZ } } is equivalent to XYZ.
$x
}
val z = '{
val a = ${ // error: Canceled quote directly inside a splice. ${ '{ XYZ } } is equivalent to XYZ.
'{
$y
}
}
}
}
3 changes: 2 additions & 1 deletion tests/run-with-compiler/quote-nested-2.check
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
val a: scala.quoted.Expr[scala.Int] = '{4}
a

(a: scala.quoted.Expr[scala.Int])
}
3 changes: 2 additions & 1 deletion tests/run-with-compiler/quote-nested-5.check
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
val a: scala.quoted.Expr[scala.Int] = '{4}
a

(a: scala.quoted.Expr[scala.Int])
}