Skip to content

[CSBindings] Delay binding chain result if type is an optional of a… #33740

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
Sep 1, 2020
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
18 changes: 18 additions & 0 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,24 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
return None;
}

// If subtyping is allowed and this is a result of an implicit member chain,
// let's delay binding it to an optional until its object type resolved too or
// it has been determined that there is no possibility to resolve it. Otherwise
// we might end up missing solutions since it's allowed to implicitly unwrap
// base type of the chain but it can't be done early - type variable
// representing chain's result type has a different l-valueness comparing
// to generic parameter of the optional.
if (kind == AllowedBindingKind::Subtypes) {
auto *locator = typeVar->getImpl().getLocator();
if (locator &&
locator->isLastElement<LocatorPathElt::UnresolvedMemberChainResult>()) {
auto objectType = type->getOptionalObjectType();
if (objectType && objectType->isTypeVariableOrMember()) {
result.PotentiallyIncomplete = true;
}
}
}

if (type->is<InOutType>() && !typeVar->getImpl().canBindToInOut())
type = LValueType::get(type->getInOutObjectType());
if (type->is<LValueType>() && !typeVar->getImpl().canBindToLValue())
Expand Down
27 changes: 27 additions & 0 deletions test/expr/delayed-ident/member_chains.swift
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,30 @@ extension CurriedGeneric where T == Int {

let _: CurriedGeneric = .createInt(CurriedGeneric())()
let _: CurriedGeneric = .create(CurriedGeneric())(Int.self)

// rdar://problem/68094328 - failed to compile unresolved member with implicit optional promotion
func rdar68094328() {
struct S {
init(string: String) {}

var value: S {
get { S(string: "") }
}

func baz(str: String) -> S {
S(string: str)
}
}

class C {
func bar(_: S) {}
}

func foo<T>(_: (C) -> (T) -> Void, _: T?) {}

func test(str: String) {
foo(C.bar, .init(string: str)) // Ok
foo(C.bar, .init(string: str).value) // Ok
foo(C.bar, .init(string: str).baz(str: "")) // Ok
}
}