1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-09 03:56:01 +01:00

Merge pull request #13991 from xokdvium/bindings-remove-find

libexpr: Remove Bindings::find
This commit is contained in:
Sergei Zimmerman 2025-09-14 21:32:31 +00:00 committed by GitHub
commit 2b0fd88324
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 36 additions and 48 deletions

View file

@ -582,7 +582,7 @@ std::optional<EvalState::Doc> EvalState::getDoc(Value & v)
} }
if (isFunctor(v)) { if (isFunctor(v)) {
try { try {
Value & functor = *v.attrs()->find(s.functor)->value; Value & functor = *v.attrs()->get(s.functor)->value;
Value * vp[] = {&v}; Value * vp[] = {&v};
Value partiallyApplied; Value partiallyApplied;
// The first parameter is not user-provided, and may be // The first parameter is not user-provided, and may be
@ -1709,8 +1709,8 @@ void EvalState::autoCallFunction(const Bindings & args, Value & fun, Value & res
forceValue(fun, pos); forceValue(fun, pos);
if (fun.type() == nAttrs) { if (fun.type() == nAttrs) {
auto found = fun.attrs()->find(s.functor); auto found = fun.attrs()->get(s.functor);
if (found != fun.attrs()->end()) { if (found) {
Value * v = allocValue(); Value * v = allocValue();
callFunction(*found->value, fun, *v, pos); callFunction(*found->value, fun, *v, pos);
forceValue(*v, pos); forceValue(*v, pos);
@ -2160,10 +2160,10 @@ bool EvalState::forceBool(Value & v, const PosIdx pos, std::string_view errorCtx
return v.boolean(); return v.boolean();
} }
Bindings::const_iterator EvalState::getAttr(Symbol attrSym, const Bindings * attrSet, std::string_view errorCtx) const Attr * EvalState::getAttr(Symbol attrSym, const Bindings * attrSet, std::string_view errorCtx)
{ {
auto value = attrSet->find(attrSym); auto value = attrSet->get(attrSym);
if (value == attrSet->end()) { if (!value) {
error<TypeError>("attribute '%s' missing", symbols[attrSym]).withTrace(noPos, errorCtx).debugThrow(); error<TypeError>("attribute '%s' missing", symbols[attrSym]).withTrace(noPos, errorCtx).debugThrow();
} }
return value; return value;
@ -2171,7 +2171,7 @@ Bindings::const_iterator EvalState::getAttr(Symbol attrSym, const Bindings * att
bool EvalState::isFunctor(const Value & fun) const bool EvalState::isFunctor(const Value & fun) const
{ {
return fun.type() == nAttrs && fun.attrs()->find(s.functor) != fun.attrs()->end(); return fun.type() == nAttrs && fun.attrs()->get(s.functor);
} }
void EvalState::forceFunction(Value & v, const PosIdx pos, std::string_view errorCtx) void EvalState::forceFunction(Value & v, const PosIdx pos, std::string_view errorCtx)
@ -2252,8 +2252,8 @@ bool EvalState::isDerivation(Value & v)
std::optional<std::string> std::optional<std::string>
EvalState::tryAttrsToString(const PosIdx pos, Value & v, NixStringContext & context, bool coerceMore, bool copyToStore) EvalState::tryAttrsToString(const PosIdx pos, Value & v, NixStringContext & context, bool coerceMore, bool copyToStore)
{ {
auto i = v.attrs()->find(s.toString); auto i = v.attrs()->get(s.toString);
if (i != v.attrs()->end()) { if (i) {
Value v1; Value v1;
callFunction(*i->value, v, v1, pos); callFunction(*i->value, v, v1, pos);
return coerceToString( return coerceToString(
@ -2298,8 +2298,8 @@ BackedStringView EvalState::coerceToString(
auto maybeString = tryAttrsToString(pos, v, context, coerceMore, copyToStore); auto maybeString = tryAttrsToString(pos, v, context, coerceMore, copyToStore);
if (maybeString) if (maybeString)
return std::move(*maybeString); return std::move(*maybeString);
auto i = v.attrs()->find(s.outPath); auto i = v.attrs()->get(s.outPath);
if (i == v.attrs()->end()) { if (!i) {
error<TypeError>( error<TypeError>(
"cannot coerce %1% to a string: %2%", showType(v), ValuePrinter(*this, v, errorPrintOptions)) "cannot coerce %1% to a string: %2%", showType(v), ValuePrinter(*this, v, errorPrintOptions))
.withTrace(pos, errorCtx) .withTrace(pos, errorCtx)
@ -2403,8 +2403,8 @@ SourcePath EvalState::coerceToPath(const PosIdx pos, Value & v, NixStringContext
/* Similarly, handle __toString where the result may be a path /* Similarly, handle __toString where the result may be a path
value. */ value. */
if (v.type() == nAttrs) { if (v.type() == nAttrs) {
auto i = v.attrs()->find(s.toString); auto i = v.attrs()->get(s.toString);
if (i != v.attrs()->end()) { if (i) {
Value v1; Value v1;
callFunction(*i->value, v, v1, pos); callFunction(*i->value, v, v1, pos);
return coerceToPath(pos, v1, context, errorCtx); return coerceToPath(pos, v1, context, errorCtx);

View file

@ -45,8 +45,8 @@ PackageInfo::PackageInfo(EvalState & state, ref<Store> store, const std::string
std::string PackageInfo::queryName() const std::string PackageInfo::queryName() const
{ {
if (name == "" && attrs) { if (name == "" && attrs) {
auto i = attrs->find(state->s.name); auto i = attrs->get(state->s.name);
if (i == attrs->end()) if (!i)
state->error<TypeError>("derivation name missing").debugThrow(); state->error<TypeError>("derivation name missing").debugThrow();
name = state->forceStringNoCtx(*i->value, noPos, "while evaluating the 'name' attribute of a derivation"); name = state->forceStringNoCtx(*i->value, noPos, "while evaluating the 'name' attribute of a derivation");
} }
@ -56,10 +56,9 @@ std::string PackageInfo::queryName() const
std::string PackageInfo::querySystem() const std::string PackageInfo::querySystem() const
{ {
if (system == "" && attrs) { if (system == "" && attrs) {
auto i = attrs->find(state->s.system); auto i = attrs->get(state->s.system);
system = system =
i == attrs->end() !i ? "unknown"
? "unknown"
: state->forceStringNoCtx(*i->value, i->pos, "while evaluating the 'system' attribute of a derivation"); : state->forceStringNoCtx(*i->value, i->pos, "while evaluating the 'system' attribute of a derivation");
} }
return system; return system;
@ -95,9 +94,9 @@ StorePath PackageInfo::requireDrvPath() const
StorePath PackageInfo::queryOutPath() const StorePath PackageInfo::queryOutPath() const
{ {
if (!outPath && attrs) { if (!outPath && attrs) {
auto i = attrs->find(state->s.outPath); auto i = attrs->get(state->s.outPath);
NixStringContext context; NixStringContext context;
if (i != attrs->end()) if (i)
outPath = state->coerceToStorePath( outPath = state->coerceToStorePath(
i->pos, *i->value, context, "while evaluating the output path of a derivation"); i->pos, *i->value, context, "while evaluating the output path of a derivation");
} }

View file

@ -137,17 +137,6 @@ public:
attrs[size_++] = attr; attrs[size_++] = attr;
} }
const_iterator find(Symbol name) const
{
Attr key(name, 0);
auto first = attrs;
auto last = attrs + size_;
const Attr * i = std::lower_bound(first, last, key);
if (i != last && i->name == name)
return const_iterator{i};
return end();
}
const Attr * get(Symbol name) const const Attr * get(Symbol name) const
{ {
Attr key(name, 0); Attr key(name, 0);

View file

@ -613,7 +613,7 @@ public:
/** /**
* Get attribute from an attribute set and throw an error if it doesn't exist. * Get attribute from an attribute set and throw an error if it doesn't exist.
*/ */
Bindings::const_iterator getAttr(Symbol attrSym, const Bindings * attrSet, std::string_view errorCtx); const Attr * getAttr(Symbol attrSym, const Bindings * attrSet, std::string_view errorCtx);
template<typename... Args> template<typename... Args>
[[gnu::noinline]] [[gnu::noinline]]

View file

@ -1367,8 +1367,8 @@ static void derivationStrictInternal(EvalState & state, std::string_view drvName
using nlohmann::json; using nlohmann::json;
std::optional<StructuredAttrs> jsonObject; std::optional<StructuredAttrs> jsonObject;
auto pos = v.determinePos(noPos); auto pos = v.determinePos(noPos);
auto attr = attrs->find(state.s.structuredAttrs); auto attr = attrs->get(state.s.structuredAttrs);
if (attr != attrs->end() if (attr
&& state.forceBool( && state.forceBool(
*attr->value, *attr->value,
pos, pos,
@ -1378,8 +1378,8 @@ static void derivationStrictInternal(EvalState & state, std::string_view drvName
/* Check whether null attributes should be ignored. */ /* Check whether null attributes should be ignored. */
bool ignoreNulls = false; bool ignoreNulls = false;
attr = attrs->find(state.s.ignoreNulls); attr = attrs->get(state.s.ignoreNulls);
if (attr != attrs->end()) if (attr)
ignoreNulls = state.forceBool( ignoreNulls = state.forceBool(
*attr->value, *attr->value,
pos, pos,
@ -2040,8 +2040,8 @@ static void prim_findFile(EvalState & state, const PosIdx pos, Value ** args, Va
state.forceAttrs(*v2, pos, "while evaluating an element of the list passed to builtins.findFile"); state.forceAttrs(*v2, pos, "while evaluating an element of the list passed to builtins.findFile");
std::string prefix; std::string prefix;
auto i = v2->attrs()->find(state.s.prefix); auto i = v2->attrs()->get(state.s.prefix);
if (i != v2->attrs()->end()) if (i)
prefix = state.forceStringNoCtx( prefix = state.forceStringNoCtx(
*i->value, *i->value,
pos, pos,
@ -3008,8 +3008,8 @@ static void prim_unsafeGetAttrPos(EvalState & state, const PosIdx pos, Value **
auto attr = state.forceStringNoCtx( auto attr = state.forceStringNoCtx(
*args[0], pos, "while evaluating the first argument passed to builtins.unsafeGetAttrPos"); *args[0], pos, "while evaluating the first argument passed to builtins.unsafeGetAttrPos");
state.forceAttrs(*args[1], pos, "while evaluating the second argument passed to builtins.unsafeGetAttrPos"); state.forceAttrs(*args[1], pos, "while evaluating the second argument passed to builtins.unsafeGetAttrPos");
auto i = args[1]->attrs()->find(state.symbols.create(attr)); auto i = args[1]->attrs()->get(state.symbols.create(attr));
if (i == args[1]->attrs()->end()) if (!i)
v.mkNull(); v.mkNull();
else else
state.mkPos(v, i->pos); state.mkPos(v, i->pos);
@ -3076,7 +3076,7 @@ static void prim_hasAttr(EvalState & state, const PosIdx pos, Value ** args, Val
{ {
auto attr = state.forceStringNoCtx(*args[0], pos, "while evaluating the first argument passed to builtins.hasAttr"); auto attr = state.forceStringNoCtx(*args[0], pos, "while evaluating the first argument passed to builtins.hasAttr");
state.forceAttrs(*args[1], pos, "while evaluating the second argument passed to builtins.hasAttr"); state.forceAttrs(*args[1], pos, "while evaluating the second argument passed to builtins.hasAttr");
v.mkBool(args[1]->attrs()->find(state.symbols.create(attr)) != args[1]->attrs()->end()); v.mkBool(args[1]->attrs()->get(state.symbols.create(attr)));
} }
static RegisterPrimOp primop_hasAttr({ static RegisterPrimOp primop_hasAttr({
@ -3286,14 +3286,14 @@ static void prim_intersectAttrs(EvalState & state, const PosIdx pos, Value ** ar
if (left.size() < right.size()) { if (left.size() < right.size()) {
for (auto & l : left) { for (auto & l : left) {
auto r = right.find(l.name); auto r = right.get(l.name);
if (r != right.end()) if (r)
attrs.insert(*r); attrs.insert(*r);
} }
} else { } else {
for (auto & r : right) { for (auto & r : right) {
auto l = left.find(r.name); auto l = left.get(r.name);
if (l != left.end()) if (l)
attrs.insert(r); attrs.insert(r);
} }
} }

View file

@ -141,10 +141,10 @@ bool createUserEnv(
debug("evaluating user environment builder"); debug("evaluating user environment builder");
state.forceValue(topLevel, topLevel.determinePos(noPos)); state.forceValue(topLevel, topLevel.determinePos(noPos));
NixStringContext context; NixStringContext context;
auto & aDrvPath(*topLevel.attrs()->find(state.s.drvPath)); auto & aDrvPath(*topLevel.attrs()->get(state.s.drvPath));
auto topLevelDrv = state.coerceToStorePath(aDrvPath.pos, *aDrvPath.value, context, ""); auto topLevelDrv = state.coerceToStorePath(aDrvPath.pos, *aDrvPath.value, context, "");
topLevelDrv.requireDerivation(); topLevelDrv.requireDerivation();
auto & aOutPath(*topLevel.attrs()->find(state.s.outPath)); auto & aOutPath(*topLevel.attrs()->get(state.s.outPath));
auto topLevelOut = state.coerceToStorePath(aOutPath.pos, *aOutPath.value, context, ""); auto topLevelOut = state.coerceToStorePath(aOutPath.pos, *aOutPath.value, context, "");
/* Realise the resulting store expression. */ /* Realise the resulting store expression. */