From ea52d4fbc7093582eef84c57b3906e9d7e274fe8 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 6 May 2015 22:12:34 +0000 Subject: [PATCH] fix Conditional Optional binding hides compiler error Swift SVN r28220 --- lib/Sema/TypeCheckStmt.cpp | 8 ++++++++ test/expr/expressions.swift | 19 +++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/Sema/TypeCheckStmt.cpp b/lib/Sema/TypeCheckStmt.cpp index 1511b8e07aa0d..e4e1c0014951c 100644 --- a/lib/Sema/TypeCheckStmt.cpp +++ b/lib/Sema/TypeCheckStmt.cpp @@ -901,6 +901,14 @@ static void diagnoseIgnoredExpr(TypeChecker &TC, Expr *E) { return; } + // If we have an OptionalEvaluationExpr at the top level, then someone is + // "optional chaining" and ignoring the result. Produce a diagnostic if it + // doesn't make sense to ignore it. + if (auto *OEE = + dyn_cast(E->getSemanticsProvidingExpr())) + if (auto *IIO = dyn_cast(OEE->getSubExpr())) + return diagnoseIgnoredExpr(TC, IIO->getSubExpr()); + // FIXME: Complain about literals // Check if we have a call to a function marked warn_unused_result. diff --git a/test/expr/expressions.swift b/test/expr/expressions.swift index 810399a825324..3e979fcddc567 100644 --- a/test/expr/expressions.swift +++ b/test/expr/expressions.swift @@ -506,6 +506,8 @@ ruleVar = Rule("a") // expected-error {{cannot invoke initializer for type 'Rule class C { var x: C? init(other: C?) { x = other } + + func method() {} } 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() { (&x).method() // expected-error {{cannot invoke 'method' with no arguments}} } -// Unused l-value -_ // expected-error{{'_' can only appear in a pattern or on the left side of an assignment}} + +// Unused results. +func unusedExpressionResults() { + // Unused l-value + _ // expected-error{{'_' can only appear in a pattern or on the left side of an assignment}} + + + // Conditional Optional binding hides compiler error + let optionalc:C? = nil + optionalc?.method() // ok + optionalc?.method // expected-error {{expression resolves to an unused function}} +} + + + //===----------------------------------------------------------------------===// // Collection Literals