Skip to content

Commit 730127b

Browse files
committed
Constant fold on StringInterpolatorOpt
1 parent b68f94f commit 730127b

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

compiler/src/dotty/tools/dotc/transform/localopt/StringInterpolatorOpt.scala

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dotty.tools.dotc.core.NameKinds._
1111
import dotty.tools.dotc.core.Symbols._
1212
import dotty.tools.dotc.core.Types._
1313
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
14+
import dotty.tools.dotc.typer.ConstFold
1415

1516
/**
1617
* MiniPhase to transform s and raw string interpolators from using StringContext to string
@@ -162,6 +163,18 @@ class StringInterpolatorOpt extends MiniPhase {
162163
}
163164
}
164165
else
165-
tree
166+
tree.tpe match
167+
case _: ConstantType => tree
168+
case _ =>
169+
ConstFold.Apply(tree).tpe match
170+
case ConstantType(x) => Literal(x).withSpan(tree.span).ensureConforms(tree.tpe)
171+
case _ => tree
166172
}
173+
174+
override def transformSelect(tree: Select)(using Context): Tree = {
175+
ConstFold.Select(tree).tpe match
176+
case ConstantType(x) => Literal(x).withSpan(tree.span).ensureConforms(tree.tpe)
177+
case _ => tree
178+
}
179+
167180
}

tests/run-macros/tasty-extractors-1.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ ConstantType(IntConstant(2))
7373
Inlined(None, Nil, Try(Literal(IntConstant(3)), List(CaseDef(Ident("_"), None, Block(Nil, Literal(UnitConstant())))), Some(Literal(UnitConstant()))))
7474
OrType(ConstantType(IntConstant(3)), TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit"))
7575

76-
Inlined(None, Nil, Apply(Select(Literal(StringConstant("a")), "=="), List(Literal(StringConstant("b")))))
77-
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Boolean")
76+
Inlined(None, Nil, Literal(BooleanConstant(false)))
77+
ConstantType(BooleanConstant(false))
7878

7979
Inlined(None, Nil, Apply(Select(New(TypeIdent("Object")), "<init>"), Nil))
8080
TypeRef(ThisType(TypeRef(NoPrefix(), "lang")), "Object")

0 commit comments

Comments
 (0)