Skip to content

[Parse][SR-510] emit error for closures outside of a func #934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 4 additions & 2 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -917,11 +917,13 @@ ERROR(cannot_capture_fields,attribute_parsing,none,
ERROR(expected_closure_result_type,expr_parsing,none,
"expected closure result type after '->'", ())
ERROR(expected_closure_in,expr_parsing,none,
"expected 'in' after the closure signature", ())
"expected 'in' after the closure signature", ())
ERROR(unexpected_tokens_before_closure_in,expr_parsing,none,
"unexpected tokens prior to 'in'", ())
"unexpected tokens prior to 'in'", ())
ERROR(expected_closure_rbrace,expr_parsing,none,
"expected '}' at end of closure", ())
ERROR(closure_not_permitted,expr_parsing,none,
"closures are only allowed inside a function or initializer", ())

WARNING(trailing_closure_excess_newlines,expr_parsing,none,
"trailing closure is separated from call site by multiple newlines", ())
Expand Down
7 changes: 4 additions & 3 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1811,13 +1811,14 @@ ParserResult<Expr> Parser::parseExprClosure() {
parseClosureSignatureIfPresent(captureList, params, throwsLoc, arrowLoc,
explicitResultType, inLoc);

// If the closure was created in the context of an array type signature's
// size expression, there will not be a local context. A parse error will
// be reported at the signature's declaration site.
// If we get a closure expression without a local context, this is somewhere
// a closure cannot appear, such as when trying to parse raw values
// for enum cases.
if (!CurLocalContext) {
skipUntil(tok::r_brace);
if (Tok.is(tok::r_brace))
consumeToken();
diagnose(leftBrace, diag::closure_not_permitted);
return makeParserError();
}

Expand Down
2 changes: 2 additions & 0 deletions test/Parse/recovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ struct ErrorTypeInVarDeclArrayType4 {
}

struct ErrorInFunctionSignatureResultArrayType1 {
// expected-error@+1{{closures are only allowed inside a function or initializer}}
func foo() -> Int[ { // expected-error {{expected '{' in body of function declaration}}
return [0]
}
Expand Down Expand Up @@ -521,6 +522,7 @@ case let (jeb):
}

// rdar://19605164
// expected-error@+4{{closures are only allowed inside a function or initializer}}
// expected-note@+3{{to match this opening '('}}
// expected-error@+2{{use of undeclared type 'S'}}
struct Foo19605164 {
Expand Down
5 changes: 5 additions & 0 deletions test/expr/closure/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,8 @@ func r21375863() {
[UInt8](count: width*height, repeatedValue: 0)
}
}

// SR-510 `case Foo = {!@#$!@#$}` compiles and drops the case
enum SR510 {
case Foo = {} // expected-error {{closures are only allowed inside a function or initializer}}
}