From e6d07e0d89d964cde22894fca57a95177c085c8d Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Fri, 18 Mar 2022 00:58:09 +0100 Subject: Refactor to use more traces and less string manipulations --- src/libexpr/eval-inline.hh | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'src/libexpr/eval-inline.hh') diff --git a/src/libexpr/eval-inline.hh b/src/libexpr/eval-inline.hh index ad53a74cb..8980e8d4b 100644 --- a/src/libexpr/eval-inline.hh +++ b/src/libexpr/eval-inline.hh @@ -15,10 +15,10 @@ LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const char * s)) }); } -LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const Value & v, const std::string & s2)) +LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const Value & v)) { throw TypeError({ - .msg = hintfmt(s, showType(v), s2), + .msg = hintfmt(s, showType(v)), .errPos = pos }); } @@ -52,26 +52,39 @@ void EvalState::forceValue(Value & v, Callable getPos) } -inline void EvalState::forceAttrs(Value & v, const Pos & pos, const std::string & errorCtx) +inline void EvalState::forceAttrs(Value & v, const Pos & pos, const std::string_view & errorCtx) { forceAttrs(v, [&]() { return pos; }, errorCtx); } template -inline void EvalState::forceAttrs(Value & v, Callable getPos, const std::string & errorCtx) +inline void EvalState::forceAttrs(Value & v, Callable getPos, const std::string_view & errorCtx) { - forceValue(v, getPos); - if (v.type() != nAttrs) - throwTypeError(getPos(), "%2%value is %1% while a set was expected", v, errorCtx); + try { + forceValue(v, noPos); + if (v.type() != nAttrs) { + throwTypeError(noPos, "value is %1% while a set was expected", v); + } + } catch (Error & e) { + Pos pos = getPos(); + e.addTrace(pos, errorCtx); + throw; + } } -inline void EvalState::forceList(Value & v, const Pos & pos, const std::string & errorCtx) +inline void EvalState::forceList(Value & v, const Pos & pos, const std::string_view & errorCtx) { - forceValue(v, pos); - if (!v.isList()) - throwTypeError(pos, "%2%value is %1% while a list was expected", v, errorCtx); + try { + forceValue(v, noPos); + if (!v.isList()) { + throwTypeError(noPos, "value is %1% while a list was expected", v); + } + } catch (Error & e) { + e.addTrace(pos, errorCtx); + throw; + } } /* Note: Various places expect the allocated memory to be zeroed. */ -- cgit v1.2.3