Skip to content

Commit 44b8353

Browse files
Merge pull request #8693 from dotty-staging/remove-name-patterns
Remove quoted name splices
2 parents e31f142 + 5c40b96 commit 44b8353

File tree

10 files changed

+39
-108
lines changed

10 files changed

+39
-108
lines changed

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ object desugar {
156156
* - all package object members
157157
*/
158158
def valDef(vdef0: ValDef)(implicit ctx: Context): Tree = {
159-
val vdef @ ValDef(name, tpt, rhs) = transformQuotedPatternName(vdef0)
159+
val vdef @ ValDef(name, tpt, rhs) = vdef0
160160
val mods = vdef.mods
161161
val setterNeeded =
162162
mods.is(Mutable)
@@ -204,7 +204,7 @@ object desugar {
204204
* def f$default$2[T](x: Int) = x + "m"
205205
*/
206206
private def defDef(meth0: DefDef, isPrimaryConstructor: Boolean = false)(implicit ctx: Context): Tree = {
207-
val meth @ DefDef(_, tparams, vparamss, tpt, rhs) = transformQuotedPatternName(meth0)
207+
val meth @ DefDef(_, tparams, vparamss, tpt, rhs) = meth0
208208
val methName = normalizeName(meth, tpt).asTermName
209209
val mods = meth.mods
210210
val epbuf = ListBuffer[ValDef]()
@@ -279,26 +279,6 @@ object desugar {
279279
}
280280
}
281281

282-
/** Transforms a definition with a name starting with a `$` in a quoted pattern into a `quoted.binding.Binding` splice.
283-
*
284-
* The desugaring consists in adding the `@patternBindHole` annotation. This annotation is used during typing to perform the full transformation.
285-
*
286-
* A definition
287-
* ```scala
288-
* case '{ def $a(...) = ...; ... `$a`() ... } => a
289-
* ```
290-
* into
291-
* ```scala
292-
* case '{ @patternBindHole def `$a`(...) = ...; ... `$a`() ... } => a
293-
* ```
294-
*/
295-
def transformQuotedPatternName(tree: ValOrDefDef)(implicit ctx: Context): ValOrDefDef =
296-
if (ctx.mode.is(Mode.QuotedPattern) && !isBackquoted(tree) && tree.name != nme.ANON_FUN && tree.name.startsWith("$")) {
297-
val mods = tree.mods.withAddedAnnotation(New(ref(defn.InternalQuoted_patternBindHoleAnnot.typeRef)).withSpan(tree.span))
298-
tree.withMods(mods)
299-
}
300-
else tree
301-
302282
/** Add an explicit ascription to the `expectedTpt` to every tail splice.
303283
*
304284
* - `'{ x }` -> `'{ x }`

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,6 @@ class Definitions {
695695
@tu lazy val InternalQuoted_exprNestedSplice : Symbol = InternalQuotedModule.requiredMethod("exprNestedSplice")
696696
@tu lazy val InternalQuoted_typeQuote : Symbol = InternalQuotedModule.requiredMethod("typeQuote")
697697
@tu lazy val InternalQuoted_patternHole: Symbol = InternalQuotedModule.requiredMethod("patternHole")
698-
@tu lazy val InternalQuoted_patternBindHoleAnnot: ClassSymbol = InternalQuotedModule.requiredClass("patternBindHole")
699698
@tu lazy val InternalQuoted_patternTypeAnnot: ClassSymbol = InternalQuotedModule.requiredClass("patternType")
700699
@tu lazy val InternalQuoted_QuoteTypeTagAnnot: ClassSymbol = InternalQuotedModule.requiredClass("quoteTypeTag")
701700
@tu lazy val InternalQuoted_fromAboveAnnot: ClassSymbol = InternalQuotedModule.requiredClass("fromAbove")

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1963,7 +1963,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
19631963
def Definitions_isTupleClass(sym: Symbol): Boolean = defn.isTupleClass(sym)
19641964

19651965
def Definitions_InternalQuoted_patternHole: Symbol = defn.InternalQuoted_patternHole
1966-
def Definitions_InternalQuoted_patternBindHoleAnnot: Symbol = defn.InternalQuoted_patternBindHoleAnnot
19671966
def Definitions_InternalQuoted_patternTypeAnnot: Symbol = defn.InternalQuoted_patternTypeAnnot
19681967
def Definitions_InternalQuoted_fromAboveAnnot: Symbol = defn.InternalQuoted_fromAboveAnnot
19691968

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -219,23 +219,6 @@ trait QuotesAndSplices {
219219
TypeTree(tree.tpe.dealias).withSpan(tree.span)
220220
else
221221
tree
222-
case ddef: ValOrDefDef =>
223-
if (ddef.symbol.hasAnnotation(defn.InternalQuoted_patternBindHoleAnnot)) {
224-
val bindingType = ddef.symbol.info match {
225-
case t: ExprType => t.resType
226-
case t: MethodType => t.toFunctionType()
227-
case t: PolyType =>
228-
HKTypeLambda(t.paramNames)(
229-
x => t.paramInfos.mapConserve(_.subst(t, x).asInstanceOf[TypeBounds]),
230-
x => t.resType.subst(t, x).toFunctionType())
231-
case t => t
232-
}
233-
assert(ddef.name.startsWith("$"))
234-
val bindName = ddef.name.toString.stripPrefix("$").toTermName
235-
val sym = ctx0.newPatternBoundSymbol(bindName, defn.StringType, ddef.span)
236-
patBuf += Bind(sym, untpd.Ident(nme.WILDCARD).withType(defn.StringType)).withSpan(ddef.span)
237-
}
238-
super.transform(tree)
239222
case tdef: TypeDef if tdef.symbol.hasAnnotation(defn.InternalQuoted_patternTypeAnnot) =>
240223
transformTypeBindingTypeDef(tdef, typePatBuf)
241224
case tree @ AppliedTypeTree(tpt, args) =>

library/src/scala/internal/quoted/CompileTime.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ object CompileTime {
2828
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.patternHole`")
2929
def patternHole[T]: T = ???
3030

31+
// TODO remove
3132
/** A splice of a name in a quoted pattern is desugared by wrapping getting this annotation */
3233
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.patternBindHole`")
3334
class patternBindHole extends Annotation

library/src/scala/internal/quoted/Matcher.scala

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,15 @@ private[quoted] object Matcher {
6969
}
7070
}
7171

72-
private def hasBindTypeAnnotation(tpt: TypeTree): Boolean = tpt match {
73-
case Annotated(tpt2, annot) => isBindAnnotation(annot) || hasBindTypeAnnotation(tpt2)
74-
case _ => false
75-
}
76-
7772
private def hasPatternTypeAnnotation(sym: Symbol) = sym.annots.exists(isPatternTypeAnnotation)
7873

79-
private def hasBindAnnotation(sym: Symbol) = sym.annots.exists(isBindAnnotation)
80-
8174
private def hasFromAboveAnnotation(sym: Symbol) = sym.annots.exists(isFromAboveAnnotation)
8275

8376
private def isPatternTypeAnnotation(tree: Tree): Boolean = tree match {
8477
case New(tpt) => tpt.symbol == internal.Definitions_InternalQuoted_patternTypeAnnot
8578
case annot => annot.symbol.owner == internal.Definitions_InternalQuoted_patternTypeAnnot
8679
}
8780

88-
private def isBindAnnotation(tree: Tree): Boolean = tree match {
89-
case New(tpt) => tpt.symbol == internal.Definitions_InternalQuoted_patternBindHoleAnnot
90-
case annot => annot.symbol.owner == internal.Definitions_InternalQuoted_patternBindHoleAnnot
91-
}
92-
9381
private def isFromAboveAnnotation(tree: Tree): Boolean = tree match {
9482
case New(tpt) => tpt.symbol == internal.Definitions_InternalQuoted_fromAboveAnnot
9583
case annot => annot.symbol.owner == internal.Definitions_InternalQuoted_fromAboveAnnot
@@ -139,9 +127,6 @@ private[quoted] object Matcher {
139127
sFlags.is(Lazy) == pFlags.is(Lazy) && sFlags.is(Mutable) == pFlags.is(Mutable)
140128
}
141129

142-
def bindingMatch(sym: Symbol) =
143-
matched(sym.name)
144-
145130
(scrutinee, pattern) match {
146131

147132
// Match a scala.internal.Quoted.patternHole typed as a repeated argument and return the scrutinee tree
@@ -254,28 +239,21 @@ private[quoted] object Matcher {
254239
tycon1 =?= tycon2 && args1 =?= args2
255240

256241
case (ValDef(_, tpt1, rhs1), ValDef(_, tpt2, rhs2)) if checkValFlags() =>
257-
val bindMatch =
258-
if (hasBindAnnotation(pattern.symbol) || hasBindTypeAnnotation(tpt2)) bindingMatch(scrutinee.symbol)
259-
else matched
260242
def rhsEnv = summon[Env] + (scrutinee.symbol -> pattern.symbol)
261-
bindMatch && tpt1 =?= tpt2 && treeOptMatches(rhs1, rhs2)(using summon[Context], rhsEnv)
243+
tpt1 =?= tpt2 && treeOptMatches(rhs1, rhs2)(using summon[Context], rhsEnv)
262244

263245
case (DefDef(_, typeParams1, paramss1, tpt1, Some(rhs1)), DefDef(_, typeParams2, paramss2, tpt2, Some(rhs2))) =>
264-
val bindMatch =
265-
if (hasBindAnnotation(pattern.symbol)) bindingMatch(scrutinee.symbol)
266-
else matched
267246
def rhsEnv =
268247
val oldEnv: Env = summon[Env]
269248
val newEnv: List[(Symbol, Symbol)] =
270249
(scrutinee.symbol -> pattern.symbol) :: typeParams1.zip(typeParams2).map((tparam1, tparam2) => tparam1.symbol -> tparam2.symbol) :::
271250
paramss1.flatten.zip(paramss2.flatten).map((param1, param2) => param1.symbol -> param2.symbol)
272251
oldEnv ++ newEnv
273252

274-
bindMatch &&
275-
typeParams1 =?= typeParams2 &&
276-
matchLists(paramss1, paramss2)(_ =?= _) &&
277-
tpt1 =?= tpt2 &&
278-
withEnv(rhsEnv)(rhs1 =?= rhs2)
253+
typeParams1 =?= typeParams2
254+
&& matchLists(paramss1, paramss2)(_ =?= _)
255+
&& tpt1 =?= tpt2
256+
&& withEnv(rhsEnv)(rhs1 =?= rhs2)
279257

280258
case (Closure(_, tpt1), Closure(_, tpt2)) =>
281259
// TODO match tpt1 with tpt2?

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,9 +1497,6 @@ trait CompilerInterface {
14971497
/** Symbol of scala.internal.Quoted.patternHole */
14981498
def Definitions_InternalQuoted_patternHole: Symbol
14991499

1500-
/** Symbol of scala.internal.Quoted.patternBindHole */
1501-
def Definitions_InternalQuoted_patternBindHoleAnnot: Symbol
1502-
15031500
/** Symbol of scala.internal.Quoted.patternType */
15041501
def Definitions_InternalQuoted_patternTypeAnnot: Symbol
15051502

tests/pos/quotedPatterns.scala

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,17 @@ object Test {
1313
case '{ ((a: Int) => 3)($y) } => y
1414
case '{ 1 + ($y: Int)} => y
1515
case '{ val a = 1 + ($y: Int); 3 } => y
16-
case '{ val $y: Int = $z; println(`$y`); 1 } =>
17-
val a: String = y
16+
case '{ val y: Int = $z; println(y); 1 } =>
1817
z
19-
case '{ (($y: Int) => 1 + `$y` + ($z: Int))(2) } =>
20-
val a: String = y
18+
case '{ ((y: Int) => 1 + y + ($z: Int))(2) } =>
2119
z
22-
case '{ def $ff: Int = $z; `$ff` } =>
23-
val a: String = ff
20+
case '{ def ff: Int = $z; ff } =>
2421
z
25-
case '{ def $ff(i: Int): Int = $z; 2 } =>
26-
val a: String = ff
22+
case '{ def ff(i: Int): Int = $z; 2 } =>
2723
z
28-
case '{ def $ff(i: Int)(j: Int): Int = $z; 2 } =>
29-
val a: String = ff
24+
case '{ def ff(i: Int)(j: Int): Int = $z; 2 } =>
3025
z
31-
case '{ def $ff[T](i: T): Int = $z; 2 } =>
32-
val a: String = ff
26+
case '{ def ff[T](i: T): Int = $z; 2 } =>
3327
z
3428
case '{ poly[$t]($x); 4 } => ???
3529
case '{ type $X; poly[`$X`]($x); 4 } => ???

tests/run-macros/quote-matcher-runtime.check

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ Pattern: scala.internal.quoted.CompileTime.patternHole[scala.Function1[scala.Int
237237
Result: Some(List(Expr(((x: scala.Int) => "abc"))))
238238

239239
Scrutinee: ((x: scala.Int) => "abc")
240-
Pattern: ((x: scala.Int @scala.internal.quoted.CompileTime.patternBindHole) => scala.internal.quoted.CompileTime.patternHole[scala.Predef.String])
241-
Result: Some(List(String(x), Expr("abc")))
240+
Pattern: ((x: scala.Int) => scala.internal.quoted.CompileTime.patternHole[scala.Predef.String])
241+
Result: Some(List(Expr("abc")))
242242

243243
Scrutinee: scala.StringContext.apply("abc", "xyz")
244244
Pattern: scala.StringContext.apply("abc", "xyz")
@@ -267,10 +267,10 @@ Scrutinee: {
267267
()
268268
}
269269
Pattern: {
270-
@scala.internal.quoted.CompileTime.patternBindHole val a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int]
270+
val a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int]
271271
()
272272
}
273-
Result: Some(List(String(a), Expr(45)))
273+
Result: Some(List(Expr(45)))
274274

275275
Scrutinee: {
276276
val a: scala.Int = 45
@@ -297,7 +297,7 @@ Scrutinee: {
297297
()
298298
}
299299
Pattern: {
300-
@scala.internal.quoted.CompileTime.patternBindHole var a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int]
300+
var a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int]
301301
()
302302
}
303303
Result: None
@@ -369,7 +369,7 @@ Scrutinee: {
369369
()
370370
}
371371
Pattern: {
372-
@scala.internal.quoted.CompileTime.patternBindHole val a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int]
372+
val a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int]
373373
()
374374
}
375375
Result: None
@@ -379,7 +379,7 @@ Scrutinee: {
379379
()
380380
}
381381
Pattern: {
382-
@scala.internal.quoted.CompileTime.patternBindHole var a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int]
382+
var a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int]
383383
()
384384
}
385385
Result: None
@@ -419,7 +419,7 @@ Scrutinee: {
419419
()
420420
}
421421
Pattern: {
422-
@scala.internal.quoted.CompileTime.patternBindHole val a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int]
422+
val a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int]
423423
()
424424
}
425425
Result: None
@@ -429,7 +429,7 @@ Scrutinee: {
429429
()
430430
}
431431
Pattern: {
432-
@scala.internal.quoted.CompileTime.patternBindHole lazy val a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int]
432+
lazy val a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int]
433433
()
434434
}
435435
Result: None
@@ -499,10 +499,10 @@ Scrutinee: {
499499
()
500500
}
501501
Pattern: {
502-
@scala.internal.quoted.CompileTime.patternBindHole def a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int]
502+
def a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int]
503503
()
504504
}
505-
Result: Some(List(String(a), Expr(45)))
505+
Result: Some(List(Expr(45)))
506506

507507
Scrutinee: {
508508
def a(x: scala.Int): scala.Int = 45
@@ -569,20 +569,20 @@ Scrutinee: {
569569
()
570570
}
571571
Pattern: {
572-
def a(x: scala.Int @scala.internal.quoted.CompileTime.patternBindHole): scala.Int = 45
572+
def a(x: scala.Int): scala.Int = 45
573573
()
574574
}
575-
Result: Some(List(String(x)))
575+
Result: Some(List())
576576

577577
Scrutinee: {
578578
def a(x: scala.Int): scala.Int = 45
579579
()
580580
}
581581
Pattern: {
582-
def a(x: scala.Int @scala.internal.quoted.CompileTime.patternBindHole): scala.Int = 45
582+
def a(x: scala.Int): scala.Int = 45
583583
()
584584
}
585-
Result: Some(List(String(x)))
585+
Result: Some(List())
586586

587587
Scrutinee: {
588588
def a(x: scala.Int): scala.Int = x

tests/run-macros/quote-matcher-runtime/quoted_2.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,43 +81,43 @@ object Test {
8181
matches((() => "abc")(), (patternHole[() => String]).apply())
8282
matches((x: Int) => "abc", patternHole[Int=> String])
8383
matches(((x: Int) => "abc")(4), (patternHole[Int => String]).apply(4))
84-
matches((x: Int) => "abc", (x: Int @patternBindHole) => patternHole[String])
84+
matches((x: Int) => "abc", (x: Int) => patternHole[String])
8585
matches(StringContext("abc", "xyz"), StringContext("abc", "xyz"))
8686
matches(StringContext("abc", "xyz"), StringContext(patternHole, patternHole))
8787
matches(StringContext("abc", "xyz"), StringContext(patternHole[Seq[String]]: _*))
8888
matches({ val a: Int = 45 }, { val a: Int = 45 })
89-
matches({ val a: Int = 45 }, { @patternBindHole val a: Int = patternHole })
89+
matches({ val a: Int = 45 }, { val a: Int = patternHole })
9090
matches({ val a: Int = 45 }, { lazy val a: Int = 45 })
9191
matches({ val a: Int = 45 }, { var a: Int = 45 })
92-
matches({ val a: Int = 45 }, { @patternBindHole var a: Int = patternHole })
92+
matches({ val a: Int = 45 }, { var a: Int = patternHole })
9393
matches({ val a: Int = 45; a + a }, { val x: Int = 45; x + x })
9494
matches({ val a: Int = 45; val b = a }, { val x: Int = 45; val y = x })
9595
matches({ val a: Int = 45; a + a }, { val x: Int = 45; x + patternHole[Int] })
9696
matches({ lazy val a: Int = 45 }, { val a: Int = 45 })
9797
matches({ lazy val a: Int = 45 }, { lazy val a: Int = 45 })
9898
matches({ lazy val a: Int = 45 }, { var a: Int = 45 })
99-
matches({ lazy val a: Int = 45 }, { @patternBindHole val a: Int = patternHole })
100-
matches({ lazy val a: Int = 45 }, { @patternBindHole var a: Int = patternHole })
99+
matches({ lazy val a: Int = 45 }, { val a: Int = patternHole })
100+
matches({ lazy val a: Int = 45 }, { var a: Int = patternHole })
101101
matches({ var a: Int = 45 }, { val a: Int = 45 })
102102
matches({ var a: Int = 45 }, { lazy val a: Int = 45 })
103103
matches({ var a: Int = 45 }, { var a: Int = 45 })
104-
matches({ var a: Int = 45 }, { @patternBindHole val a: Int = patternHole })
105-
matches({ var a: Int = 45 }, { @patternBindHole lazy val a: Int = patternHole })
104+
matches({ var a: Int = 45 }, { val a: Int = patternHole })
105+
matches({ var a: Int = 45 }, { lazy val a: Int = patternHole })
106106
matches({ println(); println() }, { println(); println() })
107107
matches({ { println() }; println() }, { println(); println() })
108108
matches({ println(); { println() } }, { println(); println() })
109109
matches({ println(); println() }, { println(); { println() } })
110110
matches({ println(); println() }, { { println() }; println() })
111111
matches({ def a: Int = 45 }, { def a: Int = 45 })
112-
matches({ def a: Int = 45 }, { @patternBindHole def a: Int = patternHole[Int] })
112+
matches({ def a: Int = 45 }, { def a: Int = patternHole[Int] })
113113
matches({ def a(x: Int): Int = 45 }, { def a(x: Int): Int = 45 })
114114
matches({ def a(x: Int): Int = 45 }, { def a(x: Int, y: Int): Int = 45 })
115115
matches({ def a(x: Int): Int = 45 }, { def a(x: Int)(y: Int): Int = 45 })
116116
matches({ def a(x: Int, y: Int): Int = 45 }, { def a(x: Int): Int = 45 })
117117
matches({ def a(x: Int)(y: Int): Int = 45 }, { def a(x: Int): Int = 45 })
118118
matches({ def a(x: String): Int = 45 }, { def a(x: String): Int = 45 })
119-
matches({ def a(x: Int): Int = 45 }, { def a(x: Int @patternBindHole): Int = 45 })
120-
matches({ def a(x: Int): Int = 45 }, { def a(x: Int @patternBindHole): Int = 45 })
119+
matches({ def a(x: Int): Int = 45 }, { def a(x: Int): Int = 45 })
120+
matches({ def a(x: Int): Int = 45 }, { def a(x: Int): Int = 45 })
121121
matches({ def a(x: Int): Int = x }, { def b(y: Int): Int = y })
122122
matches({ def a: Int = a }, { def b: Int = b })
123123
matches({ def a: Int = a; a + a }, { def a: Int = a; a + a })

0 commit comments

Comments
 (0)