@@ -2189,6 +2189,12 @@ namespace {
2189
2189
2190
2190
Type varType;
2191
2191
2192
+ // Determine whether optionality will be required.
2193
+ auto ROK = ReferenceOwnership::Strong;
2194
+ if (auto *OA = var->getAttrs ().getAttribute <ReferenceOwnershipAttr>())
2195
+ ROK = OA->get ();
2196
+ auto optionality = optionalityOf (ROK);
2197
+
2192
2198
// If we have a type from an initializer expression, and that
2193
2199
// expression does not produce an InOut type, use it. This
2194
2200
// will avoid exponential typecheck behavior in the case of
@@ -2197,18 +2203,17 @@ namespace {
2197
2203
// FIXME: This should be handled in the solver, not here.
2198
2204
//
2199
2205
// Otherwise, create a new type variable.
2200
- bool assumedInitializerType = false ;
2201
2206
if (!var->hasNonPatternBindingInit () &&
2202
- !var->hasAttachedPropertyWrapper ()) {
2207
+ !var->hasAttachedPropertyWrapper () &&
2208
+ optionality != ReferenceOwnershipOptionality::Required) {
2203
2209
if (auto boundExpr = locator.trySimplifyToExpr ()) {
2204
2210
if (!boundExpr->isSemanticallyInOutExpr ()) {
2205
2211
varType = CS.getType (boundExpr)->getRValueType ();
2206
- assumedInitializerType = true ;
2207
2212
}
2208
2213
}
2209
2214
}
2210
2215
2211
- if (!assumedInitializerType )
2216
+ if (!varType )
2212
2217
varType = CS.createTypeVariable (CS.getConstraintLocator (locator),
2213
2218
TVO_CanBindToNoEscape);
2214
2219
@@ -2226,22 +2231,8 @@ namespace {
2226
2231
2227
2232
// If there is an externally-imposed type.
2228
2233
2229
- auto ROK = ReferenceOwnership::Strong;
2230
- if (auto *OA = var->getAttrs ().getAttribute <ReferenceOwnershipAttr>())
2231
- ROK = OA->get ();
2232
- switch (optionalityOf (ROK)) {
2234
+ switch (optionality) {
2233
2235
case ReferenceOwnershipOptionality::Required:
2234
- if (assumedInitializerType) {
2235
- // Already Optional<T>
2236
- if (varType->getOptionalObjectType ())
2237
- break ;
2238
-
2239
- // Create a fresh type variable to handle overloaded expressions.
2240
- if (varType->is <TypeVariableType>())
2241
- varType = CS.createTypeVariable (CS.getConstraintLocator (locator),
2242
- TVO_CanBindToNoEscape);
2243
- }
2244
-
2245
2236
varType = TypeChecker::getOptionalType (var->getLoc (), varType);
2246
2237
assert (!varType->hasError ());
2247
2238
0 commit comments