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