Skip to content

[Sema] NFC: Use exhaustive switches in PatternKind::Named #15540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2005,33 +2005,47 @@ namespace {

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

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

// If we have a type from an initializer expression, and that
// expression does not produce an InOut type, use it. This
// will avoid exponential typecheck behavior in the case of
// tuples, nested arrays, and dictionary literals.
//
// Otherwise, create a new type variable.
if (!isWeak && haveBoundExpr) {
auto boundExprTy = CS.getType(boundExpr);
if (!boundExprTy->is<InOutType>())
return boundExprTy->getRValueType();
switch (ROK) {
case ReferenceOwnership::Strong:
case ReferenceOwnership::Unowned:
case ReferenceOwnership::Unmanaged:
if (!var->hasNonPatternBindingInit()) {
if (auto boundExpr = locator.trySimplifyToExpr()) {
auto boundExprTy = CS.getType(boundExpr);
if (!boundExprTy->is<InOutType>())
return boundExprTy->getRValueType();
}
}
break;
case ReferenceOwnership::Weak:
break;
}

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

// For weak variables, use Optional<T>.
if (isWeak)
switch (ROK) {
case ReferenceOwnership::Strong:
case ReferenceOwnership::Unowned:
case ReferenceOwnership::Unmanaged:
return ty;
case ReferenceOwnership::Weak:
// For weak variables, use Optional<T>.
return CS.getTypeChecker().getOptionalType(var->getLoc(), ty);
}

return ty;
llvm_unreachable("Unhandled ReferenceOwnership kind");
}

case PatternKind::Typed: {
Expand Down