Skip to content

Commit 70316a6

Browse files
committed
Use attachment to tag backquoted trees
1 parent c62eee7 commit 70316a6

File tree

8 files changed

+25
-39
lines changed

8 files changed

+25
-39
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
142142

143143
/** Is tree a variable pattern? */
144144
def isVarPattern(pat: Tree): Boolean = unsplice(pat) match {
145-
case x: BackquotedIdent => false
146-
case x: Ident => x.name.isVariableName
145+
case x: Ident => x.name.isVariableName && !x.isBackquoted
147146
case _ => false
148147
}
149148

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ object Trees {
3131
@sharable var ntrees: Int = 0
3232

3333
/** Property key for trees with documentation strings attached */
34-
val DocComment: Property.StickyKey[Comments.Comment] = new Property.StickyKey
34+
val DocComment: Property.StickyKey[Comments.Comment] = Property.StickyKey()
35+
36+
/** Property key for backquoted identifiers and definitions */
37+
val Backquoted: Property.StickyKey[Unit] = Property.StickyKey()
3538

3639
/** Trees take a parameter indicating what the type of their `tpe` field
3740
* is. Two choices: `Type` or `Untyped`.
@@ -376,15 +379,7 @@ object Trees {
376379
type ThisTree[-T >: Untyped] = Ident[T]
377380
def qualifier: Tree[T] = genericEmptyTree
378381

379-
/** Is this a `BackquotedIdent` ? */
380-
def isBackquoted: Boolean = false
381-
}
382-
383-
class BackquotedIdent[-T >: Untyped]private[ast] (name: Name)(implicit @constructorOnly src: SourceFile)
384-
extends Ident[T](name) {
385-
override def isBackquoted: Boolean = true
386-
387-
override def toString: String = s"BackquotedIdent($name)"
382+
def isBackquoted: Boolean = hasAttachment(Backquoted)
388383
}
389384

