1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-03 02:01:48 +02:00

Introduce an Error builder to tackle complexity

This commit is contained in:
Guillaume Maudoux 2022-10-22 23:37:54 +02:00
parent 4a909c142c
commit 8c3afd2d68
5 changed files with 160 additions and 139 deletions

View file

@ -87,6 +87,47 @@ struct DebugTrace {
void debugError(Error * e, Env & env, Expr & expr);
template<class ErrorType>
class ErrorBuilder
{
EvalState & state;
ErrorInfo info;
public:
[[gnu::noinline]]
ErrorBuilder(EvalState & s);
[[gnu::noinline]]
ErrorBuilder<ErrorType> & atPos(PosIdx pos);
template<typename... Args>
[[gnu::noinline]]
ErrorBuilder<ErrorType> & msg(const std::string & fs, const Args & ... args)
{
hintformat f(fs);
formatHelper(f, args...);
info.msg = f;
return *this;
}
[[gnu::noinline]]
ErrorBuilder<ErrorType> & withTrace(PosIdx pos, const std::string_view text);
[[gnu::noinline]]
ErrorBuilder<ErrorType> & withFrameTrace(PosIdx pos, const std::string_view text);
[[gnu::noinline]]
ErrorBuilder<ErrorType> & suggestions(Suggestions & s);
[[gnu::noinline]]
ErrorBuilder<ErrorType> & withFrame(const Env & e, const Expr & ex);
[[gnu::noinline, gnu::noreturn]]
void debugThrow();
};
class EvalState : public std::enable_shared_from_this<EvalState>
{
public:
@ -167,6 +208,13 @@ 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 ...);
return *errorBuilder;
}
private:
SrcToStore srcToStore;
@ -312,31 +360,6 @@ public:
std::string_view forceString(Value & v, PathSet & context, const PosIdx pos, std::string_view errorCtx);
std::string_view forceStringNoCtx(Value & v, const PosIdx pos, std::string_view errorCtx);
template <typename ErrorType>
[[gnu::noinline, gnu::noreturn]]
void throwErrorWithTrace(
PosIdx pos, const char* format,
const std::string_view s1, const std::string_view s2,
const Symbol * sym1, const Symbol * sym2,
Value * val1, Value * val2,
PosIdx pos1,
const std::string_view s3,
const Suggestions * suggestions,
PosIdx tracePos, const std::string_view traceStr,
Env * env, Expr * expr);
template <typename ErrorType>
[[gnu::noinline, gnu::noreturn]]
void throwError(
PosIdx pos, const char* format,
const std::string_view s1, const std::string_view s2,
const Symbol * sym1, const Symbol * sym2,
Value * val1, Value * val2,
PosIdx pos1,
const std::string_view s3,
const Suggestions * suggestions,
Env * env, Expr * expr);
[[gnu::noinline]]
void addErrorTrace(Error & e, const char * s, const std::string & s2) const;
[[gnu::noinline]]