Skip to content

Commit 8508e18

Browse files
authored
Merge pull request #58531 from xedin/rdar-92327807
[CSSimplify] Don't attempt to synthesize ~= for optional base types
2 parents dabaa69 + f0ba1a8 commit 8508e18

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
@@ -9216,17 +9216,21 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
92169216
dyn_cast<EnumElementPattern>(patternLoc->getPattern())) {
92179217
auto enumType = baseObjTy->getMetatypeInstanceType();
92189218

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

9225-
// Let's consider current member constraint solved because it's
9226-
// replaced by a new set of constraints that would resolve member
9227-
// type.
9228-
if (!hadErrors)
9229-
return SolutionKind::Solved;
9228+
// Let's consider current member constraint solved because it's
9229+
// replaced by a new set of constraints that would resolve member
9230+
// type.
9231+
if (!hadErrors)
9232+
return SolutionKind::Solved;
9233+
}
92309234
}
92319235
}
92329236
}
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)