Skip to content

Commit cc85edf

Browse files
committed
Clean up implementation, add more tests
1 parent 14076df commit cc85edf

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

lib/Parse/ParseStmt.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,16 +1626,16 @@ Parser::parseStmtConditionElement(SmallVectorImpl<StmtConditionElement> &result,
16261626
SyntaxParsingContext InitCtxt(SyntaxContext, SyntaxKind::InitializerClause);
16271627
consumeToken();
16281628
Init = parseExprBasic(diag::expected_expr_conditional_var);
1629-
} else if (!ThePattern.isNull() && !ThePattern.getPtrOrNull()->getBoundName().empty()) {
1630-
auto bindingName = new DeclNameRef(ThePattern.getPtrOrNull()->getBoundName());
1631-
auto loc = new DeclNameLoc(ThePattern.getPtrOrNull()->getEndLoc());
1632-
auto declRefExpr = new (Context) UnresolvedDeclRefExpr(*bindingName,
1629+
} else if (!ThePattern.getPtrOrNull()->getBoundName().empty()) {
1630+
auto bindingName = DeclNameRef(ThePattern.getPtrOrNull()->getBoundName());
1631+
auto loc = DeclNameLoc(ThePattern.getPtrOrNull()->getEndLoc());
1632+
auto declRefExpr = new (Context) UnresolvedDeclRefExpr(bindingName,
16331633
DeclRefKind::Ordinary,
1634-
*loc);
1634+
loc);
16351635

16361636
declRefExpr->setImplicit();
16371637
Init = makeParserResult(declRefExpr);
1638-
} else if (!ThePattern.isNull() && BindingKindStr != "case") {
1638+
} else if (BindingKindStr != "case") {
16391639
// If the pattern is present but isn't an identifier, the user wrote
16401640
// something invalid like `let foo.bar`. Emit a special diagnostic for this,
16411641
// with a fix-it prepending "<#identifier#> = "

test/stmt/if_while_var.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,19 @@ if var nonOptional.property { } // expected-error{{unwrap condition requires a v
4848
guard let _ = nonOptionalStruct() else { fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
4949
guard let _ = nonOptionalEnum() else { fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalEnum'}}
5050

51+
let optional: String? = nil
52+
if case let optional? { _ = optional } // expected-error{{variable binding in a condition requires an initializer}}
53+
if case let .some(optional) { _ = optional } // expected-error{{variable binding in a condition requires an initializer}}
54+
if case .some(let optional) { _ = optional } // expected-error{{variable binding in a condition requires an initializer}}
55+
5156
if case let x? = nonOptionalStruct() { _ = x } // expected-error{{'?' pattern cannot match values of type 'NonOptionalStruct'}}
5257
if case let x? = nonOptionalEnum() { _ = x } // expected-error{{'?' pattern cannot match values of type 'NonOptionalEnum'}}
5358

59+
if let x { _ = x } // expected-error{{cannot find 'x' in scope}}
60+
61+
if let optional: String { _ = optional }
62+
if let optional: Int { _ = optional } // expected-error{{cannot convert value of type 'String?' to specified type 'Int?'}}
63+
5464
class B {} // expected-note * {{did you mean 'B'?}}
5565
class D : B {}// expected-note * {{did you mean 'D'?}}
5666

0 commit comments

Comments
 (0)