@@ -235,105 +235,6 @@ class BuilderClosureVisitor
235
235
return nullptr ; \
236
236
}
237
237
238
- // / Provide a type for each variable that occurs within the given pattern,
239
- // / by matching the pattern structurally with its already-computed pattern
240
- // / type. The variables will either get a concrete type (when present in
241
- // / the pattern type) or a fresh type variable bound to that part of the
242
- // / pattern via a one-way constraint.
243
- void bindVariablesInPattern (Pattern *pattern, Type patternType,
244
- ConstraintLocator *locator) {
245
- switch (pattern->getKind ()) {
246
- case PatternKind::Paren: {
247
- // Parentheses don't affect the type, but unwrap a paren type if we have
248
- // one.
249
- Type subPatternType;
250
- if (auto parenType = dyn_cast<ParenType>(patternType.getPointer ()))
251
- subPatternType = parenType->getUnderlyingType ();
252
- else
253
- subPatternType = patternType;
254
- return bindVariablesInPattern (
255
- cast<ParenPattern>(pattern)->getSubPattern (),
256
- subPatternType, locator);
257
- }
258
-
259
- case PatternKind::Var:
260
- // Var doesn't affect the type.
261
- return bindVariablesInPattern (cast<VarPattern>(pattern)->getSubPattern (),
262
- patternType, locator);
263
-
264
- case PatternKind::Any:
265
- // Nothing to bind.
266
- return ;
267
-
268
- case PatternKind::Named: {
269
- auto var = cast<NamedPattern>(pattern)->getDecl ();
270
-
271
- // / Create a fresh type variable to describe the type of the
272
- Type varType = cs->createTypeVariable (locator, TVO_CanBindToNoEscape);
273
-
274
- auto ROK = ReferenceOwnership::Strong;
275
- if (auto *OA = var->getAttrs ().getAttribute <ReferenceOwnershipAttr>())
276
- ROK = OA->get ();
277
- switch (optionalityOf (ROK)) {
278
- case ReferenceOwnershipOptionality::Required:
279
- // FIXME: Can we assert this rather than just checking it.
280
- if (auto optPatternType =
281
- dyn_cast<OptionalType>(patternType.getPointer ())) {
282
- // Add a one-way constraint from the type variable to the wrapped
283
- // type of the optional.
284
- cs->addConstraint (
285
- ConstraintKind::OneWayEqual, varType, optPatternType->getBaseType (),
286
- locator);
287
-
288
- // Make the variable type optional.
289
- varType = TypeChecker::getOptionalType (var->getLoc (), varType);
290
- break ;
291
- }
292
-
293
- // Fall through to treat this normally.
294
- LLVM_FALLTHROUGH;
295
-
296
- case ReferenceOwnershipOptionality::Allowed:
297
- case ReferenceOwnershipOptionality::Disallowed:
298
- // Add the one-way constraint from the variable type to the pattern
299
- // type.
300
- cs->addConstraint (ConstraintKind::OneWayEqual, varType, patternType,
301
- locator);
302
- break ;
303
- }
304
-
305
- // Bind the type of the variable.
306
- cs->setType (var, varType);
307
- return ;
308
- }
309
-
310
- case PatternKind::Typed: {
311
- // Ignore the type itself; it's part of patternType now.
312
- return bindVariablesInPattern (
313
- cast<TypedPattern>(pattern)->getSubPattern (),
314
- patternType, locator);
315
- }
316
-
317
- case PatternKind::Tuple: {
318
- auto tuplePat = cast<TuplePattern>(pattern);
319
- auto tupleType = patternType->castTo <TupleType>();
320
- for (unsigned i = 0 , e = tuplePat->getNumElements (); i != e; ++i) {
321
- bindVariablesInPattern (tuplePat->getElement (i).getPattern (),
322
- tupleType->getElementType (i), locator);
323
- }
324
- return ;
325
- }
326
-
327
- // FIXME: Refutable patterns will generate additional constraints.
328
- #define PATTERN (Id, Parent )
329
- #define REFUTABLE_PATTERN (Id, Parent ) case PatternKind::Id:
330
- #include " swift/AST/PatternNodes.def"
331
- llvm_unreachable (" Refutable patterns are not supported here" );
332
- }
333
-
334
- llvm_unreachable (" Unhandled pattern kind" );
335
- }
336
-
337
238
void visitPatternBindingDecl (PatternBindingDecl *patternBinding) {
338
239
// If any of the entries lacks an initializer, don't handle this node.
339
240
if (!llvm::all_of (range (patternBinding->getNumPatternEntries ()),
@@ -367,8 +268,8 @@ class BuilderClosureVisitor
367
268
368
269
// Bind the variables that occur in the pattern to the corresponding
369
270
// type entry for the pattern itself.
370
- bindVariablesInPattern (pattern, cs->getType (pattern),
371
- cs->getConstraintLocator (target.getAsExpr ()));
271
+ cs->bindVariablesInPattern (
272
+ pattern, cs->getConstraintLocator (target.getAsExpr ()));
372
273
}
373
274
}
374
275
0 commit comments