Skip to content

Commit 2b0fd88

Browse files
authored
Merge pull request #13991 from xokdvium/bindings-remove-find
libexpr: Remove Bindings::find
2 parents ffc14ac + d830840 commit 2b0fd88

File tree

6 files changed

+36
-48
lines changed

6 files changed

+36
-48
lines changed

src/libexpr/eval.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ std::optional<EvalState::Doc> EvalState::getDoc(Value & v)
582582
}
583583
if (isFunctor(v)) {
584584
try {
585-
Value & functor = *v.attrs()->find(s.functor)->value;
585+
Value & functor = *v.attrs()->get(s.functor)->value;
586586
Value * vp[] = {&v};
587587
Value partiallyApplied;
588588
// The first parameter is not user-provided, and may be
@@ -1709,8 +1709,8 @@ void EvalState::autoCallFunction(const Bindings & args, Value & fun, Value & res
17091709
forceValue(fun, pos);
17101710

17111711
if (fun.type() == nAttrs) {
1712-
auto found = fun.attrs()->find(s.functor);
1713-
if (found != fun.attrs()->end()) {
1712+
auto found = fun.attrs()->get(s.functor);
1713+
if (found) {
17141714
Value * v = allocValue();
17151715
callFunction(*found->value, fun, *v, pos);
17161716
forceValue(*v, pos);
@@ -2160,18 +2160,18 @@ bool EvalState::forceBool(Value & v, const PosIdx pos, std::string_view errorCtx
21602160
return v.boolean();
21612161
}
21622162

2163-
Bindings::const_iterator EvalState::getAttr(Symbol attrSym, const Bindings * attrSet, std::string_view errorCtx)
2163+
const Attr * EvalState::getAttr(Symbol attrSym, const Bindings * attrSet, std::string_view errorCtx)
21642164
{
2165-
auto value = attrSet->find(attrSym);
2166-
if (value == attrSet->end()) {
2165+
auto value = attrSet->get(attrSym);
2166+
if (!value) {
21672167
error<TypeError>("attribute '%s' missing", symbols[attrSym]).withTrace(noPos, errorCtx).debugThrow();
21682168
}
21692169
return value;
21702170
}
21712171

21722172
bool EvalState::isFunctor(const Value & fun) const
21732173
{
2174-
return fun.type() == nAttrs && fun.attrs()->find(s.functor) != fun.attrs()->end();
2174+
return fun.type() == nAttrs && fun.attrs()->get(s.functor);
21752175
}
21762176

21772177
void EvalState::forceFunction(Value & v, const PosIdx pos, std::string_view errorCtx)
@@ -2252,8 +2252,8 @@ bool EvalState::isDerivation(Value & v)
22522252
std::optional<std::string>
22532253
EvalState::tryAttrsToString(const PosIdx pos, Value & v, NixStringContext & context, bool coerceMore, bool copyToStore)
22542254
{
2255-
auto i = v.attrs()->find(s.toString);
2256-
if (i != v.attrs()->end()) {
2255+
auto i = v.attrs()->get(s.toString);
2256+
if (i) {
22572257
Value v1;
22582258
callFunction(*i->value, v, v1, pos);
22592259
return coerceToString(
@@ -2298,8 +2298,8 @@ BackedStringView EvalState::coerceToString(
22982298
auto maybeString = tryAttrsToString(pos, v, context, coerceMore, copyToStore);
22992299
if (maybeString)
23002300
return std::move(*maybeString);
2301-
auto i = v.attrs()->find(s.outPath);
2302-
if (i == v.attrs()->end()) {
2301+
auto i = v.attrs()->get(s.outPath);
2302+
if (!i) {
23032303
error<TypeError>(
23042304
"cannot coerce %1% to a string: %2%", showType(v), ValuePrinter(*this, v, errorPrintOptions))
23052305
.withTrace(pos, errorCtx)
@@ -2403,8 +2403,8 @@ SourcePath EvalState::coerceToPath(const PosIdx pos, Value & v, NixStringContext
24032403
/* Similarly, handle __toString where the result may be a path
24042404
value. */
24052405
if (v.type() == nAttrs) {
2406-
auto i = v.attrs()->find(s.toString);
2407-
if (i != v.attrs()->end()) {
2406+
auto i = v.attrs()->get(s.toString);
2407+
if (i) {
24082408
Value v1;
24092409
callFunction(*i->value, v, v1, pos);
24102410
return coerceToPath(pos, v1, context, errorCtx);

src/libexpr/get-drvs.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ PackageInfo::PackageInfo(EvalState & state, ref<Store> store, const std::string
4545
std::string PackageInfo::queryName() const
4646
{
4747
if (name == "" && attrs) {
48-
auto i = attrs->find(state->s.name);
49-
if (i == attrs->end())
48+
auto i = attrs->get(state->s.name);
49+
if (!i)
5050
state->error<TypeError>("derivation name missing").debugThrow();
5151
name = state->forceStringNoCtx(*i->value, noPos, "while evaluating the 'name' attribute of a derivation");
5252
}
@@ -56,11 +56,10 @@ std::string PackageInfo::queryName() const
5656
std::string PackageInfo::querySystem() const
5757
{
5858
if (system == "" && attrs) {
59-
auto i = attrs->find(state->s.system);
59+
auto i = attrs->get(state->s.system);
6060
system =
61-
i == attrs->end()
62-
? "unknown"
63-
: state->forceStringNoCtx(*i->value, i->pos, "while evaluating the 'system' attribute of a derivation");
61+
!i ? "unknown"
62+
: state->forceStringNoCtx(*i->value, i->pos, "while evaluating the 'system' attribute of a derivation");
6463
}
6564
return system;
6665
}
@@ -95,9 +94,9 @@ StorePath PackageInfo::requireDrvPath() const
9594
StorePath PackageInfo::queryOutPath() const
9695
{
9796
if (!outPath && attrs) {
98-
auto i = attrs->find(state->s.outPath);
97+
auto i = attrs->get(state->s.outPath);
9998
NixStringContext context;
100-
if (i != attrs->end())
99+
if (i)
101100
outPath = state->coerceToStorePath(
102101
i->pos, *i->value, context, "while evaluating the output path of a derivation");
103102
}

src/libexpr/include/nix/expr/attr-set.hh

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,6 @@ public:
137137
attrs[size_++] = attr;
138138
}
139139

140-
const_iterator find(Symbol name) const
141-
{
142-
Attr key(name, 0);
143-
auto first = attrs;
144-
auto last = attrs + size_;
145-
const Attr * i = std::lower_bound(first, last, key);
146-
if (i != last && i->name == name)
147-
return const_iterator{i};
148-
return end();
149-
}
150-
151140
const Attr * get(Symbol name) const
152141
{
153142
Attr key(name, 0);

src/libexpr/include/nix/expr/eval.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ public:
613613
/**
614614
* Get attribute from an attribute set and throw an error if it doesn't exist.
615615
*/
616-
Bindings::const_iterator getAttr(Symbol attrSym, const Bindings * attrSet, std::string_view errorCtx);
616+
const Attr * getAttr(Symbol attrSym, const Bindings * attrSet, std::string_view errorCtx);
617617

618618
template<typename... Args>
619619
[[gnu::noinline]]

src/libexpr/primops.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,8 +1367,8 @@ static void derivationStrictInternal(EvalState & state, std::string_view drvName
13671367
using nlohmann::json;
13681368
std::optional<StructuredAttrs> jsonObject;
13691369
auto pos = v.determinePos(noPos);
1370-
auto attr = attrs->find(state.s.structuredAttrs);
1371-
if (attr != attrs->end()
1370+
auto attr = attrs->get(state.s.structuredAttrs);
1371+
if (attr
13721372
&& state.forceBool(
13731373
*attr->value,
13741374
pos,
@@ -1378,8 +1378,8 @@ static void derivationStrictInternal(EvalState & state, std::string_view drvName
13781378

13791379
/* Check whether null attributes should be ignored. */
13801380
bool ignoreNulls = false;
1381-
attr = attrs->find(state.s.ignoreNulls);
1382-
if (attr != attrs->end())
1381+
attr = attrs->get(state.s.ignoreNulls);
1382+
if (attr)
13831383
ignoreNulls = state.forceBool(
13841384
*attr->value,
13851385
pos,
@@ -2040,8 +2040,8 @@ static void prim_findFile(EvalState & state, const PosIdx pos, Value ** args, Va
20402040
state.forceAttrs(*v2, pos, "while evaluating an element of the list passed to builtins.findFile");
20412041

20422042
std::string prefix;
2043-
auto i = v2->attrs()->find(state.s.prefix);
2044-
if (i != v2->attrs()->end())
2043+
auto i = v2->attrs()->get(state.s.prefix);
2044+
if (i)
20452045
prefix = state.forceStringNoCtx(
20462046
*i->value,
20472047
pos,
@@ -3008,8 +3008,8 @@ static void prim_unsafeGetAttrPos(EvalState & state, const PosIdx pos, Value **
30083008
auto attr = state.forceStringNoCtx(
30093009
*args[0], pos, "while evaluating the first argument passed to builtins.unsafeGetAttrPos");
30103010
state.forceAttrs(*args[1], pos, "while evaluating the second argument passed to builtins.unsafeGetAttrPos");
3011-
auto i = args[1]->attrs()->find(state.symbols.create(attr));
3012-
if (i == args[1]->attrs()->end())
3011+
auto i = args[1]->attrs()->get(state.symbols.create(attr));
3012+
if (!i)
30133013
v.mkNull();
30143014
else
30153015
state.mkPos(v, i->pos);
@@ -3076,7 +3076,7 @@ static void prim_hasAttr(EvalState & state, const PosIdx pos, Value ** args, Val
30763076
{
30773077
auto attr = state.forceStringNoCtx(*args[0], pos, "while evaluating the first argument passed to builtins.hasAttr");
30783078
state.forceAttrs(*args[1], pos, "while evaluating the second argument passed to builtins.hasAttr");
3079-
v.mkBool(args[1]->attrs()->find(state.symbols.create(attr)) != args[1]->attrs()->end());
3079+
v.mkBool(args[1]->attrs()->get(state.symbols.create(attr)));
30803080
}
30813081

30823082
static RegisterPrimOp primop_hasAttr({
@@ -3286,14 +3286,14 @@ static void prim_intersectAttrs(EvalState & state, const PosIdx pos, Value ** ar
32863286

32873287
if (left.size() < right.size()) {
32883288
for (auto & l : left) {
3289-
auto r = right.find(l.name);
3290-
if (r != right.end())
3289+
auto r = right.get(l.name);
3290+
if (r)
32913291
attrs.insert(*r);
32923292
}
32933293
} else {
32943294
for (auto & r : right) {
3295-
auto l = left.find(r.name);
3296-
if (l != left.end())
3295+
auto l = left.get(r.name);
3296+
if (l)
32973297
attrs.insert(r);
32983298
}
32993299
}

src/nix/nix-env/user-env.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ bool createUserEnv(
141141
debug("evaluating user environment builder");
142142
state.forceValue(topLevel, topLevel.determinePos(noPos));
143143
NixStringContext context;
144-
auto & aDrvPath(*topLevel.attrs()->find(state.s.drvPath));
144+
auto & aDrvPath(*topLevel.attrs()->get(state.s.drvPath));
145145
auto topLevelDrv = state.coerceToStorePath(aDrvPath.pos, *aDrvPath.value, context, "");
146146
topLevelDrv.requireDerivation();
147-
auto & aOutPath(*topLevel.attrs()->find(state.s.outPath));
147+
auto & aOutPath(*topLevel.attrs()->get(state.s.outPath));
148148
auto topLevelOut = state.coerceToStorePath(aOutPath.pos, *aOutPath.value, context, "");
149149

150150
/* Realise the resulting store expression. */

0 commit comments

Comments
 (0)