diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 297832818..fb4cfdccf 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -1946,7 +1946,7 @@ void ExprOpConcatLists::eval(EvalState & state, Env & env, Value & v) } -void EvalState::concatLists(Value & v, size_t nrLists, Value * * lists, const PosIdx pos, std::string_view errorCtx) +void EvalState::concatLists(Value & v, size_t nrLists, Value * const * lists, const PosIdx pos, std::string_view errorCtx) { nrListConcats++; diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index 4a271f4ef..7db911fce 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -671,7 +671,7 @@ public: const SingleDerivedPath & p, Value & v); - void concatLists(Value & v, size_t nrLists, Value * * lists, const PosIdx pos, std::string_view errorCtx); + void concatLists(Value & v, size_t nrLists, Value * const * lists, const PosIdx pos, std::string_view errorCtx); /** * Print statistics, if enabled. diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 32913d72e..9449a8f7c 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -3372,7 +3372,6 @@ static void prim_sort(EvalState & state, const PosIdx pos, Value * * args, Value auto list = state.buildList(len); for (const auto & [n, v] : enumerate(list)) state.forceValue(*(v = args[1]->listElems()[n]), pos); - v.mkList(list); auto comparator = [&](Value * a, Value * b) { /* Optimization: if the comparator is lessThan, bypass @@ -3391,7 +3390,9 @@ static void prim_sort(EvalState & state, const PosIdx pos, Value * * args, Value /* FIXME: std::sort can segfault if the comparator is not a strict weak ordering. What to do? std::stable_sort() seems more resilient, but no guarantees... */ - std::stable_sort(v.listElems(), v.listElems() + len, comparator); + std::stable_sort(list.begin(), list.end(), comparator); + + v.mkList(list); } static RegisterPrimOp primop_sort({ diff --git a/src/libexpr/value.hh b/src/libexpr/value.hh index 9f0600efb..885621cf5 100644 --- a/src/libexpr/value.hh +++ b/src/libexpr/value.hh @@ -246,7 +246,7 @@ public: Bindings * attrs; struct { size_t size; - Value * * elems; + Value * const * elems; } bigList; Value * smallList[2]; ClosureThunk thunk; @@ -425,7 +425,7 @@ public: return internalType == tList1 || internalType == tList2 || internalType == tListN; } - Value * * listElems() + Value * const * listElems() { return internalType == tList1 || internalType == tList2 ? smallList : bigList.elems; }