Skip to content

Commit 003301b

Browse files
committed
Add structural equality === for some trees
1 parent a43d8c1 commit 003301b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,26 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
663663
case _ =>
664664
false
665665
}
666+
667+
/** Structural tree comparison (since == on trees is reference equality).
668+
* For the moment, only Ident, Select, Literal, Apply and TypeApply are supported
669+
*/
670+
implicit class StructuralEqDeco(t1: Tree) {
671+
def === (t2: Tree)(implicit ctx: Context): Boolean = (t1, t2) match {
672+
case (t1: Ident, t2: Ident) =>
673+
t1.symbol == t2.symbol
674+
case (t1 @ Select(q1, _), t2 @ Select(q2, _)) =>
675+
t1.symbol == t2.symbol && q1 === q2
676+
case (Literal(c1), Literal(c2)) =>
677+
c1 == c2
678+
case (Apply(f1, as1), Apply(f2, as2)) =>
679+
f1 === f2 && as1.corresponds(as2)(_ === _)
680+
case (TypeApply(f1, ts1), TypeApply(f2, ts2)) =>
681+
f1 === f2 && ts1.tpes.corresponds(ts2.tpes)(_ =:= _)
682+
case _ =>
683+
false
684+
}
685+
}
666686
}
667687

668688
object TreeInfo {

0 commit comments

Comments
 (0)