Skip to content

Commit cf30d30

Browse files
committed
Use QuotedExpr instead of Quote trees
1 parent 161ef49 commit cf30d30

File tree

6 files changed

+23
-32
lines changed

6 files changed

+23
-32
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,7 @@ object desugar {
19861986
trees foreach collect
19871987
case Block(Nil, expr) =>
19881988
collect(expr)
1989-
case Quote(expr) =>
1989+
case QuotedExpr(expr, _) =>
19901990
new UntypedTreeTraverser {
19911991
def traverse(tree: untpd.Tree)(using Context): Unit = tree match {
19921992
case Splice(expr) => collect(expr)

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
111111
override def isType: Boolean = !isTerm
112112
}
113113
case class Throw(expr: Tree)(implicit @constructorOnly src: SourceFile) extends TermTree
114-
case class Quote(quoted: Tree)(implicit @constructorOnly src: SourceFile) extends TermTree
115114
case class Splice(expr: Tree)(implicit @constructorOnly src: SourceFile) extends TermTree {
116115
def isInBraces: Boolean = span.end != expr.span.end
117116
}
@@ -624,10 +623,6 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
624623
case tree: Throw if expr eq tree.expr => tree
625624
case _ => finalize(tree, untpd.Throw(expr)(tree.source))
626625
}
627-
def Quote(tree: Tree)(quoted: Tree)(using Context): Tree = tree match {
628-
case tree: Quote if quoted eq tree.quoted => tree
629-
case _ => finalize(tree, untpd.Quote(quoted)(tree.source))
630-
}
631626
def Splice(tree: Tree)(expr: Tree)(using Context): Tree = tree match {
632627
case tree: Splice if expr eq tree.expr => tree
633628
case _ => finalize(tree, untpd.Splice(expr)(tree.source))
@@ -713,8 +708,6 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
713708
cpy.Tuple(tree)(transform(trees))
714709
case Throw(expr) =>
715710
cpy.Throw(tree)(transform(expr))
716-
case Quote(t) =>
717-
cpy.Quote(tree)(transform(t))
718711
case Splice(expr) =>
719712
cpy.Splice(tree)(transform(expr))
720713
case ForYield(enums, expr) =>
@@ -774,8 +767,6 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
774767
this(x, trees)
775768
case Throw(expr) =>
776769
this(x, expr)
777-
case Quote(t) =>
778-
this(x, t)
779770
case Splice(expr) =>
780771
this(x, expr)
781772
case ForYield(enums, expr) =>

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,7 @@ object Parsers {
12621262
}
12631263
}
12641264
in.nextToken()
1265-
Quote(t)
1265+
QuotedExpr(t, EmptyTree)
12661266
}
12671267
else
12681268
if !in.featureEnabled(Feature.symbolLiterals) then
@@ -2494,10 +2494,10 @@ object Parsers {
24942494
case QUOTE =>
24952495
atSpan(in.skipToken()) {
24962496
withinStaged(StageKind.Quoted | (if (location.inPattern) StageKind.QuotedPattern else 0)) {
2497-
Quote {
2497+
val expr =
24982498
if (in.token == LBRACKET) inBrackets(typ())
24992499
else stagedBlock()
2500-
}
2500+
QuotedExpr(expr, EmptyTree)
25012501
}
25022502
}
25032503
case NEW =>

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,16 +712,14 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
712712
}
713713
case Number(digits, kind) =>
714714
digits
715-
case Quote(tree) if tree.isTerm =>
716-
keywordStr("'{") ~ toTextGlobal(dropBlock(tree)) ~ keywordStr("}")
717715
case Splice(tree) =>
718716
keywordStr("${") ~ toTextGlobal(dropBlock(tree)) ~ keywordStr("}")
719717
case Thicket(trees) =>
720718
"Thicket {" ~~ toTextGlobal(trees, "\n") ~~ "}"
721719
case MacroTree(call) =>
722720
keywordStr("macro ") ~ toTextGlobal(call)
723721
case QuotedExpr(expr, tpt) =>
724-
val tptText = (keywordStr("[") ~ toTextGlobal(tpt) ~ keywordStr("]")).provided(printDebug)
722+
val tptText = (keywordStr("[") ~ toTextGlobal(tpt) ~ keywordStr("]")).provided(!tpt.isEmpty && printDebug)
725723
keywordStr("'") ~ tptText ~ keywordStr("{") ~ toTextGlobal(expr) ~ keywordStr("}")
726724
case SplicedExpr(expr, tpt) =>
727725
val tptText = (keywordStr("[") ~ toTextGlobal(tpt) ~ keywordStr("]")).provided(printDebug)

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ trait QuotesAndSplices {
3434
/** Translate `'{ e }` into `scala.quoted.Expr.apply(e)` and `'[T]` into `scala.quoted.Type.apply[T]`
3535
* while tracking the quotation level in the context.
3636
*/
37-
def typedQuote(tree: untpd.Quote, pt: Type)(using Context): Tree = {
37+
def typedQuote(tree: untpd.QuotedExpr, pt: Type)(using Context): Tree = {
3838
record("typedQuote")
39-
tree.quoted match {
39+
tree.expr match {
4040
case untpd.Splice(innerExpr) if tree.isTerm && !ctx.mode.is(Mode.Pattern) =>
4141
report.warning("Canceled splice directly inside a quote. '{ ${ XYZ } } is equivalent to XYZ.", tree.srcPos)
4242
case _ =>
@@ -50,15 +50,15 @@ trait QuotesAndSplices {
5050

5151
if ctx.mode.is(Mode.Pattern) then
5252
typedQuotePattern(tree, pt, quotes).withSpan(tree.span)
53-
else if tree.quoted.isType then
53+
else if tree.expr.isType then
5454
val msg = em"""Quoted types `'[..]` can only be used in patterns.
5555
|
5656
|Hint: To get a scala.quoted.Type[T] use scala.quoted.Type.of[T] instead.
5757
|"""
5858
report.error(msg, tree.srcPos)
5959
EmptyTree
6060
else
61-
val exprQuoteTree = untpd.Apply(untpd.ref(defn.QuotedRuntime_exprQuote.termRef), tree.quoted)
61+
val exprQuoteTree = untpd.Apply(untpd.ref(defn.QuotedRuntime_exprQuote.termRef), tree.expr)
6262
val quotedExpr = typedApply(exprQuoteTree, pt)(using quoteContext) match
6363
case Apply(TypeApply(fn, tpt :: Nil), quotedExpr :: Nil) => QuotedExpr(quotedExpr, tpt)
6464
makeInlineable(quotedExpr.select(nme.apply).appliedTo(quotes).withSpan(tree.span))
@@ -74,7 +74,7 @@ trait QuotesAndSplices {
7474
record("typedSplice")
7575
checkSpliceOutsideQuote(tree)
7676
tree.expr match {
77-
case untpd.Quote(innerExpr) if innerExpr.isTerm =>
77+
case untpd.QuotedExpr(innerExpr, _) if innerExpr.isTerm =>
7878
report.warning("Canceled quote directly inside a splice. ${ '{ XYZ } } is equivalent to XYZ.", tree.srcPos)
7979
return typed(innerExpr, pt)
8080
case _ =>
@@ -382,13 +382,13 @@ trait QuotesAndSplices {
382382
* ) => ...
383383
* ```
384384
*/
385-
private def typedQuotePattern(tree: untpd.Quote, pt: Type, qctx: Tree)(using Context): Tree = {
386-
if tree.quoted.isTerm && !pt.derivesFrom(defn.QuotedExprClass) then
385+
private def typedQuotePattern(tree: untpd.QuotedExpr, pt: Type, qctx: Tree)(using Context): Tree = {
386+
val quoted = tree.expr
387+
if quoted.isTerm && !pt.derivesFrom(defn.QuotedExprClass) then
387388
report.error("Quote pattern can only match scrutinees of type scala.quoted.Expr", tree.srcPos)
388-
else if tree.quoted.isType && !pt.derivesFrom(defn.QuotedTypeClass) then
389+
else if quoted.isType && !pt.derivesFrom(defn.QuotedTypeClass) then
389390
report.error("Quote pattern can only match scrutinees of type scala.quoted.Type", tree.srcPos)
390391

391-
val quoted = tree.quoted
392392
val exprPt = pt.baseType(if quoted.isType then defn.QuotedTypeClass else defn.QuotedExprClass)
393393
val quotedPt = exprPt.argInfos.headOption match {
394394
case Some(argPt: ValueType) => argPt // excludes TypeBounds
@@ -440,12 +440,12 @@ trait QuotesAndSplices {
440440
if splices.isEmpty then ref(defn.EmptyTupleModule.termRef)
441441
else typed(untpd.Tuple(splices.map(x => untpd.TypedSplice(replaceBindingsInTree.transform(x)))).withSpan(quoted.span), patType)
442442

443-
val quoteClass = if (tree.quoted.isTerm) defn.QuotedExprClass else defn.QuotedTypeClass
443+
val quoteClass = if (quoted.isTerm) defn.QuotedExprClass else defn.QuotedTypeClass
444444
val quotedPattern =
445-
if (tree.quoted.isTerm) tpd.QuotedExpr(shape, TypeTree(defn.AnyType)).select(nme.apply).appliedTo(qctx)
445+
if (quoted.isTerm) tpd.QuotedExpr(shape, TypeTree(defn.AnyType)).select(nme.apply).appliedTo(qctx)
446446
else ref(defn.QuotedTypeModule_of.termRef).appliedToTypeTree(shape).appliedTo(qctx)
447447

448-
val matchModule = if tree.quoted.isTerm then defn.QuoteMatching_ExprMatch else defn.QuoteMatching_TypeMatch
448+
val matchModule = if quoted.isTerm then defn.QuoteMatching_ExprMatch else defn.QuoteMatching_TypeMatch
449449
val unapplyFun = qctx.asInstance(defn.QuoteMatchingClass.typeRef).select(matchModule).select(nme.unapply)
450450

451451
UnApply(

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,9 +2046,12 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
20462046
}
20472047

20482048
def typedQuotedExpr(tree: untpd.QuotedExpr, pt: Type)(using Context): Tree =
2049-
val tpt1 = checkSimpleKinded(typedType(tree.tpt, mapPatternBounds = true))
2050-
val expr1 = typed(tree.expr, tpt1.tpe.widenSkolem)(using StagingLevel.quoteContext)
2051-
assignType(cpy.QuotedExpr(tree)(expr1, tpt1), tpt1)
2049+
if tree.tpt.isEmpty then
2050+
typedQuote(tree, pt)
2051+
else
2052+
val tpt1 = checkSimpleKinded(typedType(tree.tpt, mapPatternBounds = true))
2053+
val expr1 = typed(tree.expr, tpt1.tpe.widenSkolem)(using StagingLevel.quoteContext)
2054+
assignType(cpy.QuotedExpr(tree)(expr1, tpt1), tpt1)
20522055

20532056
def typedSplicedExpr(tree: untpd.SplicedExpr, pt: Type)(using Context): Tree =
20542057
val tpt1 = checkSimpleKinded(typedType(tree.tpt, mapPatternBounds = true))
@@ -3088,7 +3091,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
30883091
case tree: untpd.ParsedTry => typedTry(tree, pt)
30893092
case tree @ untpd.PostfixOp(qual, Ident(nme.WILDCARD)) => typedAsFunction(tree, pt)
30903093
case untpd.EmptyTree => tpd.EmptyTree
3091-
case tree: untpd.Quote => typedQuote(tree, pt)
30923094
case tree: untpd.QuotedExpr => typedQuotedExpr(tree, pt)
30933095
case tree: untpd.Splice => typedSplice(tree, pt)
30943096
case tree: untpd.SplicedExpr => typedSplicedExpr(tree, pt)

0 commit comments

Comments
 (0)