primops/libexpr: use new attr-call extractor everywhere; use function's pos if attr-set pos == noPos

This commit is contained in:
Maximilian Bosch 2021-04-11 13:55:07 +02:00
parent f60473a077
commit ad7b47dc85
No known key found for this signature in database
GPG key ID: 091DBF4D1FC46B8E

View file

@ -556,15 +556,29 @@ static Bindings::iterator extractAttrNameFromPrimopCall(
) { ) {
Bindings::iterator value = attrSet->find(state.symbols.create(attrName)); Bindings::iterator value = attrSet->find(state.symbols.create(attrName));
if (value == attrSet->end()) { if (value == attrSet->end()) {
auto e = TypeError({ hintformat errorMsg = hintfmt(
.msg = hintfmt("attribute '%s' missing for call to '%s'", attrName, funcName), "attribute '%s' missing for call to '%s'",
.errPos = *attrSet->pos, attrName,
}); funcName
);
// Adding another trace for the function name to make it clear Pos aPos = *attrSet->pos;
// which call received wrong arguments. if (aPos == noPos) {
e.addTrace(pos, hintfmt("while invoking '%s'", funcName)); throw TypeError({
throw e; .msg = errorMsg,
.errPos = pos,
});
} else {
auto e = TypeError({
.msg = errorMsg,
.errPos = aPos,
});
// Adding another trace for the function name to make it clear
// which call received wrong arguments.
e.addTrace(pos, hintfmt("while invoking '%s'", funcName));
throw e;
}
} }
return value; return value;
@ -841,12 +855,14 @@ static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * *
state.forceAttrs(*args[0], pos); state.forceAttrs(*args[0], pos);
/* Figure out the name first (for stack backtraces). */ /* Figure out the name first (for stack backtraces). */
Bindings::iterator attr = args[0]->attrs->find(state.sName); Bindings::iterator attr = extractAttrNameFromPrimopCall(
if (attr == args[0]->attrs->end()) state,
throw EvalError({ "derivationStrict",
.msg = hintfmt("required attribute 'name' missing"), state.sName,
.errPos = pos args[0]->attrs,
}); pos
);
string drvName; string drvName;
Pos & posDrvName(*attr->pos); Pos & posDrvName(*attr->pos);
try { try {
@ -1394,12 +1410,13 @@ static void prim_findFile(EvalState & state, const Pos & pos, Value * * args, Va
if (i != v2.attrs->end()) if (i != v2.attrs->end())
prefix = state.forceStringNoCtx(*i->value, pos); prefix = state.forceStringNoCtx(*i->value, pos);
i = v2.attrs->find(state.symbols.create("path")); i = extractAttrNameFromPrimopCall(
if (i == v2.attrs->end()) state,
throw EvalError({ "findFile",
.msg = hintfmt("attribute 'path' missing"), "path",
.errPos = pos v2.attrs,
}); pos
);
PathSet context; PathSet context;
string path = state.coerceToString(pos, *i->value, context, false, false); string path = state.coerceToString(pos, *i->value, context, false, false);
@ -2041,12 +2058,13 @@ void prim_getAttr(EvalState & state, const Pos & pos, Value * * args, Value & v)
string attr = state.forceStringNoCtx(*args[0], pos); string attr = state.forceStringNoCtx(*args[0], pos);
state.forceAttrs(*args[1], pos); state.forceAttrs(*args[1], pos);
// !!! Should we create a symbol here or just do a lookup? // !!! Should we create a symbol here or just do a lookup?
Bindings::iterator i = args[1]->attrs->find(state.symbols.create(attr)); Bindings::iterator i = extractAttrNameFromPrimopCall(
if (i == args[1]->attrs->end()) state,
throw EvalError({ "getAttr",
.msg = hintfmt("attribute '%1%' missing", attr), attr,
.errPos = pos args[1]->attrs,
}); pos
);
// !!! add to stack trace? // !!! add to stack trace?
if (state.countCalls && i->pos) state.attrSelects[*i->pos]++; if (state.countCalls && i->pos) state.attrSelects[*i->pos]++;
state.forceValue(*i->value, pos); state.forceValue(*i->value, pos);