Skip to content

Commit 18abd7e

Browse files
authored
Merge pull request #15542 from davezarzycki/nfc_opt_weak_var_typechecking
[Sema] NFC - Make #15419 work with weak vars
2 parents d4fd608 + f4d4a3d commit 18abd7e

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
@@ -1981,41 +1981,40 @@ namespace {
19811981
case PatternKind::Named: {
19821982
auto var = cast<NamedPattern>(pattern)->getDecl();
19831983

1984-
auto ROK = ReferenceOwnership::Strong; // The default.
1985-
if (auto *OA = var->getAttrs().getAttribute<ReferenceOwnershipAttr>())
1986-
ROK = OA->get();
1987-
19881984
// If we have a type from an initializer expression, and that
19891985
// expression does not produce an InOut type, use it. This
19901986
// will avoid exponential typecheck behavior in the case of
19911987
// tuples, nested arrays, and dictionary literals.
19921988
//
19931989
// Otherwise, create a new type variable.
1994-
switch (ROK) {
1995-
case ReferenceOwnership::Strong:
1996-
case ReferenceOwnership::Unowned:
1997-
case ReferenceOwnership::Unmanaged:
1998-
if (!var->hasNonPatternBindingInit()) {
1999-
if (auto boundExpr = locator.trySimplifyToExpr()) {
2000-
if (!boundExpr->isSemanticallyInOutExpr())
2001-
return CS.getType(boundExpr)->getRValueType();
2002-
}
1990+
auto ty = Type();
1991+
if (!var->hasNonPatternBindingInit()) {
1992+
if (auto boundExpr = locator.trySimplifyToExpr()) {
1993+
if (!boundExpr->isSemanticallyInOutExpr())
1994+
ty = CS.getType(boundExpr)->getRValueType();
20031995
}
2004-
break;
2005-
case ReferenceOwnership::Weak:
2006-
break;
20071996
}
20081997

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

20122002
switch (ROK) {
20132003
case ReferenceOwnership::Strong:
20142004
case ReferenceOwnership::Unowned:
20152005
case ReferenceOwnership::Unmanaged:
2016-
return ty;
2006+
if (ty)
2007+
return ty;
2008+
return CS.createTypeVariable(CS.getConstraintLocator(locator),
2009+
TVO_CanBindToInOut);
20172010
case ReferenceOwnership::Weak:
20182011
// For weak variables, use Optional<T>.
2012+
if (ty && ty->getOptionalObjectType())
2013+
return ty; // Already Optional<T>.
2014+
// Create a fresh type variable to handle overloaded expressions.
2015+
if (!ty || ty->is<TypeVariableType>())
2016+
ty = CS.createTypeVariable(CS.getConstraintLocator(locator),
2017+
TVO_CanBindToInOut);
20192018
return CS.getTypeChecker().getOptionalType(var->getLoc(), ty);
20202019
}
20212020

0 commit comments

Comments
 (0)