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:
Chris Lattner
2015-03-15 07:06:22 +00:00
parent a8fe87968e
commit 20f8f09ea8
96 changed files with 590 additions and 540 deletions

View File

@@ -1307,7 +1307,6 @@ static void synthesizeAddressedMaterializeForSet(FuncDecl *materializeForSet,
SourceLoc(),
bindingPattern,
addressorResult,
/*conditional*/ false,
materializeForSet);
bindingDecl->setImplicit(IsImplicit);
body.push_back(bindingDecl);
@@ -1481,8 +1480,7 @@ void swift::synthesizeObservingAccessors(VarDecl *VD, TypeChecker &TC) {
auto tmpPBD = new (Ctx) PatternBindingDecl(SourceLoc(),
StaticSpellingKind::None,
SourceLoc(),
tmpPattern, OldValueExpr,
/*conditional*/ false, Set);
tmpPattern, OldValueExpr, Set);
tmpPBD->setImplicit();
SetterBody.push_back(tmpPBD);
SetterBody.push_back(OldValue);
@@ -1625,9 +1623,7 @@ static FuncDecl *completeLazyPropertyGetter(VarDecl *VD, VarDecl *Storage,
auto *Tmp1PBD = new (Ctx) PatternBindingDecl(/*StaticLoc*/SourceLoc(),
StaticSpellingKind::None,
/*VarLoc*/SourceLoc(),
Tmp1PBDPattern, Tmp1Init,
/*isConditional*/false,
Get);
Tmp1PBDPattern, Tmp1Init, Get);
Body.push_back(Tmp1PBD);
Body.push_back(Tmp1VD);
@@ -1682,7 +1678,6 @@ static FuncDecl *completeLazyPropertyGetter(VarDecl *VD, VarDecl *Storage,
StaticSpellingKind::None,
InitValue->getStartLoc(),
Tmp2PBDPattern, nullptr,
/*isConditional*/false,
Get);
Tmp2PBD->setInit(InitValue, /*already type checked*/wasChecked);
Body.push_back(Tmp2PBD);
@@ -1734,7 +1729,6 @@ void TypeChecker::completeLazyVarImplementation(VarDecl *VD) {
StaticSpellingKind::None,
/*varloc*/VD->getLoc(),
PBDPattern, /*init*/nullptr,
/*isConditional*/false,
VD->getDeclContext());
PBD->setImplicit();
addMemberToContextIfNeeded(PBD, VD->getDeclContext());