From eaecaaa00ba79b05f49a908f2c96c6507d3a2d7b Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Mon, 14 Mar 2022 11:39:53 -0600 Subject: [PATCH] more debug_throw coverage of EvalErrors --- src/libexpr/eval-cache.cc | 19 +++++++++++-------- src/libexpr/parser.y | 5 +++-- src/libexpr/primops.cc | 4 ++-- src/libexpr/primops/fetchTree.cc | 32 ++++++++++++++++---------------- src/libexpr/value-to-json.cc | 5 +++-- 5 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/libexpr/eval-cache.cc b/src/libexpr/eval-cache.cc index d6b9ea29b..b102684ec 100644 --- a/src/libexpr/eval-cache.cc +++ b/src/libexpr/eval-cache.cc @@ -506,14 +506,14 @@ std::string AttrCursor::getString() debug("using cached string attribute '%s'", getAttrPathStr()); return s->first; } else - throw TypeError("'%s' is not a string", getAttrPathStr()); + root->state.debug_throw(TypeError("'%s' is not a string", getAttrPathStr())); } } auto & v = forceValue(); if (v.type() != nString && v.type() != nPath) - throw TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type())); + root->state.debug_throw(TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type()))); return v.type() == nString ? v.string.s : v.path; } @@ -537,7 +537,7 @@ string_t AttrCursor::getStringWithContext() return *s; } } else - throw TypeError("'%s' is not a string", getAttrPathStr()); + root->state.debug_throw(TypeError("'%s' is not a string", getAttrPathStr())); } } @@ -548,7 +548,10 @@ string_t AttrCursor::getStringWithContext() else if (v.type() == nPath) return {v.path, {}}; else - throw TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type())); + { + root->state.debug_throw(TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type()))); + return {v.path, {}}; // should never execute + } } bool AttrCursor::getBool() @@ -561,14 +564,14 @@ bool AttrCursor::getBool() debug("using cached Boolean attribute '%s'", getAttrPathStr()); return *b; } else - throw TypeError("'%s' is not a Boolean", getAttrPathStr()); + root->state.debug_throw(TypeError("'%s' is not a Boolean", getAttrPathStr())); } } auto & v = forceValue(); if (v.type() != nBool) - throw TypeError("'%s' is not a Boolean", getAttrPathStr()); + root->state.debug_throw(TypeError("'%s' is not a Boolean", getAttrPathStr())); return v.boolean; } @@ -583,14 +586,14 @@ std::vector AttrCursor::getAttrs() debug("using cached attrset attribute '%s'", getAttrPathStr()); return *attrs; } else - throw TypeError("'%s' is not an attribute set", getAttrPathStr()); + root->state.debug_throw(TypeError("'%s' is not an attribute set", getAttrPathStr())); } } auto & v = forceValue(); if (v.type() != nAttrs) - throw TypeError("'%s' is not an attribute set", getAttrPathStr()); + root->state.debug_throw(TypeError("'%s' is not an attribute set", getAttrPathStr())); std::vector attrs; for (auto & attr : *getValue().attrs) diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index d9291e7a2..184fba03e 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -783,13 +783,14 @@ Path EvalState::findFile(SearchPath & searchPath, const std::string_view path, c if (hasPrefix(path, "nix/")) return concatStrings(corepkgsPrefix, path.substr(4)); - throw ThrownError({ + debug_throw(ThrownError({ .msg = hintfmt(evalSettings.pureEval ? "cannot look up '<%s>' in pure evaluation mode (use '--impure' to override)" : "file '%s' was not found in the Nix search path (add it using $NIX_PATH or -I)", path), .errPos = pos - }); + })); + return Path(); // should never execute due to debug_throw above. } diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 80d78e150..4908482cf 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -2542,7 +2542,7 @@ static void prim_zipAttrsWith(EvalState & state, const Pos & pos, Value * * args attrsSeen[attr.name].first++; } catch (TypeError & e) { e.addTrace(pos, hintfmt("while invoking '%s'", "zipAttrsWith")); - throw; + state.debug_throw(e); } } @@ -3127,7 +3127,7 @@ static void prim_concatMap(EvalState & state, const Pos & pos, Value * * args, V state.forceList(lists[n], lists[n].determinePos(args[0]->determinePos(pos))); } catch (TypeError &e) { e.addTrace(pos, hintfmt("while invoking '%s'", "concatMap")); - throw; + state.debug_throw(e); } len += lists[n].listSize(); } diff --git a/src/libexpr/primops/fetchTree.cc b/src/libexpr/primops/fetchTree.cc index d09e2d9e1..c9fcbb8f5 100644 --- a/src/libexpr/primops/fetchTree.cc +++ b/src/libexpr/primops/fetchTree.cc @@ -108,16 +108,16 @@ static void fetchTree( if (auto aType = args[0]->attrs->get(state.sType)) { if (type) - throw Error({ + state.debug_throw(EvalError({ .msg = hintfmt("unexpected attribute 'type'"), .errPos = pos - }); + })); type = state.forceStringNoCtx(*aType->value, *aType->pos); } else if (!type) - throw Error({ + state.debug_throw(EvalError({ .msg = hintfmt("attribute 'type' is missing in call to 'fetchTree'"), .errPos = pos - }); + })); attrs.emplace("type", type.value()); @@ -138,16 +138,16 @@ static void fetchTree( else if (attr.value->type() == nInt) attrs.emplace(attr.name, uint64_t(attr.value->integer)); else - throw TypeError("fetchTree argument '%s' is %s while a string, Boolean or integer is expected", - attr.name, showType(*attr.value)); + state.debug_throw(TypeError("fetchTree argument '%s' is %s while a string, Boolean or integer is expected", + attr.name, showType(*attr.value))); } if (!params.allowNameArgument) if (auto nameIter = attrs.find("name"); nameIter != attrs.end()) - throw Error({ + state.debug_throw(EvalError({ .msg = hintfmt("attribute 'name' isn’t supported in call to 'fetchTree'"), .errPos = pos - }); + })); input = fetchers::Input::fromAttrs(std::move(attrs)); } else { @@ -167,7 +167,7 @@ static void fetchTree( input = lookupInRegistries(state.store, input).first; if (evalSettings.pureEval && !input.isImmutable()) - throw Error("in pure evaluation mode, 'fetchTree' requires an immutable input, at %s", pos); + state.debug_throw(EvalError("in pure evaluation mode, 'fetchTree' requires an immutable input, at %s", pos)); auto [tree, input2] = input.fetch(state.store); @@ -206,17 +206,17 @@ static void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v, else if (n == "name") name = state.forceStringNoCtx(*attr.value, *attr.pos); else - throw EvalError({ + state.debug_throw(EvalError({ .msg = hintfmt("unsupported argument '%s' to '%s'", attr.name, who), .errPos = *attr.pos - }); + })); } if (!url) - throw EvalError({ + state.debug_throw(EvalError({ .msg = hintfmt("'url' argument required"), .errPos = pos - }); + })); } else url = state.forceStringNoCtx(*args[0], pos); @@ -228,7 +228,7 @@ static void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v, name = baseNameOf(*url); if (evalSettings.pureEval && !expectedHash) - throw Error("in pure evaluation mode, '%s' requires a 'sha256' argument", who); + state.debug_throw(EvalError("in pure evaluation mode, '%s' requires a 'sha256' argument", who)); auto storePath = unpack @@ -240,8 +240,8 @@ static void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v, ? state.store->queryPathInfo(storePath)->narHash : hashFile(htSHA256, state.store->toRealPath(storePath)); if (hash != *expectedHash) - throw Error((unsigned int) 102, "hash mismatch in file downloaded from '%s':\n specified: %s\n got: %s", - *url, expectedHash->to_string(Base32, true), hash.to_string(Base32, true)); + state.debug_throw(EvalError((unsigned int) 102, "hash mismatch in file downloaded from '%s':\n specified: %s\n got: %s", + *url, expectedHash->to_string(Base32, true), hash.to_string(Base32, true))); } state.allowPath(storePath); diff --git a/src/libexpr/value-to-json.cc b/src/libexpr/value-to-json.cc index 517da4c01..d8bf73cd5 100644 --- a/src/libexpr/value-to-json.cc +++ b/src/libexpr/value-to-json.cc @@ -84,7 +84,8 @@ void printValueAsJSON(EvalState & state, bool strict, .msg = hintfmt("cannot convert %1% to JSON", showType(v)), .errPos = v.determinePos(pos) }); - throw e.addTrace(pos, hintfmt("message for the trace")); + e.addTrace(pos, hintfmt("message for the trace")); + state.debug_throw(e); } } @@ -98,7 +99,7 @@ void printValueAsJSON(EvalState & state, bool strict, void ExternalValueBase::printValueAsJSON(EvalState & state, bool strict, JSONPlaceholder & out, PathSet & context) const { - throw TypeError("cannot convert %1% to JSON", showType()); + state.debug_throw(TypeError("cannot convert %1% to JSON", showType())); }