When adding '!' in fix-it, make sure that it doesn't fall into the

optional evaluation context that produced the optional.

<rdar://problem/20377684> Oscillating fixit for optional chain calling method that returns non-optional

Swift SVN r28212
This commit is contained in:
Chris Willmore
2015-05-06 19:05:45 +00:00
parent f506d1e0aa
commit f3670d3a6f
2 changed files with 8 additions and 1 deletions

View File

@@ -5905,7 +5905,8 @@ Expr *ConstraintSystem::applySolution(Solution &solution, Expr *expr,
auto diag = TC.diagnose(affected->getLoc(),
diag::missing_unwrap_optional, type);
bool parensNeeded = (getInfixData(DC, affected).getPrecedence() <
IntrinsicPrecedences::PostfixUnaryExpr);
IntrinsicPrecedences::PostfixUnaryExpr) ||
isa<OptionalEvaluationExpr>(affected);
if (parensNeeded) {
diag.fixItInsert(affected->getStartLoc(), "(")
.fixItInsertAfter(affected->getEndLoc(), ")!");

View File

@@ -7,6 +7,7 @@ func f3(Int...) -> Int { }
class A { }
class B : A {
func iAmAB() {}
func createB() -> B { return B() }
}
func f4() -> B { }
@@ -52,6 +53,11 @@ func forgotOptionalBang(a: A, obj: AnyObject) {
var a = A(), b = B()
b = a as? B // expected-error{{value of optional type 'B?' not unwrapped; did you mean to use '!' or '?'?}}{{7-7=(}}{{14-14=)!}}
// rdar://problem/20377684 -- take care that the '!' doesn't fall into an
// optional evaluation context
let bo: B? = b
let b2: B = bo?.createB() // expected-error{{value of optional type 'B?' not unwrapped; did you mean to use '!' or '?'?}}{{15-15=(}}{{28-28=)!}}
}
func forgotAnyObjectBang(obj: AnyObject) {