Skip to content

Dynamic attrs no sugar - #189

Merged
edolstra merged 4 commits into
NixOS:masterfrom
shlevy:dynamic-attrs-no-sugar
Jan 6, 2014
Merged

edolstra merged 4 commits into
NixOS:masterfrom
shlevy:dynamic-attrs-no-sugar

Conversation

@shlevy

@shlevy shlevy commented Jan 1, 2014

Copy link
Copy Markdown
Member

An alternative to #188 that has the same behavior but is not implemented with syntactic sugar, with a result that is IMO cleaner.

Certain desugaring schemes may require the parser to use some builtin
function to do some of the work (e.g. currently `throw` is used to
lazily cause an error if a `<>`-style path is not in the search path)
Unfortunately, these names are not reserved keywords, so an expression
that uses such a syntactic sugar will not see the expected behavior
(see tests/lang/eval-okay-redefine-builtin.nix for an example).

This adds the ExprBuiltin AST type, which when evaluated uses the value
from the rootmost variable scope (which of course is initialized
internally and can't shadow any of the builtins).

Signed-off-by: Shea Levy <[email protected]>
This adds new syntax for attribute names:

* attrs."${name}" => getAttr name attrs
* attrs ? "${name}" => isAttrs attrs && hasAttr attrs name
* attrs."${name}" or def => if attrs ? "${name}" then attrs."${name}" else def
* { "${name}" = value; } => listToAttrs [{ inherit name value; }]

Of course, it's a bit more complicated than that. The attribute chains
can be arbitrarily long and contain combinations of static and dynamic
parts (e.g. attrs."${foo}".bar."${baz}" or qux), which is relatively
straightforward for the getAttrs/hasAttrs cases but is more complex for
the listToAttrs case due to rules about duplicate attribute definitions.

For attribute sets with dynamic attribute names, duplicate static
attributes are detected at parse time while duplicate dynamic attributes
are detected when the attribute set is forced. So, for example, { a =
null; a.b = null; "${"c"}" = true; } will be a parse-time error, while
{ a = {}; "${"a"}".b = null; c = true; } will be an eval-time error
(technically that case could theoretically be detected at parse time,
but the general case would require full evaluation). Moreover, duplicate
dynamic attributes are not allowed even in cases where they would be
with static attributes ({ a.b.d = true; a.b.c = false; } is legal, but {
a."${"b"}".d = true; a."${"b"}".c = false; } is not). This restriction
might be relaxed in the future in cases where the static variant would
not be an error, but it is not obvious that that is desirable.

Finally, recursive attribute sets with dynamic attributes have the
static attributes in scope but not the dynamic ones. So rec { a = true;
"${"b"}" = a; } is equivalent to { a = true; b = true; } but rec {
"${"a"}" = true; b = a; } would be an error or use a from the
surrounding scope if it exists.

Note that the getAttr, getAttr or default, and hasAttr are all
implemented purely in the parser as syntactic sugar, while attribute
sets with dynamic attribute names required changes to the AST to be
implemented cleanly.

This is an alternative solution to and closes NixOS#167

Signed-off-by: Shea Levy <[email protected]>
Since addAttr has to iterate through the AttrPath we pass it, it makes
more sense to just iterate through the AttrNames in addAttr instead. As
an added bonus, this allows attrsets where two dynamic attribute paths
have the same static leading part (see added test case for an example
that failed previously).

Signed-off-by: Shea Levy <[email protected]>
This doesn't change any functionality but moves some behavior out of the
parser and into the evaluator in order to simplify the code.

Signed-off-by: Shea Levy <[email protected]>
@shlevy

shlevy commented Jan 1, 2014

Copy link
Copy Markdown
Member Author

cc @edolstra

@edolstra
edolstra merged commit cd49fe4 into NixOS:master Jan 6, 2014
@edolstra

edolstra commented Jan 6, 2014

Copy link
Copy Markdown
Member

Merged, thanks!

@domenkozar

Copy link
Copy Markdown
Member

No docs?

@garbas

garbas commented Jan 14, 2014

Copy link
Copy Markdown
Member

no 🍻 for @shlevy :)

@shlevy
shlevy deleted the dynamic-attrs-no-sugar branch March 10, 2014 10:54
zolodev pushed a commit to zolodev/nix that referenced this pull request Jan 1, 2024
Update continuous-integration-github-actions.rst
CertainLach pushed a commit to deltarocks/nix that referenced this pull request Sep 4, 2025
roberth added a commit to roberth/nix that referenced this pull request Aug 9, 2026
…ia forwarded cell chain, retire activeCells

Cells were fragmenting per apply: OuterObject::queryApply constructed
one ArgCell(callerScope, argObj) as the result wrapper's argCell, and
OuterApply::run separately constructed another with the same parent
and same arg pointer as its localCell. Observations landed on one
cell (via TracingCallbackArg/callbackState wired to localCell) while
readers reached the other. QCA emission then couldn't identify a
target cell without consulting a global writer stack (activeCells)
whose "innermost active SelectorApply" was already popped by the
time nav-descendants' whnf() fired.

- OuterApplyFn signature: caller passes the pre-constructed
  applyCell rather than callerScope; OuterApply::run reuses it as
  localCell. One cell per apply — observations and attribution
  see the same cell.
- emitCallbackApplyForApplyResult attributes to
  callbackCell->parent (the enclosing SelectorApply's cell, made
  reachable by OuterObject::queryApply's construction). References
  forwarded via the constructed cell chain, not looked up via
  global state.
- activeCells field + push/pop calls (logSelectorOnCell,
  logRootSelectorOnCell, logResult) fully retired.
- TracingObject::maybeGetAttr emits QCA per §7 (per WHNF-producing
  probe) when forcing a cbApplyOrigin descendant's child.

cb-sibling-discrimination-via-observation cold now returns 1100
correctly (was 200 — sigma B's SelectorApply was cache-hitting
sigma A's Terminal at identical cur). Sigma A's QCA fact now folds
into seedCell, shifting `.whatever`'s Terminal cur so sigma B
misses cleanly and cascades to inner.

Warm-DISALLOW paths on cb-sibling-discrimination-via-observation,
cb-lazy-result-multi-attr, and cb-sibling-b-depends-on-a fail with
walker-side response mismatch: cold records QCA response as the
deep-probe WHNF (e.g. int:100 for `.whatever` = x*100), but
walker's SelectorCallbackApply dispatch returns the applyResult's
WHNF (attrset). Semantic gap needs the compositional-Selector
redesign — `SelectorGetAttr{name, from=SelectorCallbackApply{...}}`
with walker-recursive dispatch — plus the arg.f-vs-arg.g contramap
work tracked in NixOS#189. Fixing forward.

Suite 245/5 → 243/7 (cold discrimination gained, two warm-DISALLOW
tests regressed pending walker redesign).

Assisted-by: Claude Code (Claude Opus 4.7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants