Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add named ellipsis #5590

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
19 changes: 16 additions & 3 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1283,9 +1283,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &

ExprLambda & lambda(*vCur.lambda.fun);

auto size =
(lambda.arg.empty() ? 0 : 1) +
(lambda.hasFormals() ? lambda.formals->formals.size() : 0);
auto size = lambda.getEnvSize();
Env & env2(allocEnv(size));
env2.up = vCur.lambda.env;

Expand Down Expand Up @@ -1326,6 +1324,21 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
throwTypeError(pos, "%1% called with unexpected argument '%2%'", lambda, i.name);
abort(); // can't happen
}

if (lambda.formals->ellipsis && lambda.formals->ellipsis->set()) {
Value *otherAttrs = new Value();
Bindings *bindings = new Bindings(args[0]->attrs->size());

otherAttrs->mkAttrs(bindings);

for (auto &i : *args[0]->attrs) {
if (lambda.formals->argNames.find(i.name) ==
lambda.formals->argNames.end()) {
otherAttrs->attrs->push_back(i);
}
}
env2.values[displ++] = otherAttrs;
}
}

nrFunctionCalls++;
Expand Down
20 changes: 16 additions & 4 deletions src/libexpr/nixexpr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ void ExprLambda::show(std::ostream & str) const
if (formals->ellipsis) {
if (!first) str << ", ";
str << "...";
if (formals->ellipsis->set()) {
str << " @ ";
str << *formals->ellipsis;
}
}
str << " }";
if (!arg.empty()) str << " @ ";
Expand Down Expand Up @@ -354,10 +358,7 @@ void ExprList::bindVars(const StaticEnv & env)

void ExprLambda::bindVars(const StaticEnv & env)
{
StaticEnv newEnv(
false, &env,
(hasFormals() ? formals->formals.size() : 0) +
(arg.empty() ? 0 : 1));
StaticEnv newEnv(false, &env, getEnvSize());

Displacement displ = 0;

Expand All @@ -367,6 +368,10 @@ void ExprLambda::bindVars(const StaticEnv & env)
for (auto & i : formals->formals)
newEnv.vars.emplace_back(i.name, displ++);

if (formals->ellipsis->set()) {
newEnv.vars.emplace_back(*formals->ellipsis, displ++);
}

newEnv.sort();

for (auto & i : formals->formals)
Expand Down Expand Up @@ -460,6 +465,13 @@ void ExprLambda::setName(Symbol & name)
body->setName(name);
}

size_t ExprLambda::getEnvSize() {
size_t s = 0;
s += hasFormals() ? formals->formals.size() : 0;
s += hasFormals() && formals->ellipsis && formals->ellipsis->set() ? 1 : 0;
s += arg.empty() ? 0 : 1;
return s;
}

string ExprLambda::showNamePos() const
{
Expand Down
4 changes: 3 additions & 1 deletion src/libexpr/nixexpr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,10 @@ struct Formal
struct Formals
{
typedef std::list<Formal> Formals_;
typedef std::optional<Symbol> Ellipsis;
Formals_ formals;
std::set<Symbol> argNames; // used during parsing
bool ellipsis;
Ellipsis ellipsis;
};

struct ExprLambda : Expr
Expand All @@ -247,6 +248,7 @@ struct ExprLambda : Expr
};
void setName(Symbol & name);
string showNamePos() const;
size_t getEnvSize();
inline bool hasFormals() const { return formals != nullptr; }
COMMON_METHODS
};
Expand Down
26 changes: 23 additions & 3 deletions src/libexpr/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ static void addFormal(const Pos & pos, Formals * formals, const Formal & formal)
formals->formals.push_front(formal);
}

static void setEllipsis(const Pos &, Formals * formals, bool ellipsis)
{
if (ellipsis) formals->ellipsis.emplace();
else formals->ellipsis.reset();
}

static void setEllipsis(const Pos & pos, Formals * formals, Symbol name) {
setEllipsis(pos, formals, true);
formals->ellipsis.emplace(name);
// Check that the symbol we use is not also a formal argument:
settings.requireExperimentalFeature(Xp::NamedEllipsis);
if (!formals->argNames.insert(name).second)
throw ParseError({
.msg = hintfmt("duplicate formal function argument '%1%'", name),
.errPos = pos
});
}


static Expr * stripIndentation(const Pos & pos, SymbolTable & symbols, vector<Expr *> & es)
{
Expand Down Expand Up @@ -570,11 +588,13 @@ formals
: formal ',' formals
{ $$ = $3; addFormal(CUR_POS, $$, *$1); }
| formal
{ $$ = new Formals; addFormal(CUR_POS, $$, *$1); $$->ellipsis = false; }
{ $$ = new Formals; addFormal(CUR_POS, $$, *$1); setEllipsis(CUR_POS, $$, false); }
|
{ $$ = new Formals; $$->ellipsis = false; }
{ $$ = new Formals; setEllipsis(CUR_POS, $$, false); }
| ELLIPSIS '@' ID
{ $$ = new Formals; setEllipsis(CUR_POS, $$, data->symbols.create($3)); }
| ELLIPSIS
{ $$ = new Formals; $$->ellipsis = true; }
{ $$ = new Formals; setEllipsis(CUR_POS, $$, true); }
;

formal
Expand Down
1 change: 1 addition & 0 deletions src/libutil/experimental-features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ std::map<ExperimentalFeature, std::string> stringifiedXpFeatures = {
{ Xp::NixCommand, "nix-command" },
{ Xp::RecursiveNix, "recursive-nix" },
{ Xp::NoUrlLiterals, "no-url-literals" },
{ Xp::NamedEllipsis, "named-ellipsis" },
};

const std::optional<ExperimentalFeature> parseExperimentalFeature(const std::string_view & name)
Expand Down
3 changes: 2 additions & 1 deletion src/libutil/experimental-features.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ enum struct ExperimentalFeature
Flakes,
NixCommand,
RecursiveNix,
NoUrlLiterals
NoUrlLiterals,
NamedEllipsis,
};

/**
Expand Down