Skip to content

Commit f0ba1a8

Browse files
committed
[CSSimplify] Don't attempt to synthesize ~= for optional base types
`EnumElement` patterns with optional base type do member lookup on both optional type and its wrapped type but do not synthesize `~=` operator call. Resolves: rdar://92327807
1 parent 3664f59 commit f0ba1a8

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9212,17 +9212,21 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
92129212
dyn_cast<EnumElementPattern>(patternLoc->getPattern())) {
92139213
auto enumType = baseObjTy->getMetatypeInstanceType();
92149214

9215-
// If the synthesis of ~= resulted in errors (i.e. broken stdlib)
9216-
// that would be diagnosed inline, so let's just fall through and
9217-
// let this situation be diagnosed as a missing member.
9218-
auto hadErrors = inferEnumMemberThroughTildeEqualsOperator(
9215+
// Optional base type does not trigger `~=` synthesis, but it tries
9216+
// to find member on both `Optional` and its wrapped type.
9217+
if (!enumType->getOptionalObjectType()) {
9218+
// If the synthesis of ~= resulted in errors (i.e. broken stdlib)
9219+
// that would be diagnosed inline, so let's just fall through and
9220+
// let this situation be diagnosed as a missing member.
9221+
auto hadErrors = inferEnumMemberThroughTildeEqualsOperator(
92199222
*this, enumElement, enumType, memberTy, locator);
92209223

9221-
// Let's consider current member constraint solved because it's
9222-
// replaced by a new set of constraints that would resolve member
9223-
// type.
9224-
if (!hadErrors)
9225-
return SolutionKind::Solved;
9224+
// Let's consider current member constraint solved because it's
9225+
// replaced by a new set of constraints that would resolve member
9226+
// type.
9227+
if (!hadErrors)
9228+
return SolutionKind::Solved;
9229+
}
92269230
}
92279231
}
92289232
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
enum MyEnum {
4+
case test(Int)
5+
}
6+
7+
func test(result: MyEnum, optResult: MyEnum?) {
8+
if let .co = result { // expected-error {{pattern matching in a condition requires the 'case' keyword}}
9+
// expected-error@-1 {{type 'MyEnum' has no member 'co'}}
10+
}
11+
12+
if let .co(42) = result { // expected-error {{pattern matching in a condition requires the 'case' keyword}}
13+
// expected-error@-1 {{type of expression is ambiguous without more context}}
14+
}
15+
16+
if let .co = optResult { // expected-error {{pattern matching in a condition requires the 'case' keyword}}
17+
// expected-error@-1 {{type 'MyEnum?' has no member 'co'}}
18+
}
19+
20+
let _ = {
21+
if let .co = result { // expected-error 2 {{pattern matching in a condition requires the 'case' keyword}}
22+
// expected-error@-1 {{type 'MyEnum' has no member 'co'}}
23+
}
24+
}
25+
26+
let _ = {
27+
if let .co = optResult { // expected-error 2 {{pattern matching in a condition requires the 'case' keyword}}
28+
// expected-error@-1 {{type 'MyEnum?' has no member 'co'}}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)