@@ -2005,33 +2005,47 @@ namespace {
2005
2005
2006
2006
case PatternKind::Named: {
2007
2007
auto var = cast<NamedPattern>(pattern)->getDecl ();
2008
- auto isWeak = false ;
2009
- if (auto *OA = var->getAttrs ().getAttribute <ReferenceOwnershipAttr>())
2010
- isWeak = OA->get () == ReferenceOwnership::Weak;
2011
2008
2012
- auto boundExpr = locator.trySimplifyToExpr ();
2013
- auto haveBoundExpr = boundExpr && !var->hasNonPatternBindingInit ();
2009
+ auto ROK = ReferenceOwnership::Strong; // The default.
2010
+ if (auto *OA = var->getAttrs ().getAttribute <ReferenceOwnershipAttr>())
2011
+ ROK = OA->get ();
2014
2012
2015
2013
// If we have a type from an initializer expression, and that
2016
2014
// expression does not produce an InOut type, use it. This
2017
2015
// will avoid exponential typecheck behavior in the case of
2018
2016
// tuples, nested arrays, and dictionary literals.
2019
2017
//
2020
2018
// Otherwise, create a new type variable.
2021
- if (!isWeak && haveBoundExpr) {
2022
- auto boundExprTy = CS.getType (boundExpr);
2023
- if (!boundExprTy->is <InOutType>())
2024
- return boundExprTy->getRValueType ();
2019
+ switch (ROK) {
2020
+ case ReferenceOwnership::Strong:
2021
+ case ReferenceOwnership::Unowned:
2022
+ case ReferenceOwnership::Unmanaged:
2023
+ if (!var->hasNonPatternBindingInit ()) {
2024
+ if (auto boundExpr = locator.trySimplifyToExpr ()) {
2025
+ auto boundExprTy = CS.getType (boundExpr);
2026
+ if (!boundExprTy->is <InOutType>())
2027
+ return boundExprTy->getRValueType ();
2028
+ }
2029
+ }
2030
+ break ;
2031
+ case ReferenceOwnership::Weak:
2032
+ break ;
2025
2033
}
2026
2034
2027
2035
Type ty = CS.createTypeVariable (CS.getConstraintLocator (locator),
2028
2036
TVO_CanBindToInOut);
2029
2037
2030
- // For weak variables, use Optional<T>.
2031
- if (isWeak)
2038
+ switch (ROK) {
2039
+ case ReferenceOwnership::Strong:
2040
+ case ReferenceOwnership::Unowned:
2041
+ case ReferenceOwnership::Unmanaged:
2042
+ return ty;
2043
+ case ReferenceOwnership::Weak:
2044
+ // For weak variables, use Optional<T>.
2032
2045
return CS.getTypeChecker ().getOptionalType (var->getLoc (), ty);
2046
+ }
2033
2047
2034
- return ty ;
2048
+ llvm_unreachable ( " Unhandled ReferenceOwnership kind " ) ;
2035
2049
}
2036
2050
2037
2051
case PatternKind::Typed: {
0 commit comments