aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr/eval.hh
diff options
context:
space:
mode:
authorGuillaume Maudoux <guillaume.maudoux@tweag.io>2022-10-25 01:46:10 +0200
committerGuillaume Maudoux <guillaume.maudoux@tweag.io>2022-10-25 01:46:10 +0200
commite93bf69b448d4f4ce6c3fe7b7acfa904afe058c0 (patch)
tree902c6994294be36fe9ebd9c7e2149f9010776b82 /src/libexpr/eval.hh
parent8bd8583bc7a430eeee0f5d5e5cb502158419a500 (diff)
Rework error throwing, and test it
Diffstat (limited to 'src/libexpr/eval.hh')
-rw-r--r--src/libexpr/eval.hh63
1 files changed, 32 insertions, 31 deletions
diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh
index 5e88eb950..1f610a02f 100644
--- a/src/libexpr/eval.hh
+++ b/src/libexpr/eval.hh
@@ -87,48 +87,40 @@ struct DebugTrace {
void debugError(Error * e, Env & env, Expr & expr);
-template<class ErrorType>
class ErrorBuilder
{
-
+ private:
EvalState & state;
ErrorInfo info;
- public:
- [[gnu::noinline]]
- ErrorBuilder(EvalState & s);
-
- [[gnu::noinline]]
- ErrorBuilder<ErrorType> & atPos(PosIdx pos);
+ ErrorBuilder(EvalState & s, ErrorInfo && i): state(s), info(i) { }
+ public:
template<typename... Args>
- [[gnu::noinline]]
- ErrorBuilder<ErrorType> & msg(const std::string & fs, const Args & ... args)
+ [[nodiscard, gnu::noinline]]
+ static ErrorBuilder * create(EvalState & s, const Args & ... args)
{
- hintformat f(fs);
- formatHelper(f, args...);
- info.msg = f;
- return *this;
+ return new ErrorBuilder(s, ErrorInfo { .msg = hintfmt(args...) });
}
- [[gnu::noinline]]
- ErrorBuilder<ErrorType> & withTrace(PosIdx pos, const std::string_view text);
+ [[nodiscard, gnu::noinline]]
+ ErrorBuilder & atPos(PosIdx pos);
- [[gnu::noinline]]
- ErrorBuilder<ErrorType> & withFrameTrace(PosIdx pos, const std::string_view text);
+ [[nodiscard, gnu::noinline]]
+ ErrorBuilder & withTrace(PosIdx pos, const std::string_view text);
- [[gnu::noinline]]
- ErrorBuilder<ErrorType> & suggestions(Suggestions & s);
+ [[nodiscard, gnu::noinline]]
+ ErrorBuilder & withFrameTrace(PosIdx pos, const std::string_view text);
- [[gnu::noinline]]
- ErrorBuilder<ErrorType> & withFrame(const Env & e, const Expr & ex);
+ [[nodiscard, gnu::noinline]]
+ ErrorBuilder & withSuggestions(Suggestions & s);
- [[gnu::noinline, gnu::noreturn]]
- void ErrorBuilder<ErrorType>::debugThrow() {
- // NOTE: We always use the -LastTrace version as we push the new trace in withFrame()
- state.debugThrowLastTrace(ErrorType(info));
- }
+ [[nodiscard, gnu::noinline]]
+ ErrorBuilder & withFrame(const Env & e, const Expr & ex);
+ template<class ErrorType>
+ [[gnu::noinline, gnu::noreturn]]
+ void debugThrow();
};
@@ -212,10 +204,12 @@ public:
throw std::move(error);
}
- template<class E, typename... Args>
- ErrorBuilder<E> & error(const std::string & fs, const Args & ... args) {
- ErrorBuilder<E> * errorBuilder = new ErrorBuilder<E>(*this);
- errorBuilder->msg(fs, args ...);
+ ErrorBuilder * errorBuilder;
+
+ template<typename... Args>
+ [[nodiscard, gnu::noinline]]
+ ErrorBuilder & error(const Args & ... args) {
+ errorBuilder = ErrorBuilder::create(*this, args...);
return *errorBuilder;
}
@@ -648,6 +642,13 @@ extern EvalSettings evalSettings;
static const std::string corepkgsPrefix{"/__corepkgs__/"};
+template<class ErrorType>
+void ErrorBuilder::debugThrow()
+{
+ // NOTE: We always use the -LastTrace version as we push the new trace in withFrame()
+ state.debugThrowLastTrace(ErrorType(info));
+}
+
}
#include "eval-inline.hh"