From bbb9fa0f7a1030bb47ea8e35d7a06ef5d8c48838 Mon Sep 17 00:00:00 2001 From: Matt Bovel Date: Thu, 13 Nov 2025 23:43:43 +0000 Subject: [PATCH] Try harder to get a constant rhs for `inline val`s --- compiler/src/dotty/tools/dotc/inlines/Inliner.scala | 5 ++++- tests/run/i24420-inline-val-constant.scala | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/run/i24420-inline-val-constant.scala diff --git a/compiler/src/dotty/tools/dotc/inlines/Inliner.scala b/compiler/src/dotty/tools/dotc/inlines/Inliner.scala index 356e5ad40fdd..947f11eaccc9 100644 --- a/compiler/src/dotty/tools/dotc/inlines/Inliner.scala +++ b/compiler/src/dotty/tools/dotc/inlines/Inliner.scala @@ -928,7 +928,10 @@ class Inliner(val call: tpd.Tree)(using Context): override def typedValDef(vdef: untpd.ValDef, sym: Symbol)(using Context): Tree = val vdef1 = if sym.is(Inline) then - val rhs = typed(vdef.rhs) + val rhs = + typed(vdef.rhs) match + case ConstantValue(v) => Literal(Constant(v)) + case rhs => rhs sym.info = rhs.tpe untpd.cpy.ValDef(vdef)(vdef.name, untpd.TypeTree(rhs.tpe), untpd.TypedSplice(rhs)) else vdef diff --git a/tests/run/i24420-inline-val-constant.scala b/tests/run/i24420-inline-val-constant.scala new file mode 100644 index 000000000000..7fc889872736 --- /dev/null +++ b/tests/run/i24420-inline-val-constant.scala @@ -0,0 +1,9 @@ +inline def f1(): Long = + 1L + +inline def f3(): Long = + inline val x = f1() + x + +@main def Test: Unit = + assert(f3() == 1L)