@@ -2197,6 +2197,18 @@ namespace {
2197
2197
// / type information with fresh type variables.
2198
2198
// /
2199
2199
// / \param pattern The pattern.
2200
+ // /
2201
+ // / \param locator The locator to use for generated constraints and
2202
+ // / type variables.
2203
+ // /
2204
+ // / \param externalPatternType The type imposed by the enclosing pattern,
2205
+ // / if any. This will be non-null in cases where there is, e.g., a
2206
+ // / pattern such as "is SubClass".
2207
+ // /
2208
+ // / \param bindPatternVarsOneWay When true, generate fresh type variables
2209
+ // / for the types of each variable declared within the pattern, along
2210
+ // / with a one-way constraint binding that to the type to which the
2211
+ // / variable will be ascribed or inferred.
2200
2212
Type getTypeForPattern (Pattern *pattern, ConstraintLocatorBuilder locator,
2201
2213
Type externalPatternType,
2202
2214
bool bindPatternVarsOneWay) {
@@ -2257,14 +2269,9 @@ namespace {
2257
2269
if (bindPatternVarsOneWay) {
2258
2270
oneWayVarType = CS.createTypeVariable (
2259
2271
CS.getConstraintLocator (locator), TVO_CanBindToNoEscape);
2260
- if (externalPatternType) {
2261
- CS.addConstraint (
2262
- ConstraintKind::OneWayEqual, oneWayVarType,
2263
- externalPatternType, locator);
2264
- } else {
2265
- CS.addConstraint (
2266
- ConstraintKind::OneWayEqual, oneWayVarType, varType, locator);
2267
- }
2272
+ CS.addConstraint (
2273
+ ConstraintKind::OneWayEqual, oneWayVarType,
2274
+ externalPatternType ? externalPatternType : varType, locator);
2268
2275
}
2269
2276
2270
2277
// If there is an externally-imposed type.
@@ -2357,16 +2364,13 @@ namespace {
2357
2364
case PatternKind::OptionalSome: {
2358
2365
// Remove an optional from the object type.
2359
2366
if (externalPatternType) {
2360
- if (Type objType = externalPatternType->getOptionalObjectType ()) {
2361
- externalPatternType = objType;
2362
- } else {
2363
- Type objVar = CS.createTypeVariable (
2364
- CS.getConstraintLocator (locator), TVO_CanBindToNoEscape);
2365
- CS.addConstraint (
2366
- ConstraintKind::Equal, OptionalType::get (objVar),
2367
- externalPatternType, locator);
2368
- externalPatternType = objVar;
2369
- }
2367
+ Type objVar = CS.createTypeVariable (
2368
+ CS.getConstraintLocator (locator), TVO_CanBindToNoEscape);
2369
+ CS.addConstraint (
2370
+ ConstraintKind::OptionalObject, externalPatternType,
2371
+ objVar, locator);
2372
+
2373
+ externalPatternType = objVar;
2370
2374
}
2371
2375
2372
2376
// The subpattern must have optional type.
0 commit comments