Skip to content

Commit 16b5fe3

Browse files
authored
Merge pull request #15540 from davezarzycki/nfc_flip_named_patternkind_polarity
[Sema] NFC: Use exhaustive switches in PatternKind::Named
2 parents 01a9f61 + 62b5dc1 commit 16b5fe3

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

lib/Sema/CSGen.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,33 +2005,47 @@ namespace {
20052005

20062006
case PatternKind::Named: {
20072007
auto var = cast<NamedPattern>(pattern)->getDecl();
2008-
auto isWeak = false;
2009-
if (auto *OA = var->getAttrs().getAttribute<ReferenceOwnershipAttr>())
2010-
isWeak = OA->get() == ReferenceOwnership::Weak;
20112008

2012-
auto boundExpr = locator.trySimplifyToExpr();
2013-
auto haveBoundExpr = boundExpr && !var->hasNonPatternBindingInit();
2009+
auto ROK = ReferenceOwnership::Strong; // The default.
2010+
if (auto *OA = var->getAttrs().getAttribute<ReferenceOwnershipAttr>())
2011+
ROK = OA->get();
20142012

20152013
// If we have a type from an initializer expression, and that
20162014
// expression does not produce an InOut type, use it. This
20172015
// will avoid exponential typecheck behavior in the case of
20182016
// tuples, nested arrays, and dictionary literals.
20192017
//
20202018
// Otherwise, create a new type variable.
2021-
if (!isWeak && haveBoundExpr) {
2022-
auto boundExprTy = CS.getType(boundExpr);
2023-
if (!boundExprTy->is<InOutType>())
2024-
return boundExprTy->getRValueType();
2019+
switch (ROK) {
2020+
case ReferenceOwnership::Strong:
2021+
case ReferenceOwnership::Unowned:
2022+
case ReferenceOwnership::Unmanaged:
2023+
if (!var->hasNonPatternBindingInit()) {
2024+
if (auto boundExpr = locator.trySimplifyToExpr()) {
2025+
auto boundExprTy = CS.getType(boundExpr);
2026+
if (!boundExprTy->is<InOutType>())
2027+
return boundExprTy->getRValueType();
2028+
}
2029+
}
2030+
break;
2031+
case ReferenceOwnership::Weak:
2032+
break;
20252033
}
20262034

20272035
Type ty = CS.createTypeVariable(CS.getConstraintLocator(locator),
20282036
TVO_CanBindToInOut);
20292037

2030-
// For weak variables, use Optional<T>.
2031-
if (isWeak)
2038+
switch (ROK) {
2039+
case ReferenceOwnership::Strong:
2040+
case ReferenceOwnership::Unowned:
2041+
case ReferenceOwnership::Unmanaged:
2042+
return ty;
2043+
case ReferenceOwnership::Weak:
2044+
// For weak variables, use Optional<T>.
20322045
return CS.getTypeChecker().getOptionalType(var->getLoc(), ty);
2046+
}
20332047

2034-
return ty;
2048+
llvm_unreachable("Unhandled ReferenceOwnership kind");
20352049
}
20362050

20372051
case PatternKind::Typed: {

0 commit comments

Comments
 (0)