Skip to content

Commit e438888

Browse files
committed
restore proper handling of no formals vs. 0 formals
e.g. (foo@{}: 1) { a = 3; } should error, but wasn't with the previous commit
1 parent 4a80c92 commit e438888

File tree

9 files changed

+16
-18
lines changed

9 files changed

+16
-18
lines changed

src/libexpr-tests/primops.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ TEST_F(PrimOpTest, derivation)
771771
ASSERT_EQ(v.type(), nFunction);
772772
ASSERT_TRUE(v.isLambda());
773773
ASSERT_NE(v.lambda().fun, nullptr);
774-
ASSERT_TRUE(v.lambda().fun->hasFormals());
774+
ASSERT_TRUE(v.lambda().fun->hasFormals);
775775
}
776776

777777
TEST_F(PrimOpTest, currentTime)

src/libexpr/eval.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,13 +1496,13 @@ void EvalState::callFunction(Value & fun, std::span<Value *> args, Value & vRes,
14961496

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

1499-
auto size = (!lambda.arg ? 0 : 1) + lambda.nFormals;
1499+
auto size = (!lambda.arg ? 0 : 1) + (lambda.hasFormals ? lambda.getFormals().size() : 0);
15001500
Env & env2(mem.allocEnv(size));
15011501
env2.up = vCur.lambda().env;
15021502

15031503
Displacement displ = 0;
15041504

1505-
if (!lambda.hasFormals())
1505+
if (!lambda.hasFormals)
15061506
env2.values[displ++] = args[0];
15071507
else {
15081508
try {
@@ -1747,7 +1747,7 @@ void EvalState::autoCallFunction(const Bindings & args, Value & fun, Value & res
17471747
}
17481748
}
17491749

1750-
if (!fun.isLambda() || !fun.lambda().fun->hasFormals()) {
1750+
if (!fun.isLambda() || !fun.lambda().fun->hasFormals) {
17511751
res = fun;
17521752
return;
17531753
}

src/libexpr/include/nix/expr/nixexpr.hh

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ struct ExprLambda : Expr
490490
Symbol arg;
491491

492492
bool ellipsis;
493+
bool hasFormals;
493494
uint16_t nFormals;
494495
Formal * formalsStart;
495496

@@ -501,6 +502,7 @@ struct ExprLambda : Expr
501502
: pos(pos)
502503
, arg(arg)
503504
, ellipsis(formals.ellipsis)
505+
, hasFormals(true)
504506
, nFormals(formals.formals.size())
505507
, formalsStart(alloc.allocate_object<Formal>(nFormals))
506508
, body(body)
@@ -511,7 +513,7 @@ struct ExprLambda : Expr
511513
ExprLambda(std::pmr::polymorphic_allocator<char> & alloc, PosIdx pos, Symbol arg, Expr * body)
512514
: pos(pos)
513515
, arg(arg)
514-
, nFormals(0)
516+
, hasFormals(false)
515517
, formalsStart(nullptr)
516518
, body(body) {};
517519

@@ -529,11 +531,6 @@ struct ExprLambda : Expr
529531
void setName(Symbol name) override;
530532
std::string showNamePos(const EvalState & state) const;
531533

532-
inline bool hasFormals() const
533-
{
534-
return nFormals > 0;
535-
}
536-
537534
std::vector<Formal> getFormalsLexicographic(const SymbolTable & symbols) const
538535
{
539536
std::vector<Formal> result(getFormals().begin(), getFormals().end());
@@ -551,6 +548,7 @@ struct ExprLambda : Expr
551548

552549
std::span<Formal> getFormals() const
553550
{
551+
assert(hasFormals);
554552
return {formalsStart, nFormals};
555553
}
556554

src/libexpr/nixexpr.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ void ExprList::show(const SymbolTable & symbols, std::ostream & str) const
154154
void ExprLambda::show(const SymbolTable & symbols, std::ostream & str) const
155155
{
156156
str << "(";
157-
if (hasFormals()) {
157+
if (hasFormals) {
158158
str << "{ ";
159159
bool first = true;
160160
// the natural Symbol ordering is by creation time, which can lead to the
@@ -451,14 +451,14 @@ void ExprLambda::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv>
451451
if (es.debugRepl)
452452
es.exprEnvs.insert(std::make_pair(this, env));
453453

454-
auto newEnv = std::make_shared<StaticEnv>(nullptr, env, nFormals + (!arg ? 0 : 1));
454+
auto newEnv = std::make_shared<StaticEnv>(nullptr, env, (hasFormals ? getFormals().size() : 0) + (!arg ? 0 : 1));
455455

456456
Displacement displ = 0;
457457

458458
if (arg)
459459
newEnv->vars.emplace_back(arg, displ++);
460460

461-
if (hasFormals()) {
461+
if (hasFormals) {
462462
for (auto & i : getFormals())
463463
newEnv->vars.emplace_back(i.name, displ++);
464464

src/libexpr/primops.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3363,7 +3363,7 @@ static void prim_functionArgs(EvalState & state, const PosIdx pos, Value ** args
33633363
if (!args[0]->isLambda())
33643364
state.error<TypeError>("'functionArgs' requires a function").atPos(pos).debugThrow();
33653365

3366-
if (!args[0]->lambda().fun->hasFormals()) {
3366+
if (!args[0]->lambda().fun->hasFormals) {
33673367
v.mkAttrs(&Bindings::emptyBindings);
33683368
return;
33693369
}

src/libexpr/value-to-xml.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static void printValueAsXML(
145145
posToXML(state, xmlAttrs, state.positions[v.lambda().fun->pos]);
146146
XMLOpenElement _(doc, "function", xmlAttrs);
147147

148-
if (v.lambda().fun->hasFormals()) {
148+
if (v.lambda().fun->hasFormals) {
149149
XMLAttrs attrs;
150150
if (v.lambda().fun->arg)
151151
attrs["name"] = state.symbols[v.lambda().fun->arg];

src/libflake/flake.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ static Flake readFlake(
281281
if (auto outputs = vInfo.attrs()->get(sOutputs)) {
282282
expectType(state, nFunction, *outputs->value, outputs->pos);
283283

284-
if (outputs->value->isLambda() && outputs->value->lambda().fun->hasFormals()) {
284+
if (outputs->value->isLambda() && outputs->value->lambda().fun->hasFormals) {
285285
for (auto & formal : outputs->value->lambda().fun->getFormals()) {
286286
if (formal.name != state.s.self)
287287
flake.inputs.emplace(

src/nix/flake.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ struct CmdFlakeCheck : FlakeCommand
468468
if (!v.isLambda()) {
469469
throw Error("overlay is not a function, but %s instead", showType(v));
470470
}
471-
if (v.lambda().fun->hasFormals() || !argHasName(v.lambda().fun->arg, "final"))
471+
if (v.lambda().fun->hasFormals || !argHasName(v.lambda().fun->arg, "final"))
472472
throw Error("overlay does not take an argument named 'final'");
473473
// FIXME: if we have a 'nixpkgs' input, use it to
474474
// evaluate the overlay.

src/nix/nix-build/nix-build.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ static void main_nix_build(int argc, char ** argv)
415415
return false;
416416
}
417417
bool add = false;
418-
if (v.type() == nFunction && v.lambda().fun->hasFormals()) {
418+
if (v.type() == nFunction && v.lambda().fun->hasFormals) {
419419
for (auto & i : v.lambda().fun->getFormals()) {
420420
if (state->symbols[i.name] == "inNixShell") {
421421
add = true;

0 commit comments

Comments
 (0)