mirror of
https://github.com/NixOS/nix
synced 2025-06-25 10:41:16 +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:
parent
587b5f5361
commit
b73e706589
5 changed files with 11 additions and 23 deletions
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
namespace nix {
|
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 });
|
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.
|
* @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;
|
bool hasPos = pos && *pos;
|
||||||
if (hasPos) {
|
if (hasPos) {
|
||||||
oss << indent << ANSI_BLUE << "at " ANSI_WARNING << *pos << ANSI_NORMAL << ":";
|
oss << indent << ANSI_BLUE << "at " ANSI_WARNING << *pos << ANSI_NORMAL << ":";
|
||||||
|
|
|
@ -78,7 +78,7 @@ enum struct TracePrint {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Trace {
|
struct Trace {
|
||||||
std::shared_ptr<Pos> pos;
|
std::shared_ptr<const Pos> pos;
|
||||||
HintFmt hint;
|
HintFmt hint;
|
||||||
TracePrint print = TracePrint::Default;
|
TracePrint print = TracePrint::Default;
|
||||||
};
|
};
|
||||||
|
@ -88,7 +88,7 @@ inline std::strong_ordering operator<=>(const Trace& lhs, const Trace& rhs);
|
||||||
struct ErrorInfo {
|
struct ErrorInfo {
|
||||||
Verbosity level;
|
Verbosity level;
|
||||||
HintFmt msg;
|
HintFmt msg;
|
||||||
std::shared_ptr<Pos> pos;
|
std::shared_ptr<const Pos> pos;
|
||||||
std::list<Trace> traces;
|
std::list<Trace> traces;
|
||||||
/**
|
/**
|
||||||
* Some messages are generated directly by expressions; notably `builtins.warn`, `abort`, `throw`.
|
* Some messages are generated directly by expressions; notably `builtins.warn`, `abort`, `throw`.
|
||||||
|
@ -172,7 +172,7 @@ public:
|
||||||
err.status = status;
|
err.status = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
void atPos(std::shared_ptr<Pos> pos) {
|
void atPos(std::shared_ptr<const Pos> pos) {
|
||||||
err.pos = pos;
|
err.pos = pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,12 +182,12 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
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...));
|
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(); }
|
bool hasTrace() const { return !err.traces.empty(); }
|
||||||
|
|
||||||
|
|
|
@ -46,12 +46,10 @@ struct Pos
|
||||||
Pos(Pos & other) = default;
|
Pos(Pos & other) = default;
|
||||||
Pos(const Pos & other) = default;
|
Pos(const Pos & other) = default;
|
||||||
Pos(Pos && other) = default;
|
Pos(Pos && other) = default;
|
||||||
Pos(const Pos * other);
|
|
||||||
|
|
||||||
explicit operator bool() const { return line > 0; }
|
explicit operator bool() const { return line > 0; }
|
||||||
|
|
||||||
/* TODO: Why std::shared_ptr<Pos> and not std::shared_ptr<const Pos>? */
|
operator std::shared_ptr<const Pos>() const;
|
||||||
operator std::shared_ptr<Pos>() const;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the contents of the source file.
|
* Return the contents of the source file.
|
||||||
|
|
|
@ -166,7 +166,7 @@ Activity::Activity(Logger & logger, Verbosity lvl, ActivityType type,
|
||||||
logger.startActivity(id, lvl, type, s, fields, parent);
|
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) {
|
if (pos) {
|
||||||
json["line"] = pos->line;
|
json["line"] = pos->line;
|
||||||
|
|
|
@ -2,19 +2,9 @@
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
Pos::Pos(const Pos * other)
|
Pos::operator std::shared_ptr<const Pos>() const
|
||||||
{
|
{
|
||||||
if (!other) {
|
return std::make_shared<const Pos>(*this);
|
||||||
return;
|
|
||||||
}
|
|
||||||
line = other->line;
|
|
||||||
column = other->column;
|
|
||||||
origin = other->origin;
|
|
||||||
}
|
|
||||||
|
|
||||||
Pos::operator std::shared_ptr<Pos>() const
|
|
||||||
{
|
|
||||||
return std::make_shared<Pos>(&*this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<LinesOfCode> Pos::getCodeLines() const
|
std::optional<LinesOfCode> Pos::getCodeLines() const
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue