Skip to content
Merged
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
10 changes: 10 additions & 0 deletions lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,16 @@ Parser::parseStmtConditionElement(SmallVectorImpl<StmtConditionElement> &result,
auto diagLoc = ThePattern.get()->getSemanticsProvidingPattern()->getStartLoc();
diagnose(diagLoc, diag::conditional_var_valid_identifiers_only)
.fixItInsert(diagLoc, "<#identifier#> = ");

// For better recovery, assume the expression pattern as the initializer,
// and synthesize an optional AnyPattern.
auto *semanticPattern = ThePattern.get()->getSemanticsProvidingPattern();
if (auto *EP = dyn_cast<ExprPattern>(semanticPattern)) {
Init = makeParserResult(EP->getSubExpr());
auto *AP = AnyPattern::createImplicit(Context);
ThePattern =
makeParserResult(OptionalSomePattern::createImplicit(Context, AP));
}
} else {
diagnose(Tok, diag::conditional_var_initializer_required);
}
Expand Down
15 changes: 12 additions & 3 deletions test/stmt/if_while_var.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ if var nonOptional { nonOptional = nonOptionalStruct(); _ = nonOptional } // exp
guard let nonOptional else { _ = nonOptional; fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
guard var nonOptional else { _ = nonOptional; fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}

if let nonOptional.property { } // expected-error{{unwrap condition requires a valid identifier}} expected-error{{pattern matching in a condition requires the 'case' keyword}}
if var nonOptional.property { } // expected-error{{unwrap condition requires a valid identifier}} expected-error{{pattern matching in a condition requires the 'case' keyword}}
if let nonOptional.property { }
// expected-error@-1 {{unwrap condition requires a valid identifier}}
// expected-error@-2 {{initializer for conditional binding must have Optional type, not 'Any'}}

if var nonOptional.property { }
// expected-error@-1 {{unwrap condition requires a valid identifier}}
// expected-error@-2 {{initializer for conditional binding must have Optional type, not 'Any'}}

guard let _ = nonOptionalStruct() else { fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
guard let _ = nonOptionalEnum() else { fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalEnum'}}
Expand All @@ -65,7 +70,11 @@ class B {} // expected-note * {{did you mean 'B'?}}
class D : B {}// expected-note * {{did you mean 'D'?}}

// TODO poor recovery in these cases
if let {} // expected-error {{expected '{' after 'if' condition}} expected-error {{pattern matching in a condition requires the 'case' keyword}} expected-error {{unwrap condition requires a valid identifier}}
if let {}
// expected-error@-1 {{expected '{' after 'if' condition}}
// expected-error@-2 {{unwrap condition requires a valid identifier}}
// expected-error@-3 {{initializer for conditional binding must have Optional type, not '() -> ()'}}

if let x = { } // expected-error{{'{' after 'if'}} expected-error{{initializer for conditional binding must have Optional type, not '() -> ()'}}
// expected-warning@-1{{value 'x' was defined but never used}}

Expand Down