Skip to content

Commit a806f71

Browse files
committed
Factor out common code
1 parent 66a3880 commit a806f71

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ object desugar {
140140
* def x_=($1: <TypeTree()>): Unit = ()
141141
*/
142142
def valDef(vdef0: ValDef)(implicit ctx: Context): Tree = {
143-
val vdef = transformQuotedPatternName(vdef0)
144-
val ValDef(name, tpt, rhs) = vdef
143+
val vdef @ ValDef(name, tpt, rhs) = transformQuotedPatternName(vdef0)
145144
val mods = vdef.mods
146145
val setterNeeded =
147146
(mods is Mutable) && ctx.owner.isClass && (!(mods is PrivateLocal) || (ctx.owner is Trait))
@@ -165,14 +164,6 @@ object desugar {
165164
else vdef
166165
}
167166

168-
def transformQuotedPatternName(vdef: ValDef)(implicit ctx: Context): ValDef = {
169-
if (ctx.mode.is(Mode.QuotedPattern) && !vdef.isBackquoted && vdef.name.startsWith("$")) {
170-
val name = vdef.name.toString.substring(1).toTermName
171-
val mods = vdef.mods.withAddedAnnotation(New(ref(defn.InternalQuoted_patternBindHoleAnnot.typeRef)).withSpan(vdef.span))
172-
cpy.ValDef(vdef)(name).withMods(mods)
173-
} else vdef
174-
}
175-
176167
def makeImplicitParameters(tpts: List[Tree], contextualFlag: FlagSet = EmptyFlags, forPrimaryConstructor: Boolean = false)(implicit ctx: Context): List[ValDef] =
177168
for (tpt <- tpts) yield {
178169
val paramFlags: FlagSet = if (forPrimaryConstructor) PrivateLocalParamAccessor else Param
@@ -207,9 +198,7 @@ object desugar {
207198
* inline def f(x: Boolean): Any = (if (x) 1 else ""): Any
208199
*/
209200
private def defDef(meth0: DefDef, isPrimaryConstructor: Boolean = false)(implicit ctx: Context): Tree = {
210-
val meth = transformQuotedPatternName(meth0)
211-
212-
val DefDef(_, tparams, vparamss, tpt, rhs) = meth
201+
val meth @ DefDef(_, tparams, vparamss, tpt, rhs) = transformQuotedPatternName(meth0)
213202
val methName = normalizeName(meth, tpt).asTermName
214203
val mods = meth.mods
215204
val epbuf = new ListBuffer[ValDef]
@@ -283,12 +272,30 @@ object desugar {
283272
}
284273
}
285274

286-
def transformQuotedPatternName(ddef: DefDef)(implicit ctx: Context): DefDef = {
287-
if (ctx.mode.is(Mode.QuotedPattern) && !ddef.isBackquoted && ddef.name != nme.ANON_FUN && ddef.name.startsWith("$")) {
288-
val name = ddef.name.toString.substring(1).toTermName
289-
val mods = ddef.mods.withAddedAnnotation(New(ref(defn.InternalQuoted_patternBindHoleAnnot.typeRef)).withSpan(ddef.span))
290-
cpy.DefDef(ddef)(name).withMods(mods)
291-
} else ddef
275+
/** Transforms a definition with a name starting with a `$` in a quoted pattern into a `quoted.binding.Binding` splice.
276+
*
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.
279+
*
280+
* A definition
281+
* ```scala
282+
* case '{ def $a(...) = ... a() ...; ... a() ... }
283+
* ```
284+
* into
285+
* ```scala
286+
* case '{ @patternBindHole def a(...) = ... a() ...; ... a() ... }
287+
* ```
288+
*/
289+
def transformQuotedPatternName(tree: ValOrDefDef)(implicit ctx: Context): ValOrDefDef = {
290+
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+
}
296+
val mods = tree.mods.withAddedAnnotation(New(ref(defn.InternalQuoted_patternBindHoleAnnot.typeRef)).withSpan(tree.span))
297+
newTree.withMods(mods)
298+
} else tree
292299
}
293300

294301
// Add all evidence parameters in `params` as implicit parameters to `meth` */

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ object Trees {
357357

358358
/** A ValDef or DefDef tree */
359359
abstract class ValOrDefDef[-T >: Untyped](implicit @constructorOnly src: SourceFile) extends MemberDef[T] with WithLazyField[Tree[T]] {
360+
type ThisTree[-T >: Untyped] <: ValOrDefDef[T]
360361
def name: TermName
361362
def tpt: Tree[T]
362363
def unforcedRhs: LazyTree = unforced

0 commit comments

Comments
 (0)