@@ -2205,21 +2205,31 @@ namespace {
2205
2205
TVO_CanBindToNoEscape);
2206
2206
}
2207
2207
2208
+ // Local function that must be called for each "return" throughout this
2209
+ // function, to set the type of the pattern.
2210
+ auto setType = [&](Type type) {
2211
+ CS.setType (pattern, type);
2212
+ return type;
2213
+ };
2214
+
2208
2215
switch (pattern->getKind ()) {
2209
2216
case PatternKind::Paren:
2210
2217
// Parentheses don't affect the canonical type, but record them as
2211
2218
// type sugar.
2212
- return ParenType::get (
2213
- CS.getASTContext (),
2214
- getTypeForPattern (
2215
- cast<ParenPattern>(pattern)->getSubPattern (), locator));
2219
+ return setType (
2220
+ ParenType::get (
2221
+ CS.getASTContext (),
2222
+ getTypeForPattern (
2223
+ cast<ParenPattern>(pattern)->getSubPattern (), locator)));
2216
2224
case PatternKind::Var:
2217
2225
// Var doesn't affect the type.
2218
- return getTypeForPattern (cast<VarPattern>(pattern)->getSubPattern (),
2219
- locator);
2226
+ return setType (
2227
+ getTypeForPattern (
2228
+ cast<VarPattern>(pattern)->getSubPattern (), locator));
2220
2229
case PatternKind::Any: {
2221
- return CS.createTypeVariable (CS.getConstraintLocator (locator),
2222
- TVO_CanBindToNoEscape);
2230
+ return setType (
2231
+ CS.createTypeVariable (CS.getConstraintLocator (locator),
2232
+ TVO_CanBindToNoEscape));
2223
2233
}
2224
2234
2225
2235
case PatternKind::Named: {
@@ -2233,10 +2243,10 @@ namespace {
2233
2243
ROK = OA->get ();
2234
2244
switch (optionalityOf (ROK)) {
2235
2245
case ReferenceOwnershipOptionality::Required:
2236
- return TypeChecker::getOptionalType (var->getLoc (), varType);
2246
+ return setType ( TypeChecker::getOptionalType (var->getLoc (), varType) );
2237
2247
case ReferenceOwnershipOptionality::Allowed:
2238
2248
case ReferenceOwnershipOptionality::Disallowed:
2239
- return varType;
2249
+ return setType ( varType) ;
2240
2250
}
2241
2251
}
2242
2252
@@ -2249,7 +2259,7 @@ namespace {
2249
2259
2250
2260
// For a typed pattern, simply return the opened type of the pattern.
2251
2261
// FIXME: Error recovery if the type is an error type?
2252
- return openedType;
2262
+ return setType ( openedType) ;
2253
2263
}
2254
2264
2255
2265
case PatternKind::Tuple: {
@@ -2263,15 +2273,15 @@ namespace {
2263
2273
LocatorPathElt::TupleElement (i)));
2264
2274
tupleTypeElts.push_back (TupleTypeElt (eltTy, tupleElt.getLabel ()));
2265
2275
}
2266
- return TupleType::get (tupleTypeElts, CS.getASTContext ());
2276
+ return setType ( TupleType::get (tupleTypeElts, CS.getASTContext () ));
2267
2277
}
2268
2278
2269
2279
case PatternKind::OptionalSome: {
2270
2280
// The subpattern must have optional type.
2271
2281
Type subPatternType = getTypeForPattern (
2272
2282
cast<OptionalSomePattern>(pattern)->getSubPattern (), locator);
2273
2283
2274
- return OptionalType::get (subPatternType);
2284
+ return setType ( OptionalType::get (subPatternType) );
2275
2285
}
2276
2286
2277
2287
case PatternKind::Is: {
@@ -2288,11 +2298,11 @@ namespace {
2288
2298
ConstraintKind::CheckedCast, subPatternType, castType, locator);
2289
2299
}
2290
2300
2291
- return subPatternType;
2301
+ return setType ( subPatternType) ;
2292
2302
}
2293
2303
2294
2304
case PatternKind::Bool:
2295
- return CS.getASTContext ().getBoolDecl ()->getDeclaredType ();
2305
+ return setType ( CS.getASTContext ().getBoolDecl ()->getDeclaredType () );
2296
2306
2297
2307
// Refutable patterns occur when checking the PatternBindingDecls in an
2298
2308
// if/let or while/let condition. They always require an initial value,
@@ -2301,8 +2311,9 @@ namespace {
2301
2311
case PatternKind::Expr:
2302
2312
// TODO: we could try harder here, e.g. for enum elements to provide the
2303
2313
// enum type.
2304
- return CS.createTypeVariable (CS.getConstraintLocator (locator),
2305
- TVO_CanBindToNoEscape);
2314
+ return setType (
2315
+ CS.createTypeVariable (
2316
+ CS.getConstraintLocator (locator), TVO_CanBindToNoEscape));
2306
2317
}
2307
2318
2308
2319
llvm_unreachable (" Unhandled pattern kind" );
@@ -3882,11 +3893,7 @@ static bool generateInitPatternConstraints(
3882
3893
auto locator =
3883
3894
cs.getConstraintLocator (initializer, LocatorPathElt::ContextualType ());
3884
3895
Type patternType = cs.generateConstraints (pattern, locator);
3885
- if (!patternType)
3886
- return true ;
3887
-
3888
- // Record the type of this pattern.
3889
- cs.setType (pattern, patternType);
3896
+ assert (patternType && " All patterns have a type" );
3890
3897
3891
3898
if (auto wrappedVar = target.getInitializationWrappedVar ()) {
3892
3899
// Add an equal constraint between the pattern type and the
0 commit comments