Skip to content

Commit ac7529f

Browse files
committed
Improve diagnostic in cases like 'if let foo.bar'
1 parent 38e2b70 commit ac7529f

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,8 @@ ERROR(conditional_var_initializer_required,none,
10751075
ERROR(wrong_condition_case_location,none,
10761076
"pattern matching binding is spelled with 'case %0', not '%0 case'",
10771077
(StringRef))
1078+
ERROR(conditional_var_valid_identifiers_only,none,
1079+
"unwrap condition requires a valid identifier", ())
10781080

10791081
// If Statement
10801082
ERROR(expected_condition_if,PointsToFirstBadToken,

lib/Parse/ParseStmt.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,13 @@ Parser::parseStmtConditionElement(SmallVectorImpl<StmtConditionElement> &result,
16351635

16361636
declRefExpr->setImplicit();
16371637
Init = makeParserResult(declRefExpr);
1638+
} else if (!ThePattern.isNull()) {
1639+
// If the pattern is present but isn't an identifier, the user wrote
1640+
// something invalid like `let foo.bar`. Emit a special diagnostic for this,
1641+
// with a fix-it prepending "<#identifier#> = "
1642+
auto diagLoc = ThePattern.get()->getSemanticsProvidingPattern()->getStartLoc();
1643+
diagnose(diagLoc, diag::conditional_var_valid_identifiers_only)
1644+
.fixItInsert(diagLoc, "<#identifier#> = ");
16381645
} else {
16391646
diagnose(Tok, diag::conditional_var_initializer_required);
16401647
}

test/stmt/if_while_var.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ if var nonOptional { nonOptional = nonOptionalStruct(); _ = nonOptional } // exp
4242
guard let nonOptional else { _ = nonOptional; fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
4343
guard var nonOptional else { _ = nonOptional; fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
4444

45-
if let nonOptional.property { } // expected-error{{variable binding in a condition requires an initializer}} expected-error{{pattern matching in a condition requires the 'case' keyword}}
46-
if var nonOptional.property { } // expected-error{{variable binding in a condition requires an initializer}} expected-error{{pattern matching in a condition requires the 'case' keyword}}
45+
if let nonOptional.property { } // expected-error{{unwrap condition requires a valid identifier}} expected-error{{pattern matching in a condition requires the 'case' keyword}}
46+
if var nonOptional.property { } // expected-error{{unwrap condition requires a valid identifier}} expected-error{{pattern matching in a condition requires the 'case' keyword}}
4747

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'}}

0 commit comments

Comments
 (0)