Skip to content

Commit c9aa734

Browse files
authored
Merge pull request #73105 from xedin/reland-delay-inference-from-OptionalObject-6.0
[6.0][CSBindings] Delay inference through OptionalObject if "object" is l-value capable
2 parents 45198f0 + bf7089d commit c9aa734

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1692,6 +1692,26 @@ 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+
16951715
case ConstraintKind::Bind:
16961716
case ConstraintKind::Equal:
16971717
case ConstraintKind::BindParam:
@@ -1701,7 +1721,6 @@ void PotentialBindings::infer(Constraint *constraint) {
17011721
case ConstraintKind::Conversion:
17021722
case ConstraintKind::ArgumentConversion:
17031723
case ConstraintKind::OperatorArgumentConversion:
1704-
case ConstraintKind::OptionalObject:
17051724
case ConstraintKind::UnresolvedMemberChainBase: {
17061725
auto binding = inferFromRelational(constraint);
17071726
if (!binding)

test/Constraints/diagnostics.swift

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

1126-
let _ = [i, j, k].reduce(0 as Int?) {
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 -> ()'}}
11271128
$0 && $1 ? $0! + $1! : ($0 ? $0! : ($1 ? $1! : nil))
1128-
// expected-error@-1 4 {{optional type 'Int?' cannot be used as a boolean; test for '!= nil' instead}}
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'}}
11291131
}
11301132

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

15581560
let _ = "" // This is a comment
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=}}
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=}}
15601562

15611563
let _ = "" // This is a comment
15621564
/*
15631565
* The blank line below is part of the test case, do not delete it
15641566
*/
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=}}
15651568

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=}}
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=}}
15691570
"").isEmpty {}
15701571

15711572
if ("" // This is a comment
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=}}
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=}}
15731574
}

test/Constraints/optional.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,3 +598,19 @@ 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)