Skip to content

Commit

Permalink
Add builtins.unsafeGetLambdaPos
Browse files Browse the repository at this point in the history
This is useful for a potential pure-Nix implementation of NixOS#3904.
  • Loading branch information
lf- committed Aug 8, 2020
1 parent edfd676 commit 189795f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,17 @@ static void prim_genericClosure(EvalState & state, const Pos & pos, Value * * ar
v.listElems()[n++] = i;
}

/* Return position information of the given lambda. */
void prim_unsafeGetLambdaPos(EvalState & state, const Pos & pos, Value * * args, Value & v)
{
/* ensure the argument is a function */
state.forceValue(*args[0], pos);
if (args[0]->type != tLambda) {
throwTypeError(pos, "value is %1% while a lambda was expected", *args[0]);
}

state.mkPos(v, &args[0]->lambda.fun->pos);
}

static void prim_abort(EvalState & state, const Pos & pos, Value * * args, Value & v)
{
Expand Down Expand Up @@ -2314,6 +2325,7 @@ void EvalState::createBaseEnv()
addPrimOp("__isBool", 1, prim_isBool);
addPrimOp("__isPath", 1, prim_isPath);
addPrimOp("__genericClosure", 1, prim_genericClosure);
addPrimOp("__unsafeGetLambdaPos", 1, prim_unsafeGetLambdaPos);
addPrimOp("abort", 1, prim_abort);
addPrimOp("__addErrorContext", 2, prim_addErrorContext);
addPrimOp("__tryEval", 1, prim_tryEval);
Expand Down
1 change: 1 addition & 0 deletions tests/lang/eval-okay-getlambdapos.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ column = 9; file = "eval-okay-getlambdapos.nix"; line = 2; }
4 changes: 4 additions & 0 deletions tests/lang/eval-okay-getlambdapos.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let
fun = { foo }: {};
pos = builtins.unsafeGetLambdaPos fun;
in { inherit (pos) column line; file = baseNameOf pos.file; }

0 comments on commit 189795f

Please sign in to comment.