@@ -51,29 +51,25 @@ class CrossStageSafety extends TreeMapWithStages {
51
51
if (tree.source != ctx.source && tree.source.exists)
52
52
transform(tree)(using ctx.withSource(tree.source))
53
53
else tree match
54
+ case CancelledQuote (tree) =>
55
+ transform(tree) // Optimization: `'{ $x }` --> `x`
54
56
case tree : Quote =>
55
- tree.cancelled match
56
- case Some (tree1) =>
57
- transform(tree1)
58
- case None =>
59
- if (ctx.property(InAnnotation ).isDefined)
60
- report.error(" Cannot have a quote in an annotation" , tree.srcPos)
61
- val body1 = transformQuoteBody(tree.body, tree.span)
62
- val stripAnnotationsDeep : TypeMap = new TypeMap :
63
- def apply (tp : Type ): Type = mapOver(tp.stripAnnots)
64
- val bodyType1 = healType(tree.srcPos)(stripAnnotationsDeep(tree.bodyType))
65
- cpy.Quote (tree)(body1).withBodyType(bodyType1)
57
+ if (ctx.property(InAnnotation ).isDefined)
58
+ report.error(" Cannot have a quote in an annotation" , tree.srcPos)
59
+ val body1 = transformQuoteBody(tree.body, tree.span)
60
+ val stripAnnotationsDeep : TypeMap = new TypeMap :
61
+ def apply (tp : Type ): Type = mapOver(tp.stripAnnots)
62
+ val bodyType1 = healType(tree.srcPos)(stripAnnotationsDeep(tree.bodyType))
63
+ cpy.Quote (tree)(body1).withBodyType(bodyType1)
66
64
65
+ case CancelledSplice (tree) =>
66
+ transform(tree) // Optimization: `${ 'x }` --> `x`
67
67
case tree : Splice =>
68
- tree.cancelled match
69
- case Some (tree1) =>
70
- transform(tree1)
71
- case None =>
72
- val body1 = transform(tree.expr)(using spliceContext)
73
- val tpe1 =
74
- if level == 0 then tree.tpe
75
- else healType(tree.srcPos)(tree.tpe.widenTermRefExpr)
76
- untpd.cpy.Splice (tree)(body1).withType(tpe1)
68
+ val body1 = transform(tree.expr)(using spliceContext)
69
+ val tpe1 =
70
+ if level == 0 then tree.tpe
71
+ else healType(tree.srcPos)(tree.tpe.widenTermRefExpr)
72
+ untpd.cpy.Splice (tree)(body1).withType(tpe1)
77
73
78
74
case tree @ QuotedTypeOf (body) =>
79
75
if (ctx.property(InAnnotation ).isDefined)
@@ -224,4 +220,20 @@ class CrossStageSafety extends TreeMapWithStages {
224
220
| - but the access is at level $level. $hint""" , pos)
225
221
tp
226
222
}
223
+
224
+ private object CancelledQuote :
225
+ def unapply (tree : Quote ): Option [Tree ] =
226
+ def rec (tree : Tree ): Option [Tree ] = tree match
227
+ case Block (Nil , expr) => rec(expr)
228
+ case Splice (inner) => Some (inner)
229
+ case _ => None
230
+ rec(tree.body)
231
+
232
+ private object CancelledSplice :
233
+ def unapply (tree : Splice ): Option [Tree ] =
234
+ def rec (tree : Tree ): Option [Tree ] = tree match
235
+ case Block (Nil , expr) => rec(expr)
236
+ case Quote (inner) => Some (inner)
237
+ case _ => None
238
+ rec(tree.expr)
227
239
}
0 commit comments