Skip to content

Commit 3d9f753

Browse files
authored
Merge pull request #30111 from DougGregor/pattern-implicit-some-harder
[Constraint system] Handle implicit "some" patterns implicitly, better.
2 parents 0a855a3 + 769ea07 commit 3d9f753

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6034,10 +6034,29 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
60346034
}
60356035
}
60366036

6037+
// If we have candidates, and we're doing a member lookup for a pattern
6038+
// match, unwrap optionals and try again to allow implicit creation of
6039+
// optional "some" patterns (spelled "?").
6040+
if (result.ViableCandidates.empty() && result.UnviableCandidates.empty() &&
6041+
memberLocator &&
6042+
memberLocator->isLastElement<LocatorPathElt::PatternMatch>() &&
6043+
instanceTy->getOptionalObjectType() &&
6044+
baseObjTy->is<AnyMetatypeType>()) {
6045+
SmallVector<Type, 2> optionals;
6046+
Type instanceObjectTy = instanceTy->lookThroughAllOptionalTypes(optionals);
6047+
Type metaObjectType = MetatypeType::get(instanceObjectTy);
6048+
auto result = performMemberLookup(
6049+
constraintKind, memberName, metaObjectType,
6050+
functionRefKind, memberLocator, includeInaccessibleMembers);
6051+
result.numImplicitOptionalUnwraps = optionals.size();
6052+
result.actualBaseType = metaObjectType;
6053+
return result;
6054+
}
6055+
60376056
// If we're looking into a metatype for an unresolved member lookup, look
60386057
// through optional types.
60396058
//
6040-
// FIXME: The short-circuit here is lame.
6059+
// FIXME: Unify with the above code path.
60416060
if (result.ViableCandidates.empty() &&
60426061
baseObjTy->is<AnyMetatypeType>() &&
60436062
constraintKind == ConstraintKind::UnresolvedValueMember) {
@@ -6097,25 +6116,6 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
60976116
}
60986117
}
60996118

6100-
// If we have candidates, and we're doing a member lookup for a pattern
6101-
// match, unwrap optionals and try again to allow implicit creation of
6102-
// optional "some" patterns (spelled "?").
6103-
if (result.ViableCandidates.empty() && result.UnviableCandidates.empty() &&
6104-
memberLocator &&
6105-
memberLocator->isLastElement<LocatorPathElt::PatternMatch>() &&
6106-
instanceTy->getOptionalObjectType() &&
6107-
baseObjTy->is<AnyMetatypeType>()) {
6108-
SmallVector<Type, 2> optionals;
6109-
Type instanceObjectTy = instanceTy->lookThroughAllOptionalTypes(optionals);
6110-
Type metaObjectType = MetatypeType::get(instanceObjectTy);
6111-
auto result = performMemberLookup(
6112-
constraintKind, memberName, metaObjectType,
6113-
functionRefKind, memberLocator, includeInaccessibleMembers);
6114-
result.numImplicitOptionalUnwraps = optionals.size();
6115-
result.actualBaseType = metaObjectType;
6116-
return result;
6117-
}
6118-
61196119
// If we have no viable or unviable candidates, and we're generating,
61206120
// diagnostics, rerun the query with inaccessible members included, so we can
61216121
// include them in the unviable candidates list.

test/stmt/if_while_var.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,22 @@ enum CaseStaticAmbiguity {
219219

220220
static func C(_: Int) -> CaseStaticAmbiguity { return .C(true) }
221221
}
222+
223+
// Case name/static member ambiguity along with implicit optional unwrapping.
224+
enum HasPayload {
225+
case payload(Int, Bool)
226+
227+
static func payload(_ bool: Bool, int: Int) -> HasPayload {
228+
.payload(int, bool)
229+
}
230+
}
231+
232+
class UsesPayload {
233+
private var eOpt: HasPayload? = nil
234+
235+
deinit {
236+
if case .payload(_, let x) = eOpt {
237+
_ = x
238+
}
239+
}
240+
}

0 commit comments

Comments
 (0)