Skip to content

Commit b255543

Browse files
authored
Merge pull request #6347 from dotty-staging/do-not-rename-quoted-bindings
Do not rename names starting with `$` in quoted patterns
2 parents 5359574 + 046152e commit b255543

File tree

3 files changed

+10
-18
lines changed

3 files changed

+10
-18
lines changed

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,27 +274,21 @@ object desugar {
274274

275275
/** Transforms a definition with a name starting with a `$` in a quoted pattern into a `quoted.binding.Binding` splice.
276276
*
277-
* The desugaring consists in renaming the the definition and adding the `@patternBindHole` annotation. This
278-
* annotation is used during typing to perform the full transformation.
277+
* The desugaring consists in adding the `@patternBindHole` annotation. This annotation is used during typing to perform the full transformation.
279278
*
280279
* A definition
281280
* ```scala
282-
* case '{ def $a(...) = ... a() ...; ... a() ... }
281+
* case '{ def $a(...) = ...; ... `$a`() ... } => a
283282
* ```
284283
* into
285284
* ```scala
286-
* case '{ @patternBindHole def a(...) = ... a() ...; ... a() ... }
285+
* case '{ @patternBindHole def `$a`(...) = ...; ... `$a`() ... } => a
287286
* ```
288287
*/
289288
def transformQuotedPatternName(tree: ValOrDefDef)(implicit ctx: Context): ValOrDefDef = {
290289
if (ctx.mode.is(Mode.QuotedPattern) && !tree.isBackquoted && tree.name != nme.ANON_FUN && tree.name.startsWith("$")) {
291-
val name = tree.name.toString.substring(1).toTermName
292-
val newTree: ValOrDefDef = tree match {
293-
case tree: ValDef => cpy.ValDef(tree)(name)
294-
case tree: DefDef => cpy.DefDef(tree)(name)
295-
}
296290
val mods = tree.mods.withAddedAnnotation(New(ref(defn.InternalQuoted_patternBindHoleAnnot.typeRef)).withSpan(tree.span))
297-
newTree.withMods(mods)
291+
tree.withMods(mods)
298292
} else tree
299293
}
300294

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,9 @@ class Typer extends Namer
19861986
case t => t
19871987
}
19881988
val bindingExprTpe = AppliedType(defn.QuotedMatchingBindingType, bindingType :: Nil)
1989-
val sym = ctx0.newPatternBoundSymbol(ddef.name, bindingExprTpe, ddef.span)
1989+
assert(ddef.name.startsWith("$"))
1990+
val bindName = ddef.name.toString.stripPrefix("$").toTermName
1991+
val sym = ctx0.newPatternBoundSymbol(bindName, bindingExprTpe, ddef.span)
19901992
patBuf += Bind(sym, untpd.Ident(nme.WILDCARD).withType(bindingExprTpe)).withSpan(ddef.span)
19911993
}
19921994
super.transform(tree)

tests/pos/quotedPatterns.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@ object Test {
1212
case '{ ((a: Int) => 3)($y) } => y
1313
case '{ 1 + ($y: Int)} => y
1414
case '{ val a = 1 + ($y: Int); 3 } => y
15-
// currently gives an unreachable case warning
16-
// but only when used in conjunction with the others.
17-
// I believe this is because implicit arguments are not taken
18-
// into account when checking whether we have already seen an `unapply` before.
19-
case '{ val $y: Int = $z; 1 } =>
15+
case '{ val $y: Int = $z; println(`$y`); 1 } =>
2016
val a: quoted.matching.Bind[Int] = y
2117
z
22-
case '{ (($y: Int) => 1 + y + ($z: Int))(2) } =>
18+
case '{ (($y: Int) => 1 + `$y` + ($z: Int))(2) } =>
2319
val a: quoted.matching.Bind[Int] = y
2420
z
25-
case '{ def $ff: Int = $z; ff } =>
21+
case '{ def $ff: Int = $z; `$ff` } =>
2622
val a: quoted.matching.Bind[Int] = ff
2723
z
2824
case '{ def $ff(i: Int): Int = $z; 2 } =>

0 commit comments

Comments
 (0)