Skip to content

Commit fecf5cf

Browse files
committed
Simplify inherited attribute handling
This reduces the difference between inherited and non-inherited attribute handling to the choice of which env to use (in recs and lets) by setting the AttrDef::e to a new ExprVar in the parser rather than carrying a separate AttrDef::v VarRef member. As an added bonus, this allows inherited attributes that inherit from a with to delay forcing evaluation of the with's attributes. Signed-off-by: Shea Levy <[email protected]>
1 parent 17d4f4c commit fecf5cf

6 files changed

Lines changed: 44 additions & 36 deletions

File tree

‎src/libexpr/eval.cc‎

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -521,23 +521,17 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
521521
environment, while the inherited attributes are evaluated
522522
in the original environment. */
523523
unsigned int displ = 0;
524-
foreach (AttrDefs::iterator, i, attrs)
525-
if (i->second.inherited) {
526-
/* !!! handle overrides? */
527-
Value * vAttr = state.lookupVar(&env, i->second.var);
528-
env2.values[displ++] = vAttr;
529-
v.attrs->push_back(Attr(i->first, vAttr, &i->second.pos));
530-
} else {
531-
Value * vAttr;
532-
if (hasOverrides) {
533-
vAttr = state.allocValue();
534-
mkThunk(*vAttr, env2, i->second.e);
535-
} else
536-
vAttr = i->second.e->maybeThunk(state, env2);
537-
env2.values[displ++] = vAttr;
538-
v.attrs->push_back(Attr(i->first, vAttr, &i->second.pos));
539-
}
540-
524+
foreach (AttrDefs::iterator, i, attrs) {
525+
Value * vAttr;
526+
if (hasOverrides && !i->second.inherited) {
527+
vAttr = state.allocValue();
528+
mkThunk(*vAttr, env2, i->second.e);
529+
} else
530+
vAttr = i->second.e->maybeThunk(state, i->second.inherited ? env : env2);
531+
env2.values[displ++] = vAttr;
532+
v.attrs->push_back(Attr(i->first, vAttr, &i->second.pos));
533+
}
534+
541535
/* If the rec contains an attribute called `__overrides', then
542536
evaluate it, and add the attributes in that set to the rec.
543537
This allows overriding of recursive attributes, which is
@@ -563,10 +557,7 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
563557

564558
else {
565559
foreach (AttrDefs::iterator, i, attrs)
566-
if (i->second.inherited)
567-
v.attrs->push_back(Attr(i->first, state.lookupVar(&env, i->second.var), &i->second.pos));
568-
else
569-
v.attrs->push_back(Attr(i->first, i->second.e->maybeThunk(state, env), &i->second.pos));
560+
v.attrs->push_back(Attr(i->first, i->second.e->maybeThunk(state, env), &i->second.pos));
570561
}
571562
}
572563

@@ -583,10 +574,7 @@ void ExprLet::eval(EvalState & state, Env & env, Value & v)
583574
environment. */
584575
unsigned int displ = 0;
585576
foreach (ExprAttrs::AttrDefs::iterator, i, attrs->attrs)
586-
if (i->second.inherited)
587-
env2.values[displ++] = state.lookupVar(&env, i->second.var);
588-
else
589-
env2.values[displ++] = i->second.e->maybeThunk(state, env2);
577+
env2.values[displ++] = i->second.e->maybeThunk(state, i->second.inherited ? env : env2);
590578

591579
body->eval(state, env2, v);
592580
}

‎src/libexpr/nixexpr.cc‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,12 @@ void ExprAttrs::bindVars(const StaticEnv & env)
230230
newEnv.vars[i->first] = i->second.displ = displ++;
231231

232232
foreach (AttrDefs::iterator, i, attrs)
233-
if (i->second.inherited) i->second.var.bind(env);
234-
else i->second.e->bindVars(newEnv);
233+
i->second.e->bindVars(i->second.inherited ? env : newEnv);
235234
}
236235

237236
else
238237
foreach (AttrDefs::iterator, i, attrs)
239-
if (i->second.inherited) i->second.var.bind(env);
240-
else i->second.e->bindVars(env);
238+
i->second.e->bindVars(env);
241239
}
242240

243241
void ExprList::bindVars(const StaticEnv & env)
@@ -274,8 +272,7 @@ void ExprLet::bindVars(const StaticEnv & env)
274272
newEnv.vars[i->first] = i->second.displ = displ++;
275273

276274
foreach (ExprAttrs::AttrDefs::iterator, i, attrs->attrs)
277-
if (i->second.inherited) i->second.var.bind(env);
278-
else i->second.e->bindVars(newEnv);
275+
i->second.e->bindVars(i->second.inherited ? env : newEnv);
279276

280277
body->bindVars(newEnv);
281278
}

‎src/libexpr/nixexpr.hh‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,10 @@ struct ExprAttrs : Expr
159159
bool recursive;
160160
struct AttrDef {
161161
bool inherited;
162-
Expr * e; // if not inherited
163-
VarRef var; // if inherited
162+
Expr * e;
164163
Pos pos;
165164
unsigned int displ; // displacement
166-
AttrDef(Expr * e, const Pos & pos) : inherited(false), e(e), pos(pos) { };
167-
AttrDef(const Symbol & name, const Pos & pos) : inherited(true), var(name), pos(pos) { };
165+
AttrDef(Expr * e, const Pos & pos, bool inherited=false) : inherited(inherited), e(e), pos(pos) { };
168166
AttrDef() { };
169167
};
170168
typedef std::map<Symbol, AttrDef> AttrDefs;

‎src/libexpr/parser.y‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ binds
402402
if ($$->attrs.find(*i) != $$->attrs.end())
403403
dupAttr(*i, makeCurPos(@3, data), $$->attrs[*i].pos);
404404
Pos pos = makeCurPos(@3, data);
405-
$$->attrs[*i] = ExprAttrs::AttrDef(*i, pos);
405+
$$->attrs[*i] = ExprAttrs::AttrDef(new ExprVar(*i), pos, true);
406406
}
407407
}
408408
| binds INHERIT '(' expr ')' attrs ';'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"b-overridden"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
let
2+
pkgs_ = with pkgs; {
3+
a = derivation {
4+
name = "a";
5+
system = builtins.currentSystem;
6+
builder = "/bin/sh";
7+
args = [ "-c" "touch $out" ];
8+
inherit b;
9+
};
10+
11+
inherit b;
12+
};
13+
14+
packageOverrides = p: {
15+
b = derivation {
16+
name = "b-overridden";
17+
system = builtins.currentSystem;
18+
builder = "/bin/sh";
19+
args = [ "-c" "touch $out" ];
20+
};
21+
};
22+
23+
pkgs = pkgs_ // (packageOverrides pkgs_);
24+
in pkgs.a.b.name

0 commit comments

Comments
 (0)