mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Land: <rdar://problem/19382905> improve 'if let' to support refutable patterns and untie it from optionals
This changes 'if let' conditions to take general refutable patterns, instead of
taking a irrefutable pattern and implicitly matching against an optional.
Where before you might have written:
if let x = foo() {
you now need to write:
if let x? = foo() {
The upshot of this is that you can write anything in an 'if let' that you can
write in a 'case let' in a switch statement, which is pretty general.
To aid with migration, this special cases certain really common patterns like
the above (and any other irrefutable cases, like "if let (a,b) = foo()", and
tells you where to insert the ?. It also special cases type annotations like
"if let x : AnyObject = " since they are no longer allowed.
For transitional purposes, I have intentionally downgraded the most common
diagnostic into a warning instead of an error. This means that you'll get:
t.swift:26:10: warning: condition requires a refutable pattern match; did you mean to match an optional?
if let a = f() {
^
?
I think this is important to stage in, because this is a pretty significant
source breaking change and not everyone internally may want to deal with it
at the same time. I filed 20166013 to remember to upgrade this to an error.
In addition to being a nice user feature, this is a nice cleanup of the guts
of the compiler, since it eliminates the "isConditional()" bit from
PatternBindingDecl, along with the special case logic in the compiler to handle
it (which variously added and removed Optional around these things).
Swift SVN r26150
This commit is contained in:
@@ -992,7 +992,12 @@ ParserResult<Pattern> Parser::parseMatchingPatternVarOrLet() {
|
||||
assert(Tok.isAny(tok::kw_let, tok::kw_var) && "expects var or let");
|
||||
bool isLet = Tok.is(tok::kw_let);
|
||||
SourceLoc varLoc = consumeToken();
|
||||
|
||||
return parseMatchingPatternAsLetOrVar(isLet, varLoc);
|
||||
}
|
||||
|
||||
ParserResult<Pattern> Parser::parseMatchingPatternAsLetOrVar(bool isLet,
|
||||
SourceLoc varLoc) {
|
||||
// 'var' and 'let' patterns shouldn't nest.
|
||||
if (InVarOrLetPattern)
|
||||
diagnose(varLoc, diag::var_pattern_in_var, unsigned(isLet));
|
||||
@@ -1008,6 +1013,47 @@ ParserResult<Pattern> Parser::parseMatchingPatternVarOrLet() {
|
||||
subPattern.get()));
|
||||
}
|
||||
|
||||
// Swift 1.x supported irrefutable patterns that unwrapped optionals and
|
||||
// allowed type annotations. We specifically handle the common cases of
|
||||
// "if let x = foo()" and "if let x : AnyObject = foo()" for good QoI and
|
||||
// migration. These are not valid modern 'if let' sequences: the former
|
||||
// isn't refutable, and the later isn't a valid matching pattern.
|
||||
//
|
||||
ParserResult<Pattern> Parser::
|
||||
parseSwift1IfLetPattern(bool isLet, SourceLoc VarLoc) {
|
||||
assert(Tok.is(tok::identifier) && peekToken().isAny(tok::equal, tok::colon) &&
|
||||
"caller didn't check our requirements");
|
||||
|
||||
Identifier Name;
|
||||
SourceLoc idLoc = consumeIdentifier(&Name);
|
||||
|
||||
// Produce a nice diagnostic - complain about (and rewrite to 'x?').
|
||||
diagnose(idLoc, diag::expected_refutable_pattern_maybe_optional)
|
||||
.fixItInsertAfter(idLoc, "?");
|
||||
|
||||
// If the type annotation is present, parse it and error.
|
||||
SourceLoc ColonLoc;
|
||||
if (consumeIf(tok::colon, ColonLoc)) {
|
||||
auto type = parseType();
|
||||
if (type.hasCodeCompletion())
|
||||
return makeParserCodeCompletionResult<Pattern>();
|
||||
if (type.isNull())
|
||||
return nullptr;
|
||||
|
||||
diagnose(idLoc, diag::refutable_pattern_no_type_annotation)
|
||||
.fixItRemove(SourceRange(ColonLoc, type.get()->getEndLoc()));
|
||||
}
|
||||
|
||||
// Return a pattern of "let x?".
|
||||
auto result = createBindingFromPattern(idLoc, Name, isLet);
|
||||
result = new (Context) OptionalSomePattern(result, idLoc, true);
|
||||
result = new (Context) VarPattern(VarLoc, isLet, result);
|
||||
|
||||
return makeParserResult(result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// matching-pattern ::= 'is' type
|
||||
ParserResult<Pattern> Parser::parseMatchingPatternIs() {
|
||||
SourceLoc isLoc = consumeToken(tok::kw_is);
|
||||
|
||||
Reference in New Issue
Block a user