Skip to content

[Constraint system] Handle implicit "some" patterns implicitly, better. #30111

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 2 commits into from
Feb 28, 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
40 changes: 20 additions & 20 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6034,10 +6034,29 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
}
}

// If we have candidates, and we're doing a member lookup for a pattern
// match, unwrap optionals and try again to allow implicit creation of
// optional "some" patterns (spelled "?").
if (result.ViableCandidates.empty() && result.UnviableCandidates.empty() &&
memberLocator &&
memberLocator->isLastElement<LocatorPathElt::PatternMatch>() &&
instanceTy->getOptionalObjectType() &&
baseObjTy->is<AnyMetatypeType>()) {
SmallVector<Type, 2> optionals;
Type instanceObjectTy = instanceTy->lookThroughAllOptionalTypes(optionals);
Type metaObjectType = MetatypeType::get(instanceObjectTy);
auto result = performMemberLookup(
constraintKind, memberName, metaObjectType,
functionRefKind, memberLocator, includeInaccessibleMembers);
result.numImplicitOptionalUnwraps = optionals.size();
result.actualBaseType = metaObjectType;
return result;
}

// If we're looking into a metatype for an unresolved member lookup, look
// through optional types.
//
// FIXME: The short-circuit here is lame.
// FIXME: Unify with the above code path.
if (result.ViableCandidates.empty() &&
baseObjTy->is<AnyMetatypeType>() &&
constraintKind == ConstraintKind::UnresolvedValueMember) {
Expand Down Expand Up @@ -6097,25 +6116,6 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
}
}

// If we have candidates, and we're doing a member lookup for a pattern
// match, unwrap optionals and try again to allow implicit creation of
// optional "some" patterns (spelled "?").
if (result.ViableCandidates.empty() && result.UnviableCandidates.empty() &&
memberLocator &&
memberLocator->isLastElement<LocatorPathElt::PatternMatch>() &&
instanceTy->getOptionalObjectType() &&
baseObjTy->is<AnyMetatypeType>()) {
SmallVector<Type, 2> optionals;
Type instanceObjectTy = instanceTy->lookThroughAllOptionalTypes(optionals);
Type metaObjectType = MetatypeType::get(instanceObjectTy);
auto result = performMemberLookup(
constraintKind, memberName, metaObjectType,
functionRefKind, memberLocator, includeInaccessibleMembers);
result.numImplicitOptionalUnwraps = optionals.size();
result.actualBaseType = metaObjectType;
return result;
}

// If we have no viable or unviable candidates, and we're generating,
// diagnostics, rerun the query with inaccessible members included, so we can
// include them in the unviable candidates list.
Expand Down
19 changes: 19 additions & 0 deletions test/stmt/if_while_var.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,22 @@ enum CaseStaticAmbiguity {

static func C(_: Int) -> CaseStaticAmbiguity { return .C(true) }
}

// Case name/static member ambiguity along with implicit optional unwrapping.
enum HasPayload {
case payload(Int, Bool)

static func payload(_ bool: Bool, int: Int) -> HasPayload {
.payload(int, bool)
}
}

class UsesPayload {
private var eOpt: HasPayload? = nil

deinit {
if case .payload(_, let x) = eOpt {
_ = x
}
}
}