Skip to content

Commit bbb9fa0

Browse files
committed
Try harder to get a constant rhs for inline vals
1 parent 7a0d2be commit bbb9fa0

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

compiler/src/dotty/tools/dotc/inlines/Inliner.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,10 @@ class Inliner(val call: tpd.Tree)(using Context):
928928
override def typedValDef(vdef: untpd.ValDef, sym: Symbol)(using Context): Tree =
929929
val vdef1 =
930930
if sym.is(Inline) then
931-
val rhs = typed(vdef.rhs)
931+
val rhs =
932+
typed(vdef.rhs) match
933+
case ConstantValue(v) => Literal(Constant(v))
934+
case rhs => rhs
932935
sym.info = rhs.tpe
933936
untpd.cpy.ValDef(vdef)(vdef.name, untpd.TypeTree(rhs.tpe), untpd.TypedSplice(rhs))
934937
else vdef
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
inline def f1(): Long =
2+
1L
3+
4+
inline def f3(): Long =
5+
inline val x = f1()
6+
x
7+
8+
@main def Test: Unit =
9+
assert(f3() == 1L)

0 commit comments

Comments
 (0)