mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-10 00:08:07 +02:00
Make function arguments retain position info
This allows querying the location of function arguments. E.g. builtins.unsafeGetAttrPos "x" (builtins.functionArgs ({ x }: null)) => { column = 57; file = "/home/infinisil/src/nix/inst/test.nix"; line = 1; }
This commit is contained in:
parent
a7540294cf
commit
c34e96f7e0
5 changed files with 14 additions and 5 deletions
|
@ -209,9 +209,10 @@ struct ExprList : Expr
|
|||
|
||||
struct Formal
|
||||
{
|
||||
Pos pos;
|
||||
Symbol name;
|
||||
Expr * def;
|
||||
Formal(const Symbol & name, Expr * def) : name(name), def(def) { };
|
||||
Formal(const Pos & pos, const Symbol & name, Expr * def) : pos(pos), name(name), def(def) { };
|
||||
};
|
||||
|
||||
struct Formals
|
||||
|
|
|
@ -531,8 +531,8 @@ formals
|
|||
;
|
||||
|
||||
formal
|
||||
: ID { $$ = new Formal(data->symbols.create($1), 0); }
|
||||
| ID '?' expr { $$ = new Formal(data->symbols.create($1), $3); }
|
||||
: ID { $$ = new Formal(CUR_POS, data->symbols.create($1), 0); }
|
||||
| ID '?' expr { $$ = new Formal(CUR_POS, data->symbols.create($1), $3); }
|
||||
;
|
||||
|
||||
%%
|
||||
|
|
|
@ -1354,9 +1354,12 @@ static void prim_functionArgs(EvalState & state, const Pos & pos, Value * * args
|
|||
}
|
||||
|
||||
state.mkAttrs(v, args[0]->lambda.fun->formals->formals.size());
|
||||
for (auto & i : args[0]->lambda.fun->formals->formals)
|
||||
for (auto & i : args[0]->lambda.fun->formals->formals) {
|
||||
// !!! should optimise booleans (allocate only once)
|
||||
mkBool(*state.allocAttr(v, i.name), i.def);
|
||||
Value * value = state.allocValue();
|
||||
v.attrs->push_back(Attr(i.name, value, &i.pos));
|
||||
mkBool(*value, i.def);
|
||||
}
|
||||
v.attrs->sort();
|
||||
}
|
||||
|
||||
|
|
1
tests/lang/eval-okay-getattrpos-functionargs.exp
Normal file
1
tests/lang/eval-okay-getattrpos-functionargs.exp
Normal file
|
@ -0,0 +1 @@
|
|||
{ column = 11; file = "eval-okay-getattrpos-functionargs.nix"; line = 2; }
|
4
tests/lang/eval-okay-getattrpos-functionargs.nix
Normal file
4
tests/lang/eval-okay-getattrpos-functionargs.nix
Normal file
|
@ -0,0 +1,4 @@
|
|||
let
|
||||
fun = { foo }: {};
|
||||
pos = builtins.unsafeGetAttrPos "foo" (builtins.functionArgs fun);
|
||||
in { inherit (pos) column line; file = baseNameOf pos.file; }
|
Loading…
Reference in a new issue