@@ -2382,7 +2382,7 @@ namespace {
2382
2382
// / for the types of each variable declared within the pattern, along
2383
2383
// / with a one-way constraint binding that to the type to which the
2384
2384
// / variable will be ascribed or inferred.
2385
- Type getTypeForPattern (
2385
+ Optional< Type> getTypeForPattern (
2386
2386
Pattern *pattern, ConstraintLocatorBuilder locator,
2387
2387
bool bindPatternVarsOneWay,
2388
2388
PatternBindingDecl *patternBinding = nullptr ,
@@ -2406,14 +2406,21 @@ namespace {
2406
2406
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2407
2407
bindPatternVarsOneWay);
2408
2408
2409
- return setType (ParenType::get (CS.getASTContext (), underlyingType));
2409
+ if (!underlyingType)
2410
+ return None;
2411
+
2412
+ return setType (ParenType::get (CS.getASTContext (), *underlyingType));
2410
2413
}
2411
2414
case PatternKind::Binding: {
2412
2415
auto *subPattern = cast<BindingPattern>(pattern)->getSubPattern ();
2413
2416
auto type = getTypeForPattern (subPattern, locator,
2414
2417
bindPatternVarsOneWay);
2418
+
2419
+ if (!type)
2420
+ return None;
2421
+
2415
2422
// Var doesn't affect the type.
2416
- return setType (type);
2423
+ return setType (* type);
2417
2424
}
2418
2425
case PatternKind::Any: {
2419
2426
Type type;
@@ -2587,6 +2594,9 @@ namespace {
2587
2594
2588
2595
Type type = TypeChecker::typeCheckPattern (contextualPattern);
2589
2596
2597
+ if (!type)
2598
+ return None;
2599
+
2590
2600
// Look through reference storage types.
2591
2601
type = type->getReferenceStorageReferent ();
2592
2602
@@ -2598,16 +2608,19 @@ namespace {
2598
2608
auto *subPattern = cast<TypedPattern>(pattern)->getSubPattern ();
2599
2609
// Determine the subpattern type. It will be convertible to the
2600
2610
// ascribed type.
2601
- Type subPatternType = getTypeForPattern (
2611
+ auto subPatternType = getTypeForPattern (
2602
2612
subPattern,
2603
2613
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2604
2614
bindPatternVarsOneWay);
2605
2615
2616
+ if (!subPatternType)
2617
+ return None;
2618
+
2606
2619
// NOTE: The order here is important! Pattern matching equality is
2607
2620
// not symmetric (we need to fix that either by using a different
2608
2621
// constraint, or actually making it symmetric).
2609
2622
CS.addConstraint (
2610
- ConstraintKind::Equal, openedType, subPatternType,
2623
+ ConstraintKind::Equal, openedType, * subPatternType,
2611
2624
locator.withPathElement (LocatorPathElt::PatternMatch (pattern)));
2612
2625
2613
2626
// FIXME [OPAQUE SUPPORT]: the distinction between where we want opaque
@@ -2627,12 +2640,15 @@ namespace {
2627
2640
auto &tupleElt = tuplePat->getElement (i);
2628
2641
2629
2642
auto *eltPattern = tupleElt.getPattern ();
2630
- Type eltTy = getTypeForPattern (
2643
+ auto eltTy = getTypeForPattern (
2631
2644
eltPattern,
2632
2645
locator.withPathElement (LocatorPathElt::PatternMatch (eltPattern)),
2633
2646
bindPatternVarsOneWay);
2634
2647
2635
- tupleTypeElts.push_back (TupleTypeElt (eltTy, tupleElt.getLabel ()));
2648
+ if (!eltTy)
2649
+ return None;
2650
+
2651
+ tupleTypeElts.push_back (TupleTypeElt (*eltTy, tupleElt.getLabel ()));
2636
2652
}
2637
2653
2638
2654
return setType (TupleType::get (tupleTypeElts, CS.getASTContext ()));
@@ -2641,12 +2657,15 @@ namespace {
2641
2657
case PatternKind::OptionalSome: {
2642
2658
auto *subPattern = cast<OptionalSomePattern>(pattern)->getSubPattern ();
2643
2659
// The subpattern must have optional type.
2644
- Type subPatternType = getTypeForPattern (
2660
+ auto subPatternType = getTypeForPattern (
2645
2661
subPattern,
2646
2662
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2647
2663
bindPatternVarsOneWay);
2648
2664
2649
- return setType (OptionalType::get (subPatternType));
2665
+ if (!subPatternType)
2666
+ return None;
2667
+
2668
+ return setType (OptionalType::get (*subPatternType));
2650
2669
}
2651
2670
2652
2671
case PatternKind::Is: {
@@ -2675,12 +2694,14 @@ namespace {
2675
2694
subPattern,
2676
2695
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2677
2696
bindPatternVarsOneWay);
2697
+ if (!subPatternType)
2698
+ return None;
2678
2699
2679
2700
// NOTE: The order here is important! Pattern matching equality is
2680
2701
// not symmetric (we need to fix that either by using a different
2681
2702
// constraint, or actually making it symmetric).
2682
2703
CS.addConstraint (
2683
- ConstraintKind::Equal, castType, subPatternType,
2704
+ ConstraintKind::Equal, castType, * subPatternType,
2684
2705
locator.withPathElement (LocatorPathElt::PatternMatch (pattern)));
2685
2706
}
2686
2707
return setType (isType);
@@ -2741,6 +2762,9 @@ namespace {
2741
2762
TypeResolverContext::InExpression, patternMatchLoc);
2742
2763
}();
2743
2764
2765
+ if (!parentType)
2766
+ return None;
2767
+
2744
2768
// Perform member lookup into the parent's metatype.
2745
2769
Type parentMetaType = MetatypeType::get (parentType);
2746
2770
CS.addValueMemberConstraint (
@@ -2770,13 +2794,16 @@ namespace {
2770
2794
// When there is a subpattern, the member will have function type,
2771
2795
// and we're matching the type of that subpattern to the parameter
2772
2796
// types.
2773
- Type subPatternType = getTypeForPattern (
2797
+ auto subPatternType = getTypeForPattern (
2774
2798
subPattern,
2775
2799
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2776
2800
bindPatternVarsOneWay);
2777
2801
2802
+ if (!subPatternType)
2803
+ return None;
2804
+
2778
2805
SmallVector<AnyFunctionType::Param, 4 > params;
2779
- decomposeTuple (subPatternType, params);
2806
+ decomposeTuple (* subPatternType, params);
2780
2807
2781
2808
// Remove parameter labels; they aren't used when matching cases,
2782
2809
// but outright conflicts will be checked during coercion.
@@ -2811,10 +2838,24 @@ namespace {
2811
2838
}
2812
2839
2813
2840
case PatternKind::Expr: {
2814
- // We generate constraints for ExprPatterns in a separate pass. For
2815
- // now, just create a type variable.
2816
- return setType (CS.createTypeVariable (CS.getConstraintLocator (locator),
2817
- TVO_CanBindToNoEscape));
2841
+ auto *EP = cast<ExprPattern>(pattern);
2842
+ Type patternTy = CS.createTypeVariable (CS.getConstraintLocator (locator),
2843
+ TVO_CanBindToNoEscape);
2844
+
2845
+ auto target = SyntacticElementTarget::forExprPattern (EP);
2846
+
2847
+ if (CS.preCheckTarget (target, /* replaceInvalidRefWithErrors=*/ true ,
2848
+ /* leaveClosureBodyUnchecked=*/ false )) {
2849
+ return None;
2850
+ }
2851
+ CS.setType (EP->getMatchVar (), patternTy);
2852
+
2853
+ if (CS.generateConstraints (target))
2854
+ return None;
2855
+
2856
+ CS.setTargetFor (EP, target);
2857
+ CS.setExprPatternFor (EP->getSubExpr (), EP);
2858
+ return setType (patternTy);
2818
2859
}
2819
2860
}
2820
2861
@@ -4197,10 +4238,14 @@ static bool generateInitPatternConstraints(ConstraintSystem &cs,
4197
4238
4198
4239
Type patternType;
4199
4240
if (auto pattern = target.getInitializationPattern ()) {
4200
- patternType = cs.generateConstraints (
4241
+ auto ty = cs.generateConstraints (
4201
4242
pattern, locator, target.shouldBindPatternVarsOneWay (),
4202
4243
target.getInitializationPatternBindingDecl (),
4203
4244
target.getInitializationPatternBindingIndex ());
4245
+ if (!ty)
4246
+ return true ;
4247
+
4248
+ patternType = *ty;
4204
4249
} else {
4205
4250
patternType = cs.createTypeVariable (locator, TVO_CanBindToNoEscape);
4206
4251
}
@@ -4359,7 +4404,7 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
4359
4404
// Collect constraints from the element pattern.
4360
4405
auto elementLocator = cs.getConstraintLocator (
4361
4406
sequenceExpr, ConstraintLocator::SequenceElementType);
4362
- Type initType =
4407
+ auto initType =
4363
4408
cs.generateConstraints (pattern, elementLocator,
4364
4409
target.shouldBindPatternVarsOneWay (), nullptr , 0 );
4365
4410
if (!initType)
@@ -4378,7 +4423,7 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
4378
4423
// resolving `optional object` constraint which is sometimes too eager.
4379
4424
cs.addConstraint (ConstraintKind::Conversion, nextType,
4380
4425
OptionalType::get (elementType), elementTypeLoc);
4381
- cs.addConstraint (ConstraintKind::Conversion, elementType, initType,
4426
+ cs.addConstraint (ConstraintKind::Conversion, elementType, * initType,
4382
4427
elementLocator);
4383
4428
}
4384
4429
@@ -4404,7 +4449,7 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
4404
4449
4405
4450
// Populate all of the information for a for-each loop.
4406
4451
forEachStmtInfo.elementType = elementType;
4407
- forEachStmtInfo.initType = initType;
4452
+ forEachStmtInfo.initType = * initType;
4408
4453
target.setPattern (pattern);
4409
4454
target.getForEachStmtInfo () = forEachStmtInfo;
4410
4455
return target;
@@ -4584,7 +4629,7 @@ bool ConstraintSystem::generateConstraints(
4584
4629
4585
4630
// Generate constraints to bind all of the internal declarations
4586
4631
// and verify the pattern.
4587
- Type patternType = generateConstraints (
4632
+ auto patternType = generateConstraints (
4588
4633
pattern, locator, /* shouldBindPatternVarsOneWay*/ true ,
4589
4634
target.getPatternBindingOfUninitializedVar (),
4590
4635
target.getIndexOfUninitializedVar ());
@@ -4613,25 +4658,13 @@ Expr *ConstraintSystem::generateConstraints(
4613
4658
return generateConstraintsFor (*this , expr, dc);
4614
4659
}
4615
4660
4616
- Type ConstraintSystem::generateConstraints (
4661
+ Optional< Type> ConstraintSystem::generateConstraints (
4617
4662
Pattern *pattern, ConstraintLocatorBuilder locator,
4618
4663
bool bindPatternVarsOneWay, PatternBindingDecl *patternBinding,
4619
4664
unsigned patternIndex) {
4620
4665
ConstraintGenerator cg (*this , nullptr );
4621
- auto ty = cg.getTypeForPattern (pattern, locator, bindPatternVarsOneWay,
4622
- patternBinding, patternIndex);
4623
- assert (ty);
4624
-
4625
- // Gather the ExprPatterns, and form a conjunction for their expressions.
4626
- SmallVector<ExprPattern *, 4 > exprPatterns;
4627
- pattern->forEachNode ([&](Pattern *P) {
4628
- if (auto *EP = dyn_cast<ExprPattern>(P))
4629
- exprPatterns.push_back (EP);
4630
- });
4631
- if (!exprPatterns.empty ())
4632
- generateConstraints (exprPatterns, getConstraintLocator (pattern));
4633
-
4634
- return ty;
4666
+ return cg.getTypeForPattern (pattern, locator, bindPatternVarsOneWay,
4667
+ patternBinding, patternIndex);
4635
4668
}
4636
4669
4637
4670
bool ConstraintSystem::generateConstraints (StmtCondition condition,
@@ -4713,14 +4746,16 @@ bool ConstraintSystem::generateConstraints(
4713
4746
// Generate constraints for the pattern, including one-way bindings for
4714
4747
// any variables that show up in this pattern, because those variables
4715
4748
// can be referenced in the guard expressions and the body.
4716
- Type patternType = generateConstraints (
4749
+ auto patternType = generateConstraints (
4717
4750
pattern, locator, /* bindPatternVarsOneWay=*/ true ,
4718
4751
/* patternBinding=*/ nullptr , /* patternBindingIndex=*/ 0 );
4752
+ if (!patternType)
4753
+ return true ;
4719
4754
4720
4755
// Convert the subject type to the pattern, which establishes the
4721
4756
// bindings.
4722
4757
addConstraint (
4723
- ConstraintKind::Conversion, subjectType, patternType, locator);
4758
+ ConstraintKind::Conversion, subjectType, * patternType, locator);
4724
4759
4725
4760
// Generate constraints for the guard expression, if there is one.
4726
4761
Expr *guardExpr = caseLabelItem.getGuardExpr ();
0 commit comments