Skip to content

Commit f4d4a3d

Browse files
committed
[Sema] NFC - Generalize #15419 to work with weak vars
1 parent 99ccfb1 commit f4d4a3d

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

lib/Sema/CSGen.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,41 +2003,40 @@ namespace {
20032003
case PatternKind::Named: {
20042004
auto var = cast<NamedPattern>(pattern)->getDecl();
20052005

2006-
auto ROK = ReferenceOwnership::Strong; // The default.
2007-
if (auto *OA = var->getAttrs().getAttribute<ReferenceOwnershipAttr>())
2008-
ROK = OA->get();
2009-
20102006
// If we have a type from an initializer expression, and that
20112007
// expression does not produce an InOut type, use it. This
20122008
// will avoid exponential typecheck behavior in the case of
20132009
// tuples, nested arrays, and dictionary literals.
20142010
//
20152011
// Otherwise, create a new type variable.
2016-
switch (ROK) {
2017-
case ReferenceOwnership::Strong:
2018-
case ReferenceOwnership::Unowned:
2019-
case ReferenceOwnership::Unmanaged:
2020-
if (!var->hasNonPatternBindingInit()) {
2021-
if (auto boundExpr = locator.trySimplifyToExpr()) {
2022-
if (!boundExpr->isSemanticallyInOutExpr())
2023-
return CS.getType(boundExpr)->getRValueType();
2024-
}
2012+
auto ty = Type();
2013+
if (!var->hasNonPatternBindingInit()) {
2014+
if (auto boundExpr = locator.trySimplifyToExpr()) {
2015+
if (!boundExpr->isSemanticallyInOutExpr())
2016+
ty = CS.getType(boundExpr)->getRValueType();
20252017
}
2026-
break;
2027-
case ReferenceOwnership::Weak:
2028-
break;
20292018
}
20302019

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

20342024
switch (ROK) {
20352025
case ReferenceOwnership::Strong:
20362026
case ReferenceOwnership::Unowned:
20372027
case ReferenceOwnership::Unmanaged:
2038-
return ty;
2028+
if (ty)
2029+
return ty;
2030+
return CS.createTypeVariable(CS.getConstraintLocator(locator),
2031+
TVO_CanBindToInOut);
20392032
case ReferenceOwnership::Weak:
20402033
// For weak variables, use Optional<T>.
2034+
if (ty && ty->getOptionalObjectType())
2035+
return ty; // Already Optional<T>.
2036+
// Create a fresh type variable to handle overloaded expressions.
2037+
if (!ty || ty->is<TypeVariableType>())
2038+
ty = CS.createTypeVariable(CS.getConstraintLocator(locator),
2039+
TVO_CanBindToInOut);
20412040
return CS.getTypeChecker().getOptionalType(var->getLoc(), ty);
20422041
}
20432042

0 commit comments

Comments
 (0)