aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr
diff options
context:
space:
mode:
Diffstat (limited to 'src/libexpr')
-rw-r--r--src/libexpr/eval.cc12
-rw-r--r--src/libexpr/nixexpr.hh25
2 files changed, 29 insertions, 8 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index c72b69af2..22b5890bb 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -1595,7 +1595,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
if (!j) {
if (!i.def) {
error<TypeError>("function '%1%' called without required argument '%2%'",
- (lambda.name ? std::string(symbols[lambda.name]) : "anonymous lambda"),
+ lambda.getName(symbols),
symbols[i.name])
.atPos(lambda.pos)
.withTrace(pos, "from call site")
@@ -1621,7 +1621,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
formalNames.insert(symbols[formal.name]);
auto suggestions = Suggestions::bestMatches(formalNames, symbols[i.name]);
error<TypeError>("function '%1%' called with unexpected argument '%2%'",
- (lambda.name ? std::string(symbols[lambda.name]) : "anonymous lambda"),
+ lambda.getName(symbols),
symbols[i.name])
.atPos(lambda.pos)
.withTrace(pos, "from call site")
@@ -1642,9 +1642,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
? makeDebugTraceStacker(
*this, *lambda.body, env2, positions[lambda.pos],
"while calling %s",
- lambda.name
- ? concatStrings("'", symbols[lambda.name], "'")
- : "anonymous lambda")
+ lambda.getQuotedName(symbols))
: nullptr;
lambda.body->eval(*this, env2, vCur);
@@ -1654,9 +1652,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
e,
lambda.pos,
"while calling %s",
- lambda.name
- ? concatStrings("'", symbols[lambda.name], "'")
- : "anonymous lambda");
+ lambda.getQuotedName(symbols));
if (pos) addErrorTrace(e, pos, "from call site");
}
throw;
diff --git a/src/libexpr/nixexpr.hh b/src/libexpr/nixexpr.hh
index 64a9ddeec..d18c37dda 100644
--- a/src/libexpr/nixexpr.hh
+++ b/src/libexpr/nixexpr.hh
@@ -297,6 +297,31 @@ struct ExprLambda : Expr
std::string showNamePos(const EvalState & state) const;
inline bool hasFormals() const { return formals != nullptr; }
PosIdx getPos() const override { return pos; }
+
+ /** Returns the name of the lambda,
+ * or "anonymous lambda" if it doesn't have one.
+ */
+ inline std::string getName(SymbolTable const & symbols) const
+ {
+ if (this->name) {
+ return symbols[this->name];
+ }
+
+ return "anonymous lambda";
+ }
+
+ /** Returns the name of the lambda in single quotes,
+ * or "anonymous lambda" if it doesn't have one.
+ */
+ inline std::string getQuotedName(SymbolTable const & symbols) const
+ {
+ if (this->name) {
+ return concatStrings("'", symbols[this->name], "'");
+ }
+
+ return "anonymous lambda";
+ }
+
COMMON_METHODS
};