diff options
author | eldritch horrors <pennae@lix.systems> | 2024-03-06 05:24:35 +0100 |
---|---|---|
committer | eldritch horrors <pennae@lix.systems> | 2024-03-05 23:46:18 -0700 |
commit | 89e99d94e4ae492db09c0ebc0c35e4890ef7db25 (patch) | |
tree | eab2defee911a6c0e39d4b073062690bd9a3a058 /src/libexpr | |
parent | e9b5929b22116cb714adfe88ba39a817e89b019c (diff) |
Merge pull request #9634 from 9999years/combine-abstract-pos-and-pos
Combine `AbstractPos`, `PosAdapter`, and `Pos`
(cherry picked from commit 113499d16fc87d53b73fb62fe6242154909756ed)
===
this is a bit cursed because originally it was based on InputAccessor
code that we don't have and moved/patched features we likewise don't
have (fetchToStore caching, all the individual accessors,
ContentAddressMethod). the commit is adjusted accordingly to
match (remove caching, ignore accessors, use FileIngestionMethod).
note that `state.rootPath . CanonPath == abs` and
computeStorePathForPath works relative to cwd, so the slight rewrite in
the moved fetchToStore is legal.
Change-Id: I05fd340c273f0bcc8ffabfebdc4a88b98083bce5
Diffstat (limited to 'src/libexpr')
-rw-r--r-- | src/libexpr/eval.cc | 9 | ||||
-rw-r--r-- | src/libexpr/eval.hh | 2 | ||||
-rw-r--r-- | src/libexpr/nixexpr.cc | 63 | ||||
-rw-r--r-- | src/libexpr/nixexpr.hh | 26 | ||||
-rw-r--r-- | src/libexpr/primops.cc | 6 | ||||
-rw-r--r-- | src/libexpr/value.hh | 1 |
6 files changed, 12 insertions, 95 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index debeaf75f..8e3077a67 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -14,6 +14,7 @@ #include "profiles.hh" #include "print.hh" #include "gc-small-vector.hh" +#include "fetch-to-store.hh" #include <algorithm> #include <chrono> @@ -879,7 +880,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 : static_cast<std::shared_ptr<AbstractPos>>(positions[expr.getPos()]), + .pos = error->info().errPos ? error->info().errPos : positions[expr.getPos()], .expr = expr, .env = env, .hint = error->info().msg, @@ -916,7 +917,7 @@ static std::unique_ptr<DebugTraceStacker> makeDebugTraceStacker( EvalState & state, Expr & expr, Env & env, - std::shared_ptr<AbstractPos> && pos, + std::shared_ptr<Pos> && pos, const char * s, const std::string & s2) { @@ -1214,7 +1215,7 @@ void EvalState::cacheFile( *this, *e, this->baseEnv, - e->getPos() ? static_cast<std::shared_ptr<AbstractPos>>(positions[e->getPos()]) : nullptr, + e->getPos() ? std::make_shared<Pos>(positions[e->getPos()]) : nullptr, "while evaluating the file '%1%':", resolvedPath.to_string()) : nullptr; @@ -2388,7 +2389,7 @@ StorePath EvalState::copyPathToStore(NixStringContext & context, const SourcePat auto dstPath = i != srcToStore.end() ? i->second : [&]() { - auto dstPath = path.fetchToStore(store, path.baseName(), nullptr, repair); + auto dstPath = fetchToStore(*store, path, path.baseName(), FileIngestionMethod::Recursive, nullptr, repair); allowPath(dstPath); srcToStore.insert_or_assign(path, dstPath); printMsg(lvlChatty, "copied source '%1%' -> '%2%'", path, store->printStorePath(dstPath)); diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index 9cf50ba6c..d070fdab8 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -141,7 +141,7 @@ struct RegexCache; std::shared_ptr<RegexCache> makeRegexCache(); struct DebugTrace { - std::shared_ptr<AbstractPos> pos; + std::shared_ptr<Pos> pos; const Expr & expr; const Env & env; hintformat hint; diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc index b34112be1..eb5d0d3d4 100644 --- a/src/libexpr/nixexpr.cc +++ b/src/libexpr/nixexpr.cc @@ -11,58 +11,6 @@ namespace nix { ExprBlackHole eBlackHole; -struct PosAdapter : AbstractPos -{ - Pos::Origin origin; - - PosAdapter(Pos::Origin origin) - : origin(std::move(origin)) - { - } - - std::optional<std::string> getSource() const override - { - return std::visit(overloaded { - [](const Pos::none_tag &) -> std::optional<std::string> { - return std::nullopt; - }, - [](const Pos::Stdin & s) -> std::optional<std::string> { - // Get rid of the null terminators added by the parser. - return std::string(s.source->c_str()); - }, - [](const Pos::String & s) -> std::optional<std::string> { - // Get rid of the null terminators added by the parser. - return std::string(s.source->c_str()); - }, - [](const SourcePath & path) -> std::optional<std::string> { - try { - return path.readFile(); - } catch (Error &) { - return std::nullopt; - } - } - }, origin); - } - - void print(std::ostream & out) const override - { - std::visit(overloaded { - [&](const Pos::none_tag &) { out << "«none»"; }, - [&](const Pos::Stdin &) { out << "«stdin»"; }, - [&](const Pos::String & s) { out << "«string»"; }, - [&](const SourcePath & path) { out << path; } - }, origin); - } -}; - -Pos::operator std::shared_ptr<AbstractPos>() const -{ - auto pos = std::make_shared<PosAdapter>(origin); - pos->line = line; - pos->column = column; - return pos; -} - // FIXME: remove, because *symbols* are abstract and do not have a single // textual representation; see printIdentifier() std::ostream & operator <<(std::ostream & str, const SymbolStr & symbol) @@ -268,17 +216,6 @@ void ExprPos::show(const SymbolTable & symbols, std::ostream & str) const } -std::ostream & operator << (std::ostream & str, const Pos & pos) -{ - if (auto pos2 = (std::shared_ptr<AbstractPos>) pos) { - str << *pos2; - } else - str << "undefined position"; - - return str; -} - - std::string showAttrPath(const SymbolTable & symbols, const AttrPath & attrPath) { std::ostringstream out; diff --git a/src/libexpr/nixexpr.hh b/src/libexpr/nixexpr.hh index b38f19b41..6f2a4c0f9 100644 --- a/src/libexpr/nixexpr.hh +++ b/src/libexpr/nixexpr.hh @@ -8,6 +8,7 @@ #include "symbol-table.hh" #include "error.hh" #include "chunked-vector.hh" +#include "position.hh" namespace nix { @@ -29,27 +30,6 @@ public: using EvalError::EvalError; }; -/** - * Position objects. - */ -struct Pos -{ - uint32_t line; - uint32_t column; - - struct none_tag { }; - struct Stdin { ref<std::string> source; }; - struct String { ref<std::string> source; }; - - typedef std::variant<none_tag, Stdin, String, SourcePath> Origin; - - Origin origin; - - explicit operator bool() const { return line > 0; } - - operator std::shared_ptr<AbstractPos>() const; -}; - class PosIdx { friend class PosTable; @@ -82,7 +62,7 @@ public: mutable uint32_t idx = std::numeric_limits<uint32_t>::max(); // Used for searching in PosTable::[]. - explicit Origin(uint32_t idx): idx(idx), origin{Pos::none_tag()} {} + explicit Origin(uint32_t idx): idx(idx), origin{std::monostate()} {} public: const Pos::Origin origin; @@ -133,8 +113,6 @@ public: inline PosIdx noPos = {}; -std::ostream & operator << (std::ostream & str, const Pos & pos); - struct Env; struct Value; diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index d64f2d51e..1a961582f 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -14,6 +14,7 @@ #include "value-to-json.hh" #include "value-to-xml.hh" #include "primops.hh" +#include "fetch-to-store.hh" #include <boost/container/small_vector.hpp> #include <nlohmann/json.hpp> @@ -2235,9 +2236,8 @@ static void addPath( }); if (!expectedHash || !state.store->isValidPath(*expectedStorePath)) { - StorePath dstPath = settings.readOnlyMode - ? state.store->computeStorePathForPath(name, path, method, htSHA256, filter).first - : state.store->addToStore(name, path, method, htSHA256, filter, state.repair, refs); + auto dstPath = fetchToStore( + *state.store, state.rootPath(CanonPath(path)), name, method, &filter, state.repair); if (expectedHash && expectedStorePath != dstPath) state.debugThrowLastTrace(Error("store path mismatch in (possibly filtered) path added from '%s'", path)); state.allowAndSetStorePathString(dstPath, v); diff --git a/src/libexpr/value.hh b/src/libexpr/value.hh index 224ffac9c..f90082e61 100644 --- a/src/libexpr/value.hh +++ b/src/libexpr/value.hh @@ -7,6 +7,7 @@ #include "symbol-table.hh" #include "value/context.hh" #include "input-accessor.hh" +#include "source-path.hh" #if HAVE_BOEHMGC #include <gc/gc_allocator.h> |