@@ -2390,7 +2390,7 @@ namespace {
2390
2390
// / for the types of each variable declared within the pattern, along
2391
2391
// / with a one-way constraint binding that to the type to which the
2392
2392
// / variable will be ascribed or inferred.
2393
- Type getTypeForPattern (
2393
+ Optional< Type> getTypeForPattern (
2394
2394
Pattern *pattern, ConstraintLocatorBuilder locator,
2395
2395
bool bindPatternVarsOneWay,
2396
2396
PatternBindingDecl *patternBinding = nullptr ,
@@ -2414,14 +2414,21 @@ namespace {
2414
2414
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2415
2415
bindPatternVarsOneWay);
2416
2416
2417
- return setType (ParenType::get (CS.getASTContext (), underlyingType));
2417
+ if (!underlyingType)
2418
+ return None;
2419
+
2420
+ return setType (ParenType::get (CS.getASTContext (), *underlyingType));
2418
2421
}
2419
2422
case PatternKind::Binding: {
2420
2423
auto *subPattern = cast<BindingPattern>(pattern)->getSubPattern ();
2421
2424
auto type = getTypeForPattern (subPattern, locator,
2422
2425
bindPatternVarsOneWay);
2426
+
2427
+ if (!type)
2428
+ return None;
2429
+
2423
2430
// Var doesn't affect the type.
2424
- return setType (type);
2431
+ return setType (* type);
2425
2432
}
2426
2433
case PatternKind::Any: {
2427
2434
Type type;
@@ -2595,6 +2602,9 @@ namespace {
2595
2602
2596
2603
Type type = TypeChecker::typeCheckPattern (contextualPattern);
2597
2604
2605
+ if (!type)
2606
+ return None;
2607
+
2598
2608
// Look through reference storage types.
2599
2609
type = type->getReferenceStorageReferent ();
2600
2610
@@ -2606,16 +2616,19 @@ namespace {
2606
2616
auto *subPattern = cast<TypedPattern>(pattern)->getSubPattern ();
2607
2617
// Determine the subpattern type. It will be convertible to the
2608
2618
// ascribed type.
2609
- Type subPatternType = getTypeForPattern (
2619
+ auto subPatternType = getTypeForPattern (
2610
2620
subPattern,
2611
2621
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2612
2622
bindPatternVarsOneWay);
2613
2623
2624
+ if (!subPatternType)
2625
+ return None;
2626
+
2614
2627
// NOTE: The order here is important! Pattern matching equality is
2615
2628
// not symmetric (we need to fix that either by using a different
2616
2629
// constraint, or actually making it symmetric).
2617
2630
CS.addConstraint (
2618
- ConstraintKind::Equal, openedType, subPatternType,
2631
+ ConstraintKind::Equal, openedType, * subPatternType,
2619
2632
locator.withPathElement (LocatorPathElt::PatternMatch (pattern)));
2620
2633
2621
2634
// FIXME [OPAQUE SUPPORT]: the distinction between where we want opaque
@@ -2635,12 +2648,15 @@ namespace {
2635
2648
auto &tupleElt = tuplePat->getElement (i);
2636
2649
2637
2650
auto *eltPattern = tupleElt.getPattern ();
2638
- Type eltTy = getTypeForPattern (
2651
+ auto eltTy = getTypeForPattern (
2639
2652
eltPattern,
2640
2653
locator.withPathElement (LocatorPathElt::PatternMatch (eltPattern)),
2641
2654
bindPatternVarsOneWay);
2642
2655
2643
- tupleTypeElts.push_back (TupleTypeElt (eltTy, tupleElt.getLabel ()));
2656
+ if (!eltTy)
2657
+ return None;
2658
+
2659
+ tupleTypeElts.push_back (TupleTypeElt (*eltTy, tupleElt.getLabel ()));
2644
2660
}
2645
2661
2646
2662
return setType (TupleType::get (tupleTypeElts, CS.getASTContext ()));
@@ -2649,12 +2665,15 @@ namespace {
2649
2665
case PatternKind::OptionalSome: {
2650
2666
auto *subPattern = cast<OptionalSomePattern>(pattern)->getSubPattern ();
2651
2667
// The subpattern must have optional type.
2652
- Type subPatternType = getTypeForPattern (
2668
+ auto subPatternType = getTypeForPattern (
2653
2669
subPattern,
2654
2670
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2655
2671
bindPatternVarsOneWay);
2656
2672
2657
- return setType (OptionalType::get (subPatternType));
2673
+ if (!subPatternType)
2674
+ return None;
2675
+
2676
+ return setType (OptionalType::get (*subPatternType));
2658
2677
}
2659
2678
2660
2679
case PatternKind::Is: {
@@ -2683,12 +2702,14 @@ namespace {
2683
2702
subPattern,
2684
2703
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2685
2704
bindPatternVarsOneWay);
2705
+ if (!subPatternType)
2706
+ return None;
2686
2707
2687
2708
// NOTE: The order here is important! Pattern matching equality is
2688
2709
// not symmetric (we need to fix that either by using a different
2689
2710
// constraint, or actually making it symmetric).
2690
2711
CS.addConstraint (
2691
- ConstraintKind::Equal, castType, subPatternType,
2712
+ ConstraintKind::Equal, castType, * subPatternType,
2692
2713
locator.withPathElement (LocatorPathElt::PatternMatch (pattern)));
2693
2714
}
2694
2715
return setType (isType);
@@ -2749,6 +2770,9 @@ namespace {
2749
2770
TypeResolverContext::InExpression, patternMatchLoc);
2750
2771
}();
2751
2772
2773
+ if (!parentType)
2774
+ return None;
2775
+
2752
2776
// Perform member lookup into the parent's metatype.
2753
2777
Type parentMetaType = MetatypeType::get (parentType);
2754
2778
CS.addValueMemberConstraint (
@@ -2778,13 +2802,16 @@ namespace {
2778
2802
// When there is a subpattern, the member will have function type,
2779
2803
// and we're matching the type of that subpattern to the parameter
2780
2804
// types.
2781
- Type subPatternType = getTypeForPattern (
2805
+ auto subPatternType = getTypeForPattern (
2782
2806
subPattern,
2783
2807
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2784
2808
bindPatternVarsOneWay);
2785
2809
2810
+ if (!subPatternType)
2811
+ return None;
2812
+
2786
2813
SmallVector<AnyFunctionType::Param, 4 > params;
2787
- decomposeTuple (subPatternType, params);
2814
+ decomposeTuple (* subPatternType, params);
2788
2815
2789
2816
// Remove parameter labels; they aren't used when matching cases,
2790
2817
// but outright conflicts will be checked during coercion.
@@ -2819,10 +2846,24 @@ namespace {
2819
2846
}
2820
2847
2821
2848
case PatternKind::Expr: {
2822
- // We generate constraints for ExprPatterns in a separate pass. For
2823
- // now, just create a type variable.
2824
- return setType (CS.createTypeVariable (CS.getConstraintLocator (locator),
2825
- TVO_CanBindToNoEscape));
2849
+ auto *EP = cast<ExprPattern>(pattern);
2850
+ Type patternTy = CS.createTypeVariable (CS.getConstraintLocator (locator),
2851
+ TVO_CanBindToNoEscape);
2852
+
2853
+ auto target = SyntacticElementTarget::forExprPattern (EP);
2854
+
2855
+ if (CS.preCheckTarget (target, /* replaceInvalidRefWithErrors=*/ true ,
2856
+ /* leaveClosureBodyUnchecked=*/ false )) {
2857
+ return None;
2858
+ }
2859
+ CS.setType (EP->getMatchVar (), patternTy);
2860
+
2861
+ if (CS.generateConstraints (target))
2862
+ return None;
2863
+
2864
+ CS.setTargetFor (EP, target);
2865
+ CS.setExprPatternFor (EP->getSubExpr (), EP);
2866
+ return setType (patternTy);
2826
2867
}
2827
2868
}
2828
2869
@@ -4208,10 +4249,14 @@ static bool generateInitPatternConstraints(ConstraintSystem &cs,
4208
4249
4209
4250
Type patternType;
4210
4251
if (auto pattern = target.getInitializationPattern ()) {
4211
- patternType = cs.generateConstraints (
4252
+ auto ty = cs.generateConstraints (
4212
4253
pattern, locator, target.shouldBindPatternVarsOneWay (),
4213
4254
target.getInitializationPatternBindingDecl (),
4214
4255
target.getInitializationPatternBindingIndex ());
4256
+ if (!ty)
4257
+ return true ;
4258
+
4259
+ patternType = *ty;
4215
4260
} else {
4216
4261
patternType = cs.createTypeVariable (locator, TVO_CanBindToNoEscape);
4217
4262
}
@@ -4370,7 +4415,7 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
4370
4415
// Collect constraints from the element pattern.
4371
4416
auto elementLocator = cs.getConstraintLocator (
4372
4417
sequenceExpr, ConstraintLocator::SequenceElementType);
4373
- Type initType =
4418
+ auto initType =
4374
4419
cs.generateConstraints (pattern, elementLocator,
4375
4420
target.shouldBindPatternVarsOneWay (), nullptr , 0 );
4376
4421
if (!initType)
@@ -4389,7 +4434,7 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
4389
4434
// resolving `optional object` constraint which is sometimes too eager.
4390
4435
cs.addConstraint (ConstraintKind::Conversion, nextType,
4391
4436
OptionalType::get (elementType), elementTypeLoc);
4392
- cs.addConstraint (ConstraintKind::Conversion, elementType, initType,
4437
+ cs.addConstraint (ConstraintKind::Conversion, elementType, * initType,
4393
4438
elementLocator);
4394
4439
}
4395
4440
@@ -4415,7 +4460,7 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
4415
4460
4416
4461
// Populate all of the information for a for-each loop.
4417
4462
forEachStmtInfo.elementType = elementType;
4418
- forEachStmtInfo.initType = initType;
4463
+ forEachStmtInfo.initType = * initType;
4419
4464
target.setPattern (pattern);
4420
4465
target.getForEachStmtInfo () = forEachStmtInfo;
4421
4466
return target;
@@ -4595,7 +4640,7 @@ bool ConstraintSystem::generateConstraints(
4595
4640
4596
4641
// Generate constraints to bind all of the internal declarations
4597
4642
// and verify the pattern.
4598
- Type patternType = generateConstraints (
4643
+ auto patternType = generateConstraints (
4599
4644
pattern, locator, /* shouldBindPatternVarsOneWay*/ true ,
4600
4645
target.getPatternBindingOfUninitializedVar (),
4601
4646
target.getIndexOfUninitializedVar ());
@@ -4624,25 +4669,13 @@ Expr *ConstraintSystem::generateConstraints(
4624
4669
return generateConstraintsFor (*this , expr, dc);
4625
4670
}
4626
4671
4627
- Type ConstraintSystem::generateConstraints (
4672
+ Optional< Type> ConstraintSystem::generateConstraints (
4628
4673
Pattern *pattern, ConstraintLocatorBuilder locator,
4629
4674
bool bindPatternVarsOneWay, PatternBindingDecl *patternBinding,
4630
4675
unsigned patternIndex) {
4631
4676
ConstraintGenerator cg (*this , nullptr );
4632
- auto ty = cg.getTypeForPattern (pattern, locator, bindPatternVarsOneWay,
4633
- patternBinding, patternIndex);
4634
- assert (ty);
4635
-
4636
- // Gather the ExprPatterns, and form a conjunction for their expressions.
4637
- SmallVector<ExprPattern *, 4 > exprPatterns;
4638
- pattern->forEachNode ([&](Pattern *P) {
4639
- if (auto *EP = dyn_cast<ExprPattern>(P))
4640
- exprPatterns.push_back (EP);
4641
- });
4642
- if (!exprPatterns.empty ())
4643
- generateConstraints (exprPatterns, getConstraintLocator (pattern));
4644
-
4645
- return ty;
4677
+ return cg.getTypeForPattern (pattern, locator, bindPatternVarsOneWay,
4678
+ patternBinding, patternIndex);
4646
4679
}
4647
4680
4648
4681
bool ConstraintSystem::generateConstraints (StmtCondition condition,
0 commit comments