Add helper function to check whether a function arg is 'X' or '_X'

Also allow '_'.
This commit is contained in:
Eelco Dolstra 2021-11-04 14:52:35 +01:00
parent c5fd0b46ae
commit c4bd6a15c2

View file

@ -252,6 +252,14 @@ struct CmdFlakeInfo : CmdFlakeMetadata
} }
}; };
static bool argHasName(std::string_view arg, std::string_view expected)
{
return
arg == expected
|| arg == "_"
|| (hasPrefix(arg, "_") && arg.substr(1) == expected);
}
struct CmdFlakeCheck : FlakeCommand struct CmdFlakeCheck : FlakeCommand
{ {
bool build = true; bool build = true;
@ -346,14 +354,14 @@ struct CmdFlakeCheck : FlakeCommand
auto checkOverlay = [&](const std::string & attrPath, Value & v, const Pos & pos) { auto checkOverlay = [&](const std::string & attrPath, Value & v, const Pos & pos) {
try { try {
state->forceValue(v, pos); state->forceValue(v, pos);
if (!v.isLambda() || v.lambda.fun->hasFormals() || if (!v.isLambda()
(std::string(v.lambda.fun->arg) != "final" && || v.lambda.fun->hasFormals()
std::string(v.lambda.fun->arg) != "_final")) || !argHasName(v.lambda.fun->arg, "final"))
throw Error("overlay does not take an argument named 'final'"); throw Error("overlay does not take an argument named 'final'");
auto body = dynamic_cast<ExprLambda *>(v.lambda.fun->body); auto body = dynamic_cast<ExprLambda *>(v.lambda.fun->body);
if (!body || body->hasFormals() || if (!body
(std::string(body->arg) != "prev" && || body->hasFormals()
std::string(body->arg) != "_prev")) || !argHasName(body->arg, "prev"))
throw Error("overlay does not take an argument named 'prev'"); throw Error("overlay does not take an argument named 'prev'");
// FIXME: if we have a 'nixpkgs' input, use it to // FIXME: if we have a 'nixpkgs' input, use it to
// evaluate the overlay. // evaluate the overlay.