Skip to content

[CSBindings] Delay inference through OptionalObject if "object" is l-value capable #73083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,26 @@ 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<TypeVariableType>()) {
if (optionalVar->getImpl().canBindToLValue()) {
auto objectType =
constraint->getSecondType()->lookThroughAllOptionalTypes();
if (objectType->isTypeVariableOrMember())
return;
}
}

LLVM_FALLTHROUGH;
}

case ConstraintKind::Bind:
case ConstraintKind::Equal:
case ConstraintKind::BindParam:
Expand All @@ -1722,7 +1742,6 @@ 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)
Expand Down
15 changes: 8 additions & 7 deletions test/Constraints/diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1123,9 +1123,11 @@ func rdar17170728() {
var j: Int?
var k: Int? = 2

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

let _ = [i, j, k].reduce(0 as Int?) { // expected-error {{missing argument label 'into:' in call}}
Expand Down Expand Up @@ -1556,18 +1558,17 @@ 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}} {{1558:13-1559:10=}}
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1560:13-1561: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}} {{1563:13-1567:10=}}

?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1561:13-1566: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-1569: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-1570: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}} {{1571:9-1572:12=}}
?? "").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=}}
}
16 changes: 16 additions & 0 deletions test/Constraints/optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,19 @@ 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'}}
}
}
}