Skip to content

[Sema] NFC - Make #15419 work with weak vars #15542

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
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
37 changes: 18 additions & 19 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2003,41 +2003,40 @@ namespace {
case PatternKind::Named: {
auto var = cast<NamedPattern>(pattern)->getDecl();

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.
switch (ROK) {
case ReferenceOwnership::Strong:
case ReferenceOwnership::Unowned:
case ReferenceOwnership::Unmanaged:
if (!var->hasNonPatternBindingInit()) {
if (auto boundExpr = locator.trySimplifyToExpr()) {
if (!boundExpr->isSemanticallyInOutExpr())
return CS.getType(boundExpr)->getRValueType();
}
auto ty = Type();
if (!var->hasNonPatternBindingInit()) {
if (auto boundExpr = locator.trySimplifyToExpr()) {
if (!boundExpr->isSemanticallyInOutExpr())
ty = CS.getType(boundExpr)->getRValueType();
}
break;
case ReferenceOwnership::Weak:
break;
}

Type ty = CS.createTypeVariable(CS.getConstraintLocator(locator),
TVO_CanBindToInOut);
auto ROK = ReferenceOwnership::Strong; // The default.
if (auto *OA = var->getAttrs().getAttribute<ReferenceOwnershipAttr>())
ROK = OA->get();

switch (ROK) {
case ReferenceOwnership::Strong:
case ReferenceOwnership::Unowned:
case ReferenceOwnership::Unmanaged:
return ty;
if (ty)
return ty;
return CS.createTypeVariable(CS.getConstraintLocator(locator),
TVO_CanBindToInOut);
case ReferenceOwnership::Weak:
// For weak variables, use Optional<T>.
if (ty && ty->getOptionalObjectType())
return ty; // Already Optional<T>.
// Create a fresh type variable to handle overloaded expressions.
if (!ty || ty->is<TypeVariableType>())
ty = CS.createTypeVariable(CS.getConstraintLocator(locator),
TVO_CanBindToInOut);
return CS.getTypeChecker().getOptionalType(var->getLoc(), ty);
}

Expand Down