Skip to content

Commit 70276e5

Browse files
authored
Merge pull request swiftlang#73351 from apple/revert-73105-reland-delay-inference-from-OptionalObject-6.0
Revert "[6.0][CSBindings] Delay inference through OptionalObject if "object" is l-value capable"
2 parents a0f488b + f1bc0f6 commit 70276e5

File tree

3 files changed

+8
-44
lines changed

3 files changed

+8
-44
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,26 +1692,6 @@ PotentialBindings::inferFromRelational(Constraint *constraint) {
16921692
/// those types should be opened.
16931693
void PotentialBindings::infer(Constraint *constraint) {
16941694
switch (constraint->getKind()) {
1695-
case ConstraintKind::OptionalObject: {
1696-
// Inference through optional object is allowed if
1697-
// one of the types is resolved or "optional" type variable
1698-
// cannot be bound to l-value, otherwise there is a
1699-
// risk of binding "optional" to an optional type (inferred from
1700-
// the "object") and discovering an l-value binding for it later.
1701-
auto optionalType = constraint->getFirstType();
1702-
1703-
if (auto *optionalVar = optionalType->getAs<TypeVariableType>()) {
1704-
if (optionalVar->getImpl().canBindToLValue()) {
1705-
auto objectType =
1706-
constraint->getSecondType()->lookThroughAllOptionalTypes();
1707-
if (objectType->isTypeVariableOrMember())
1708-
return;
1709-
}
1710-
}
1711-
1712-
LLVM_FALLTHROUGH;
1713-
}
1714-
17151695
case ConstraintKind::Bind:
17161696
case ConstraintKind::Equal:
17171697
case ConstraintKind::BindParam:
@@ -1721,6 +1701,7 @@ void PotentialBindings::infer(Constraint *constraint) {
17211701
case ConstraintKind::Conversion:
17221702
case ConstraintKind::ArgumentConversion:
17231703
case ConstraintKind::OperatorArgumentConversion:
1704+
case ConstraintKind::OptionalObject:
17241705
case ConstraintKind::UnresolvedMemberChainBase: {
17251706
auto binding = inferFromRelational(constraint);
17261707
if (!binding)

test/Constraints/diagnostics.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,11 +1123,9 @@ func rdar17170728() {
11231123
var j: Int?
11241124
var k: Int? = 2
11251125

1126-
let _ = [i, j, k].reduce(0 as Int?) { // expected-error {{missing argument label 'into:' in call}}
1127-
// expected-error@-1 {{cannot convert value of type 'Int?' to expected argument type '(inout @escaping (Bool, Bool) -> Bool?, Int?) throws -> ()'}}
1126+
let _ = [i, j, k].reduce(0 as Int?) {
11281127
$0 && $1 ? $0! + $1! : ($0 ? $0! : ($1 ? $1! : nil))
1129-
// expected-error@-1 {{binary operator '+' cannot be applied to two 'Bool' operands}}
1130-
// expected-error@-2 4 {{cannot force unwrap value of non-optional type 'Bool'}}
1128+
// expected-error@-1 4 {{optional type 'Int?' cannot be used as a boolean; test for '!= nil' instead}}
11311129
}
11321130

11331131
let _ = [i, j, k].reduce(0 as Int?) { // expected-error {{missing argument label 'into:' in call}}
@@ -1558,17 +1556,18 @@ func testNilCoalescingOperatorRemoveFix() {
15581556
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=}}
15591557

15601558
let _ = "" // This is a comment
1561-
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1560:13-1561:10=}}
1559+
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1558:13-1559:10=}}
15621560

15631561
let _ = "" // This is a comment
15641562
/*
15651563
* The blank line below is part of the test case, do not delete it
15661564
*/
1567-
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1563:13-1567:10=}}
15681565

1569-
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-1570:9=}}
1566+
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1561:13-1566:10=}}
1567+
1568+
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-1569:9=}}
15701569
"").isEmpty {}
15711570

15721571
if ("" // This is a comment
1573-
?? "").isEmpty {} // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1572:9-1573:12=}}
1572+
?? "").isEmpty {} // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1571:9-1572:12=}}
15741573
}

test/Constraints/optional.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -598,19 +598,3 @@ do {
598598
test(x!) // expected-error {{no exact matches in call to local function 'test'}}
599599
// expected-error@-1 {{cannot force unwrap value of non-optional type 'Double'}}
600600
}
601-
602-
// Diagnose cases of invalid chaining when parameter is not optional based on context.
603-
do {
604-
class Test {
605-
var value: Int = 42
606-
}
607-
608-
class Container {
609-
let test: Test = Test()
610-
611-
func loop() {
612-
[test].forEach { $0?.value = 42 }
613-
// expected-error@-1 {{cannot use optional chaining on non-optional value of type 'Test'}}
614-
}
615-
}
616-
}

0 commit comments

Comments
 (0)