Skip to content

Commit ea52d4f

Browse files
committed
fix <rdar://problem/20749592> Conditional Optional binding hides compiler error
Swift SVN r28220
1 parent ab09922 commit ea52d4f

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/Sema/TypeCheckStmt.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,14 @@ static void diagnoseIgnoredExpr(TypeChecker &TC, Expr *E) {
901901
return;
902902
}
903903

904+
// If we have an OptionalEvaluationExpr at the top level, then someone is
905+
// "optional chaining" and ignoring the result. Produce a diagnostic if it
906+
// doesn't make sense to ignore it.
907+
if (auto *OEE =
908+
dyn_cast<OptionalEvaluationExpr>(E->getSemanticsProvidingExpr()))
909+
if (auto *IIO = dyn_cast<InjectIntoOptionalExpr>(OEE->getSubExpr()))
910+
return diagnoseIgnoredExpr(TC, IIO->getSubExpr());
911+
904912
// FIXME: Complain about literals
905913

906914
// Check if we have a call to a function marked warn_unused_result.

test/expr/expressions.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ ruleVar = Rule("a") // expected-error {{cannot invoke initializer for type 'Rule
506506
class C {
507507
var x: C?
508508
init(other: C?) { x = other }
509+
510+
func method() {}
509511
}
510512
511513
var c = C(3) // expected-error {{cannot invoke initializer for type 'C' with an argument list of type '(Int)'}} expected-note{{expected an argument list of type '(other: C?)'}}
@@ -587,8 +589,21 @@ func test() {
587589
(&x).method() // expected-error {{cannot invoke 'method' with no arguments}}
588590
}
589591
590-
// Unused l-value
591-
_ // expected-error{{'_' can only appear in a pattern or on the left side of an assignment}}
592+
593+
// Unused results.
594+
func unusedExpressionResults() {
595+
// Unused l-value
596+
_ // expected-error{{'_' can only appear in a pattern or on the left side of an assignment}}
597+
598+
599+
// <rdar://problem/20749592> Conditional Optional binding hides compiler error
600+
let optionalc:C? = nil
601+
optionalc?.method() // ok
602+
optionalc?.method // expected-error {{expression resolves to an unused function}}
603+
}
604+
605+
606+
592607
593608
//===----------------------------------------------------------------------===//
594609
// Collection Literals

0 commit comments

Comments
 (0)