Skip to content

Commit a0b0e36

Browse files
Merge pull request #29213 from varungandhi-apple/vg-fix-implicit-tupling-bug
We did the implicit untupling but forgot to do implicit tupling. Oops.
2 parents 13e865c + f5cb50c commit a0b0e36

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,14 @@ enum SR11212Tests {
14071407
}
14081408
}
14091409

1410+
// rdar://problem/58578342
1411+
func sr11212_content_generic_pattern_untupled3(b: Box<((Int, Int), Int)>) -> (Int, Int, Int) {
1412+
switch b {
1413+
case let .box((x, y), z): return (x, y, z)
1414+
// 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}}
1415+
}
1416+
}
1417+
14101418
func sr11212_content_generic_pattern_ambiguous1(b: Box<(Int, Int)>) -> (Int, Int) {
14111419
switch b {
14121420
case .box(let b_): return b_

0 commit comments

Comments
 (0)