@@ -1981,41 +1981,40 @@ namespace {
1981
1981
case PatternKind::Named: {
1982
1982
auto var = cast<NamedPattern>(pattern)->getDecl ();
1983
1983
1984
- auto ROK = ReferenceOwnership::Strong; // The default.
1985
- if (auto *OA = var->getAttrs ().getAttribute <ReferenceOwnershipAttr>())
1986
- ROK = OA->get ();
1987
-
1988
1984
// If we have a type from an initializer expression, and that
1989
1985
// expression does not produce an InOut type, use it. This
1990
1986
// will avoid exponential typecheck behavior in the case of
1991
1987
// tuples, nested arrays, and dictionary literals.
1992
1988
//
1993
1989
// 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 ();
2003
1995
}
2004
- break ;
2005
- case ReferenceOwnership::Weak:
2006
- break ;
2007
1996
}
2008
1997
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 ();
2011
2001
2012
2002
switch (ROK) {
2013
2003
case ReferenceOwnership::Strong:
2014
2004
case ReferenceOwnership::Unowned:
2015
2005
case ReferenceOwnership::Unmanaged:
2016
- return ty;
2006
+ if (ty)
2007
+ return ty;
2008
+ return CS.createTypeVariable (CS.getConstraintLocator (locator),
2009
+ TVO_CanBindToInOut);
2017
2010
case ReferenceOwnership::Weak:
2018
2011
// 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);
2019
2018
return CS.getTypeChecker ().getOptionalType (var->getLoc (), ty);
2020
2019
}
2021
2020
0 commit comments