Skip to content

Commit 54708cc

Browse files
committed
Add ConstantTree extractor
1 parent 6a25712 commit 54708cc

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ object ConstFold:
2929
def Apply[T <: Apply](tree: T)(using Context): T =
3030
tree.fun match
3131
case Select(xt, op) if foldedBinops.contains(op) =>
32-
treeConstant(xt) match
33-
case Some(x) =>
32+
xt match
33+
case ConstantTree(x) =>
3434
tree.args match
3535
case yt :: Nil =>
36-
treeConstant(yt) match
37-
case Some(y) => tree.withFoldedType(foldBinop(op, x, y))
36+
yt match
37+
case ConstantTree(y) => tree.withFoldedType(foldBinop(op, x, y))
3838
case _ => tree
3939
case _ => tree
4040
case _ => tree
@@ -46,8 +46,8 @@ object ConstFold:
4646

4747
def Select[T <: Select](tree: T)(using Context): T =
4848
if foldedUnops.contains(tree.name) then
49-
treeConstant(tree.qualifier) match
50-
case Some(x) => tree.withFoldedType(foldUnop(tree.name, x))
49+
tree.qualifier match
50+
case ConstantTree(x) => tree.withFoldedType(foldUnop(tree.name, x))
5151
case _ => tree
5252
else tree
5353

@@ -59,15 +59,16 @@ object ConstFold:
5959
tree.withFoldedType(Constant(targ.tpe))
6060
case _ => tree
6161

62-
private def treeConstant(tree: Tree)(using Context): Option[Constant] =
63-
tree match
64-
case Inlined(_, Nil, expr) => treeConstant(expr)
65-
case Typed(expr, _) => treeConstant(expr)
66-
case Literal(c) if c.tag == Constants.NullTag => Some(c)
67-
case _ =>
68-
tree.tpe.widenTermRefExpr.normalized.simplified match
69-
case ConstantType(c) => Some(c)
70-
case _ => None
62+
private object ConstantTree:
63+
def unapply(tree: Tree)(using Context): Option[Constant] =
64+
tree match
65+
case Inlined(_, Nil, expr) => unapply(expr)
66+
case Typed(expr, _) => unapply(expr)
67+
case Literal(c) if c.tag == Constants.NullTag => Some(c)
68+
case _ =>
69+
tree.tpe.widenTermRefExpr.normalized.simplified match
70+
case ConstantType(c) => Some(c)
71+
case _ => None
7172

7273
extension [T <: Tree](tree: T)(using Context)
7374
private def withFoldedType(c: Constant | Null): T =

0 commit comments

Comments
 (0)