Skip to content

Commit

Permalink
report inherit attr errors at the duplicate name
Browse files Browse the repository at this point in the history
previously we reported the error at the beginning of the binding
block (for plain inherits) or the beginning of the attr list (for
inherit-from), effectively hiding where exactly the error happened.

this also carries over to runtime positions of attributes in sets as
reported by unsafeGetAttrPos. we're not worried about this changing
observable eval behavior because it *is* marked unsafe, and the new
behavior is much more useful.
  • Loading branch information
pennae committed Mar 6, 2024
1 parent 4147ecf commit 1edd6fa
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 22 deletions.
6 changes: 6 additions & 0 deletions doc/manual/rl-next/inherit-error-positions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
synopsis: fix duplicate attribute error positions for `inherit`
prs: 9874
---

When an inherit caused a duplicate attribute error the position of the error was not reported correctly, placing the error with the inherit itself or at the start of the bindings block instead of the offending attribute name.
25 changes: 13 additions & 12 deletions src/libexpr/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void yyerror(YYLTYPE * loc, yyscan_t scanner, ParserState * state, const char *
nix::StringToken uri;
nix::StringToken str;
std::vector<nix::AttrName> * attrNames;
std::vector<std::pair<nix::AttrName, nix::PosIdx>> * inheritAttrs;
std::vector<std::pair<nix::PosIdx, nix::Expr *>> * string_parts;
std::vector<std::pair<nix::PosIdx, std::variant<nix::Expr *, nix::StringToken>>> * ind_string_parts;
}
Expand All @@ -97,7 +98,8 @@ void yyerror(YYLTYPE * loc, yyscan_t scanner, ParserState * state, const char *
%type <attrs> binds
%type <formals> formals
%type <formal> formal
%type <attrNames> attrs attrpath
%type <attrNames> attrpath
%type <inheritAttrs> attrs
%type <string_parts> string_parts_interpolated
%type <ind_string_parts> ind_string_parts
%type <e> path_start string_parts string_attr
Expand Down Expand Up @@ -309,13 +311,12 @@ binds
: binds attrpath '=' expr ';' { $$ = $1; state->addAttr($$, std::move(*$2), $4, state->at(@2)); delete $2; }
| binds INHERIT attrs ';'
{ $$ = $1;
for (auto & i : *$3) {
for (auto & [i, iPos] : *$3) {
if ($$->attrs.find(i.symbol) != $$->attrs.end())
state->dupAttr(i.symbol, state->at(@3), $$->attrs[i.symbol].pos);
auto pos = state->at(@3);
state->dupAttr(i.symbol, iPos, $$->attrs[i.symbol].pos);
$$->attrs.emplace(
i.symbol,
ExprAttrs::AttrDef(new ExprVar(CUR_POS, i.symbol), pos, ExprAttrs::AttrDef::Kind::Inherited));
ExprAttrs::AttrDef(new ExprVar(iPos, i.symbol), iPos, ExprAttrs::AttrDef::Kind::Inherited));
}
delete $3;
}
Expand All @@ -325,14 +326,14 @@ binds
$$->inheritFromExprs = std::make_unique<std::vector<Expr *>>();
$$->inheritFromExprs->push_back($4);
auto from = new nix::ExprInheritFrom(state->at(@4), $$->inheritFromExprs->size() - 1);
for (auto & i : *$6) {
for (auto & [i, iPos] : *$6) {
if ($$->attrs.find(i.symbol) != $$->attrs.end())
state->dupAttr(i.symbol, state->at(@6), $$->attrs[i.symbol].pos);
state->dupAttr(i.symbol, iPos, $$->attrs[i.symbol].pos);
$$->attrs.emplace(
i.symbol,
ExprAttrs::AttrDef(
new ExprSelect(CUR_POS, from, i.symbol),
state->at(@6),
new ExprSelect(iPos, from, i.symbol),
iPos,
ExprAttrs::AttrDef::Kind::InheritedFrom));
}
delete $6;
Expand All @@ -341,20 +342,20 @@ binds
;

attrs
: attrs attr { $$ = $1; $1->push_back(AttrName(state->symbols.create($2))); }
: attrs attr { $$ = $1; $1->emplace_back(AttrName(state->symbols.create($2)), state->at(@2)); }
| attrs string_attr
{ $$ = $1;
ExprString * str = dynamic_cast<ExprString *>($2);
if (str) {
$$->push_back(AttrName(state->symbols.create(str->s)));
$$->emplace_back(AttrName(state->symbols.create(str->s)), state->at(@2));
delete str;
} else
throw ParseError({
.msg = HintFmt("dynamic attributes not allowed in inherit"),
.pos = state->positions[state->at(@2)]
});
}
| { $$ = new AttrPath; }
| { $$ = new std::vector<std::pair<AttrName, PosIdx>>; }
;

attrpath
Expand Down
1 change: 1 addition & 0 deletions tests/functional/lang/eval-okay-inherit-attr-pos.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ { column = 17; file = "/pwd/lang/eval-okay-inherit-attr-pos.nix"; line = 4; } { column = 19; file = "/pwd/lang/eval-okay-inherit-attr-pos.nix"; line = 4; } { column = 21; file = "/pwd/lang/eval-okay-inherit-attr-pos.nix"; line = 5; } { column = 23; file = "/pwd/lang/eval-okay-inherit-attr-pos.nix"; line = 5; } ]
12 changes: 12 additions & 0 deletions tests/functional/lang/eval-okay-inherit-attr-pos.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let
d = 0;
x = 1;
y = { inherit d x; };
z = { inherit (y) d x; };
in
[
(builtins.unsafeGetAttrPos "d" y)
(builtins.unsafeGetAttrPos "x" y)
(builtins.unsafeGetAttrPos "d" z)
(builtins.unsafeGetAttrPos "x" z)
]
4 changes: 2 additions & 2 deletions tests/functional/lang/parse-fail-dup-attrs-2.err.exp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error: attribute 'x' already defined at «stdin»:9:5
at «stdin»:10:17:
at «stdin»:10:18:
9| x = 789;
10| inherit (as) x;
| ^
| ^
11| };
4 changes: 2 additions & 2 deletions tests/functional/lang/parse-fail-dup-attrs-3.err.exp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error: attribute 'x' already defined at «stdin»:9:5
at «stdin»:10:17:
at «stdin»:10:18:
9| x = 789;
10| inherit (as) x;
| ^
| ^
11| };
6 changes: 3 additions & 3 deletions tests/functional/lang/parse-fail-dup-attrs-7.err.exp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error: attribute 'x' already defined at «stdin»:6:12
at «stdin»:7:12:
error: attribute 'x' already defined at «stdin»:6:13
at «stdin»:7:13:
6| inherit x;
7| inherit x;
| ^
| ^
8| };
6 changes: 3 additions & 3 deletions tests/functional/lang/parse-fail-regression-20060610.err.exp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error: undefined variable 'gcc'
at «stdin»:8:12:
7|
at «stdin»:9:13:
8| body = ({
| ^
9| inherit gcc;
| ^
10| }).gcc;

0 comments on commit 1edd6fa

Please sign in to comment.