diff options
author | eldritch horrors <pennae@lix.systems> | 2024-03-08 04:49:08 +0100 |
---|---|---|
committer | eldritch horrors <pennae@lix.systems> | 2024-03-09 00:05:41 -0700 |
commit | 87e6ac5eb706593d15d29b070eac5f05e305a787 (patch) | |
tree | 79062f2ab0e8a298de9b68419fa4094994e26e22 /src/libexpr/eval.cc | |
parent | 896e525681bbf696c330af4e51c5e161d3818350 (diff) |
Merge pull request #9753 from 9999years/print-value-on-type-error
Print the value in `value is X while a Y is expected` error
(cherry picked from commit 5f72a97092da6af28a7d2b2a50d74e9d34fae7e1)
Change-Id: Idb4bc903ae59a0f5b6fb3b1da4d47970fe0a6efe
Diffstat (limited to 'src/libexpr/eval.cc')
-rw-r--r-- | src/libexpr/eval.cc | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 729b17887..5e511f49b 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -2,6 +2,7 @@ #include "eval-settings.hh" #include "hash.hh" #include "primops.hh" +#include "print-options.hh" #include "types.hh" #include "util.hh" #include "store-api.hh" @@ -24,9 +25,9 @@ #include <unistd.h> #include <sys/time.h> #include <sys/resource.h> -#include <iostream> #include <fstream> #include <functional> +#include <iostream> #include <sys/resource.h> #include <nlohmann/json.hpp> @@ -1173,7 +1174,10 @@ inline bool EvalState::evalBool(Env & env, Expr * e, const PosIdx pos, std::stri Value v; e->eval(*this, env, v); if (v.type() != nBool) - error("value is %1% while a Boolean was expected", showType(v)).withFrame(env, *e).debugThrow<TypeError>(); + error("expected a Boolean but found %1%: %2%", + showType(v), + ValuePrinter(*this, v, errorPrintOptions)) + .withFrame(env, *e).debugThrow<TypeError>(); return v.boolean; } catch (Error & e) { e.addTrace(positions[pos], errorCtx); @@ -1187,7 +1191,10 @@ inline void EvalState::evalAttrs(Env & env, Expr * e, Value & v, const PosIdx po try { e->eval(*this, env, v); if (v.type() != nAttrs) - error("value is %1% while a set was expected", showType(v)).withFrame(env, *e).debugThrow<TypeError>(); + error("expected a set but found %1%: %2%", + showType(v), + ValuePrinter(*this, v, errorPrintOptions)) + .withFrame(env, *e).debugThrow<TypeError>(); } catch (Error & e) { e.addTrace(positions[pos], errorCtx); throw; @@ -2096,7 +2103,10 @@ NixInt EvalState::forceInt(Value & v, const PosIdx pos, std::string_view errorCt try { forceValue(v, pos); if (v.type() != nInt) - error("value is %1% while an integer was expected", showType(v)).debugThrow<TypeError>(); + error("expected an integer but found %1%: %2%", + showType(v), + ValuePrinter(*this, v, errorPrintOptions)) + .debugThrow<TypeError>(); return v.integer; } catch (Error & e) { e.addTrace(positions[pos], errorCtx); @@ -2112,7 +2122,10 @@ NixFloat EvalState::forceFloat(Value & v, const PosIdx pos, std::string_view err if (v.type() == nInt) return v.integer; else if (v.type() != nFloat) - error("value is %1% while a float was expected", showType(v)).debugThrow<TypeError>(); + error("expected a float but found %1%: %2%", + showType(v), + ValuePrinter(*this, v, errorPrintOptions)) + .debugThrow<TypeError>(); return v.fpoint; } catch (Error & e) { e.addTrace(positions[pos], errorCtx); @@ -2126,7 +2139,10 @@ bool EvalState::forceBool(Value & v, const PosIdx pos, std::string_view errorCtx try { forceValue(v, pos); if (v.type() != nBool) - error("value is %1% while a Boolean was expected", showType(v)).debugThrow<TypeError>(); + error("expected a Boolean but found %1%: %2%", + showType(v), + ValuePrinter(*this, v, errorPrintOptions)) + .debugThrow<TypeError>(); return v.boolean; } catch (Error & e) { e.addTrace(positions[pos], errorCtx); @@ -2146,7 +2162,10 @@ void EvalState::forceFunction(Value & v, const PosIdx pos, std::string_view erro try { forceValue(v, pos); if (v.type() != nFunction && !isFunctor(v)) - error("value is %1% while a function was expected", showType(v)).debugThrow<TypeError>(); + error("expected a function but found %1%: %2%", + showType(v), + ValuePrinter(*this, v, errorPrintOptions)) + .debugThrow<TypeError>(); } catch (Error & e) { e.addTrace(positions[pos], errorCtx); throw; @@ -2159,7 +2178,10 @@ std::string_view EvalState::forceString(Value & v, const PosIdx pos, std::string try { forceValue(v, pos); if (v.type() != nString) - error("value is %1% while a string was expected", showType(v)).debugThrow<TypeError>(); + error("expected a string but found %1%: %2%", + showType(v), + ValuePrinter(*this, v, errorPrintOptions)) + .debugThrow<TypeError>(); return v.string.s; } catch (Error & e) { e.addTrace(positions[pos], errorCtx); |