From 189795f1aa0474256dd93429e6a94b78fa84a733 Mon Sep 17 00:00:00 2001 From: lf- Date: Fri, 7 Aug 2020 23:07:43 -0700 Subject: [PATCH] Add builtins.unsafeGetLambdaPos This is useful for a potential pure-Nix implementation of #3904. --- src/libexpr/primops.cc | 12 ++++++++++++ tests/lang/eval-okay-getlambdapos.exp | 1 + tests/lang/eval-okay-getlambdapos.nix | 4 ++++ 3 files changed, 17 insertions(+) create mode 100644 tests/lang/eval-okay-getlambdapos.exp create mode 100644 tests/lang/eval-okay-getlambdapos.nix diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 65d36ca0e8f..4d4689016d2 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -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) { @@ -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); diff --git a/tests/lang/eval-okay-getlambdapos.exp b/tests/lang/eval-okay-getlambdapos.exp new file mode 100644 index 00000000000..3e5b68d52f0 --- /dev/null +++ b/tests/lang/eval-okay-getlambdapos.exp @@ -0,0 +1 @@ +{ column = 9; file = "eval-okay-getlambdapos.nix"; line = 2; } diff --git a/tests/lang/eval-okay-getlambdapos.nix b/tests/lang/eval-okay-getlambdapos.nix new file mode 100644 index 00000000000..9ea6537f85d --- /dev/null +++ b/tests/lang/eval-okay-getlambdapos.nix @@ -0,0 +1,4 @@ +let + fun = { foo }: {}; + pos = builtins.unsafeGetLambdaPos fun; +in { inherit (pos) column line; file = baseNameOf pos.file; }