Skip to content

Commit a236c62

Browse files
committed
Fix handling of anonymous closure arguments within macro expansions
We were incorrectly emitting a nonsensical error. Fixes rdar://114340542. (cherry picked from commit 06bcd06)
1 parent fdf50a9 commit a236c62

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3082,9 +3082,18 @@ Expr *Parser::parseExprAnonClosureArg() {
30823082
diagnose(Loc, diag::anon_closure_arg_not_in_closure);
30833083
return new (Context) ErrorExpr(Loc);
30843084
}
3085-
// When the closure already has explicit parameters, offer their names as
3086-
// replacements.
3085+
3086+
// Check whether the closure already has explicit parameters.
30873087
if (auto *params = closure->getParameters()) {
3088+
// If the explicit parameters are due to the anonymous parameters having
3089+
// already been set, retrieve the parameter from there.
3090+
if (closure->hasAnonymousClosureVars() && ArgNo < params->size()) {
3091+
return new (Context) DeclRefExpr(params->get(ArgNo), DeclNameLoc(Loc),
3092+
/*Implicit=*/false);
3093+
}
3094+
3095+
// If the closure already has an explicit parameter, offer its name as
3096+
// a replacement.
30883097
if (ArgNo < params->size() && params->get(ArgNo)->hasName()) {
30893098
auto paramName = params->get(ArgNo)->getNameStr();
30903099
diagnose(Loc, diag::anon_closure_arg_in_closure_with_args_typo, paramName)

test/Macros/macro_expand.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ func testStringifyWithLocalTypes() {
219219
})
220220
}
221221

222+
// Stringify in closures that have anonymous parameters.
223+
func testStringifyWithAnonymousParameters() {
224+
{
225+
_ = #stringify($0 + $1)
226+
}(1, 2)
227+
}
228+
222229
func maybeThrowing() throws -> Int { 5 }
223230
224231
#if TEST_DIAGNOSTICS

0 commit comments

Comments
 (0)