From eba0892d08dca56905f251cfa9e1d048daf8c31d Mon Sep 17 00:00:00 2001 From: Daiderd Jordan Date: Sat, 11 Jan 2020 15:06:57 +0100 Subject: libexpr: show expression in assertion errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Includes the expression of the condition in the assertion message if the assertion failed, making assertions much easier to debug. eg. error: assertion (withPython -> (python2Packages != null)) failed at pkgs/tools/security/nmap/default.nix:11:1 (cherry picked from commit 307bcb9a8e7a16bfc451e055a620b766df9d3f7d) Signed-off-by: Domen Kožar --- src/libexpr/eval.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/libexpr/eval.cc') diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 09276a189535..3426afb6cf6e 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -520,9 +520,9 @@ LocalNoInlineNoReturn(void throwTypeError(const char * s, const ExprLambda & fun throw TypeError(format(s) % fun.showNamePos() % s2 % pos); } -LocalNoInlineNoReturn(void throwAssertionError(const char * s, const Pos & pos)) +LocalNoInlineNoReturn(void throwAssertionError(const char * s, const string & s1, const Pos & pos)) { - throw AssertionError(format(s) % pos); + throw AssertionError(format(s) % s1 % pos); } LocalNoInlineNoReturn(void throwUndefinedVarError(const char * s, const string & s1, const Pos & pos)) @@ -1253,8 +1253,11 @@ void ExprIf::eval(EvalState & state, Env & env, Value & v) void ExprAssert::eval(EvalState & state, Env & env, Value & v) { - if (!state.evalBool(env, cond, pos)) - throwAssertionError("assertion failed at %1%", pos); + if (!state.evalBool(env, cond, pos)) { + std::ostringstream out; + cond->show(out); + throwAssertionError("assertion %1% failed at %2%", out.str(), pos); + } body->eval(state, env, v); } -- cgit 1.4.1