390385
class SearchFailureIdent[-T >: Untyped] private[ast] (name: Name)(implicit @constructorOnly src: SourceFile)
@@ -947,7 +942,6 @@ object Trees {
947942
type LazyTreeList = Trees.LazyTreeList[T]
948943

949944
type Ident = Trees.Ident[T]
950-
type BackquotedIdent = Trees.BackquotedIdent[T]
951945
type SearchFailureIdent = Trees.SearchFailureIdent[T]
952946
type Select = Trees.Select[T]
953947
type SelectWithSig = Trees.SelectWithSig[T]
@@ -1036,9 +1030,6 @@ object Trees {
10361030
postProcess(tree, copied.withSpan(tree.span).withAttachmentsFrom(tree))
10371031

10381032
def Ident(tree: Tree)(name: Name)(implicit ctx: Context): Ident = tree match {
1039-
case tree: BackquotedIdent =>
1040-
if (name == tree.name) tree
1041-
else finalize(tree, new BackquotedIdent(name)(sourceFile(tree)))
10421033
case tree: Ident if name == tree.name => tree
10431034
case _ => finalize(tree, untpd.Ident(name)(sourceFile(tree)))
10441035
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
299299
// ------ Creation methods for untyped only -----------------
300300

301301
def Ident(name: Name)(implicit src: SourceFile): Ident = new Ident(name)
302-
def BackquotedIdent(name: Name)(implicit src: SourceFile): BackquotedIdent = new BackquotedIdent(name)
303302
def SearchFailureIdent(name: Name)(implicit src: SourceFile): SearchFailureIdent = new SearchFailureIdent(name)
304303
def Select(qualifier: Tree, name: Name)(implicit src: SourceFile): Select = new Select(qualifier, name)
305304
def SelectWithSig(qualifier: Tree, name: Name, sig: Signature)(implicit src: SourceFile): Select = new SelectWithSig(qualifier, name, sig)

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,8 @@ object Parsers {
628628
makeIdent(in.token, in.offset, ident().toTypeName)
629629

630630
private def makeIdent(tok: Token, offset: Offset, name: Name) = {
631-
val tree =
632-
if (tok == BACKQUOTED_IDENT) BackquotedIdent(name)
633-
else Ident(name)
631+
val tree = Ident(name)
632+
if (tok == BACKQUOTED_IDENT) tree.pushAttachment(Backquoted, ())
634633

635634
// Make sure that even trees with parsing errors have a offset that is within the offset
636635
val errorOffset = offset min (in.lastOffset - 1)
@@ -2526,10 +2525,11 @@ object Parsers {
25262525
}
25272526
} else EmptyTree
25282527
lhs match {
2529-
case (id: BackquotedIdent) :: Nil if id.name.isTermName =>
2530-
finalizeDef(BackquotedValDef(id.name.asTermName, tpt, rhs), mods, start)
2531-
case Ident(name: TermName) :: Nil =>
2532-
finalizeDef(ValDef(name, tpt, rhs), mods, start)
2528+
case (id @ Ident(name: TermName)) :: Nil =>
2529+
if (id.isBackquoted)
2530+
finalizeDef(BackquotedValDef(id.name.asTermName, tpt, rhs), mods, start)
2531+
else
2532+
finalizeDef(ValDef(name, tpt, rhs), mods, start)
25332533
case _ =>
25342534
PatDef(mods, lhs, tpt, rhs)
25352535
}

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,24 +334,23 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
334334
}
335335

336336
tree match {
337-
case id: Trees.BackquotedIdent[_] if !homogenizedView =>
338-
"`" ~ toText(id.name) ~ "`"
339337
case id: Trees.SearchFailureIdent[_] =>
340338
tree.typeOpt match {
341339
case reason: Implicits.SearchFailureType =>
342340
toText(id.name) ~ "implicitly[" ~ toText(reason.clarify(reason.expectedType)) ~ "]"
343341
case _ =>
344342
toText(id.name)
345343
}
346-
case Ident(name) =>
344+
case id @ Ident(name) =>
347345
val txt = tree.typeOpt match {
348346
case tp: NamedType if name != nme.WILDCARD =>
349347
val pre = if (tp.symbol.is(JavaStatic)) tp.prefix.widen else tp.prefix
350348
toTextPrefix(pre) ~ withPos(selectionString(tp), tree.sourcePos)
351349
case _ =>
352350
toText(name)
353351
}
354-
if (name.isTypeName) typeText(txt)
352+
if (tree.isBackquoted && !homogenizedView) "`" ~ toText(name) ~ "`"
353+
else if (name.isTypeName) typeText(txt)
355354
else txt
356355
case tree @ Select(qual, name) =>
357356
if (!printDebug && tree.hasType && tree.symbol == defn.QuotedType_splice) typeText("${") ~ toTextLocal(qual) ~ typeText("}")

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ object PatternMatcher {
188188
case Typed(_, tpt) if tpt.tpe.isRepeatedParam => true
189189
case Bind(nme.WILDCARD, WildcardPattern()) => true // don't skip when binding an interesting symbol!
190190
case t if isWildcardArg(t) => true
191-
case x: BackquotedIdent => false
192-
case x: Ident => x.name.isVariableName
191+
case x: Ident => x.name.isVariableName && !x.isBackquoted
193192
case Alternative(ps) => ps.forall(unapply)
194193
case EmptyTree => true
195194
case _ => false

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
330330
Typ(c.value.asInstanceOf[Symbol].termRef, false)
331331
else
332332
Typ(ConstantType(c), false)
333-
case _: BackquotedIdent => Typ(pat.tpe, false)
333+
case pat: Ident if pat.isBackquoted => Typ(pat.tpe, false)
334334
case Ident(nme.WILDCARD) =>
335335
Or(Typ(pat.tpe.stripAnnots, false) :: nullSpace :: Nil)
336336
case Ident(_) | Select(_, _) =>

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -752,16 +752,15 @@ trait Checking {
752752
isInfix(sym.owner.linkedClass)
753753

754754
tree.op match {
755-
case _: untpd.BackquotedIdent =>
756-
()
757-
case Ident(name: Name) =>
755+
case id @ Ident(name: Name) =>
758756
name.toTermName match {
759757
case name: SimpleName
760-
if !name.exists(isOperatorPart) &&
761-
!isInfix(meth) &&
762-
!meth.maybeOwner.is(Scala2x) &&
763-
!infixOKSinceFollowedBy(tree.right) &&
764-
ctx.settings.strict.value =>
758+
if !id.isBackquoted &&
759+
!name.exists(isOperatorPart) &&
760+
!isInfix(meth) &&
761+
!meth.maybeOwner.is(Scala2x) &&
762+
!infixOKSinceFollowedBy(tree.right) &&
763+
ctx.settings.strict.value =>
765764
val (kind, alternative) =
766765
if (ctx.mode.is(Mode.Type))
767766
("type", (n: Name) => s"prefix syntax $n[...]")

0 commit comments

Comments
 (0)