Skip to content

Commit e6b2db5

Browse files
committed
Refine purity checking for constants
A pure expression such as `(a + b).c` of constant type is now characterized again as a pure path (since it will be eventually replaced by that constant, which is a path). This fixes instability problems in pickling and improves inlining. For instance `assert(0 == 0)` is reduced to `()` again with this change.
1 parent ae72183 commit e6b2db5

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,21 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
423423

424424
private def minOf(l0: PurityLevel, ls: List[PurityLevel]) = (l0 /: ls)(_ `min` _)
425425

426-
def isPurePath(tree: Tree)(implicit ctx: Context): Boolean = exprPurity(tree) == PurePath
427-
def isPureExpr(tree: Tree)(implicit ctx: Context): Boolean = exprPurity(tree) >= Pure
428-
def isIdempotentExpr(tree: Tree)(implicit ctx: Context): Boolean = exprPurity(tree) >= Idempotent
429-
def isIdempotentPath(tree: Tree)(implicit ctx: Context): Boolean = exprPurity(tree) >= IdempotentPath
426+
def isPurePath(tree: Tree)(implicit ctx: Context): Boolean = tree.tpe match {
427+
case tpe: ConstantType => exprPurity(tree) >= Pure
428+
case _ => exprPurity(tree) == PurePath
429+
}
430+
431+
def isPureExpr(tree: Tree)(implicit ctx: Context): Boolean =
432+
exprPurity(tree) >= Pure
433+
434+
def isIdempotentPath(tree: Tree)(implicit ctx: Context): Boolean = tree.tpe match {
435+
case tpe: ConstantType => exprPurity(tree) >= Idempotent
436+
case _ => exprPurity(tree) >= IdempotentPath
437+
}
438+
439+
def isIdempotentExpr(tree: Tree)(implicit ctx: Context): Boolean =
440+
exprPurity(tree) >= Idempotent
430441

431442
def isPureBinding(tree: Tree)(implicit ctx: Context): Boolean = statPurity(tree) >= Pure
432443

0 commit comments

Comments
 (0)