refactor(3p/nix/libexpr): Make nix::AttrName a std::variant

nix:AttrName was one of the few classes that relied on the default
constructor of nix::Symbol (which I am trying to remove in a separate
change).

The class essentially represents the name of an attribute in a set,
which is either just a string expression or a dynamically evaluated
expression (e.g. string interpolation).

Previously it would be constructed by only setting one of the fields
and defaulting the other, now it is an explicit std::variant.

Note that there are several code paths where not all eventualities are
handled and this code is bug-for-bug compatible with those, except
that unknown conditions (which should never work) are now throwing
instead of silently doing ... something.

The language tests pass with this change, and the depot derivations
that I tested with evaluated successfully.

Change-Id: Icf1ee60a5f8308f4ab18a82749e00cf37a938a8f
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1138
Reviewed-by: edef <edef@edef.eu>
Reviewed-by: glittershark <grfn@gws.fyi>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2020-07-13 20:52:35 +01:00 committed by tazjin
parent 693661fe87
commit f3165f48aa
7 changed files with 67 additions and 53 deletions

View file

@ -6,6 +6,7 @@
#include <fstream>
#include <iostream>
#include <new>
#include <variant>
#include <absl/strings/match.h>
#include <gc/gc.h>
@ -24,6 +25,7 @@
#include "libutil/hash.hh"
#include "libutil/json.hh"
#include "libutil/util.hh"
#include "libutil/visitor.hh"
namespace nix {
@ -185,13 +187,15 @@ static void* oomHandler(size_t requested) {
#endif
static Symbol getName(const AttrName& name, EvalState& state, Env& env) {
if (name.symbol.set()) {
return name.symbol;
}
Value nameValue;
name.expr->eval(state, env, nameValue);
state.forceStringNoCtx(nameValue);
return state.symbols.Create(nameValue.string.s);
return std::visit(
util::overloaded{[&](const Symbol& name) -> Symbol { return name; },
[&](Expr* expr) -> Symbol {
Value nameValue;
expr->eval(state, env, nameValue);
state.forceStringNoCtx(nameValue);
return state.symbols.Create(nameValue.string.s);
}},
name);
}
static bool gcInitialised = false;
@ -890,12 +894,7 @@ static std::string showAttrPath(EvalState& state, Env& env,
} else {
first = false;
}
try {
out << getName(i, state, env);
} catch (Error& e) {
assert(!i.symbol.set());
out << "\"${" << *i.expr << "}\"";
}
out << getName(i, state, env);
}
return out.str();
}