mirror of
https://github.com/NixOS/nix.git
synced 2025-11-08 11:36:03 +01:00
Cleanup: Use C++23 "explicit this" for recursive lambdas
Try to pass by reference where possible. Co-authored-by: Sergei Zimmerman <sergei@zimmerman.foo>
This commit is contained in:
parent
3b2186e1c8
commit
1507843f6c
15 changed files with 60 additions and 112 deletions
|
|
@ -2160,30 +2160,28 @@ void EvalState::forceValueDeep(Value & v)
|
|||
{
|
||||
std::set<const Value *> seen;
|
||||
|
||||
std::function<void(Value & v)> recurse;
|
||||
|
||||
recurse = [&](Value & v) {
|
||||
[&, &state(*this)](this const auto & recurse, Value & v) {
|
||||
if (!seen.insert(&v).second)
|
||||
return;
|
||||
|
||||
forceValue(v, v.determinePos(noPos));
|
||||
state.forceValue(v, v.determinePos(noPos));
|
||||
|
||||
if (v.type() == nAttrs) {
|
||||
for (auto & i : *v.attrs())
|
||||
try {
|
||||
// If the value is a thunk, we're evaling. Otherwise no trace necessary.
|
||||
auto dts = debugRepl && i.value->isThunk() ? makeDebugTraceStacker(
|
||||
*this,
|
||||
auto dts = state.debugRepl && i.value->isThunk() ? makeDebugTraceStacker(
|
||||
state,
|
||||
*i.value->thunk().expr,
|
||||
*i.value->thunk().env,
|
||||
i.pos,
|
||||
"while evaluating the attribute '%1%'",
|
||||
symbols[i.name])
|
||||
state.symbols[i.name])
|
||||
: nullptr;
|
||||
|
||||
recurse(*i.value);
|
||||
} catch (Error & e) {
|
||||
addErrorTrace(e, i.pos, "while evaluating the attribute '%1%'", symbols[i.name]);
|
||||
state.addErrorTrace(e, i.pos, "while evaluating the attribute '%1%'", state.symbols[i.name]);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
|
@ -2192,9 +2190,7 @@ void EvalState::forceValueDeep(Value & v)
|
|||
for (auto v2 : v.listView())
|
||||
recurse(*v2);
|
||||
}
|
||||
};
|
||||
|
||||
recurse(v);
|
||||
}(v);
|
||||
}
|
||||
|
||||
NixInt EvalState::forceInt(Value & v, const PosIdx pos, std::string_view errorCtx)
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value ** args, Va
|
|||
|
||||
std::istringstream tomlStream(std::string{toml});
|
||||
|
||||
auto visit = [&](auto & self, Value & v, toml::value t) -> void {
|
||||
auto visit = [&](this auto & self, Value & v, toml::value t) -> void {
|
||||
switch (t.type()) {
|
||||
case toml::value_t::table: {
|
||||
auto table = toml::get<toml::table>(t);
|
||||
|
|
@ -100,7 +100,7 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value ** args, Va
|
|||
|
||||
for (auto & elem : table) {
|
||||
forceNoNullByte(elem.first);
|
||||
self(self, attrs.alloc(elem.first), elem.second);
|
||||
self(attrs.alloc(elem.first), elem.second);
|
||||
}
|
||||
|
||||
v.mkAttrs(attrs);
|
||||
|
|
@ -110,7 +110,7 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value ** args, Va
|
|||
|
||||
auto list = state.buildList(array.size());
|
||||
for (const auto & [n, v] : enumerate(list))
|
||||
self(self, *(v = state.allocValue()), array[n]);
|
||||
self(*(v = state.allocValue()), array[n]);
|
||||
v.mkList(list);
|
||||
} break;
|
||||
case toml::value_t::boolean:
|
||||
|
|
@ -155,7 +155,6 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value ** args, Va
|
|||
|
||||
try {
|
||||
visit(
|
||||
visit,
|
||||
val,
|
||||
toml::parse(
|
||||
tomlStream,
|
||||
|
|
|
|||
|
|
@ -9,8 +9,7 @@ NixStringContextElem NixStringContextElem::parse(std::string_view s0, const Expe
|
|||
{
|
||||
std::string_view s = s0;
|
||||
|
||||
std::function<SingleDerivedPath()> parseRest;
|
||||
parseRest = [&]() -> SingleDerivedPath {
|
||||
auto parseRest = [&](this auto & parseRest) -> SingleDerivedPath {
|
||||
// Case on whether there is a '!'
|
||||
size_t index = s.find("!");
|
||||
if (index == std::string_view::npos) {
|
||||
|
|
|
|||
|
|
@ -502,8 +502,8 @@ lockFlake(const Settings & settings, EvalState & state, const FlakeRef & topRef,
|
|||
|
||||
/* Get the overrides (i.e. attributes of the form
|
||||
'inputs.nixops.inputs.nixpkgs.url = ...'). */
|
||||
std::function<void(const FlakeInput & input, const InputAttrPath & prefix)> addOverrides;
|
||||
addOverrides = [&](const FlakeInput & input, const InputAttrPath & prefix) {
|
||||
auto addOverrides =
|
||||
[&](this const auto & addOverrides, const FlakeInput & input, const InputAttrPath & prefix) -> void {
|
||||
for (auto & [idOverride, inputOverride] : input.overrides) {
|
||||
auto inputAttrPath(prefix);
|
||||
inputAttrPath.push_back(idOverride);
|
||||
|
|
|
|||
|
|
@ -147,11 +147,10 @@ LockFile::LockFile(const fetchers::Settings & fetchSettings, std::string_view co
|
|||
if (version < 5 || version > 7)
|
||||
throw Error("lock file '%s' has unsupported version %d", path, version);
|
||||
|
||||
std::map<std::string, ref<Node>> nodeMap;
|
||||
std::string rootKey = json["root"];
|
||||
std::map<std::string, ref<Node>> nodeMap{{rootKey, root}};
|
||||
|
||||
std::function<void(Node & node, const nlohmann::json & jsonNode)> getInputs;
|
||||
|
||||
getInputs = [&](Node & node, const nlohmann::json & jsonNode) {
|
||||
[&](this const auto & getInputs, Node & node, const nlohmann::json & jsonNode) {
|
||||
if (jsonNode.find("inputs") == jsonNode.end())
|
||||
return;
|
||||
for (auto & i : jsonNode["inputs"].items()) {
|
||||
|
|
@ -179,11 +178,7 @@ LockFile::LockFile(const fetchers::Settings & fetchSettings, std::string_view co
|
|||
throw Error("lock file contains cycle to root node");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
std::string rootKey = json["root"];
|
||||
nodeMap.insert_or_assign(rootKey, root);
|
||||
getInputs(*root, json["nodes"][rootKey]);
|
||||
}(*root, json["nodes"][rootKey]);
|
||||
|
||||
// FIXME: check that there are no cycles in version >= 7. Cycles
|
||||
// between inputs are only possible using 'follows' indirections.
|
||||
|
|
@ -197,9 +192,7 @@ std::pair<nlohmann::json, LockFile::KeyMap> LockFile::toJSON() const
|
|||
KeyMap nodeKeys;
|
||||
boost::unordered_flat_set<std::string> keys;
|
||||
|
||||
std::function<std::string(const std::string & key, ref<const Node> node)> dumpNode;
|
||||
|
||||
dumpNode = [&](std::string key, ref<const Node> node) -> std::string {
|
||||
auto dumpNode = [&](this auto & dumpNode, std::string key, ref<const Node> node) -> std::string {
|
||||
auto k = nodeKeys.find(node);
|
||||
if (k != nodeKeys.end())
|
||||
return k->second;
|
||||
|
|
@ -276,17 +269,13 @@ std::optional<FlakeRef> LockFile::isUnlocked(const fetchers::Settings & fetchSet
|
|||
{
|
||||
std::set<ref<const Node>> nodes;
|
||||
|
||||
std::function<void(ref<const Node> node)> visit;
|
||||
|
||||
visit = [&](ref<const Node> node) {
|
||||
[&](this const auto & visit, ref<const Node> node) {
|
||||
if (!nodes.insert(node).second)
|
||||
return;
|
||||
for (auto & i : node->inputs)
|
||||
if (auto child = std::get_if<0>(&i.second))
|
||||
visit(*child);
|
||||
};
|
||||
|
||||
visit(root);
|
||||
}(root);
|
||||
|
||||
/* Return whether the input is either locked, or, if
|
||||
`allow-dirty-locks` is enabled, it has a NAR hash. In the
|
||||
|
|
@ -332,9 +321,7 @@ std::map<InputAttrPath, Node::Edge> LockFile::getAllInputs() const
|
|||
std::set<ref<Node>> done;
|
||||
std::map<InputAttrPath, Node::Edge> res;
|
||||
|
||||
std::function<void(const InputAttrPath & prefix, ref<Node> node)> recurse;
|
||||
|
||||
recurse = [&](const InputAttrPath & prefix, ref<Node> node) {
|
||||
[&](this const auto & recurse, const InputAttrPath & prefix, ref<Node> node) {
|
||||
if (!done.insert(node).second)
|
||||
return;
|
||||
|
||||
|
|
@ -345,9 +332,7 @@ std::map<InputAttrPath, Node::Edge> LockFile::getAllInputs() const
|
|||
if (auto child = std::get_if<0>(&input))
|
||||
recurse(inputAttrPath, *child);
|
||||
}
|
||||
};
|
||||
|
||||
recurse({}, root);
|
||||
}({}, root);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1391,8 +1391,7 @@ void adl_serializer<Derivation>::to_json(json & res, const Derivation & d)
|
|||
}
|
||||
|
||||
{
|
||||
std::function<nlohmann::json(const DerivedPathMap<StringSet>::ChildNode &)> doInput;
|
||||
doInput = [&](const auto & inputNode) {
|
||||
auto doInput = [&](this const auto & doInput, const auto & inputNode) -> nlohmann::json {
|
||||
auto value = nlohmann::json::object();
|
||||
value["outputs"] = inputNode.value;
|
||||
{
|
||||
|
|
@ -1454,8 +1453,7 @@ Derivation adl_serializer<Derivation>::from_json(const json & _json, const Exper
|
|||
}
|
||||
|
||||
try {
|
||||
std::function<DerivedPathMap<StringSet>::ChildNode(const nlohmann::json &)> doInput;
|
||||
doInput = [&](const auto & _json) {
|
||||
auto doInput = [&](this const auto & doInput, const auto & _json) -> DerivedPathMap<StringSet>::ChildNode {
|
||||
auto & json = getObject(_json);
|
||||
DerivedPathMap<StringSet>::ChildNode node;
|
||||
node.value = getStringSet(valueAt(json, "outputs"));
|
||||
|
|
|
|||
|
|
@ -126,13 +126,13 @@ MissingPaths Store::queryMissing(const std::vector<DerivedPath> & targets)
|
|||
|
||||
std::function<void(DerivedPath)> doPath;
|
||||
|
||||
std::function<void(ref<SingleDerivedPath>, const DerivedPathMap<StringSet>::ChildNode &)> enqueueDerivedPaths;
|
||||
|
||||
enqueueDerivedPaths = [&](ref<SingleDerivedPath> inputDrv, const DerivedPathMap<StringSet>::ChildNode & inputNode) {
|
||||
auto enqueueDerivedPaths = [&](this auto self,
|
||||
ref<SingleDerivedPath> inputDrv,
|
||||
const DerivedPathMap<StringSet>::ChildNode & inputNode) -> void {
|
||||
if (!inputNode.value.empty())
|
||||
pool.enqueue(std::bind(doPath, DerivedPath::Built{inputDrv, inputNode.value}));
|
||||
for (const auto & [outputName, childNode] : inputNode.childMap)
|
||||
enqueueDerivedPaths(make_ref<SingleDerivedPath>(SingleDerivedPath::Built{inputDrv, outputName}), childNode);
|
||||
self(make_ref<SingleDerivedPath>(SingleDerivedPath::Built{inputDrv, outputName}), childNode);
|
||||
};
|
||||
|
||||
auto mustBuildDrv = [&](const StorePath & drvPath, const Derivation & drv) {
|
||||
|
|
@ -350,9 +350,9 @@ drvOutputReferences(Store & store, const Derivation & drv, const StorePath & out
|
|||
|
||||
std::set<Realisation> inputRealisations;
|
||||
|
||||
std::function<void(const StorePath &, const DerivedPathMap<StringSet>::ChildNode &)> accumRealisations;
|
||||
|
||||
accumRealisations = [&](const StorePath & inputDrv, const DerivedPathMap<StringSet>::ChildNode & inputNode) {
|
||||
auto accumRealisations = [&](this auto & self,
|
||||
const StorePath & inputDrv,
|
||||
const DerivedPathMap<StringSet>::ChildNode & inputNode) -> void {
|
||||
if (!inputNode.value.empty()) {
|
||||
auto outputHashes = staticOutputHashes(evalStore, evalStore.readDerivation(inputDrv));
|
||||
for (const auto & outputName : inputNode.value) {
|
||||
|
|
@ -372,7 +372,7 @@ drvOutputReferences(Store & store, const Derivation & drv, const StorePath & out
|
|||
auto d = makeConstantStorePathRef(inputDrv);
|
||||
for (const auto & [outputName, childNode] : inputNode.childMap) {
|
||||
SingleDerivedPath next = SingleDerivedPath::Built{d, outputName};
|
||||
accumRealisations(
|
||||
self(
|
||||
// TODO deep resolutions for dynamic derivations, issue #8947, would go here.
|
||||
resolveDerivedPath(store, next, evalStore_),
|
||||
childNode);
|
||||
|
|
|
|||
|
|
@ -144,11 +144,7 @@ struct NarAccessor : public SourceAccessor
|
|||
NarAccessor(const nlohmann::json & listing, GetNarBytes getNarBytes)
|
||||
: getNarBytes(getNarBytes)
|
||||
{
|
||||
using json = nlohmann::json;
|
||||
|
||||
std::function<void(NarMember &, const json &)> recurse;
|
||||
|
||||
recurse = [&](NarMember & member, const json & v) {
|
||||
[&](this const auto & recurse, NarMember & member, const nlohmann::json & v) -> void {
|
||||
std::string type = v["type"];
|
||||
|
||||
if (type == "directory") {
|
||||
|
|
@ -167,9 +163,7 @@ struct NarAccessor : public SourceAccessor
|
|||
member.target = v.value("target", "");
|
||||
} else
|
||||
return;
|
||||
};
|
||||
|
||||
recurse(root, listing);
|
||||
}(root, listing);
|
||||
}
|
||||
|
||||
NarMember * find(const CanonPath & path)
|
||||
|
|
|
|||
|
|
@ -47,12 +47,12 @@ void SourceAccessor::dumpPath(const CanonPath & path, Sink & sink, PathFilter &
|
|||
writePadding(*size, sink);
|
||||
};
|
||||
|
||||
std::function<void(const CanonPath & path)> dump;
|
||||
sink << narVersionMagic1;
|
||||
|
||||
dump = [&](const CanonPath & path) {
|
||||
[&, &this_(*this)](this const auto & dump, const CanonPath & path) -> void {
|
||||
checkInterrupt();
|
||||
|
||||
auto st = lstat(path);
|
||||
auto st = this_.lstat(path);
|
||||
|
||||
sink << "(";
|
||||
|
||||
|
|
@ -69,7 +69,7 @@ void SourceAccessor::dumpPath(const CanonPath & path, Sink & sink, PathFilter &
|
|||
/* If we're on a case-insensitive system like macOS, undo
|
||||
the case hack applied by restorePath(). */
|
||||
StringMap unhacked;
|
||||
for (auto & i : readDirectory(path))
|
||||
for (auto & i : this_.readDirectory(path))
|
||||
if (archiveSettings.useCaseHack) {
|
||||
std::string name(i.first);
|
||||
size_t pos = i.first.find(caseHackSuffix);
|
||||
|
|
@ -92,16 +92,13 @@ void SourceAccessor::dumpPath(const CanonPath & path, Sink & sink, PathFilter &
|
|||
}
|
||||
|
||||
else if (st.type == tSymlink)
|
||||
sink << "type" << "symlink" << "target" << readLink(path);
|
||||
sink << "type" << "symlink" << "target" << this_.readLink(path);
|
||||
|
||||
else
|
||||
throw Error("file '%s' has an unsupported type", path);
|
||||
|
||||
sink << ")";
|
||||
};
|
||||
|
||||
sink << narVersionMagic1;
|
||||
dump(path);
|
||||
}(path);
|
||||
}
|
||||
|
||||
time_t dumpPathAndGetMtime(const Path & path, Sink & sink, PathFilter & filter)
|
||||
|
|
|
|||
|
|
@ -24,11 +24,9 @@ void computeClosure(const set<T> startElts, set<T> & res, GetEdgesAsync<T> getEd
|
|||
|
||||
Sync<State> state_(State{0, res, 0});
|
||||
|
||||
std::function<void(const T &)> enqueue;
|
||||
|
||||
std::condition_variable done;
|
||||
|
||||
enqueue = [&](const T & current) -> void {
|
||||
auto enqueue = [&](this auto & enqueue, const T & current) -> void {
|
||||
{
|
||||
auto state(state_.lock());
|
||||
if (state->exc)
|
||||
|
|
|
|||
|
|
@ -14,9 +14,7 @@ std::vector<T> topoSort(
|
|||
std::vector<T> sorted;
|
||||
decltype(items) visited, parents;
|
||||
|
||||
std::function<void(const T & path, const T * parent)> dfsVisit;
|
||||
|
||||
dfsVisit = [&](const T & path, const T * parent) {
|
||||
auto dfsVisit = [&](this auto & dfsVisit, const T & path, const T * parent) {
|
||||
if (parents.count(path))
|
||||
throw makeCycleError(path, *parent);
|
||||
|
||||
|
|
|
|||
|
|
@ -85,9 +85,7 @@ struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption
|
|||
if (pathExists(*writeTo))
|
||||
throw Error("path '%s' already exists", writeTo->string());
|
||||
|
||||
std::function<void(Value & v, const PosIdx pos, const std::filesystem::path & path)> recurse;
|
||||
|
||||
recurse = [&](Value & v, const PosIdx pos, const std::filesystem::path & path) {
|
||||
[&](this const auto & recurse, Value & v, const PosIdx pos, const std::filesystem::path & path) -> void {
|
||||
state->forceValue(v, pos);
|
||||
if (v.type() == nString)
|
||||
// FIXME: disallow strings with contexts?
|
||||
|
|
@ -111,9 +109,7 @@ struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption
|
|||
} else
|
||||
state->error<TypeError>("value at '%s' is not a string or an attribute set", state->positions[pos])
|
||||
.debugThrow();
|
||||
};
|
||||
|
||||
recurse(*v, pos, *writeTo);
|
||||
}(*v, pos, *writeTo);
|
||||
}
|
||||
|
||||
else if (raw) {
|
||||
|
|
|
|||
|
|
@ -38,8 +38,7 @@ struct CmdFlakePrefetchInputs : FlakeCommand
|
|||
|
||||
std::atomic<size_t> nrFailed{0};
|
||||
|
||||
std::function<void(const Node & node)> visit;
|
||||
visit = [&](const Node & node) {
|
||||
auto visit = [&](this const auto & visit, const Node & node) {
|
||||
if (!state_.lock()->done.insert(&node).second)
|
||||
return;
|
||||
|
||||
|
|
|
|||
|
|
@ -267,11 +267,9 @@ struct CmdFlakeMetadata : FlakeCommand, MixJSON
|
|||
if (!lockedFlake.lockFile.root->inputs.empty())
|
||||
logger->cout(ANSI_BOLD "Inputs:" ANSI_NORMAL);
|
||||
|
||||
std::set<ref<Node>> visited;
|
||||
std::set<ref<Node>> visited{lockedFlake.lockFile.root};
|
||||
|
||||
std::function<void(const Node & node, const std::string & prefix)> recurse;
|
||||
|
||||
recurse = [&](const Node & node, const std::string & prefix) {
|
||||
[&](this const auto & recurse, const Node & node, const std::string & prefix) -> void {
|
||||
for (const auto & [i, input] : enumerate(node.inputs)) {
|
||||
bool last = i + 1 == node.inputs.size();
|
||||
|
||||
|
|
@ -298,10 +296,7 @@ struct CmdFlakeMetadata : FlakeCommand, MixJSON
|
|||
printInputAttrPath(*follows));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
visited.insert(lockedFlake.lockFile.root);
|
||||
recurse(*lockedFlake.lockFile.root, "");
|
||||
}(*lockedFlake.lockFile.root, "");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -884,8 +879,7 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
|
|||
std::vector<std::filesystem::path> changedFiles;
|
||||
std::vector<std::filesystem::path> conflictedFiles;
|
||||
|
||||
std::function<void(const SourcePath & from, const std::filesystem::path & to)> copyDir;
|
||||
copyDir = [&](const SourcePath & from, const std::filesystem::path & to) {
|
||||
[&](this const auto & copyDir, const SourcePath & from, const std::filesystem::path & to) -> void {
|
||||
createDirs(to);
|
||||
|
||||
for (auto & [name, entry] : from.readDirectory()) {
|
||||
|
|
@ -935,9 +929,7 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
|
|||
changedFiles.push_back(to2);
|
||||
notice("wrote: %s", to2);
|
||||
}
|
||||
};
|
||||
|
||||
copyDir(templateDir, flakeDir);
|
||||
}(templateDir, flakeDir);
|
||||
|
||||
if (!changedFiles.empty() && std::filesystem::exists(std::filesystem::path{flakeDir} / ".git")) {
|
||||
Strings args = {"-C", flakeDir, "add", "--intent-to-add", "--force", "--"};
|
||||
|
|
|
|||
|
|
@ -410,8 +410,7 @@ static void main_nix_build(int argc, char ** argv)
|
|||
Value vRoot;
|
||||
state->eval(e, vRoot);
|
||||
|
||||
std::function<bool(const Value & v)> takesNixShellAttr;
|
||||
takesNixShellAttr = [&](const Value & v) {
|
||||
auto takesNixShellAttr = [&](const Value & v) {
|
||||
if (!isNixShell) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -490,10 +489,9 @@ static void main_nix_build(int argc, char ** argv)
|
|||
}
|
||||
}
|
||||
|
||||
std::function<void(ref<SingleDerivedPath>, const DerivedPathMap<StringSet>::ChildNode &)> accumDerivedPath;
|
||||
|
||||
accumDerivedPath = [&](ref<SingleDerivedPath> inputDrv,
|
||||
const DerivedPathMap<StringSet>::ChildNode & inputNode) {
|
||||
auto accumDerivedPath = [&](this auto & self,
|
||||
ref<SingleDerivedPath> inputDrv,
|
||||
const DerivedPathMap<StringSet>::ChildNode & inputNode) -> void {
|
||||
if (!inputNode.value.empty())
|
||||
pathsToBuild.push_back(
|
||||
DerivedPath::Built{
|
||||
|
|
@ -501,8 +499,7 @@ static void main_nix_build(int argc, char ** argv)
|
|||
.outputs = OutputsSpec::Names{inputNode.value},
|
||||
});
|
||||
for (const auto & [outputName, childNode] : inputNode.childMap)
|
||||
accumDerivedPath(
|
||||
make_ref<SingleDerivedPath>(SingleDerivedPath::Built{inputDrv, outputName}), childNode);
|
||||
self(make_ref<SingleDerivedPath>(SingleDerivedPath::Built{inputDrv, outputName}), childNode);
|
||||
};
|
||||
|
||||
// Build or fetch all dependencies of the derivation.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue