Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libcmd/installable-flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ std::vector<ref<eval_cache::AttrCursor>> InstallableFlake::getCursors(EvalState
for (auto & attrPath : attrPaths) {
debug("trying flake output attribute '%s'", attrPath);

auto attr = root->findAlongAttrPath(parseAttrPath(state, attrPath));
auto attr = root->findAlongAttrPath(AttrPath::parse(state, attrPath));
if (attr) {
res.push_back(ref(*attr));
} else {
Expand Down
10 changes: 4 additions & 6 deletions src/libcmd/installables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,9 @@ void completeFlakeRefWithFragment(
attrPathPrefixes.push_back("");

for (auto & attrPathPrefixS : attrPathPrefixes) {
auto attrPathPrefix = parseAttrPath(*evalState, attrPathPrefixS);
auto attrPathPrefix = AttrPath::parse(*evalState, attrPathPrefixS);
auto attrPathS = attrPathPrefixS + std::string(fragment);
auto attrPath = parseAttrPath(*evalState, attrPathS);
auto attrPath = AttrPath::parse(*evalState, attrPathS);

std::string lastAttr;
if (!attrPath.empty() && !hasSuffix(attrPathS, ".")) {
Expand All @@ -375,9 +375,7 @@ void completeFlakeRefWithFragment(
/* Strip the attrpath prefix. */
attrPath2.erase(attrPath2.begin(), attrPath2.begin() + attrPathPrefix.size());
// FIXME: handle names with dots
completions.add(
flakeRefS + "#" + prefixRoot
+ concatStringsSep(".", evalState->symbols.resolve(attrPath2)));
completions.add(flakeRefS + "#" + prefixRoot + attrPath2.to_string(*evalState));
}
}
}
Expand All @@ -386,7 +384,7 @@ void completeFlakeRefWithFragment(
attrpaths. */
if (fragment.empty()) {
for (auto & attrPath : defaultFlakeAttrPaths) {
auto attr = root->findAlongAttrPath(parseAttrPath(*evalState, attrPath));
auto attr = root->findAlongAttrPath(AttrPath::parse(*evalState, attrPath));
if (!attr)
continue;
completions.add(flakeRefS + "#" + prefixRoot);
Expand Down
15 changes: 13 additions & 2 deletions src/libexpr/attr-path.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "nix/expr/attr-path.hh"
#include "nix/expr/eval-inline.hh"
#include "nix/util/strings-inline.hh"

namespace nix {

Expand Down Expand Up @@ -30,14 +31,24 @@ static Strings parseAttrPath(std::string_view s)
return res;
}

std::vector<Symbol> parseAttrPath(EvalState & state, std::string_view s)
AttrPath AttrPath::parse(EvalState & state, std::string_view s)
{
std::vector<Symbol> res;
AttrPath res;
for (auto & a : parseAttrPath(s))
res.push_back(state.symbols.create(a));
return res;
}

std::string AttrPath::to_string(EvalState & state) const
{
return dropEmptyInitThenConcatStringsSep(".", state.symbols.resolve({*this}));
}

std::vector<SymbolStr> AttrPath::resolve(EvalState & state) const
{
return state.symbols.resolve({*this});
}

std::pair<Value *, PosIdx>
findAlongAttrPath(EvalState & state, const std::string & attrPath, Bindings & autoArgs, Value & vIn)
{
Expand Down
10 changes: 5 additions & 5 deletions src/libexpr/eval-cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ void AttrCursor::fetchCachedValue()
throw CachedEvalError(parent->first, parent->second);
}

std::vector<Symbol> AttrCursor::getAttrPath() const
AttrPath AttrCursor::getAttrPath() const
{
if (parent) {
auto attrPath = parent->first->getAttrPath();
Expand All @@ -374,7 +374,7 @@ std::vector<Symbol> AttrCursor::getAttrPath() const
return {};
}

std::vector<Symbol> AttrCursor::getAttrPath(Symbol name) const
AttrPath AttrCursor::getAttrPath(Symbol name) const
{
auto attrPath = getAttrPath();
attrPath.push_back(name);
Expand All @@ -383,12 +383,12 @@ std::vector<Symbol> AttrCursor::getAttrPath(Symbol name) const

std::string AttrCursor::getAttrPathStr() const
{
return dropEmptyInitThenConcatStringsSep(".", root->state.symbols.resolve(getAttrPath()));
return getAttrPath().to_string(root->state);
}

std::string AttrCursor::getAttrPathStr(Symbol name) const
{
return dropEmptyInitThenConcatStringsSep(".", root->state.symbols.resolve(getAttrPath(name)));
return getAttrPath(name).to_string(root->state);
}

Value & AttrCursor::forceValue()
Expand Down Expand Up @@ -511,7 +511,7 @@ ref<AttrCursor> AttrCursor::getAttr(std::string_view name)
return getAttr(root->state.symbols.create(name));
}

OrSuggestions<ref<AttrCursor>> AttrCursor::findAlongAttrPath(const std::vector<Symbol> & attrPath)
OrSuggestions<ref<AttrCursor>> AttrCursor::findAlongAttrPath(const AttrPath & attrPath)
{
auto res = shared_from_this();
for (auto & attr : attrPath) {
Expand Down
6 changes: 3 additions & 3 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,7 @@ void ExprVar::eval(EvalState & state, Env & env, Value & v)
v = *v2;
}

static std::string showAttrPath(EvalState & state, Env & env, std::span<const AttrName> attrPath)
static std::string showAttrSelectionPath(EvalState & state, Env & env, std::span<const AttrName> attrPath)
{
std::ostringstream out;
bool first = true;
Expand Down Expand Up @@ -1404,7 +1404,7 @@ void ExprSelect::eval(EvalState & state, Env & env, Value & v)
env,
getPos(),
"while evaluating the attribute '%1%'",
showAttrPath(state, env, getAttrPath()))
showAttrSelectionPath(state, env, getAttrPath()))
: nullptr;

for (auto & i : getAttrPath()) {
Expand Down Expand Up @@ -1445,7 +1445,7 @@ void ExprSelect::eval(EvalState & state, Env & env, Value & v)
auto origin = std::get_if<SourcePath>(&pos2r.origin);
if (!(origin && *origin == state.derivationInternal))
state.addErrorTrace(
e, pos2, "while evaluating the attribute '%1%'", showAttrPath(state, env, getAttrPath()));
e, pos2, "while evaluating the attribute '%1%'", showAttrSelectionPath(state, env, getAttrPath()));
}
throw;
}
Expand Down
11 changes: 10 additions & 1 deletion src/libexpr/include/nix/expr/attr-path.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ findAlongAttrPath(EvalState & state, const std::string & attrPath, Bindings & au
*/
std::pair<SourcePath, uint32_t> findPackageFilename(EvalState & state, Value & v, std::string what);

std::vector<Symbol> parseAttrPath(EvalState & state, std::string_view s);
struct AttrPath : std::vector<Symbol>
{
using std::vector<Symbol>::vector;

static AttrPath parse(EvalState & state, std::string_view s);

std::string to_string(EvalState & state) const;

std::vector<SymbolStr> resolve(EvalState & state) const;
};

} // namespace nix
7 changes: 4 additions & 3 deletions src/libexpr/include/nix/expr/eval-cache.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "nix/util/sync.hh"
#include "nix/util/hash.hh"
#include "nix/expr/eval.hh"
#include "nix/expr/attr-path.hh"

#include <functional>
#include <variant>
Expand Down Expand Up @@ -124,9 +125,9 @@ public:
Value * value = nullptr,
std::optional<std::pair<AttrId, AttrValue>> && cachedValue = {});

std::vector<Symbol> getAttrPath() const;
AttrPath getAttrPath() const;

std::vector<Symbol> getAttrPath(Symbol name) const;
AttrPath getAttrPath(Symbol name) const;

std::string getAttrPathStr() const;

Expand All @@ -146,7 +147,7 @@ public:
* Get an attribute along a chain of attrsets. Note that this does
* not auto-call functors or functions.
*/
OrSuggestions<ref<AttrCursor>> findAlongAttrPath(const std::vector<Symbol> & attrPath);
OrSuggestions<ref<AttrCursor>> findAlongAttrPath(const AttrPath & attrPath);

std::string getString();

Expand Down
4 changes: 2 additions & 2 deletions src/libexpr/include/nix/expr/nixexpr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ struct AttrName

static_assert(std::is_trivially_copy_constructible_v<AttrName>);

typedef std::vector<AttrName> AttrPath;
typedef std::vector<AttrName> AttrSelectionPath;

std::string showAttrPath(const SymbolTable & symbols, std::span<const AttrName> attrPath);
std::string showAttrSelectionPath(const SymbolTable & symbols, std::span<const AttrName> attrPath);

using UpdateQueue = SmallTemporaryValueVector<conservativeStackReservation>;

Expand Down
25 changes: 17 additions & 8 deletions src/libexpr/include/nix/expr/parser-state.hh
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,25 @@ struct ParserState
static constexpr Expr::AstSymbols s = StaticEvalSymbols::create().exprSymbols;
const EvalSettings & settings;

void dupAttr(const AttrPath & attrPath, const PosIdx pos, const PosIdx prevPos);
void dupAttr(const AttrSelectionPath & attrPath, const PosIdx pos, const PosIdx prevPos);
void dupAttr(Symbol attr, const PosIdx pos, const PosIdx prevPos);
void addAttr(
ExprAttrs * attrs, AttrPath && attrPath, const ParserLocation & loc, Expr * e, const ParserLocation & exprLoc);
void addAttr(ExprAttrs * attrs, AttrPath & attrPath, const Symbol & symbol, ExprAttrs::AttrDef && def);
ExprAttrs * attrs,
AttrSelectionPath && attrPath,
const ParserLocation & loc,
Expr * e,
const ParserLocation & exprLoc);
void addAttr(ExprAttrs * attrs, AttrSelectionPath & attrPath, const Symbol & symbol, ExprAttrs::AttrDef && def);
void validateFormals(FormalsBuilder & formals, PosIdx pos = noPos, Symbol arg = {});
Expr * stripIndentation(const PosIdx pos, std::span<std::pair<PosIdx, std::variant<Expr *, StringToken>>> es);
PosIdx at(const ParserLocation & loc);
};

inline void ParserState::dupAttr(const AttrPath & attrPath, const PosIdx pos, const PosIdx prevPos)
inline void ParserState::dupAttr(const AttrSelectionPath & attrPath, const PosIdx pos, const PosIdx prevPos)
{
throw ParseError(
{.msg = HintFmt("attribute '%1%' already defined at %2%", showAttrPath(symbols, attrPath), positions[prevPos]),
{.msg = HintFmt(
"attribute '%1%' already defined at %2%", showAttrSelectionPath(symbols, attrPath), positions[prevPos]),
.pos = positions[pos]});
}

Expand All @@ -188,9 +193,13 @@ inline void ParserState::dupAttr(Symbol attr, const PosIdx pos, const PosIdx pre
}

inline void ParserState::addAttr(
ExprAttrs * attrs, AttrPath && attrPath, const ParserLocation & loc, Expr * e, const ParserLocation & exprLoc)
ExprAttrs * attrs,
AttrSelectionPath && attrPath,
const ParserLocation & loc,
Expr * e,
const ParserLocation & exprLoc)
{
AttrPath::iterator i;
AttrSelectionPath::iterator i;
// All attrpaths have at least one attr
assert(!attrPath.empty());
auto pos = at(loc);
Expand Down Expand Up @@ -236,7 +245,7 @@ inline void ParserState::addAttr(
* symbol as its last element.
*/
inline void
ParserState::addAttr(ExprAttrs * attrs, AttrPath & attrPath, const Symbol & symbol, ExprAttrs::AttrDef && def)
ParserState::addAttr(ExprAttrs * attrs, AttrSelectionPath & attrPath, const Symbol & symbol, ExprAttrs::AttrDef && def)
{
ExprAttrs::AttrDefs::iterator j = attrs->attrs->find(symbol);
if (j != attrs->attrs->end()) {
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/include/nix/expr/symbol-table.hh
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public:
return Symbol(*symbols.insert(SymbolStr::Key{store, s, buffer}).first);
}

std::vector<SymbolStr> resolve(const std::vector<Symbol> & symbols) const
std::vector<SymbolStr> resolve(const std::span<const Symbol> & symbols) const
{
std::vector<SymbolStr> result;
result.reserve(symbols.size());
Expand Down
6 changes: 3 additions & 3 deletions src/libexpr/nixexpr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void ExprSelect::show(const SymbolTable & symbols, std::ostream & str) const
{
str << "(";
e->show(symbols, str);
str << ")." << showAttrPath(symbols, getAttrPath());
str << ")." << showAttrSelectionPath(symbols, getAttrPath());
if (def) {
str << " or (";
def->show(symbols, str);
Expand All @@ -69,7 +69,7 @@ void ExprOpHasAttr::show(const SymbolTable & symbols, std::ostream & str) const
{
str << "((";
e->show(symbols, str);
str << ") ? " << showAttrPath(symbols, attrPath) << ")";
str << ") ? " << showAttrSelectionPath(symbols, attrPath) << ")";
}

void ExprAttrs::showBindings(const SymbolTable & symbols, std::ostream & str) const
Expand Down Expand Up @@ -261,7 +261,7 @@ void ExprPos::show(const SymbolTable & symbols, std::ostream & str) const
str << "__curPos";
}

std::string showAttrPath(const SymbolTable & symbols, std::span<const AttrName> attrPath)
std::string showAttrSelectionPath(const SymbolTable & symbols, std::span<const AttrName> attrPath)
{
std::ostringstream out;
bool first = true;
Expand Down
Loading
Loading