From 864c5e75077a9806a37654bb12aec50160e51da9 Mon Sep 17 00:00:00 2001 From: skye Date: Sat, 21 Feb 2026 18:04:06 -0500 Subject: [PATCH] libexpr: Replace Value::mkInt with constructor calls Change-Id: I09b8e3aa61042a60c2ae767fc44c26b66a6a6964 --- lix/libexpr/eval-expr.cc | 2 +- lix/libexpr/flake/flake.cc | 13 +++-- lix/libexpr/json-to-value.cc | 4 +- lix/libexpr/primops.cc | 43 ++++++++-------- lix/libexpr/primops/fetchMercurial.cc | 2 +- lix/libexpr/primops/fetchTree.cc | 7 ++- lix/libexpr/primops/fromTOML.cc | 2 +- lix/libexpr/value.hh | 14 ++---- tests/unit/libexpr/json.cc | 6 +-- tests/unit/libexpr/value/print.cc | 72 +++++++++------------------ 10 files changed, 68 insertions(+), 97 deletions(-) diff --git a/lix/libexpr/eval-expr.cc b/lix/libexpr/eval-expr.cc index 39eeac927..41ced3afd 100644 --- a/lix/libexpr/eval-expr.cc +++ b/lix/libexpr/eval-expr.cc @@ -528,7 +528,7 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v) } if (firstType == nInt) { - v.mkInt(n); + v = {NewValueAs::integer, n}; } else if (firstType == nFloat) { v = {NewValueAs::floating, nf}; } else if (firstType == nPath) { diff --git a/lix/libexpr/flake/flake.cc b/lix/libexpr/flake/flake.cc index b25549690..4164523fa 100644 --- a/lix/libexpr/flake/flake.cc +++ b/lix/libexpr/flake/flake.cc @@ -1010,11 +1010,14 @@ void prim_parseFlakeRef( for (const auto & [key, value] : attrs) { auto s = state.ctx.symbols.create(key); auto & vv = binds.alloc(s); - std::visit(overloaded { - [&vv](const std::string & value) { vv.mkString(value); }, - [&vv](const uint64_t & value) { vv.mkInt(value); }, - [&vv](const Explicit & value) { vv.mkBool(value.t); } - }, value); + std::visit( + overloaded{ + [&vv](const std::string & value) { vv.mkString(value); }, + [&vv](const uint64_t & value) { vv = {NewValueAs::integer, NixInt::Inner(value)}; }, + [&vv](const Explicit & value) { vv.mkBool(value.t); } + }, + value + ); } v = {NewValueAs::attrs, binds}; } diff --git a/lix/libexpr/json-to-value.cc b/lix/libexpr/json-to-value.cc index 159db4614..63a886b27 100644 --- a/lix/libexpr/json-to-value.cc +++ b/lix/libexpr/json-to-value.cc @@ -108,7 +108,7 @@ public: bool number_integer(number_integer_t val) override { - rs->value().mkInt(val); + rs->value() = {NewValueAs::integer, val}; rs->add(); return true; } @@ -121,7 +121,7 @@ public: return number_float(static_cast(val_), ""); } NixInt::Inner val = val_; - rs->value().mkInt(val); + rs->value() = {NewValueAs::integer, val}; rs->add(); return true; } diff --git a/lix/libexpr/primops.cc b/lix/libexpr/primops.cc index 9f8145346..582fcb314 100644 --- a/lix/libexpr/primops.cc +++ b/lix/libexpr/primops.cc @@ -686,13 +686,13 @@ static void prim_ceil(EvalState & state, Value * * args, Value & v) { auto value = state.forceFloat(*args[0], noPos, "while evaluating the first argument passed to builtins.ceil"); - v.mkInt(ceil(value)); + v = {NewValueAs::integer, NixInt::Inner(ceil(value))}; } static void prim_floor(EvalState & state, Value * * args, Value & v) { auto value = state.forceFloat(*args[0], noPos, "while evaluating the first argument passed to builtins.floor"); - v.mkInt(floor(value)); + v = {NewValueAs::integer, NixInt::Inner(floor(value))}; } /* Try evaluating the argument. Success => {success=true; value=something;}, @@ -1889,15 +1889,16 @@ static void prim_unsafeGetAttrPos(EvalState & state, Value * * args, Value & v) // as with black holes this cost is too high to justify another thunk type to check // for in the very hot path that is forceValue. static struct LazyPosAcessors { - PrimOp primop_lineOfPos{{.arity = 1, .fun = [](EvalState & state, Value ** args, Value & v) { - v.mkInt(state.ctx.positions[PosIdx(args[0]->integer().value)].line - ); - }}}; - PrimOp primop_columnOfPos{{.arity = 1, .fun = [](EvalState & state, Value ** args, Value & v) { - v.mkInt( - state.ctx.positions[PosIdx(args[0]->integer().value)].column - ); - }}}; + PrimOp primop_lineOfPos{ + {.arity = 1, .fun = [](EvalState & state, Value ** args, Value & v) { + v = {NewValueAs::integer, state.ctx.positions[PosIdx(args[0]->integer().value)].line}; + }} + }; + PrimOp primop_columnOfPos{ + {.arity = 1, .fun = [](EvalState & state, Value ** args, Value & v) { + v = {NewValueAs::integer, state.ctx.positions[PosIdx(args[0]->integer().value)].column}; + }} + }; Value lineOfPos, columnOfPos; @@ -2340,7 +2341,7 @@ static void prim_concatLists(EvalState & state, Value * * args, Value & v) static void prim_length(EvalState & state, Value * * args, Value & v) { state.forceList(*args[0], noPos, "while evaluating the first argument passed to builtins.length"); - v.mkInt(args[0]->listSize()); + v = {NewValueAs::integer, NixInt::Inner(args[0]->listSize())}; } /* Reduce a list by applying a binary operator, from left to @@ -2592,7 +2593,7 @@ static void prim_add(EvalState & state, Value * * args, Value & v) auto result_ = i1 + i2; if (auto result = result_.valueChecked(); result.has_value()) { - v.mkInt(*result); + v = {NewValueAs::integer, *result}; } else { state.ctx.errors.make("integer overflow in adding %1% + %2%", i1, i2).debugThrow(); } @@ -2616,7 +2617,7 @@ static void prim_sub(EvalState & state, Value * * args, Value & v) auto result_ = i1 - i2; if (auto result = result_.valueChecked(); result.has_value()) { - v.mkInt(*result); + v = {NewValueAs::integer, *result}; } else { state.ctx.errors.make("integer overflow in subtracting %1% - %2%", i1, i2).debugThrow(); } @@ -2642,7 +2643,7 @@ static void prim_mul(EvalState & state, Value * * args, Value & v) auto result_ = i1 * i2; if (auto result = result_.valueChecked(); result.has_value()) { - v.mkInt(*result); + v = {NewValueAs::integer, *result}; } else { state.ctx.errors.make("integer overflow in multiplying %1% * %2%", i1, i2).debugThrow(); } @@ -2669,7 +2670,7 @@ static void prim_div(EvalState & state, Value * * args, Value & v) /* Avoid division overflow as it might raise SIGFPE. */ auto result_ = i1 / i2; if (auto result = result_.valueChecked(); result.has_value()) { - v.mkInt(*result); + v = {NewValueAs::integer, *result}; } else { state.ctx.errors.make("integer overflow in dividing %1% / %2%", i1, i2).debugThrow(); } @@ -2680,7 +2681,7 @@ static void prim_bitAnd(EvalState & state, Value * * args, Value & v) { auto i1 = state.forceInt(*args[0], noPos, "while evaluating the first argument passed to builtins.bitAnd"); auto i2 = state.forceInt(*args[1], noPos, "while evaluating the second argument passed to builtins.bitAnd"); - v.mkInt(i1.value & i2.value); + v = {NewValueAs::integer, i1.value & i2.value}; } static void prim_bitOr(EvalState & state, Value * * args, Value & v) @@ -2688,7 +2689,7 @@ static void prim_bitOr(EvalState & state, Value * * args, Value & v) auto i1 = state.forceInt(*args[0], noPos, "while evaluating the first argument passed to builtins.bitOr"); auto i2 = state.forceInt(*args[1], noPos, "while evaluating the second argument passed to builtins.bitOr"); - v.mkInt(i1.value | i2.value); + v = {NewValueAs::integer, i1.value | i2.value}; } static void prim_bitXor(EvalState & state, Value * * args, Value & v) @@ -2696,7 +2697,7 @@ static void prim_bitXor(EvalState & state, Value * * args, Value & v) auto i1 = state.forceInt(*args[0], noPos, "while evaluating the first argument passed to builtins.bitXor"); auto i2 = state.forceInt(*args[1], noPos, "while evaluating the second argument passed to builtins.bitXor"); - v.mkInt(i1.value ^ i2.value); + v = {NewValueAs::integer, i1.value ^ i2.value}; } static void prim_lessThan(EvalState & state, Value * * args, Value & v) @@ -2778,7 +2779,7 @@ static void prim_stringLength(EvalState & state, Value * * args, Value & v) { NixStringContext context; auto s = state.coerceToString(noPos, *args[0], context, "while evaluating the argument passed to builtins.stringLength"); - v.mkInt(NixInt::Inner(s->size())); + v = {NewValueAs::integer, NixInt::Inner(s->size())}; } /* Return the cryptographic hash of a string in base-16. */ @@ -3029,7 +3030,7 @@ static void prim_compareVersions(EvalState & state, Value * * args, Value & v) auto version1 = state.forceStringNoCtx(*args[0], noPos, "while evaluating the first argument passed to builtins.compareVersions"); auto version2 = state.forceStringNoCtx(*args[1], noPos, "while evaluating the second argument passed to builtins.compareVersions"); auto result = compareVersions(version1, version2); - v.mkInt(result < 0 ? -1 : result > 0 ? 1 : 0); + v = {NewValueAs::integer, result < 0 ? -1 : result > 0 ? 1 : 0}; } static void prim_splitVersion(EvalState & state, Value * * args, Value & v) diff --git a/lix/libexpr/primops/fetchMercurial.cc b/lix/libexpr/primops/fetchMercurial.cc index 44c335287..d3f51d0dc 100644 --- a/lix/libexpr/primops/fetchMercurial.cc +++ b/lix/libexpr/primops/fetchMercurial.cc @@ -97,7 +97,7 @@ void prim_fetchMercurial(EvalState & state, Value ** args, Value & v) attrs2.alloc("rev").mkString(rev2.gitRev()); attrs2.alloc("shortRev").mkString(rev2.gitRev().substr(0, 12)); if (auto revCount = input2.getRevCount()) - attrs2.alloc("revCount").mkInt(*revCount); + attrs2.alloc("revCount") = {NewValueAs::integer, NixInt::Inner(*revCount)}; v = {NewValueAs::attrs, attrs2}; state.ctx.paths.allowPath(tree.storePath); diff --git a/lix/libexpr/primops/fetchTree.cc b/lix/libexpr/primops/fetchTree.cc index c96defe0b..278a16e8d 100644 --- a/lix/libexpr/primops/fetchTree.cc +++ b/lix/libexpr/primops/fetchTree.cc @@ -51,10 +51,9 @@ void emitTreeAttrs( } if (auto revCount = input.getRevCount()) - attrs.alloc("revCount").mkInt(*revCount); + attrs.alloc("revCount") = {NewValueAs::integer, NixInt::Inner(*revCount)}; else if (emptyRevFallback) - attrs.alloc("revCount").mkInt(0); - + attrs.alloc("revCount") = {NewValueAs::integer, 0}; } if (auto dirtyRev = fetchers::maybeGetStrAttr(input.attrs, "dirtyRev")) { @@ -63,7 +62,7 @@ void emitTreeAttrs( } if (auto lastModified = input.getLastModified()) { - attrs.alloc("lastModified").mkInt(*lastModified); + attrs.alloc("lastModified") = {NewValueAs::integer, *lastModified}; attrs.alloc("lastModifiedDate").mkString( fmt("%s", std::put_time(std::gmtime(&*lastModified), "%Y%m%d%H%M%S"))); } diff --git a/lix/libexpr/primops/fromTOML.cc b/lix/libexpr/primops/fromTOML.cc index 18dda67de..3227b34b4 100644 --- a/lix/libexpr/primops/fromTOML.cc +++ b/lix/libexpr/primops/fromTOML.cc @@ -41,7 +41,7 @@ void prim_fromTOML(EvalState & state, Value ** args, Value & val) v.mkBool(toml::get(t)); break; case toml::value_t::integer: - v.mkInt(toml::get(t)); + v = {NewValueAs::integer, toml::get(t)}; break; case toml::value_t::floating: v = {NewValueAs::floating, toml::get(t)}; diff --git a/lix/libexpr/value.hh b/lix/libexpr/value.hh index 786e5f3cf..cdefabe26 100644 --- a/lix/libexpr/value.hh +++ b/lix/libexpr/value.hh @@ -405,6 +405,10 @@ public: { } + /// Constructs a nix language value of type "int", with the integral value + /// of @ref i. + Value(integer_t, NixInt::Inner i) : Value(NewValueAs::integer, NixInt{i}) {} + /// Constructs a nix language value of type "int", with the integral value /// of @ref i. Value(integer_t, NixInt i) @@ -771,16 +775,6 @@ public: */ inline ValueType type(bool invalidIsThunk = false) const; - inline void mkInt(NixInt::Inner n) - { - mkInt(NixInt{n}); - } - - inline void mkInt(NixInt n) - { - *this = {NewValueAs::integer, n}; - } - inline void mkBool(bool b) { raw = tag(tBool, b); diff --git a/tests/unit/libexpr/json.cc b/tests/unit/libexpr/json.cc index 558b0ebde..04bd4d6ac 100644 --- a/tests/unit/libexpr/json.cc +++ b/tests/unit/libexpr/json.cc @@ -33,14 +33,12 @@ namespace nix { } TEST_F(JSONValueTest, IntPositive) { - Value v; - v.mkInt(100); + Value v = {NewValueAs::integer, 100}; ASSERT_EQ(getJSONValue(v), "100"); } TEST_F(JSONValueTest, IntNegative) { - Value v; - v.mkInt(-100); + Value v = {NewValueAs::integer, -100}; ASSERT_EQ(getJSONValue(v), "-100"); } diff --git a/tests/unit/libexpr/value/print.cc b/tests/unit/libexpr/value/print.cc index 106b56e1c..faf1e6e6c 100644 --- a/tests/unit/libexpr/value/print.cc +++ b/tests/unit/libexpr/value/print.cc @@ -24,8 +24,7 @@ struct ValuePrintingTests : LibExprTest TEST_F(ValuePrintingTests, tInt) { - Value vInt; - vInt.mkInt(10); + Value vInt = {NewValueAs::integer, 10}; test(vInt, "10"); } @@ -58,11 +57,9 @@ TEST_F(ValuePrintingTests, tNull) TEST_F(ValuePrintingTests, tAttrs) { - Value vOne; - vOne.mkInt(1); + Value vOne = {NewValueAs::integer, 1}; - Value vTwo; - vTwo.mkInt(2); + Value vTwo = {NewValueAs::integer, 2}; BindingsBuilder builder = evaluator.buildBindings(10); builder.insert(evaluator.symbols.create("one"), vOne); @@ -75,11 +72,9 @@ TEST_F(ValuePrintingTests, tAttrs) TEST_F(ValuePrintingTests, tList) { - Value vOne; - vOne.mkInt(1); + Value vOne = {NewValueAs::integer, 1}; - Value vTwo; - vTwo.mkInt(2); + Value vTwo = {NewValueAs::integer, 2}; auto vList = evaluator.mem.newList(5); vList->elems[0] = vOne; @@ -193,14 +188,11 @@ TEST_F(ValuePrintingTests, vBlackhole) TEST_F(ValuePrintingTests, depthAttrs) { - Value vZero; - vZero.mkInt(0); + Value vZero = {NewValueAs::integer, 0}; - Value vOne; - vOne.mkInt(1); + Value vOne = {NewValueAs::integer, 1}; - Value vTwo; - vTwo.mkInt(2); + Value vTwo = {NewValueAs::integer, 2}; BindingsBuilder builderEmpty = evaluator.buildBindings(0); Value vAttrsEmpty = {NewValueAs::attrs, builderEmpty.finish()}; @@ -232,11 +224,9 @@ TEST_F(ValuePrintingTests, depthAttrs) TEST_F(ValuePrintingTests, depthList) { - Value vOne; - vOne.mkInt(1); + Value vOne = {NewValueAs::integer, 1}; - Value vTwo; - vTwo.mkInt(2); + Value vTwo = {NewValueAs::integer, 2}; BindingsBuilder builder = evaluator.buildBindings(10); builder.insert(evaluator.symbols.create("one"), vOne); @@ -319,8 +309,7 @@ TEST_F(ValuePrintingTests, attrsTypeFirst) TEST_F(ValuePrintingTests, ansiColorsInt) { - Value v; - v.mkInt(10); + Value v = {NewValueAs::integer, 10}; test(v, ANSI_CYAN "10" ANSI_NORMAL, @@ -402,11 +391,9 @@ TEST_F(ValuePrintingTests, ansiColorsNull) TEST_F(ValuePrintingTests, ansiColorsAttrs) { - Value vOne; - vOne.mkInt(1); + Value vOne = {NewValueAs::integer, 1}; - Value vTwo; - vTwo.mkInt(2); + Value vTwo = {NewValueAs::integer, 2}; BindingsBuilder builder = evaluator.buildBindings(10); builder.insert(evaluator.symbols.create("one"), vOne); @@ -513,11 +500,9 @@ TEST_F(ValuePrintingTests, ansiColorsAssert) TEST_F(ValuePrintingTests, ansiColorsList) { - Value vOne; - vOne.mkInt(1); + Value vOne = {NewValueAs::integer, 1}; - Value vTwo; - vTwo.mkInt(2); + Value vTwo = {NewValueAs::integer, 2}; auto vList = evaluator.mem.newList(5); vList->elems[0] = vOne; @@ -622,8 +607,7 @@ TEST_F(ValuePrintingTests, ansiColorsBlackhole) TEST_F(ValuePrintingTests, ansiColorsAttrsRepeated) { - Value vZero; - vZero.mkInt(0); + Value vZero = {NewValueAs::integer, 0}; BindingsBuilder innerBuilder = evaluator.buildBindings(1); innerBuilder.insert(evaluator.symbols.create("x"), vZero); @@ -645,8 +629,7 @@ TEST_F(ValuePrintingTests, ansiColorsAttrsRepeated) TEST_F(ValuePrintingTests, ansiColorsListRepeated) { - Value vZero; - vZero.mkInt(0); + Value vZero = {NewValueAs::integer, 0}; BindingsBuilder innerBuilder = evaluator.buildBindings(1); innerBuilder.insert(evaluator.symbols.create("x"), vZero); @@ -667,8 +650,7 @@ TEST_F(ValuePrintingTests, ansiColorsListRepeated) TEST_F(ValuePrintingTests, listRepeated) { - Value vZero; - vZero.mkInt(0); + Value vZero = {NewValueAs::integer, 0}; BindingsBuilder innerBuilder = evaluator.buildBindings(1); innerBuilder.insert(evaluator.symbols.create("x"), vZero); @@ -691,11 +673,9 @@ TEST_F(ValuePrintingTests, listRepeated) TEST_F(ValuePrintingTests, ansiColorsAttrsElided) { - Value vOne; - vOne.mkInt(1); + Value vOne = {NewValueAs::integer, 1}; - Value vTwo; - vTwo.mkInt(2); + Value vTwo = {NewValueAs::integer, 2}; BindingsBuilder builder = evaluator.buildBindings(10); builder.insert(evaluator.symbols.create("one"), vOne); @@ -710,8 +690,7 @@ TEST_F(ValuePrintingTests, ansiColorsAttrsElided) .maxAttrs = 1 }); - Value vThree; - vThree.mkInt(3); + Value vThree = {NewValueAs::integer, 3}; builder.insert(evaluator.symbols.create("three"), vThree); vAttrs = {NewValueAs::attrs, builder.finish()}; @@ -726,11 +705,9 @@ TEST_F(ValuePrintingTests, ansiColorsAttrsElided) TEST_F(ValuePrintingTests, ansiColorsListElided) { - Value vOne; - vOne.mkInt(1); + Value vOne = {NewValueAs::integer, 1}; - Value vTwo; - vTwo.mkInt(2); + Value vTwo = {NewValueAs::integer, 2}; auto list = evaluator.mem.newList(4); Value vList{NewValueAs::list, list}; @@ -745,8 +722,7 @@ TEST_F(ValuePrintingTests, ansiColorsListElided) .maxListItems = 1 }); - Value vThree; - vThree.mkInt(3); + Value vThree = {NewValueAs::integer, 3}; list->elems[2] = vThree; list->size = 3;