Skip to content

Commit 700a57a

Browse files
committed
[CSBindings] Delay inference through OptionalObject if "object" is l-value capable
Inference cannot be allowed in cases where both sides are type variables and optional type is l-value capable because it results in binding "optional" to an optional type and later discovering a contextual type that is l-value optional i.e. if "optional type" is resolved by selecting subscript overload.
1 parent 41dc466 commit 700a57a

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,26 @@ PotentialBindings::inferFromRelational(Constraint *constraint) {
14291429
/// those types should be opened.
14301430
void PotentialBindings::infer(Constraint *constraint) {
14311431
switch (constraint->getKind()) {
1432+
case ConstraintKind::OptionalObject: {
1433+
// Inference through optional object is allowed if
1434+
// one of the types is resolved or "optional" type variable
1435+
// cannot be bound to l-value, otherwise there is a
1436+
// risk of binding "optional" to an optional type (inferred from
1437+
// the "object") and discovering an l-value binding for it later.
1438+
auto optionalType = constraint->getFirstType();
1439+
1440+
if (auto *optionalVar = optionalType->getAs<TypeVariableType>()) {
1441+
if (optionalVar->getImpl().canBindToLValue()) {
1442+
auto objectType =
1443+
constraint->getSecondType()->lookThroughAllOptionalTypes();
1444+
if (objectType->isTypeVariableOrMember())
1445+
return;
1446+
}
1447+
}
1448+
1449+
LLVM_FALLTHROUGH;
1450+
}
1451+
14321452
case ConstraintKind::Bind:
14331453
case ConstraintKind::Equal:
14341454
case ConstraintKind::BindParam:
@@ -1438,7 +1458,6 @@ void PotentialBindings::infer(Constraint *constraint) {
14381458
case ConstraintKind::Conversion:
14391459
case ConstraintKind::ArgumentConversion:
14401460
case ConstraintKind::OperatorArgumentConversion:
1441-
case ConstraintKind::OptionalObject:
14421461
case ConstraintKind::UnresolvedMemberChainBase: {
14431462
auto binding = inferFromRelational(constraint);
14441463
if (!binding)

test/Constraints/diagnostics.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,9 +1121,11 @@ func rdar17170728() {
11211121
var j: Int?
11221122
var k: Int? = 2
11231123

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

11291131
let _ = [i, j, k].reduce(0 as Int?) { // expected-error {{missing argument label 'into:' in call}}
@@ -1553,18 +1555,18 @@ func testNilCoalescingOperatorRemoveFix() {
15531555
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=}}
15541556

15551557
let _ = "" // This is a comment
1556-
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1555:13-1556:10=}}
1558+
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1557:13-1558:10=}}
15571559

15581560
let _ = "" // This is a comment
15591561
/*
15601562
* The blank line below is part of the test case, do not delete it
15611563
*/
15621564

1563-
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1558:13-1563:10=}}
1565+
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1560:13-1565:10=}}
15641566

1565-
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=}}
1567+
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=}}
15661568
"").isEmpty {}
15671569

15681570
if ("" // This is a comment
1569-
?? "").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=}}
1571+
?? "").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=}}
15701572
}

0 commit comments

Comments
 (0)