From 23a0b78a16ad49110be9e1cdc7534b83cf00dc61 Mon Sep 17 00:00:00 2001 From: skye Date: Sun, 22 Feb 2026 16:23:26 -0500 Subject: [PATCH] libexpr: Replace Value::mkFloat with constructor calls The pseudo-constructor `Value::mkFloat` would previously be called on an default-constructed (under-initialized) `Value` to create a properly initialized `Value` that represents a float. This change removes mkFloat and constructs float `Value`s directly. Change-Id: I48ae3a836842ce5e5eda6323404ba7576a6a6964 --- lix/libexpr/eval-expr.cc | 2 +- lix/libexpr/json-to-value.cc | 2 +- lix/libexpr/primops.cc | 28 +++++++++++++++++++++------- lix/libexpr/primops/fromTOML.cc | 2 +- lix/libexpr/value.hh | 5 ----- tests/unit/libexpr/value/print.cc | 6 ++---- 6 files changed, 26 insertions(+), 19 deletions(-) diff --git a/lix/libexpr/eval-expr.cc b/lix/libexpr/eval-expr.cc index 414bb297c..39eeac927 100644 --- a/lix/libexpr/eval-expr.cc +++ b/lix/libexpr/eval-expr.cc @@ -530,7 +530,7 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v) if (firstType == nInt) { v.mkInt(n); } else if (firstType == nFloat) { - v.mkFloat(nf); + v = {NewValueAs::floating, nf}; } else if (firstType == nPath) { if (!context.empty()) { state.ctx.errors diff --git a/lix/libexpr/json-to-value.cc b/lix/libexpr/json-to-value.cc index 8d63887c1..159db4614 100644 --- a/lix/libexpr/json-to-value.cc +++ b/lix/libexpr/json-to-value.cc @@ -128,7 +128,7 @@ public: bool number_float(number_float_t val, const string_t & s) override { - rs->value().mkFloat(val); + rs->value() = {NewValueAs::floating, val}; rs->add(); return true; } diff --git a/lix/libexpr/primops.cc b/lix/libexpr/primops.cc index 8980606b1..9f8145346 100644 --- a/lix/libexpr/primops.cc +++ b/lix/libexpr/primops.cc @@ -2581,8 +2581,11 @@ static void prim_add(EvalState & state, Value * * args, Value & v) state.forceValue(*args[0], noPos); state.forceValue(*args[1], noPos); if (args[0]->type() == nFloat || args[1]->type() == nFloat) - v.mkFloat(state.forceFloat(*args[0], noPos, "while evaluating the first argument of the addition") - + state.forceFloat(*args[1], noPos, "while evaluating the second argument of the addition")); + v = { + NewValueAs::floating, + state.forceFloat(*args[0], noPos, "while evaluating the first argument of the addition") + + state.forceFloat(*args[1], noPos, "while evaluating the second argument of the addition") + }; else { auto i1 = state.forceInt(*args[0], noPos, "while evaluating the first argument of the addition"); auto i2 = state.forceInt(*args[1], noPos, "while evaluating the second argument of the addition"); @@ -2601,8 +2604,11 @@ static void prim_sub(EvalState & state, Value * * args, Value & v) state.forceValue(*args[0], noPos); state.forceValue(*args[1], noPos); if (args[0]->type() == nFloat || args[1]->type() == nFloat) - v.mkFloat(state.forceFloat(*args[0], noPos, "while evaluating the first argument of the subtraction") - - state.forceFloat(*args[1], noPos, "while evaluating the second argument of the subtraction")); + v = { + NewValueAs::floating, + state.forceFloat(*args[0], noPos, "while evaluating the first argument of the subtraction") + - state.forceFloat(*args[1], noPos, "while evaluating the second argument of the subtraction") + }; else { auto i1 = state.forceInt(*args[0], noPos, "while evaluating the first argument of the subtraction"); auto i2 = state.forceInt(*args[1], noPos, "while evaluating the second argument of the subtraction"); @@ -2622,8 +2628,13 @@ static void prim_mul(EvalState & state, Value * * args, Value & v) state.forceValue(*args[0], noPos); state.forceValue(*args[1], noPos); if (args[0]->type() == nFloat || args[1]->type() == nFloat) - v.mkFloat(state.forceFloat(*args[0], noPos, "while evaluating the first of the multiplication") - * state.forceFloat(*args[1], noPos, "while evaluating the second argument of the multiplication")); + v = { + NewValueAs::floating, + state.forceFloat(*args[0], noPos, "while evaluating the first of the multiplication") + * state.forceFloat( + *args[1], noPos, "while evaluating the second argument of the multiplication" + ) + }; else { auto i1 = state.forceInt(*args[0], noPos, "while evaluating the first argument of the multiplication"); auto i2 = state.forceInt(*args[1], noPos, "while evaluating the second argument of the multiplication"); @@ -2648,7 +2659,10 @@ static void prim_div(EvalState & state, Value * * args, Value & v) state.ctx.errors.make("division by zero").debugThrow(); if (args[0]->type() == nFloat || args[1]->type() == nFloat) { - v.mkFloat(state.forceFloat(*args[0], noPos, "while evaluating the first operand of the division") / f2); + v = { + NewValueAs::floating, + state.forceFloat(*args[0], noPos, "while evaluating the first operand of the division") / f2 + }; } else { NixInt i1 = state.forceInt(*args[0], noPos, "while evaluating the first operand of the division"); NixInt i2 = state.forceInt(*args[1], noPos, "while evaluating the second operand of the division"); diff --git a/lix/libexpr/primops/fromTOML.cc b/lix/libexpr/primops/fromTOML.cc index a511cfdb9..18dda67de 100644 --- a/lix/libexpr/primops/fromTOML.cc +++ b/lix/libexpr/primops/fromTOML.cc @@ -44,7 +44,7 @@ void prim_fromTOML(EvalState & state, Value ** args, Value & val) v.mkInt(toml::get(t)); break; case toml::value_t::floating: - v.mkFloat(toml::get(t)); + v = {NewValueAs::floating, toml::get(t)}; break; case toml::value_t::string: v.mkString(toml::get(t)); diff --git a/lix/libexpr/value.hh b/lix/libexpr/value.hh index 4a35abf41..786e5f3cf 100644 --- a/lix/libexpr/value.hh +++ b/lix/libexpr/value.hh @@ -802,11 +802,6 @@ public: *this = {NewValueAs::external, *e}; } - inline void mkFloat(NixFloat n) - { - *this = {NewValueAs::floating, n}; - } - bool isList() const { return internalType() == tList; diff --git a/tests/unit/libexpr/value/print.cc b/tests/unit/libexpr/value/print.cc index 40c965e1f..106b56e1c 100644 --- a/tests/unit/libexpr/value/print.cc +++ b/tests/unit/libexpr/value/print.cc @@ -180,8 +180,7 @@ TEST_F(ValuePrintingTests, vExternal) TEST_F(ValuePrintingTests, vFloat) { - Value vFloat; - vFloat.mkFloat(2.0); + Value vFloat = {NewValueAs::floating, 2.0}; test(vFloat, "2"); } @@ -332,8 +331,7 @@ TEST_F(ValuePrintingTests, ansiColorsInt) TEST_F(ValuePrintingTests, ansiColorsFloat) { - Value v; - v.mkFloat(1.6); + Value v = {NewValueAs::floating, 1.6}; test(v, ANSI_CYAN "1.6" ANSI_NORMAL,