libexpr: add hidden pos origin for builtin code

currently only used to hide the nix code of derivation from stack traces
and debug frames, but perhaps we'll find it useful for other things too.

Change-Id: Ie5667873d8858d25dd4113bdf454e800b59082d7
This commit is contained in:
eldritch horrors 2024-11-27 02:09:08 +01:00
parent 63006438c4
commit e9520ffd06
5 changed files with 14 additions and 13 deletions

View file

@ -268,7 +268,6 @@ EvalState::EvalState(
std::shared_ptr<Store> buildStore)
: s(symbols)
, repair(NoRepair)
, derivationInternal(CanonPath("/builtin/derivation.nix"))
, store(store)
, buildStore(buildStore ? buildStore : store)
, regexCache(makeRegexCache())
@ -1331,13 +1330,10 @@ void ExprSelect::eval(EvalState & state, Env & env, Value & v)
state.forceValue(*vCurrent, (posCurrent ? posCurrent : this->pos));
} catch (Error & e) {
if (posCurrent) {
auto pos2r = state.positions[posCurrent];
auto origin = std::get_if<SourcePath>(&pos2r.origin);
if (!(origin && *origin == state.derivationInternal))
e.addTrace(state.positions[posCurrent], "while evaluating the attribute '%1%'",
showAttrPath(state, env, attrPath));
}
auto pos2r = state.positions[posCurrent];
if (pos2r && !std::get_if<Pos::Hidden>(&pos2r.origin))
e.addTrace(pos2r, "while evaluating the attribute '%1%'",
showAttrPath(state, env, attrPath));
throw;
}

View file

@ -284,8 +284,6 @@ public:
*/
std::optional<PathSet> allowedPaths;
const SourcePath derivationInternal;
/**
* Store used to materialise .drv files.
*/

View file

@ -2852,7 +2852,7 @@ void EvalState::createBaseEnv()
char code[] =
#include "primops/derivation.nix.gen.hh"
;
eval(*parse(code, sizeof(code), derivationInternal, {CanonPath::root}, staticBaseEnv), *vDerivation);
eval(*parse(code, sizeof(code), Pos::Hidden{}, {CanonPath::root}, staticBaseEnv), *vDerivation);
}

View file

@ -68,6 +68,9 @@ std::optional<std::string> Pos::getSource() const
} catch (Error &) {
return std::nullopt;
}
},
[](Hidden) -> std::optional<std::string> {
return std::nullopt;
}
}, origin);
}
@ -79,7 +82,8 @@ void Pos::print(std::ostream & out, bool showOrigin) const
[&](const std::monostate &) { out << "«none»"; },
[&](const Pos::Stdin &) { out << "«stdin»"; },
[&](const Pos::String & s) { out << "«string»"; },
[&](const SourcePath & path) { out << path; }
[&](const SourcePath & path) { out << path; },
[&](Hidden) { out << "«internal»"; },
}, origin);
out << ":";
}

View file

@ -38,8 +38,11 @@ struct Pos
bool operator<(const String & rhs) const
{ return *source < *rhs.source; }
};
struct Hidden {
auto operator<=>(const Hidden &) const = default;
};
typedef std::variant<std::monostate, Stdin, String, SourcePath> Origin;
typedef std::variant<std::monostate, Stdin, String, SourcePath, Hidden> Origin;
Origin origin = std::monostate();