diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index f855dc67e..dc8fd4d38 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -582,7 +582,7 @@ std::optional EvalState::getDoc(Value & v) } if (isFunctor(v)) { try { - Value & functor = *v.attrs()->find(s.functor)->value; + Value & functor = *v.attrs()->get(s.functor)->value; Value * vp[] = {&v}; Value partiallyApplied; // 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); if (fun.type() == nAttrs) { - auto found = fun.attrs()->find(s.functor); - if (found != fun.attrs()->end()) { + auto found = fun.attrs()->get(s.functor); + if (found) { Value * v = allocValue(); callFunction(*found->value, fun, *v, pos); forceValue(*v, pos); @@ -2160,10 +2160,10 @@ bool EvalState::forceBool(Value & v, const PosIdx pos, std::string_view errorCtx 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); - if (value == attrSet->end()) { + auto value = attrSet->get(attrSym); + if (!value) { error("attribute '%s' missing", symbols[attrSym]).withTrace(noPos, errorCtx).debugThrow(); } return value; @@ -2171,7 +2171,7 @@ Bindings::const_iterator EvalState::getAttr(Symbol attrSym, const Bindings * att 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) @@ -2252,8 +2252,8 @@ bool EvalState::isDerivation(Value & v) std::optional EvalState::tryAttrsToString(const PosIdx pos, Value & v, NixStringContext & context, bool coerceMore, bool copyToStore) { - auto i = v.attrs()->find(s.toString); - if (i != v.attrs()->end()) { + auto i = v.attrs()->get(s.toString); + if (i) { Value v1; callFunction(*i->value, v, v1, pos); return coerceToString( @@ -2298,8 +2298,8 @@ BackedStringView EvalState::coerceToString( auto maybeString = tryAttrsToString(pos, v, context, coerceMore, copyToStore); if (maybeString) return std::move(*maybeString); - auto i = v.attrs()->find(s.outPath); - if (i == v.attrs()->end()) { + auto i = v.attrs()->get(s.outPath); + if (!i) { error( "cannot coerce %1% to a string: %2%", showType(v), ValuePrinter(*this, v, errorPrintOptions)) .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 value. */ if (v.type() == nAttrs) { - auto i = v.attrs()->find(s.toString); - if (i != v.attrs()->end()) { + auto i = v.attrs()->get(s.toString); + if (i) { Value v1; callFunction(*i->value, v, v1, pos); return coerceToPath(pos, v1, context, errorCtx); diff --git a/src/libexpr/get-drvs.cc b/src/libexpr/get-drvs.cc index 00b673365..5a7281b2b 100644 --- a/src/libexpr/get-drvs.cc +++ b/src/libexpr/get-drvs.cc @@ -45,8 +45,8 @@ PackageInfo::PackageInfo(EvalState & state, ref store, const std::string std::string PackageInfo::queryName() const { if (name == "" && attrs) { - auto i = attrs->find(state->s.name); - if (i == attrs->end()) + auto i = attrs->get(state->s.name); + if (!i) state->error("derivation name missing").debugThrow(); name = state->forceStringNoCtx(*i->value, noPos, "while evaluating the 'name' attribute of a derivation"); } @@ -56,11 +56,10 @@ std::string PackageInfo::queryName() const std::string PackageInfo::querySystem() const { if (system == "" && attrs) { - auto i = attrs->find(state->s.system); + auto i = attrs->get(state->s.system); system = - i == attrs->end() - ? "unknown" - : state->forceStringNoCtx(*i->value, i->pos, "while evaluating the 'system' attribute of a derivation"); + !i ? "unknown" + : state->forceStringNoCtx(*i->value, i->pos, "while evaluating the 'system' attribute of a derivation"); } return system; } @@ -95,9 +94,9 @@ StorePath PackageInfo::requireDrvPath() const StorePath PackageInfo::queryOutPath() const { if (!outPath && attrs) { - auto i = attrs->find(state->s.outPath); + auto i = attrs->get(state->s.outPath); NixStringContext context; - if (i != attrs->end()) + if (i) outPath = state->coerceToStorePath( i->pos, *i->value, context, "while evaluating the output path of a derivation"); } diff --git a/src/libexpr/include/nix/expr/attr-set.hh b/src/libexpr/include/nix/expr/attr-set.hh index 132be163d..8b8edddf4 100644 --- a/src/libexpr/include/nix/expr/attr-set.hh +++ b/src/libexpr/include/nix/expr/attr-set.hh @@ -137,17 +137,6 @@ public: 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 { Attr key(name, 0); diff --git a/src/libexpr/include/nix/expr/eval.hh b/src/libexpr/include/nix/expr/eval.hh index 3639eab15..64f528581 100644 --- a/src/libexpr/include/nix/expr/eval.hh +++ b/src/libexpr/include/nix/expr/eval.hh @@ -613,7 +613,7 @@ public: /** * 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 [[gnu::noinline]] diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 0d5eb23ae..a046a2c28 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1367,8 +1367,8 @@ static void derivationStrictInternal(EvalState & state, std::string_view drvName using nlohmann::json; std::optional jsonObject; auto pos = v.determinePos(noPos); - auto attr = attrs->find(state.s.structuredAttrs); - if (attr != attrs->end() + auto attr = attrs->get(state.s.structuredAttrs); + if (attr && state.forceBool( *attr->value, pos, @@ -1378,8 +1378,8 @@ static void derivationStrictInternal(EvalState & state, std::string_view drvName /* Check whether null attributes should be ignored. */ bool ignoreNulls = false; - attr = attrs->find(state.s.ignoreNulls); - if (attr != attrs->end()) + attr = attrs->get(state.s.ignoreNulls); + if (attr) ignoreNulls = state.forceBool( *attr->value, 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"); std::string prefix; - auto i = v2->attrs()->find(state.s.prefix); - if (i != v2->attrs()->end()) + auto i = v2->attrs()->get(state.s.prefix); + if (i) prefix = state.forceStringNoCtx( *i->value, pos, @@ -3008,8 +3008,8 @@ static void prim_unsafeGetAttrPos(EvalState & state, const PosIdx pos, Value ** auto attr = state.forceStringNoCtx( *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"); - auto i = args[1]->attrs()->find(state.symbols.create(attr)); - if (i == args[1]->attrs()->end()) + auto i = args[1]->attrs()->get(state.symbols.create(attr)); + if (!i) v.mkNull(); else 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"); 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({ @@ -3286,14 +3286,14 @@ static void prim_intersectAttrs(EvalState & state, const PosIdx pos, Value ** ar if (left.size() < right.size()) { for (auto & l : left) { - auto r = right.find(l.name); - if (r != right.end()) + auto r = right.get(l.name); + if (r) attrs.insert(*r); } } else { for (auto & r : right) { - auto l = left.find(r.name); - if (l != left.end()) + auto l = left.get(r.name); + if (l) attrs.insert(r); } } diff --git a/src/nix/nix-env/user-env.cc b/src/nix/nix-env/user-env.cc index 552172825..fbdcb14f8 100644 --- a/src/nix/nix-env/user-env.cc +++ b/src/nix/nix-env/user-env.cc @@ -141,10 +141,10 @@ bool createUserEnv( debug("evaluating user environment builder"); state.forceValue(topLevel, topLevel.determinePos(noPos)); 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, ""); 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, ""); /* Realise the resulting store expression. */