Skip to content

Commit e53de14

Browse files
committed
Fix inline macros at level 0 within a splice and quote
1 parent 867e7dc commit e53de14

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,10 +1313,6 @@ object desugar {
13131313
val desugared = tree match {
13141314
case SymbolLit(str) =>
13151315
Literal(Constant(scala.Symbol(str)))
1316-
case Splice(expr) => // TODO move to typer and track level
1317-
Select(expr, nme.splice)
1318-
case TypSplice(expr) => // TODO move to typer and track level
1319-
Select(expr, tpnme.splice)
13201316
case InterpolatedString(id, segments) =>
13211317
val strs = segments map {
13221318
case ts: Thicket => ts.trees.head

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,14 +1960,26 @@ class Typer extends Namer
19601960
}
19611961
}
19621962

1963-
/** Translate `'(expr)`/`'{ expr* }` into `scala.quoted.Expr.apply(expr)` and `'[T]` into `scala.quoted.Type.apply[T]`
1963+
/** Translate '{ t }` into `scala.quoted.Expr.apply(t)` and `'[T]` into `scala.quoted.Type.apply[T]`
19641964
* while tracking the quotation level in the context.
19651965
*/
19661966
def typedQuote(tree: untpd.Quote, pt: Type)(implicit ctx: Context): Tree = track("typedQuote") {
1967-
if (tree.t.isType)
1968-
typedTypeApply(untpd.TypeApply(untpd.ref(defn.QuotedType_applyR), List(tree.t)), pt)(TreeMapWithStages.quoteContext).withSpan(tree.span)
1969-
else
1970-
typedApply(untpd.Apply(untpd.ref(defn.QuotedExpr_applyR), tree.t), pt)(TreeMapWithStages.quoteContext).withSpan(tree.span)
1967+
val tree1 =
1968+
if (tree.t.isType)
1969+
typedTypeApply(untpd.TypeApply(untpd.ref(defn.QuotedType_applyR), List(tree.t)), pt)(TreeMapWithStages.quoteContext)
1970+
else
1971+
typedApply(untpd.Apply(untpd.ref(defn.QuotedExpr_applyR), tree.t), pt)(TreeMapWithStages.quoteContext)
1972+
tree1.withSpan(tree.span)
1973+
}
1974+
1975+
/** Translate `${ t: Expr[T] }` into expresiion `t.splice` while tracking the quotation level in the context */
1976+
def typedSplice(tree: untpd.Splice, pt: Type)(implicit ctx: Context): Tree = track("typedSplice") {
1977+
typedSelect(untpd.Select(tree.expr, nme.splice), pt)(TreeMapWithStages.spliceContext).withSpan(tree.span)
1978+
}
1979+
1980+
/** Translate ${ t: Type[T] }` into type `t.splice` while tracking the quotation level in the context */
1981+
def typedTypSplice(tree: untpd.TypSplice, pt: Type)(implicit ctx: Context): Tree = track("typedTypSplice") {
1982+
typedSelect(untpd.Select(tree.expr, tpnme.splice), pt)(TreeMapWithStages.spliceContext).withSpan(tree.span)
19711983
}
19721984

19731985
/** Retrieve symbol attached to given tree */
@@ -2061,6 +2073,8 @@ class Typer extends Namer
20612073
case tree @ untpd.PostfixOp(qual, Ident(nme.WILDCARD)) => typedAsFunction(tree, pt)
20622074
case untpd.EmptyTree => tpd.EmptyTree
20632075
case tree: untpd.Quote => typedQuote(tree, pt)
2076+
case tree: untpd.Splice => typedSplice(tree, pt)
2077+
case tree: untpd.TypSplice => typedTypSplice(tree, pt)
20642078
case _ => typedUnadapted(desugar(tree), pt, locked)
20652079
}
20662080

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
val y: scala.Int = 1
3+
y.+(8)
4+
}

tests/run-with-compiler/quote-macro-in-splice/quoted_2.scala

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ import scala.quoted.Toolbox.Default._
55

66
object Test {
77
def main(args: Array[String]): Unit = {
8-
// FIXME
9-
// val x = '{
10-
// val y = 1
11-
// ${
12-
// inline def a(z: Int): Int = ${ impl('z) }
13-
// val b = a(7).toExpr
14-
// '{ y + $b }
15-
// }
16-
// }
17-
// println(x.show)
8+
val x = '{
9+
val y = 1
10+
${
11+
inline def a(z: Int): Int = ${ impl('z) }
12+
val b = a(7).toExpr
13+
'{ y + $b }
14+
}
15+
}
16+
println(x.show)
1817
}
1918

2019
}

0 commit comments

Comments
 (0)