Skip to content

Commit f0d758b

Browse files
committed
Use SplicedExpr instead of Splice trees
1 parent cf30d30 commit f0d758b

File tree

8 files changed

+19
-29
lines changed

8 files changed

+19
-29
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,9 @@ object desugar {
338338
def quotedPattern(tree: untpd.Tree, expectedTpt: untpd.Tree)(using Context): untpd.Tree = {
339339
def adaptToExpectedTpt(tree: untpd.Tree): untpd.Tree = tree match {
340340
// Add the expected type as an ascription
341-
case _: untpd.Splice =>
341+
case _: untpd.SplicedExpr =>
342342
untpd.Typed(tree, expectedTpt).withSpan(tree.span)
343-
case Typed(expr: untpd.Splice, tpt) =>
343+
case Typed(expr: untpd.SplicedExpr, tpt) =>
344344
cpy.Typed(tree)(expr, untpd.makeAndType(tpt, expectedTpt).withSpan(tpt.span))
345345

346346
// Propagate down the expected type to the leafs of the expression
@@ -1989,7 +1989,7 @@ object desugar {
19891989
case QuotedExpr(expr, _) =>
19901990
new UntypedTreeTraverser {
19911991
def traverse(tree: untpd.Tree)(using Context): Unit = tree match {
1992-
case Splice(expr) => collect(expr)
1992+
case SplicedExpr(expr, _) => collect(expr)
19931993
case _ => traverseChildren(tree)
19941994
}
19951995
}.traverse(expr)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ object Trees {
685685
case class SplicedExpr[+T <: Untyped] private[ast] (expr: Tree[T], tpt: Tree[T])(implicit @constructorOnly src: SourceFile)
686686
extends TermTree[T] {
687687
type ThisTree[+T <: Untyped] = SplicedExpr[T]
688+
def isInBraces: Boolean = span.end != expr.span.end
688689
}
689690

690691
/** A type tree that represents an existing or inferred type */

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +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 Splice(expr: Tree)(implicit @constructorOnly src: SourceFile) extends TermTree {
115-
def isInBraces: Boolean = span.end != expr.span.end
116-
}
117114
case class ForYield(enums: List[Tree], expr: Tree)(implicit @constructorOnly src: SourceFile) extends TermTree
118115
case class ForDo(enums: List[Tree], body: Tree)(implicit @constructorOnly src: SourceFile) extends TermTree
119116
case class GenFrom(pat: Tree, expr: Tree, checkMode: GenCheckMode)(implicit @constructorOnly src: SourceFile) extends Tree
@@ -623,10 +620,6 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
623620
case tree: Throw if expr eq tree.expr => tree
624621
case _ => finalize(tree, untpd.Throw(expr)(tree.source))
625622
}
626-
def Splice(tree: Tree)(expr: Tree)(using Context): Tree = tree match {
627-
case tree: Splice if expr eq tree.expr => tree
628-
case _ => finalize(tree, untpd.Splice(expr)(tree.source))
629-
}
630623
def ForYield(tree: Tree)(enums: List[Tree], expr: Tree)(using Context): TermTree = tree match {
631624
case tree: ForYield if (enums eq tree.enums) && (expr eq tree.expr) => tree
632625
case _ => finalize(tree, untpd.ForYield(enums, expr)(tree.source))
@@ -708,8 +701,6 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
708701
cpy.Tuple(tree)(transform(trees))
709702
case Throw(expr) =>
710703
cpy.Throw(tree)(transform(expr))
711-
case Splice(expr) =>
712-
cpy.Splice(tree)(transform(expr))
713704
case ForYield(enums, expr) =>
714705
cpy.ForYield(tree)(transform(enums), transform(expr))
715706
case ForDo(enums, body) =>
@@ -767,8 +758,6 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
767758
this(x, trees)
768759
case Throw(expr) =>
769760
this(x, expr)
770-
case Splice(expr) =>
771-
this(x, expr)
772761
case ForYield(enums, expr) =>
773762
this(this(x, enums), expr)
774763
case ForDo(enums, body) =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,7 @@ object Parsers {
17641764
syntaxError(em"$msg\n\nHint: $hint", Span(start, in.lastOffset))
17651765
Ident(nme.ERROR.toTypeName)
17661766
else
1767-
Splice(expr)
1767+
SplicedExpr(expr, EmptyTree)
17681768
}
17691769

17701770
/** SimpleType ::= SimpleLiteral

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,6 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
712712
}
713713
case Number(digits, kind) =>
714714
digits
715-
case Splice(tree) =>
716-
keywordStr("${") ~ toTextGlobal(dropBlock(tree)) ~ keywordStr("}")
717715
case Thicket(trees) =>
718716
"Thicket {" ~~ toTextGlobal(trees, "\n") ~~ "}"
719717
case MacroTree(call) =>
@@ -722,7 +720,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
722720
val tptText = (keywordStr("[") ~ toTextGlobal(tpt) ~ keywordStr("]")).provided(!tpt.isEmpty && printDebug)
723721
keywordStr("'") ~ tptText ~ keywordStr("{") ~ toTextGlobal(expr) ~ keywordStr("}")
724722
case SplicedExpr(expr, tpt) =>
725-
val tptText = (keywordStr("[") ~ toTextGlobal(tpt) ~ keywordStr("]")).provided(printDebug)
723+
val tptText = (keywordStr("[") ~ toTextGlobal(tpt) ~ keywordStr("]")).provided(!tpt.isEmpty && printDebug)
726724
keywordStr("$") ~ tptText ~ keywordStr("{") ~ toTextGlobal(expr) ~ keywordStr("}")
727725
case Hole(isTermHole, idx, args, content, tpt) =>
728726
val (prefix, postfix) = if isTermHole then ("{{{", "}}}") else ("[[[", "]]]")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ trait Applications extends Compatibility {
10971097
}
10981098
else {
10991099
val app = tree.fun match
1100-
case _: untpd.Splice if ctx.mode.is(Mode.QuotedPattern) => typedAppliedSplice(tree, pt)
1100+
case _: untpd.SplicedExpr if ctx.mode.is(Mode.QuotedPattern) => typedAppliedSplice(tree, pt)
11011101
case _ => realApply
11021102
app match {
11031103
case Apply(fn @ Select(left, _), right :: Nil) if fn.hasType =>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ trait QuotesAndSplices {
3737
def typedQuote(tree: untpd.QuotedExpr, pt: Type)(using Context): Tree = {
3838
record("typedQuote")
3939
tree.expr match {
40-
case untpd.Splice(innerExpr) if tree.isTerm && !ctx.mode.is(Mode.Pattern) =>
40+
case untpd.SplicedExpr(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 _ =>
4343
}
@@ -70,7 +70,7 @@ trait QuotesAndSplices {
7070
}
7171

7272
/** Translate `${ t: Expr[T] }` into expression `t.splice` while tracking the quotation level in the context */
73-
def typedSplice(tree: untpd.Splice, pt: Type)(using Context): Tree = {
73+
def typedSplice(tree: untpd.SplicedExpr, pt: Type)(using Context): Tree = {
7474
record("typedSplice")
7575
checkSpliceOutsideQuote(tree)
7676
tree.expr match {
@@ -123,7 +123,7 @@ trait QuotesAndSplices {
123123
*/
124124
def typedAppliedSplice(tree: untpd.Apply, pt: Type)(using Context): Tree = {
125125
assert(ctx.mode.is(Mode.QuotedPattern))
126-
val untpd.Apply(splice: untpd.Splice, args) = tree: @unchecked
126+
val untpd.Apply(splice: untpd.SplicedExpr, args) = tree: @unchecked
127127
if !isFullyDefined(pt, ForceDegree.flipBottom) then
128128
report.error(em"Type must be fully defined.", splice.srcPos)
129129
tree.withType(UnspecifiedErrorType)

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,12 +2054,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
20542054
assignType(cpy.QuotedExpr(tree)(expr1, tpt1), tpt1)
20552055

20562056
def typedSplicedExpr(tree: untpd.SplicedExpr, pt: Type)(using Context): Tree =
2057-
val tpt1 = checkSimpleKinded(typedType(tree.tpt, mapPatternBounds = true))
2058-
val splicedType = // Quotes ?=> Expr[T]
2059-
defn.FunctionType(1, isContextual = true)
2060-
.appliedTo(defn.QuotesClass.typeRef, defn.QuotedExprClass.typeRef.appliedTo(tpt1.tpe.widenSkolem))
2061-
val expr1 = typed(tree.expr, splicedType)(using StagingLevel.spliceContext)
2062-
assignType(cpy.SplicedExpr(tree)(expr1, tpt1), tpt1)
2057+
if tree.tpt.isEmpty then
2058+
typedSplice(tree, pt)
2059+
else
2060+
val tpt1 = checkSimpleKinded(typedType(tree.tpt, mapPatternBounds = true))
2061+
val splicedType = // Quotes ?=> Expr[T]
2062+
defn.FunctionType(1, isContextual = true)
2063+
.appliedTo(defn.QuotesClass.typeRef, defn.QuotedExprClass.typeRef.appliedTo(tpt1.tpe.widenSkolem))
2064+
val expr1 = typed(tree.expr, splicedType)(using StagingLevel.spliceContext)
2065+
assignType(cpy.SplicedExpr(tree)(expr1, tpt1), tpt1)
20632066

20642067
def completeTypeTree(tree: untpd.TypeTree, pt: Type, original: untpd.Tree)(using Context): TypeTree =
20652068
tree.withSpan(original.span).withAttachmentsFrom(original)
@@ -3092,7 +3095,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
30923095
case tree @ untpd.PostfixOp(qual, Ident(nme.WILDCARD)) => typedAsFunction(tree, pt)
30933096
case untpd.EmptyTree => tpd.EmptyTree
30943097
case tree: untpd.QuotedExpr => typedQuotedExpr(tree, pt)
3095-
case tree: untpd.Splice => typedSplice(tree, pt)
30963098
case tree: untpd.SplicedExpr => typedSplicedExpr(tree, pt)
30973099
case tree: untpd.MacroTree => report.error("Unexpected macro", tree.srcPos); tpd.nullLiteral // ill-formed code may reach here
30983100
case tree: untpd.Hole => typedHole(tree, pt)

0 commit comments

Comments
 (0)