Skip to content

Fix #6530: Only cancel term quotes with term splices (and vice versa) #6536

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
May 21, 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
15 changes: 12 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1949,9 +1949,12 @@ class Typer extends Namer
*/
def typedQuote(tree: untpd.Quote, pt: Type)(implicit ctx: Context): Tree = track("typedQuote") {
tree.quoted match {
case untpd.Splice(innerExpr) =>
case untpd.Splice(innerExpr) if tree.isTerm =>
ctx.warning("Canceled splice directly inside a quote. '{ ${ XYZ } } is equivalent to XYZ.", tree.sourcePos)
typed(innerExpr, pt)
case untpd.TypSplice(innerType) if tree.isType =>
ctx.warning("Canceled splice directly inside a quote. '[ ${ XYZ } ] is equivalent to XYZ.", tree.sourcePos)
typed(innerType, pt)
case quoted if quoted.isType =>
ctx.compilationUnit.needsStaging = true
typedTypeApply(untpd.TypeApply(untpd.ref(defn.InternalQuoted_typeQuoteR), quoted :: Nil), pt)(quoteContext).withSpan(tree.span)
Expand Down Expand Up @@ -2027,7 +2030,7 @@ class Typer extends Namer
def typedSplice(tree: untpd.Splice, pt: Type)(implicit ctx: Context): Tree = track("typedSplice") {
checkSpliceOutsideQuote(tree)
tree.expr match {
case untpd.Quote(innerExpr) =>
case untpd.Quote(innerExpr) if innerExpr.isTerm =>
ctx.warning("Canceled quote directly inside a splice. ${ '{ XYZ } } is equivalent to XYZ.", tree.sourcePos)
typed(innerExpr, pt)
case expr =>
Expand Down Expand Up @@ -2062,7 +2065,13 @@ class Typer extends Namer
def typedTypSplice(tree: untpd.TypSplice, pt: Type)(implicit ctx: Context): Tree = track("typedTypSplice") {
ctx.compilationUnit.needsStaging = true
checkSpliceOutsideQuote(tree)
typedSelect(untpd.Select(tree.expr, tpnme.splice), pt)(spliceContext).withSpan(tree.span)
tree.expr match {
case untpd.Quote(innerType) if innerType.isType =>
ctx.warning("Canceled quote directly inside a splice. ${ '[ XYZ ] } is equivalent to XYZ.", tree.sourcePos)
typed(innerType, pt)
case expr =>
typedSelect(untpd.Select(tree.expr, tpnme.splice), pt)(spliceContext).withSpan(tree.span)
}
}

private def checkSpliceOutsideQuote(tree: untpd.Tree)(implicit ctx: Context): Unit = {
Expand Down
5 changes: 5 additions & 0 deletions tests/neg/i6530.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- [E007] Type Mismatch Error: tests/neg/i6530.scala:2:26 --------------------------------------------------------------
2 | inline def q : Int = ${ '[ Int ] } // error
| ^^^^^^^^
| Found: quoted.Type[Int]
| Required: quoted.Expr[Int]
4 changes: 4 additions & 0 deletions tests/neg/i6530.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object Macros {
inline def q : Int = ${ '[ Int ] } // error
val x : Int = 1 + q
}
11 changes: 11 additions & 0 deletions tests/neg/i6530b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
object Foo {
val program = '{
val tpe: quoted.Type[Int] = ???
val expr: quoted.Expr[Int] = ???

val a: quoted.Expr[Int] = ${ '[Int] } // error
val b: quoted.Expr[Int] = '{ $tpe } // error
val c: ${ '{ 43 } } = ??? // error
val d: quoted.Type[Int] = '[ $expr ] // error
}
}