libexpr: restrict EvalErrorBuilder to building EvalErrors

Change-Id: Iadd0fee4c12c5d0b6ee74dccbf8785c829d20d95
This commit is contained in:
eldritch horrors
2025-04-05 21:57:45 +02:00
parent 15a85a936b
commit 14db530ac0
3 changed files with 18 additions and 15 deletions
+10 -10
View File
@@ -5,27 +5,27 @@
namespace nix {
template<class T>
template<std::derived_from<EvalError> T>
EvalErrorBuilder<T> EvalErrorBuilder<T>::withExitStatus(unsigned int exitStatus) &&
{
error->withExitStatus(exitStatus);
return std::move(*this);
}
template<class T>
template<std::derived_from<EvalError> T>
EvalErrorBuilder<T> EvalErrorBuilder<T>::atPos(PosIdx pos) &&
{
error->err.pos = positions[pos];
return std::move(*this);
}
template<class T>
template<std::derived_from<EvalError> T>
EvalErrorBuilder<T> EvalErrorBuilder<T>::atPos(Value & value, PosIdx fallback) &&
{
return std::move(*this).atPos(value.determinePos(fallback));
}
template<class T>
template<std::derived_from<EvalError> T>
EvalErrorBuilder<T> EvalErrorBuilder<T>::withTrace(PosIdx pos, const std::string_view text) &&
{
error->err.traces.push_front(
@@ -33,14 +33,14 @@ EvalErrorBuilder<T> EvalErrorBuilder<T>::withTrace(PosIdx pos, const std::string
return std::move(*this);
}
template<class T>
template<std::derived_from<EvalError> T>
EvalErrorBuilder<T> EvalErrorBuilder<T>::withSuggestions(Suggestions & s) &&
{
error->err.suggestions = s;
return std::move(*this);
}
template<class T>
template<std::derived_from<EvalError> T>
EvalErrorBuilder<T> EvalErrorBuilder<T>::withFrame(const Env & env, const Expr & expr) &&
{
if (debug) {
@@ -55,14 +55,14 @@ EvalErrorBuilder<T> EvalErrorBuilder<T>::withFrame(const Env & env, const Expr &
return std::move(*this);
}
template<class T>
template<std::derived_from<EvalError> T>
EvalErrorBuilder<T> EvalErrorBuilder<T>::addTrace(PosIdx pos, HintFmt hint) &&
{
error->addTrace(positions[pos], hint);
return std::move(*this);
}
template<class T>
template<std::derived_from<EvalError> T>
template<typename... Args>
EvalErrorBuilder<T>
EvalErrorBuilder<T>::addTrace(PosIdx pos, std::string_view formatString, const Args &... formatArgs) &&
@@ -72,7 +72,7 @@ EvalErrorBuilder<T>::addTrace(PosIdx pos, std::string_view formatString, const A
return std::move(*this);
}
template<class T>
template<std::derived_from<EvalError> T>
void EvalErrorBuilder<T>::debugThrow(NeverAsync) &&
{
if (debug) {
@@ -86,7 +86,7 @@ void EvalErrorBuilder<T>::debugThrow(NeverAsync) &&
throw *error;
}
template<class T>
template<std::derived_from<EvalError> T>
void EvalErrorBuilder<T>::throw_() &&
{
throw *error;
+5 -3
View File
@@ -6,6 +6,7 @@
#include "lix/libutil/types.hh"
#include "lix/libexpr/pos-idx.hh"
#include "lix/libexpr/pos-table.hh"
#include <concepts>
namespace nix {
@@ -15,13 +16,14 @@ struct Env;
struct Expr;
struct Value;
class EvalError;
class EvalState;
template<class T>
template<std::derived_from<EvalError> T>
class EvalErrorBuilder;
class EvalError : public Error
{
template<class T>
template<std::derived_from<EvalError> T>
friend class EvalErrorBuilder;
std::shared_ptr<const DebugTrace> frame;
@@ -54,7 +56,7 @@ public:
}
};
template<class T>
template<std::derived_from<EvalError> T>
class [[nodiscard]] EvalErrorBuilder final
{
const PosTable & positions;
+3 -2
View File
@@ -18,6 +18,7 @@
#include "lix/libexpr/repl-exit-status.hh"
#include "lix/libutil/backed-string-view.hh"
#include <concepts>
#include <map>
#include <optional>
#include <unordered_map>
@@ -181,7 +182,7 @@ public:
class TraceFrame
{
friend struct DebugState;
template<class T>
template<std::derived_from<EvalError> T>
friend class EvalErrorBuilder;
// holds both the data for this frame *and* a deleter that pulls this frame
@@ -356,7 +357,7 @@ struct EvalErrorContext
const PosTable & positions;
DebugState * debug;
template<class T, typename... Args>
template<std::derived_from<EvalError> T, typename... Args>
[[gnu::noinline]]
EvalErrorBuilder<T> make(const Args & ... args) {
return EvalErrorBuilder<T>(positions, debug, args...);