Skip to content

Commit 2888edc

Browse files
committed
[NFC] Sema: Tidy up a bit of control flow in TypeChecker::coercePatternToType
1 parent c1d5118 commit 2888edc

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

lib/Sema/TypeCheckPattern.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,10 +1566,12 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
15661566
}
15671567
}
15681568

1569+
Type elementType;
1570+
Pattern *sub = EEP->getSubPattern();
1571+
15691572
// If there is a subpattern, push the enum element type down onto it.
15701573
auto argType = elt->getArgumentInterfaceType();
1571-
if (EEP->hasSubPattern()) {
1572-
Pattern *sub = EEP->getSubPattern();
1574+
if (sub) {
15731575
if (!elt->hasAssociatedValues()) {
15741576
diags.diagnose(EEP->getLoc(),
15751577
diag::enum_element_pattern_assoc_values_mismatch,
@@ -1579,33 +1581,21 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
15791581
.fixItRemove(sub->getSourceRange());
15801582
return nullptr;
15811583
}
1582-
1583-
Type elementType;
1584+
15841585
if (argType)
15851586
elementType = enumTy->getTypeOfMember(elt->getModuleContext(),
15861587
elt, argType);
15871588
else
15881589
elementType = TupleType::getEmpty(Context);
1589-
auto newSubOptions = subOptions;
1590-
newSubOptions.setContext(TypeResolverContext::EnumPatternPayload);
1591-
newSubOptions |= TypeResolutionFlags::FromNonInferredPattern;
15921590

15931591
::repairTupleOrAssociatedValuePatternIfApplicable(
15941592
Context, sub, elementType, elt);
1595-
1596-
sub = coercePatternToType(
1597-
pattern.forSubPattern(sub, /*retainTopLevel=*/false), elementType,
1598-
newSubOptions);
1599-
if (!sub)
1600-
return nullptr;
1601-
1602-
EEP->setSubPattern(sub);
16031593
} else if (argType) {
16041594
// Else if the element pattern has no sub-pattern but the element type has
16051595
// associated values, expand it to be semantically equivalent to an
16061596
// element pattern of wildcards.
1607-
Type elementType = enumTy->getTypeOfMember(elt->getModuleContext(),
1608-
elt, argType);
1597+
elementType =
1598+
enumTy->getTypeOfMember(elt->getModuleContext(), elt, argType);
16091599
SmallVector<TuplePatternElt, 8> elements;
16101600
if (auto *TTy = dyn_cast<TupleType>(elementType.getPointer())) {
16111601
for (auto &elt : TTy->getElements()) {
@@ -1622,17 +1612,22 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
16221612
elements.push_back(TuplePatternElt(Identifier(), SourceLoc(),
16231613
subPattern));
16241614
}
1625-
Pattern *sub = TuplePattern::createSimple(Context, SourceLoc(),
1626-
elements, SourceLoc());
1615+
sub = TuplePattern::createSimple(Context, SourceLoc(), elements,
1616+
SourceLoc());
16271617
sub->setImplicit();
1618+
}
1619+
1620+
if (sub) {
16281621
auto newSubOptions = subOptions;
16291622
newSubOptions.setContext(TypeResolverContext::EnumPatternPayload);
16301623
newSubOptions |= TypeResolutionFlags::FromNonInferredPattern;
1624+
16311625
sub = coercePatternToType(
16321626
pattern.forSubPattern(sub, /*retainTopLevel=*/false), elementType,
16331627
newSubOptions);
16341628
if (!sub)
16351629
return nullptr;
1630+
16361631
EEP->setSubPattern(sub);
16371632
}
16381633

0 commit comments

Comments
 (0)