From 19a93dd02508a7a7dddde633f76044911acb5668 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Fri, 31 May 2024 13:12:14 -0600 Subject: [PATCH] mini-refactor "lambda.name or anonymous lambda" logic Change-Id: I08d39c4ad5b967de526c0d5c5e6299256c7967f3 --- src/libexpr/eval.cc | 12 ++++-------- src/libexpr/nixexpr.hh | 25 +++++++++++++++++++++++++ 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("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("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 };