Skip to content

Commit c64d187

Browse files
committed
Fix #6530: Only cancel term quotes with term splices (and vice versa)
1 parent fb6667b commit c64d187

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,9 +1949,12 @@ class Typer extends Namer
19491949
*/
19501950
def typedQuote(tree: untpd.Quote, pt: Type)(implicit ctx: Context): Tree = track("typedQuote") {
19511951
tree.quoted match {
1952-
case untpd.Splice(innerExpr) =>
1952+
case untpd.Splice(innerExpr) if tree.isTerm =>
19531953
ctx.warning("Canceled splice directly inside a quote. '{ ${ XYZ } } is equivalent to XYZ.", tree.sourcePos)
19541954
typed(innerExpr, pt)
1955+
case untpd.TypSplice(innerType) if tree.isType =>
1956+
ctx.warning("Canceled splice directly inside a quote. '[ ${ XYZ } ] is equivalent to XYZ.", tree.sourcePos)
1957+
typed(innerType, pt)
19551958
case quoted if quoted.isType =>
19561959
ctx.compilationUnit.needsStaging = true
19571960
typedTypeApply(untpd.TypeApply(untpd.ref(defn.InternalQuoted_typeQuoteR), quoted :: Nil), pt)(quoteContext).withSpan(tree.span)
@@ -2027,7 +2030,7 @@ class Typer extends Namer
20272030
def typedSplice(tree: untpd.Splice, pt: Type)(implicit ctx: Context): Tree = track("typedSplice") {
20282031
checkSpliceOutsideQuote(tree)
20292032
tree.expr match {
2030-
case untpd.Quote(innerExpr) =>
2033+
case untpd.Quote(innerExpr) if innerExpr.isTerm =>
20312034
ctx.warning("Canceled quote directly inside a splice. ${ '{ XYZ } } is equivalent to XYZ.", tree.sourcePos)
20322035
typed(innerExpr, pt)
20332036
case expr =>
@@ -2062,7 +2065,13 @@ class Typer extends Namer
20622065
def typedTypSplice(tree: untpd.TypSplice, pt: Type)(implicit ctx: Context): Tree = track("typedTypSplice") {
20632066
ctx.compilationUnit.needsStaging = true
20642067
checkSpliceOutsideQuote(tree)
2065-
typedSelect(untpd.Select(tree.expr, tpnme.splice), pt)(spliceContext).withSpan(tree.span)
2068+
tree.expr match {
2069+
case untpd.Quote(innerType) if innerType.isType =>
2070+
ctx.warning("Canceled quote directly inside a splice. ${ '[ XYZ ] } is equivalent to XYZ.", tree.sourcePos)
2071+
typed(innerType, pt)
2072+
case expr =>
2073+
typedSelect(untpd.Select(tree.expr, tpnme.splice), pt)(spliceContext).withSpan(tree.span)
2074+
}
20662075
}
20672076

20682077
private def checkSpliceOutsideQuote(tree: untpd.Tree)(implicit ctx: Context): Unit = {

tests/neg/i6530.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- [E007] Type Mismatch Error: tests/neg/i6530.scala:2:26 --------------------------------------------------------------
2+
2 | inline def q : Int = ${ '[ Int ] } // error
3+
| ^^^^^^^^
4+
| Found: quoted.Type[Int]
5+
| Required: quoted.Expr[Int]

tests/neg/i6530.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Macros {
2+
inline def q : Int = ${ '[ Int ] } // error
3+
val x : Int = 1 + q
4+
}

tests/neg/i6530b.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object Foo {
2+
val program = '{
3+
val tpe: quoted.Type[Int] = ???
4+
val expr: quoted.Expr[Int] = ???
5+
6+
val a: quoted.Expr[Int] = ${ '[Int] } // error
7+
val b: quoted.Expr[Int] = '{ $tpe } // error
8+
val c: ${ '{ 43 } } = ??? // error
9+
val d: quoted.Type[Int] = '[ $expr ] // error
10+
}
11+
}

0 commit comments

Comments
 (0)