Skip to content

[CSBindings] Allow subtype inference from Void? for closure result … #78906

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 1 commit into from
Jan 27, 2025
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
5 changes: 3 additions & 2 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2584,7 +2584,8 @@ bool TypeVarBindingProducer::computeNext() {
//
// Let's not perform $T? -> $T for closure result types to avoid having
// to re-discover solutions that differ only in location of optional
// injection.
// injection. `Void` is a special case because in $T_result position
// it has special semantics and enables T? -> Void conversions.
//
// The pattern with such type variables is:
//
Expand All @@ -2595,7 +2596,7 @@ bool TypeVarBindingProducer::computeNext() {
// expression is non-optional), if we allow both the solver would
// find two solutions that differ only in location of optional
// injection.
if (!TypeVar->getImpl().isClosureResultType()) {
if (!TypeVar->getImpl().isClosureResultType() || objTy->isVoid()) {
// If T is a type variable, only attempt this if both the
// type variable we are trying bindings for, and the type
// variable we will attempt to bind, both have the same
Expand Down
22 changes: 22 additions & 0 deletions test/Constraints/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1283,3 +1283,25 @@ do {
}
}
}

// Make sure that closure gets both Void? and Void attempted
// otherwise it won't be possible to type-check the second closure.
do {
struct Semaphore {
func signal() -> Int {}
}

func compute(_ completion: (Semaphore?) -> Void?) {}

func test() {
compute { $0?.signal() }
// expected-warning@-1 {{result of call to 'signal()' is unused}}

true
? compute({ $0?.signal() }) // expected-warning {{result of call to 'signal()' is unused}}
: compute({
let sem = $0!
sem.signal() // expected-warning {{result of call to 'signal()' is unused}}
})
}
}