1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-24 22:11:15 +02:00

Merge pull request #13310 from xokdvium/cleanup-position

libutil: Use `std::shared_ptr<const Pos>` and simplify `Pos` class constructors
This commit is contained in:
Eelco Dolstra 2025-06-02 14:02:25 +02:00 committed by GitHub
commit 7ad4426b8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 11 additions and 26 deletions

View file

@ -13,7 +13,7 @@
namespace nix {
void BaseError::addTrace(std::shared_ptr<Pos> && e, HintFmt hint, TracePrint print)
void BaseError::addTrace(std::shared_ptr<const Pos> && e, HintFmt hint, TracePrint print)
{
err.traces.push_front(Trace { .pos = std::move(e), .hint = hint, .print = print });
}
@ -146,7 +146,7 @@ static bool printUnknownLocations = getEnv("_NIX_EVAL_SHOW_UNKNOWN_LOCATIONS").h
*
* @return true if a position was printed.
*/
static bool printPosMaybe(std::ostream & oss, std::string_view indent, const std::shared_ptr<Pos> & pos) {
static bool printPosMaybe(std::ostream & oss, std::string_view indent, const std::shared_ptr<const Pos> & pos) {
bool hasPos = pos && *pos;
if (hasPos) {
oss << indent << ANSI_BLUE << "at " ANSI_WARNING << *pos << ANSI_NORMAL << ":";

View file

@ -78,7 +78,7 @@ enum struct TracePrint {
};
struct Trace {
std::shared_ptr<Pos> pos;
std::shared_ptr<const Pos> pos;
HintFmt hint;
TracePrint print = TracePrint::Default;
};
@ -88,7 +88,7 @@ inline std::strong_ordering operator<=>(const Trace& lhs, const Trace& rhs);
struct ErrorInfo {
Verbosity level;
HintFmt msg;
std::shared_ptr<Pos> pos;
std::shared_ptr<const Pos> pos;
std::list<Trace> traces;
/**
* Some messages are generated directly by expressions; notably `builtins.warn`, `abort`, `throw`.
@ -172,7 +172,7 @@ public:
err.status = status;
}
void atPos(std::shared_ptr<Pos> pos) {
void atPos(std::shared_ptr<const Pos> pos) {
err.pos = pos;
}
@ -182,12 +182,12 @@ public:
}
template<typename... Args>
void addTrace(std::shared_ptr<Pos> && e, std::string_view fs, const Args & ... args)
void addTrace(std::shared_ptr<const Pos> && e, std::string_view fs, const Args & ... args)
{
addTrace(std::move(e), HintFmt(std::string(fs), args...));
}
void addTrace(std::shared_ptr<Pos> && e, HintFmt hint, TracePrint print = TracePrint::Default);
void addTrace(std::shared_ptr<const Pos> && e, HintFmt hint, TracePrint print = TracePrint::Default);
bool hasTrace() const { return !err.traces.empty(); }

View file

@ -43,15 +43,10 @@ struct Pos
Pos() { }
Pos(uint32_t line, uint32_t column, Origin origin)
: line(line), column(column), origin(origin) { }
Pos(Pos & other) = default;
Pos(const Pos & other) = default;
Pos(Pos && other) = default;
Pos(const Pos * other);
explicit operator bool() const { return line > 0; }
/* TODO: Why std::shared_ptr<Pos> and not std::shared_ptr<const Pos>? */
operator std::shared_ptr<Pos>() const;
operator std::shared_ptr<const Pos>() const;
/**
* Return the contents of the source file.

View file

@ -166,7 +166,7 @@ Activity::Activity(Logger & logger, Verbosity lvl, ActivityType type,
logger.startActivity(id, lvl, type, s, fields, parent);
}
void to_json(nlohmann::json & json, std::shared_ptr<Pos> pos)
void to_json(nlohmann::json & json, std::shared_ptr<const Pos> pos)
{
if (pos) {
json["line"] = pos->line;

View file

@ -2,19 +2,9 @@
namespace nix {
Pos::Pos(const Pos * other)
Pos::operator std::shared_ptr<const Pos>() const
{
if (!other) {
return;
}
line = other->line;
column = other->column;
origin = other->origin;
}
Pos::operator std::shared_ptr<Pos>() const
{
return std::make_shared<Pos>(&*this);
return std::make_shared<const Pos>(*this);
}
std::optional<LinesOfCode> Pos::getCodeLines() const