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

Introduce AbstractPos

This commit is contained in:
Eelco Dolstra 2022-07-05 15:58:04 +02:00
parent a04ca0a522
commit 9e9170a92e
21 changed files with 158 additions and 182 deletions

View file

@ -775,7 +775,7 @@ void EvalState::runDebugRepl(const Error * error, const Env & env, const Expr &
? std::make_unique<DebugTraceStacker>(
*this,
DebugTrace {
.pos = error->info().errPos ? *error->info().errPos : positions[expr.getPos()],
.pos = error->info().errPos ? error->info().errPos : (std::shared_ptr<AbstractPos>) positions[expr.getPos()],
.expr = expr,
.env = env,
.hint = error->info().msg,
@ -957,7 +957,7 @@ void EvalState::throwMissingArgumentError(const PosIdx pos, const char * s, cons
void EvalState::addErrorTrace(Error & e, const char * s, const std::string & s2) const
{
e.addTrace(std::nullopt, s, s2);
e.addTrace(nullptr, s, s2);
}
void EvalState::addErrorTrace(Error & e, const PosIdx pos, const char * s, const std::string & s2) const
@ -969,13 +969,13 @@ static std::unique_ptr<DebugTraceStacker> makeDebugTraceStacker(
EvalState & state,
Expr & expr,
Env & env,
std::optional<ErrPos> pos,
std::shared_ptr<AbstractPos> && pos,
const char * s,
const std::string & s2)
{
return std::make_unique<DebugTraceStacker>(state,
DebugTrace {
.pos = pos,
.pos = std::move(pos),
.expr = expr,
.env = env,
.hint = hintfmt(s, s2),
@ -1171,7 +1171,7 @@ void EvalState::evalFile(const SourcePath & path, Value & v, bool mustBeTrivial)
*this,
*e,
this->baseEnv,
e->getPos() ? std::optional(ErrPos(positions[e->getPos()])) : std::nullopt,
e->getPos() ? (std::shared_ptr<AbstractPos>) positions[e->getPos()] : nullptr,
"while evaluating the file '%1%':", resolvedPath.to_string())
: nullptr;

View file

@ -76,7 +76,7 @@ struct RegexCache;
std::shared_ptr<RegexCache> makeRegexCache();
struct DebugTrace {
std::optional<ErrPos> pos;
std::shared_ptr<AbstractPos> pos;
const Expr & expr;
const Env & env;
hintformat hint;

View file

@ -150,7 +150,7 @@ DrvInfo::Outputs DrvInfo::queryOutputs(bool withPaths, bool onlyOutputsToInstall
/* Check for `meta.outputsToInstall` and return `outputs` reduced to that. */
const Value * outTI = queryMeta("outputsToInstall");
if (!outTI) return outputs;
const auto errMsg = Error("this derivation has bad 'meta.outputsToInstall'");
auto errMsg = Error("this derivation has bad 'meta.outputsToInstall'");
/* ^ this shows during `nix-env -i` right under the bad derivation */
if (!outTI->isList()) throw errMsg;
Outputs result;

View file

@ -8,6 +8,64 @@
namespace nix {
struct SourcePathAdapter : AbstractPos
{
std::string file;
std::optional<std::string> getSource() const override
{
return std::nullopt;
}
void print(std::ostream & out) const override
{
out << fmt(ANSI_BLUE "at " ANSI_WARNING "%s:%s" ANSI_NORMAL ":", file, showErrPos());
}
};
struct StringPosAdapter : AbstractPos
{
void print(std::ostream & out) const override
{
out << fmt(ANSI_BLUE "at " ANSI_WARNING "«string»:%s" ANSI_NORMAL ":", showErrPos());
}
};
struct StdinPosAdapter : AbstractPos
{
void print(std::ostream & out) const override
{
out << fmt(ANSI_BLUE "at " ANSI_WARNING "«stdin»:%s" ANSI_NORMAL ":", showErrPos());
}
};
Pos::operator std::shared_ptr<AbstractPos>() const
{
if (!line) return nullptr;
switch (origin) {
case foFile: {
auto pos = std::make_shared<SourcePathAdapter>();
pos->line = line;
pos->column = column;
pos->file = file;
return pos;
}
case foStdin: {
auto pos = std::make_shared<StdinPosAdapter>();
pos->line = line;
pos->column = column;
return pos;
}
case foString:
auto pos = std::make_shared<StringPosAdapter>();
pos->line = line;
pos->column = column;
return pos;
}
assert(false);
}
/* Displaying abstract syntax trees. */
static void showString(std::ostream & str, std::string_view s)
@ -289,7 +347,6 @@ std::string showAttrPath(const SymbolTable & symbols, const AttrPath & attrPath)
}
/* Computing levels/displacements for variables. */
void Expr::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)

View file

@ -21,8 +21,14 @@ MakeError(TypeError, EvalError);
MakeError(UndefinedVarError, Error);
MakeError(MissingArgumentError, EvalError);
/* Position objects. */
// FIXME: change this into a variant?
typedef enum {
foFile,
foStdin,
foString
} FileOrigin;
/* Position objects. */
struct Pos
{
std::string file;
@ -31,6 +37,8 @@ struct Pos
uint32_t column;
explicit operator bool() const { return line > 0; }
operator std::shared_ptr<AbstractPos>() const;
};
class PosIdx {

View file

@ -799,7 +799,7 @@ static void prim_addErrorContext(EvalState & state, const PosIdx pos, Value * *
v = *args[1];
} catch (Error & e) {
PathSet context;
e.addTrace(std::nullopt, state.coerceToString(pos, *args[0], context).toOwned());
e.addTrace(nullptr, state.coerceToString(pos, *args[0], context).toOwned());
throw;
}
}