Skip to content

[CSBindings] Try Void as a binding for closure result type variable #15134

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
Mar 12, 2018
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
22 changes: 21 additions & 1 deletion lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,28 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
break;

auto type = binding->BindingType;
if (exactTypes.insert(type->getCanonicalType()).second)
if (exactTypes.insert(type->getCanonicalType()).second) {
result.addPotentialBinding(*binding);

if (auto *locator = typeVar->getImpl().getLocator()) {
auto path = locator->getPath();
auto voidType = TupleType::getEmpty(getASTContext());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ASTContext has TheEmptyTupleType for this (and it's already a CanType).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Included the fix to #15187


// If this is a type variable representing closure result,
// which is on the right-side of some relational constraint
// let's have it try `Void` as well because there is an
// implicit conversion `() -> T` to `() -> Void` and this
// helps to avoid creating a thunk to support it.
if (!path.empty() &&
path.back().getKind() == ConstraintLocator::ClosureResult &&
binding->Kind == AllowedBindingKind::Supertypes &&
exactTypes.insert(voidType->getCanonicalType()).second) {
result.addPotentialBinding(
{voidType, binding->Kind, constraint->getKind()},
/*allowJoinMeet=*/false);
}
}
}
break;
}

Expand Down
35 changes: 35 additions & 0 deletions test/Constraints/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -652,3 +652,38 @@ func rdar36054961() {
str.replaceSubrange(range, with: str[range].reversed())
}])
}

protocol P_37790062 {
associatedtype T
var elt: T { get }
}

func rdar37790062() {
struct S<T> {
init(_ a: () -> T, _ b: () -> T) {}
}

class C1 : P_37790062 {
typealias T = Int
var elt: T { return 42 }
}

class C2 : P_37790062 {
typealias T = (String, Int, Void)
var elt: T { return ("question", 42, ()) }
}

func foo() -> Int { return 42 }
func bar() -> Void {}
func baz() -> (String, Int) { return ("question", 42) }
func bzz<T>(_ a: T) -> T { return a }
func faz<T: P_37790062>(_ a: T) -> T.T { return a.elt }

_ = S({ foo() }, { bar() }) // expected-warning {{result of call to 'foo()' is unused}}
_ = S({ baz() }, { bar() }) // expected-warning {{result of call to 'baz()' is unused}}
_ = S({ bzz(("question", 42)) }, { bar() }) // expected-warning {{result of call to 'bzz' is unused}}
_ = S({ bzz(String.self) }, { bar() }) // expected-warning {{result of call to 'bzz' is unused}}
_ = S({ bzz(((), (()))) }, { bar() }) // expected-warning {{result of call to 'bzz' is unused}}
_ = S({ bzz(C1()) }, { bar() }) // expected-warning {{result of call to 'bzz' is unused}}
_ = S({ faz(C2()) }, { bar() }) // expected-warning {{result of call to 'faz' is unused}}
}
46 changes: 46 additions & 0 deletions test/Constraints/rdar37790062.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// RUN: %target-typecheck-verify-swift

protocol A {
associatedtype V
associatedtype E: Error

init(value: V)
init(error: E)

func foo<U>(value: (V) -> U, error: (E) -> U) -> U
}

enum R<T, E: Error> : A {
case foo(T)
case bar(E)

init(value: T) { self = .foo(value) }
init(error: E) { self = .bar(error) }

func foo<R>(value: (T) -> R, error: (E) -> R) -> R {
fatalError()
}
}

protocol P {
associatedtype V

@discardableResult
func baz(callback: @escaping (V) -> Void) -> Self
}

class C<V> : P {
func baz(callback: @escaping (V) -> Void) -> Self { return self }
}
class D<T, E: Error> : C<R<T, E>> {
init(fn: (_ ret: @escaping (V) -> Void) -> Void) {}
}

extension A where V: P, V.V: A, E == V.V.E {
func bar() -> D<V.V.V, V.V.E> {
return D { complete in
foo(value: { promise in promise.baz { result in } },
error: { complete(R(error: $0)) })
}
}
}