@@ -2003,41 +2003,40 @@ namespace {
2003
2003
case PatternKind::Named: {
2004
2004
auto var = cast<NamedPattern>(pattern)->getDecl ();
2005
2005
2006
- auto ROK = ReferenceOwnership::Strong; // The default.
2007
- if (auto *OA = var->getAttrs ().getAttribute <ReferenceOwnershipAttr>())
2008
- ROK = OA->get ();
2009
-
2010
2006
// If we have a type from an initializer expression, and that
2011
2007
// expression does not produce an InOut type, use it. This
2012
2008
// will avoid exponential typecheck behavior in the case of
2013
2009
// tuples, nested arrays, and dictionary literals.
2014
2010
//
2015
2011
// 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 ();
2025
2017
}
2026
- break ;
2027
- case ReferenceOwnership::Weak:
2028
- break ;
2029
2018
}
2030
2019
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 ();
2033
2023
2034
2024
switch (ROK) {
2035
2025
case ReferenceOwnership::Strong:
2036
2026
case ReferenceOwnership::Unowned:
2037
2027
case ReferenceOwnership::Unmanaged:
2038
- return ty;
2028
+ if (ty)
2029
+ return ty;
2030
+ return CS.createTypeVariable (CS.getConstraintLocator (locator),
2031
+ TVO_CanBindToInOut);
2039
2032
case ReferenceOwnership::Weak:
2040
2033
// 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);
2041
2040
return CS.getTypeChecker ().getOptionalType (var->getLoc (), ty);
2042
2041
}
2043
2042
0 commit comments