diff --git a/lib/Sema/CSBindings.cpp b/lib/Sema/CSBindings.cpp index 56f06391f690e..8b96a726a9045 100644 --- a/lib/Sema/CSBindings.cpp +++ b/lib/Sema/CSBindings.cpp @@ -1646,26 +1646,6 @@ PotentialBindings::inferFromRelational(Constraint *constraint) { /// those types should be opened. void PotentialBindings::infer(Constraint *constraint) { switch (constraint->getKind()) { - case ConstraintKind::OptionalObject: { - // Inference through optional object is allowed if - // one of the types is resolved or "optional" type variable - // cannot be bound to l-value, otherwise there is a - // risk of binding "optional" to an optional type (inferred from - // the "object") and discovering an l-value binding for it later. - auto optionalType = constraint->getFirstType(); - - if (auto *optionalVar = optionalType->getAs()) { - if (optionalVar->getImpl().canBindToLValue()) { - auto objectType = - constraint->getSecondType()->lookThroughAllOptionalTypes(); - if (objectType->isTypeVariableOrMember()) - return; - } - } - - LLVM_FALLTHROUGH; - } - case ConstraintKind::Bind: case ConstraintKind::Equal: case ConstraintKind::BindParam: @@ -1675,6 +1655,7 @@ void PotentialBindings::infer(Constraint *constraint) { case ConstraintKind::Conversion: case ConstraintKind::ArgumentConversion: case ConstraintKind::OperatorArgumentConversion: + case ConstraintKind::OptionalObject: case ConstraintKind::UnresolvedMemberChainBase: { auto binding = inferFromRelational(constraint); if (!binding) diff --git a/lib/Sema/CSSimplify.cpp b/lib/Sema/CSSimplify.cpp index f8000f0033a05..8ac2c27c77052 100644 --- a/lib/Sema/CSSimplify.cpp +++ b/lib/Sema/CSSimplify.cpp @@ -9179,8 +9179,8 @@ ConstraintSystem::simplifyOptionalObjectConstraint( } if (optTy->isPlaceholder()) { - // object type should be simplified because it could be already bound. - recordAnyTypeVarAsPotentialHole(simplifyType(second)); + if (auto *typeVar = second->getAs()) + recordPotentialHole(typeVar); return SolutionKind::Solved; } diff --git a/test/Constraints/diagnostics.swift b/test/Constraints/diagnostics.swift index 7f66c29ae7eee..63f43e779c62e 100644 --- a/test/Constraints/diagnostics.swift +++ b/test/Constraints/diagnostics.swift @@ -1121,11 +1121,9 @@ func rdar17170728() { var j: Int? var k: Int? = 2 - let _ = [i, j, k].reduce(0 as Int?) { // expected-error {{missing argument label 'into:' in call}} - // expected-error@-1 {{cannot convert value of type 'Int?' to expected argument type '(inout @escaping (Bool, Bool) -> Bool?, Int?) throws -> ()'}} + let _ = [i, j, k].reduce(0 as Int?) { $0 && $1 ? $0! + $1! : ($0 ? $0! : ($1 ? $1! : nil)) - // expected-error@-1 {{binary operator '+' cannot be applied to two 'Bool' operands}} - // expected-error@-2 4 {{cannot force unwrap value of non-optional type 'Bool'}} + // expected-error@-1 4 {{optional type 'Int?' cannot be used as a boolean; test for '!= nil' instead}} } let _ = [i, j, k].reduce(0 as Int?) { // expected-error {{missing argument label 'into:' in call}} @@ -1555,18 +1553,18 @@ func testNilCoalescingOperatorRemoveFix() { let _ = "" /* This is a comment */ ?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{13-43=}} let _ = "" // This is a comment - ?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1557:13-1558:10=}} + ?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1555:13-1556:10=}} let _ = "" // This is a comment /* * The blank line below is part of the test case, do not delete it */ - ?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1560:13-1565:10=}} + ?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1558:13-1563:10=}} - if ("" ?? // This is a comment // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{9-1568:9=}} + if ("" ?? // This is a comment // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{9-1566:9=}} "").isEmpty {} if ("" // This is a comment - ?? "").isEmpty {} // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1570:9-1571:12=}} + ?? "").isEmpty {} // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1568:9-1569:12=}} } diff --git a/test/Constraints/optional.swift b/test/Constraints/optional.swift index eb4bb10073909..0434771e95ce0 100644 --- a/test/Constraints/optional.swift +++ b/test/Constraints/optional.swift @@ -598,19 +598,3 @@ do { test(x!) // expected-error {{no exact matches in call to local function 'test'}} // expected-error@-1 {{cannot force unwrap value of non-optional type 'Double'}} } - -// Diagnose cases of invalid chaining when parameter is not optional based on context. -do { - class Test { - var value: Int = 42 - } - - class Container { - let test: Test = Test() - - func loop() { - [test].forEach { $0?.value = 42 } - // expected-error@-1 {{cannot use optional chaining on non-optional value of type 'Test'}} - } - } -} diff --git a/test/stmt/switch_stmt2.swift b/test/stmt/switch_stmt2.swift index 2d72eea6995f9..2d4a546c4fcc0 100644 --- a/test/stmt/switch_stmt2.swift +++ b/test/stmt/switch_stmt2.swift @@ -150,25 +150,3 @@ func fallthrough_not_last(i: Int) { break } } - -// rdar://117871338 - incorrect diagnostic - type of expression is ambiguous when member is missing. -func test_invalid_optional_chaining() { - func test(_: (E) -> Void) { - } - - enum E { - case a - case b - } - - struct S { - var prop: E - } - - test { - switch $0.prop? { // expected-error {{value of type 'E' has no member 'prop'}} - case .a: break - case .b: break - } - } -}