Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1311,8 +1311,6 @@ ERROR(dollar_identifier_decl,none,
"cannot declare entity named %0; the '$' prefix is reserved for "
"implicitly-synthesized declarations", (Identifier))

ERROR(anon_closure_arg_not_in_closure,none,
"anonymous closure argument not contained in a closure", ())
ERROR(anon_closure_arg_in_closure_with_args,none,
"anonymous closure arguments cannot be used inside a closure that has "
"explicit arguments", ())
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,8 @@ ERROR(extra_trailing_closure_in_call,none,
ERROR(closure_bad_param,none,
"%select{|trailing }1closure passed to parameter of type %0 that does not "
"accept a closure", (Type, bool))
ERROR(anon_closure_arg_not_in_closure,none,
"anonymous closure argument not contained in a closure", ())
GROUPED_WARNING(unlabeled_trailing_closure_deprecated,
TrailingClosureMatching,Deprecation,
"backward matching of the unlabeled trailing closure is deprecated; label the argument with %0 to suppress this warning",
Expand Down
12 changes: 4 additions & 8 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3192,14 +3192,10 @@ Expr *Parser::parseExprAnonClosureArg() {
auto closure = dyn_cast_or_null<ClosureExpr>(
dyn_cast<AbstractClosureExpr>(CurDeclContext));
if (!closure) {
if (Context.LangOpts.DebuggerSupport) {
auto refKind = DeclRefKind::Ordinary;
auto identifier = Context.getIdentifier(Name);
return new (Context) UnresolvedDeclRefExpr(DeclNameRef(identifier),
refKind, DeclNameLoc(Loc));
}
diagnose(Loc, diag::anon_closure_arg_not_in_closure);
return new (Context) ErrorExpr(Loc);
auto refKind = DeclRefKind::Ordinary;
auto identifier = Context.getIdentifier(Name);
return new (Context) UnresolvedDeclRefExpr(DeclNameRef(identifier), refKind,
DeclNameLoc(Loc));
}

// Check whether the closure already has explicit parameters.
Expand Down
17 changes: 17 additions & 0 deletions lib/Sema/PreCheckTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,23 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
}

auto emitBasicError = [&] {
// Check for anonymous closure arguments (e.g. $0, $1) used outside
// closures.
if (Name.isSimpleName()) {
StringRef nameStr = Name.getBaseIdentifier().str();
if (nameStr.starts_with("$")) {
StringRef numStr = nameStr.substr(1);
unsigned ArgNo;
if (!numStr.getAsInteger(10, ArgNo)) {
auto *closure = dyn_cast_or_null<ClosureExpr>(DC);
if (!closure) {
Context.Diags.diagnose(Loc, diag::anon_closure_arg_not_in_closure)
.highlight(UDRE->getSourceRange());
return;
}
}
}
}
if (Name.isSimpleName(Context.Id_self)) {
// `self` gets diagnosed with a different error when it can't be found.
Context.Diags.diagnose(Loc, diag::cannot_find_self_in_scope)
Expand Down
9 changes: 9 additions & 0 deletions test/Sema/issue-54030.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %target-typecheck-verify-swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to put this test in expr/closure and give it a better name, or merge it with one of the existing test files.


// https://github.com/swiftlang/swift/issues/54030

if $0 {} // expected-error {{anonymous closure argument not contained in a closure}}

#if false
if $0 {}
#endif