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

libutil: Use std::shared_ptr<const Pos> instead of std::shared_ptr<Pos>

There's actually no mutation happening so there's no point in using
a mutable shared_ptr. Furthermore, this makes it much more evident to
the reader that no actual mutation (especially in multithreaded case)
is happening.

Also get rid of redundant constructor that isn't actually used anywhere
other than `Pos::operator std::shared_ptr<Pos>` which just passes in &*this,
(identical to just `this`), which can't be nullptr.
This commit is contained in:
Sergei Zimmerman 2025-06-01 20:55:16 +00:00
parent 587b5f5361
commit b73e706589
No known key found for this signature in database
GPG key ID: A9B0B557CA632325
5 changed files with 11 additions and 23 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

@ -46,12 +46,10 @@ struct Pos
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