Repository navigation
Dynamic attrs mixed - #188
Merged
Merged
Conversation
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]>
Merged
Member
Author
|
cc @edolstra |
Closed
zolodev
pushed a commit
to zolodev/nix
that referenced
this pull request
Jan 1, 2024
…ns/actions/checkout-2.3.5 chore(deps): bump actions/checkout from 2.3.4 to 2.3.5
CertainLach
pushed a commit
to deltarocks/nix
that referenced
this pull request
Sep 4, 2025
…84702c0d-09cb-4196-80c5-46df687b947d Release v3.10.0
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)
roberth
added a commit
to roberth/nix
that referenced
this pull request
Aug 9, 2026
…in {Tracing,Replay}Evaluator::apply
Task NixOS#188's consolidation ("one cell per apply") makes the arg
reaching {TracingEvaluator,TracingReplayEvaluator}::apply always
have an argCell already — seedCell for the primop path, or the
applyCell propagation for nested callback paths. The pre-existing
`if (!cell) cell = ArgCell::make(...)` fallback was defensive dead
code that would silently allocate a redundant cell if a future
regression let a cell-less arg through.
Replace with a throw so any regression surfaces immediately
instead of silently corrupting the cell topology. Verified no-fire
on the full functional suite (245/5 unchanged) and the 51 unit
tests — the invariant holds today.
Assisted-by: Claude Code (Claude Opus 4.7)
roberth
added a commit
to roberth/nix
that referenced
this pull request
Aug 9, 2026
Three throws that mirror the symmetric changes to tracing-evaluator.cc: - TRE::apply getId lambda: identity is a cache-boundary-proxy invariant. Missing hex is a construction-site bug, not a user-facing error. - TRE::apply outer-direction queryApply returning null: violates Object::queryApply's contract (non-null return or throw). - TRE::apply arg-cell null check: comment already said "panic on any fallback" but was throw Error. Convert to actual panic() to match the invariant enforced by NixOS#188's cell consolidation. Left as-is: - All `if (!x) return std::nullopt` in walk/computeLiveResponse/ resolveIdentity/resolveProducerChild/dispatchQueryRequest — these are legitimate cache-miss returns. - try/catch swallow blocks around computeWHNFFromObject / queryApply / whnf-parse in evalFile/evalExpr/apply — legitimate "fall through to lazy path" pattern for opportunistic lookup. - `if (!live) continue` in resolveIdentity cell walk — cell.liveObject is populated lazily during evalFile/evalExpr construction. Line 844-845 `if (!fnSelOpt) unreachable();` — already correct pattern. Suite 339/0/7. Assisted-by: Claude Code (Claude Opus 4.7)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds new syntax for attribute names:
attrs."${name}"=>getAttr name attrsattrs ? "${name}"=>isAttrs attrs && hasAttr attrs nameattrs."${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 relativelystraightforward for the
getAttrs/hasAttrscases but is more complex forthe
listToAttrscase 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 restrictionmight 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; }butrec { "${"a"}" = true; b = a; }would be an error or use a from thesurrounding scope if it exists.
Note that the
getAttr,getAttr or default, andhasAttrare allimplemented 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 #167