Skip to content

Commit f5cb50c

Browse files
[Sema] Implicitly tuple a pattern if applicable.
Fixes rdar://problem/58425942.
1 parent e0a9a74 commit f5cb50c

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

lib/Sema/TypeCheckPattern.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,10 @@ namespace {
839839
// 1b. pat
840840
// type ~ ((T1, ..., Tn)) (n >= 2)
841841
// 2. pat ~ (P1, ..., Pm) (m >= 2)
842-
void implicitlyUntuplePatternIfApplicable(DiagnosticEngine &DE,
842+
void implicitlyUntuplePatternIfApplicable(ASTContext &Ctx,
843843
Pattern *&enumElementInnerPat,
844844
Type enumPayloadType) {
845+
auto &DE = Ctx.Diags;
845846
if (auto *tupleType = dyn_cast<TupleType>(enumPayloadType.getPointer())) {
846847
if (tupleType->getNumElements() >= 2
847848
&& enumElementInnerPat->getKind() == PatternKind::Paren) {
@@ -859,9 +860,14 @@ void implicitlyUntuplePatternIfApplicable(DiagnosticEngine &DE,
859860
}
860861
} else if (auto *tupleType = enumPayloadType->getAs<TupleType>()) {
861862
if (tupleType->getNumElements() >= 2
862-
&& enumElementInnerPat->getKind() == PatternKind::Tuple)
863+
&& enumElementInnerPat->getKind() == PatternKind::Tuple) {
863864
DE.diagnose(enumElementInnerPat->getLoc(),
864865
diag::matching_many_patterns_with_tupled_assoc_value);
866+
enumElementInnerPat =
867+
new (Ctx) ParenPattern(enumElementInnerPat->getStartLoc(),
868+
enumElementInnerPat,
869+
enumElementInnerPat->getEndLoc());
870+
}
865871
}
866872
}
867873
}
@@ -1406,7 +1412,7 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
14061412
newSubOptions.setContext(TypeResolverContext::EnumPatternPayload);
14071413
newSubOptions |= TypeResolutionFlags::FromNonInferredPattern;
14081414

1409-
::implicitlyUntuplePatternIfApplicable(Context.Diags, sub, elementType);
1415+
::implicitlyUntuplePatternIfApplicable(Context, sub, elementType);
14101416

14111417
sub = coercePatternToType(
14121418
pattern.forSubPattern(sub, /*retainTopLevel=*/false), elementType,

test/Sema/exhaustive_switch.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,9 +1410,6 @@ enum SR11212Tests {
14101410
// rdar://problem/58578342
14111411
func sr11212_content_generic_pattern_untupled3(b: Box<((Int, Int), Int)>) -> (Int, Int, Int) {
14121412
switch b {
1413-
// expected-note@-1 {{add missing case: '.box((_, _))'}}
1414-
// expected-error@-2 {{switch must be exhaustive}}
1415-
// FIXME: This should not be an error, analogous to the test cases above.
14161413
case let .box((x, y), z): return (x, y, z)
14171414
// expected-warning@-1 {{the enum case has a single tuple as an associated value, but there are several patterns here, implicitly tupling the patterns and trying to match that instead}}
14181415
}

0 commit comments

Comments
 (0)