Skip to content

Commit 0bbe26d

Browse files
authored
Merge pull request #73352 from apple/revert-73083-reland-delay-inference-from-OptionalObject
Revert "[CSBindings] Delay inference through OptionalObject if "object" is l-value capable"
2 parents d380bf9 + 7594e43 commit 0bbe26d

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
@@ -1713,26 +1713,6 @@ PotentialBindings::inferFromRelational(Constraint *constraint) {
17131713
/// those types should be opened.
17141714
void PotentialBindings::infer(Constraint *constraint) {
17151715
switch (constraint->getKind()) {
1716-
case ConstraintKind::OptionalObject: {
1717-
// Inference through optional object is allowed if
1718-
// one of the types is resolved or "optional" type variable
1719-
// cannot be bound to l-value, otherwise there is a
1720-
// risk of binding "optional" to an optional type (inferred from
1721-
// the "object") and discovering an l-value binding for it later.
1722-
auto optionalType = constraint->getFirstType();
1723-
1724-
if (auto *optionalVar = optionalType->getAs<TypeVariableType>()) {
1725-
if (optionalVar->getImpl().canBindToLValue()) {
1726-
auto objectType =
1727-
constraint->getSecondType()->lookThroughAllOptionalTypes();
1728-
if (objectType->isTypeVariableOrMember())
1729-
return;
1730-
}
1731-
}
1732-
1733-
LLVM_FALLTHROUGH;
1734-
}
1735-
17361716
case ConstraintKind::Bind:
17371717
case ConstraintKind::Equal:
17381718
case ConstraintKind::BindParam:
@@ -1742,6 +1722,7 @@ void PotentialBindings::infer(Constraint *constraint) {
17421722
case ConstraintKind::Conversion:
17431723
case ConstraintKind::ArgumentConversion:
17441724
case ConstraintKind::OperatorArgumentConversion:
1725+
case ConstraintKind::OptionalObject:
17451726
case ConstraintKind::UnresolvedMemberChainBase: {
17461727
auto binding = inferFromRelational(constraint);
17471728
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)