Skip to content

Commit c62eee7

Browse files
committed
Add hasAttachment method
1 parent 22f2485 commit c62eee7

File tree

7 files changed

+19
-11
lines changed

7 files changed

+19
-11
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import dotty.tools.dotc.core.TypeError
99
import scala.annotation.tailrec
1010

1111
/** A TreeMap that maintains the necessary infrastructure to support
12-
* contxtual implicit searches (type-scope implicits are supported anyway).
12+
* contextual implicit searches (type-scope implicits are supported anyway).
1313
*
14-
* This incudes impicits defined in scope as well as imported implicits.
14+
* This incudes implicits defined in scope as well as imported implicits.
1515
*/
1616
class TreeMapWithImplicits extends tpd.TreeMap {
1717
import tpd._

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ object Trees {
437437
extends GenericApply[T] {
438438
type ThisTree[-T >: Untyped] = Apply[T]
439439

440-
def isGivenApply = getAttachment(untpd.ApplyGiven).nonEmpty
440+
def isGivenApply = hasAttachment(untpd.ApplyGiven)
441441
def setGivenApply() = { pushAttachment(untpd.ApplyGiven, ()); this }
442442
}
443443

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ object TypeTestsCasts {
298298

299299
if (sym.isTypeTest) {
300300
val argType = tree.args.head.tpe
301-
val isTrusted = tree.getAttachment(PatternMatcher.TrustedTypeTestKey).nonEmpty
301+
val isTrusted = tree.hasAttachment(PatternMatcher.TrustedTypeTestKey)
302302
if (!isTrusted && !checkable(expr.tpe, argType, tree.span))
303303
ctx.warning(i"the type test for $argType cannot be checked at runtime", tree.sourcePos)
304304
transformTypeTest(expr, tree.args.head.tpe, flagUnrelated = true)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ trait TypeAssigner {
282282
*/
283283
def accessibleSelectionType(tree: untpd.RefTree, qual1: Tree)(implicit ctx: Context): Type = {
284284
val ownType = selectionType(tree, qual1)
285-
if (tree.getAttachment(desugar.SuppressAccessCheck).isDefined) ownType
285+
if (tree.hasAttachment(desugar.SuppressAccessCheck)) ownType
286286
else ensureAccessible(ownType, qual1.isInstanceOf[Super], tree.sourcePos)
287287
}
288288

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,7 +2437,7 @@ class Typer extends Namer
24372437
}
24382438

24392439
def isSyntheticApply(tree: Tree): Boolean = tree match {
2440-
case tree: Select => tree.getAttachment(InsertedApply).isDefined
2440+
case tree: Select => tree.hasAttachment(InsertedApply)
24412441
case _ => false
24422442
}
24432443

@@ -2459,7 +2459,7 @@ class Typer extends Namer
24592459
else pt match {
24602460
case pt @ FunProto(Nil, _)
24612461
if tree.symbol.allOverriddenSymbols.exists(_.info.isNullaryMethod) &&
2462-
tree.getAttachment(DroppedEmptyArgs).isEmpty =>
2462+
!tree.hasAttachment(DroppedEmptyArgs) =>
24632463
tree.putAttachment(DroppedEmptyArgs, ())
24642464
pt.markAsDropped()
24652465
tree

compiler/src/dotty/tools/dotc/util/Attachment.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ object Attachment {
2323
else nx.getAttachment[V](key)
2424
}
2525

26+
/** Does an attachment corresponding to `key` exist? */
27+
final def hasAttachment[V](key: Key[V]): Boolean = {
28+
val nx = next
29+
if (nx == null) false
30+
else if (nx.key eq key) true
31+
else nx.hasAttachment[V](key)
32+
}
33+
2634
/** The attachment corresponding to `key`.
2735
* @throws NoSuchElementException if no attachment with key exists
2836
*/
@@ -107,7 +115,7 @@ object Attachment {
107115
}
108116

109117
final def pushAttachment[V](key: Key[V], value: V): Unit = {
110-
assert(!getAttachment(key).isDefined, s"duplicate attachment for key $key")
118+
assert(!hasAttachment(key), s"duplicate attachment for key $key")
111119
next = new Link(key, value, next)
112120
}
113121

compiler/test/dotty/tools/dotc/ast/AttachmentsTest.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ class AttachmentsTests extends DottyTest {
5151

5252
val copy = tpd.cpy.TypeDef(clazz)(rhs = tpd.EmptyTree)
5353
assertTrue("A copy should have been returned", clazz ne copy)
54-
assertTrue("Attachment should be present", copy.getAttachment(StickyTestKey).isDefined)
55-
assertTrue("Attachment shouldn't be present", copy.getAttachment(TestKey).isEmpty)
56-
assertTrue("Attachment should be present", copy.getAttachment(StickyTestKey2).isDefined)
54+
assertTrue("Attachment should be present", copy.hasAttachment(StickyTestKey))
55+
assertTrue("Attachment shouldn't be present", !copy.hasAttachment(TestKey))
56+
assertTrue("Attachment should be present", copy.hasAttachment(StickyTestKey2))
5757

5858
case _ =>
5959
fail

0 commit comments

Comments
 (0)