@@ -37,7 +37,7 @@ trait QuotesAndSplices {
37
37
38
38
import tpd ._
39
39
40
- /** Translate `'{ t }` into `scala.quoted.Expr.apply(t )` and `'[T]` into `scala.quoted.Type.apply[T]`
40
+ /** Translate `'{ e }` into `scala.quoted.Expr.apply(e )` and `'[T]` into `scala.quoted.Type.apply[T]`
41
41
* while tracking the quotation level in the context.
42
42
*/
43
43
def typedQuote (tree : untpd.Quote , pt : Type )(using Context ): Tree = {
@@ -83,7 +83,7 @@ trait QuotesAndSplices {
83
83
def spliceOwner (ctx : Context ): Symbol =
84
84
if (ctx.mode.is(Mode .QuotedPattern )) spliceOwner(ctx.outer) else ctx.owner
85
85
val pat = typedPattern(tree.expr, defn.QuotedExprClass .typeRef.appliedTo(pt))(
86
- using spliceContext.retractMode(Mode .QuotedPattern ).withOwner(spliceOwner(ctx)))
86
+ using spliceContext.retractMode(Mode .QuotedPattern ).addMode( Mode . Pattern ). withOwner(spliceOwner(ctx)))
87
87
val baseType = pat.tpe.baseType(defn.QuotedExprClass )
88
88
val argType = if baseType != NoType then baseType.argTypesHi.head else defn.NothingType
89
89
ref(defn.InternalQuoted_exprSplice ).appliedToType(argType).appliedTo(pat)
@@ -158,25 +158,11 @@ trait QuotesAndSplices {
158
158
}
159
159
160
160
if ctx.mode.is(Mode .QuotedPattern ) && level == 1 then
161
- def spliceOwner (ctx : Context ): Symbol =
162
- if (ctx.mode.is(Mode .QuotedPattern )) spliceOwner(ctx.outer) else ctx.owner
163
- val (name, expr) = tree.expr match {
164
- case Ident (name) =>
165
- val nameOfSyntheticGiven = PatMatVarName .fresh()
166
- (name.toTypeName, untpd.cpy.Ident (tree.expr)(nameOfSyntheticGiven))
167
- case expr =>
168
- report.error(" expected a name binding" , expr.srcPos)
169
- (" $error" .toTypeName, expr)
170
- }
171
-
172
- val typeSymInfo = pt match
173
- case pt : TypeBounds => pt
174
- case _ => TypeBounds .empty
175
- val typeSym = newSymbol(spliceOwner(ctx), name, EmptyFlags , typeSymInfo, NoSymbol , tree.expr.span)
176
- typeSym.addAnnotation(Annotation (New (ref(defn.InternalQuotedPatterns_patternTypeAnnot .typeRef)).withSpan(tree.expr.span)))
177
- val pat = typedPattern(expr, defn.QuotedTypeClass .typeRef.appliedTo(typeSym.typeRef))(
178
- using spliceContext.retractMode(Mode .QuotedPattern ).withOwner(spliceOwner(ctx)))
179
- pat.select(tpnme.Underlying )
161
+ report.error(
162
+ """ `$` for quote pattern varable is not supported anymore.
163
+ |Use lower cased variable name without the `$` instead.""" .stripMargin,
164
+ tree.srcPos)
165
+ ref(defn.NothingType )
180
166
else
181
167
val tree1 = typedSelect(untpd.Select (tree.expr, tpnme.Underlying ), pt)(using spliceContext).withSpan(tree.span)
182
168
val msg = em " Consider using canonical type reference ${tree1.tpe} instead "
@@ -185,6 +171,21 @@ trait QuotesAndSplices {
185
171
tree1
186
172
}
187
173
174
+ def typedQuotedTypeVar (tree : untpd.Ident , pt : Type )(using Context ): Tree =
175
+ def spliceOwner (ctx : Context ): Symbol =
176
+ if (ctx.mode.is(Mode .QuotedPattern )) spliceOwner(ctx.outer) else ctx.owner
177
+ val name = tree.name.toTypeName
178
+ val nameOfSyntheticGiven = PatMatVarName .fresh(tree.name.toTermName)
179
+ val expr = untpd.cpy.Ident (tree)(nameOfSyntheticGiven)
180
+ val typeSymInfo = pt match
181
+ case pt : TypeBounds => pt
182
+ case _ => TypeBounds .empty
183
+ val typeSym = newSymbol(spliceOwner(ctx), name, EmptyFlags , typeSymInfo, NoSymbol , tree.span)
184
+ typeSym.addAnnotation(Annotation (New (ref(defn.InternalQuotedPatterns_patternTypeAnnot .typeRef)).withSpan(tree.span)))
185
+ val pat = typedPattern(expr, defn.QuotedTypeClass .typeRef.appliedTo(typeSym.typeRef))(
186
+ using spliceContext.retractMode(Mode .QuotedPattern ).withOwner(spliceOwner(ctx)))
187
+ pat.select(tpnme.Underlying )
188
+
188
189
private def checkSpliceOutsideQuote (tree : untpd.Tree )(using Context ): Unit =
189
190
if (level == 0 && ! ctx.owner.ownersIterator.exists(_.is(Inline )))
190
191
report.error(" Splice ${...} outside quotes '{...} or inline method" , tree.srcPos)
@@ -201,17 +202,17 @@ trait QuotesAndSplices {
201
202
*
202
203
* A quote pattern
203
204
* ```
204
- * case '{ type ${given t: Type[$ t @ _]}; ${ls: Expr[List[$ t]]} } => ...
205
+ * case '{ type ${given t$giveni : Type[t @ _]}; ${ls: Expr[List[t]]} } => ...
205
206
* ```
206
207
* will return
207
208
* ```
208
209
* (
209
- * Map(<$t >: Symbol -> <$ t @ _>: Bind),
210
+ * Map(<t$giveni >: Symbol -> <t @ _>: Bind),
210
211
* <'{
211
- * @scala.internal.Quoted.patternType type $ t
212
- * scala.internal.Quoted.patternHole[List[$ t]]
212
+ * @scala.internal.Quoted.patternType type t
213
+ * scala.internal.Quoted.patternHole[List[t]]
213
214
* }>: Tree,
214
- * List(<ls: Expr[List[$ t]]>: Tree)
215
+ * List(<ls: Expr[List[t]]>: Tree)
215
216
* )
216
217
* ```
217
218
*/
@@ -278,7 +279,7 @@ trait QuotesAndSplices {
278
279
tree
279
280
case tdef : TypeDef =>
280
281
if tdef.symbol.hasAnnotation(defn.InternalQuotedPatterns_patternTypeAnnot ) then
281
- transformTypeBindingTypeDef(PatMatVarName .fresh(), tdef, typePatBuf)
282
+ transformTypeBindingTypeDef(PatMatVarName .fresh(tdef.name.toTermName ), tdef, typePatBuf)
282
283
else if tdef.symbol.isClass then
283
284
val kind = if tdef.symbol.is(Module ) then " objects" else " classes"
284
285
report.error(" Implementation restriction: cannot match " + kind, tree.srcPos)
@@ -358,38 +359,38 @@ trait QuotesAndSplices {
358
359
* within the quotes become patterns again and typed accordingly.
359
360
*
360
361
* ```
361
- * case '{ ($ls: List[$ t]) } =>
362
- * // `t` is of type `Type[T$1 ]` for some unknown T$1
363
- * // `t` is implicitly available
364
- * // `l ` is of type `Expr[List[T$1 ]]`
362
+ * case '{ ($ls: List[t]) } =>
363
+ * // `t$giveni ` is of type `Type[t ]` for some unknown `t`
364
+ * // `t$giveni ` is implicitly available
365
+ * // `ls ` is of type `Expr[List[t ]]`
365
366
* '{ val h: $t = $ls.head }
366
367
* ```
367
368
*
368
- * For each type splice we will create a new type binding in the pattern match ($ t @ _ in this case)
369
- * and a corresponding type in the quoted pattern as a hole (@patternType type $t in this case).
369
+ * For each type splice we will create a new type binding in the pattern match (` t @ _` in this case)
370
+ * and a corresponding type in the quoted pattern as a hole (` @patternType type t` in this case).
370
371
* All these generated types are inserted at the start of the quoted code.
371
372
*
372
373
* After typing the tree will resemble
373
374
*
374
375
* ```
375
- * case '{ type ${given t: Type[$ t @ _]}; ${ls: Expr[List[$ t]]} } => ...
376
+ * case '{ type ${given t$giveni : Type[t @ _]}; ${ls: Expr[List[t]]} } => ...
376
377
* ```
377
378
*
378
379
* Then the pattern is _split_ into the expression contained in the pattern replacing the splices by holes,
379
380
* and the patterns in the splices. All these are recombined into a call to `Matcher.unapply`.
380
381
*
381
382
* ```
382
383
* case scala.internal.quoted.Expr.unapply[
383
- * Tuple1[$ t @ _], // Type binging definition
384
- * Tuple2[Type[$ t], Expr[List[$ t]]] // Typing the result of the pattern match
384
+ * Tuple1[t @ _], // Type binging definition
385
+ * Tuple2[Type[t], Expr[List[t]]] // Typing the result of the pattern match
385
386
* ](
386
387
* Tuple2.unapply
387
- * [Type[$ t], Expr[List[$ t]]] //Propagated from the tuple above
388
- * (implicit t @ _, ls @ _: Expr[List[$ t]]) // from the spliced patterns
388
+ * [Type[t], Expr[List[t]]] //Propagated from the tuple above
389
+ * (given t$giveni @ _, ls @ _: Expr[List[t]]) // from the spliced patterns
389
390
* )(
390
391
* '{ // Runtime quote Matcher.unapply uses to mach against. Expression directly inside the quoted pattern without the splices
391
- * @scala.internal.Quoted.patternType type $ t
392
- * scala.internal.Quoted.patternHole[List[$ t]]
392
+ * @scala.internal.Quoted.patternType type t
393
+ * scala.internal.Quoted.patternHole[List[t]]
393
394
* },
394
395
* true, // If there is at least one type splice. Used to instantiate the context with or without GADT constraints
395
396
* x$2 // tasty.Reflection instance
@@ -409,7 +410,7 @@ trait QuotesAndSplices {
409
410
case _ => defn.AnyType
410
411
}
411
412
val quoted0 = desugar.quotedPattern(quoted, untpd.TypedSplice (TypeTree (quotedPt)))
412
- val quoteCtx = quoteContext.addMode(Mode .QuotedPattern )
413
+ val quoteCtx = quoteContext.addMode(Mode .QuotedPattern ).retractMode( Mode . Pattern )
413
414
val quoted1 =
414
415
if quoted.isType then typedType(quoted0, WildcardType )(using quoteCtx)
415
416
else typedExpr(quoted0, WildcardType )(using quoteCtx)
0 commit comments