@@ -2422,7 +2422,7 @@ namespace {
2422
2422
// / for the types of each variable declared within the pattern, along
2423
2423
// / with a one-way constraint binding that to the type to which the
2424
2424
// / variable will be ascribed or inferred.
2425
- Type getTypeForPattern (
2425
+ Optional< Type> getTypeForPattern (
2426
2426
Pattern *pattern, ConstraintLocatorBuilder locator,
2427
2427
bool bindPatternVarsOneWay,
2428
2428
PatternBindingDecl *patternBinding = nullptr ,
@@ -2446,14 +2446,21 @@ namespace {
2446
2446
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2447
2447
bindPatternVarsOneWay);
2448
2448
2449
- return setType (ParenType::get (CS.getASTContext (), underlyingType));
2449
+ if (!underlyingType)
2450
+ return None;
2451
+
2452
+ return setType (ParenType::get (CS.getASTContext (), *underlyingType));
2450
2453
}
2451
2454
case PatternKind::Binding: {
2452
2455
auto *subPattern = cast<BindingPattern>(pattern)->getSubPattern ();
2453
2456
auto type = getTypeForPattern (subPattern, locator,
2454
2457
bindPatternVarsOneWay);
2458
+
2459
+ if (!type)
2460
+ return None;
2461
+
2455
2462
// Var doesn't affect the type.
2456
- return setType (type);
2463
+ return setType (* type);
2457
2464
}
2458
2465
case PatternKind::Any: {
2459
2466
Type type;
@@ -2633,6 +2640,9 @@ namespace {
2633
2640
2634
2641
Type type = TypeChecker::typeCheckPattern (contextualPattern);
2635
2642
2643
+ if (!type)
2644
+ return None;
2645
+
2636
2646
// Look through reference storage types.
2637
2647
type = type->getReferenceStorageReferent ();
2638
2648
@@ -2644,16 +2654,19 @@ namespace {
2644
2654
auto *subPattern = cast<TypedPattern>(pattern)->getSubPattern ();
2645
2655
// Determine the subpattern type. It will be convertible to the
2646
2656
// ascribed type.
2647
- Type subPatternType = getTypeForPattern (
2657
+ auto subPatternType = getTypeForPattern (
2648
2658
subPattern,
2649
2659
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2650
2660
bindPatternVarsOneWay);
2651
2661
2662
+ if (!subPatternType)
2663
+ return None;
2664
+
2652
2665
// NOTE: The order here is important! Pattern matching equality is
2653
2666
// not symmetric (we need to fix that either by using a different
2654
2667
// constraint, or actually making it symmetric).
2655
2668
CS.addConstraint (
2656
- ConstraintKind::Equal, openedType, subPatternType,
2669
+ ConstraintKind::Equal, openedType, * subPatternType,
2657
2670
locator.withPathElement (LocatorPathElt::PatternMatch (pattern)));
2658
2671
2659
2672
// FIXME [OPAQUE SUPPORT]: the distinction between where we want opaque
@@ -2673,12 +2686,15 @@ namespace {
2673
2686
auto &tupleElt = tuplePat->getElement (i);
2674
2687
2675
2688
auto *eltPattern = tupleElt.getPattern ();
2676
- Type eltTy = getTypeForPattern (
2689
+ auto eltTy = getTypeForPattern (
2677
2690
eltPattern,
2678
2691
locator.withPathElement (LocatorPathElt::PatternMatch (eltPattern)),
2679
2692
bindPatternVarsOneWay);
2680
2693
2681
- tupleTypeElts.push_back (TupleTypeElt (eltTy, tupleElt.getLabel ()));
2694
+ if (!eltTy)
2695
+ return None;
2696
+
2697
+ tupleTypeElts.push_back (TupleTypeElt (*eltTy, tupleElt.getLabel ()));
2682
2698
}
2683
2699
2684
2700
return setType (TupleType::get (tupleTypeElts, CS.getASTContext ()));
@@ -2687,12 +2703,15 @@ namespace {
2687
2703
case PatternKind::OptionalSome: {
2688
2704
auto *subPattern = cast<OptionalSomePattern>(pattern)->getSubPattern ();
2689
2705
// The subpattern must have optional type.
2690
- Type subPatternType = getTypeForPattern (
2706
+ auto subPatternType = getTypeForPattern (
2691
2707
subPattern,
2692
2708
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2693
2709
bindPatternVarsOneWay);
2694
2710
2695
- return setType (OptionalType::get (subPatternType));
2711
+ if (!subPatternType)
2712
+ return None;
2713
+
2714
+ return setType (OptionalType::get (*subPatternType));
2696
2715
}
2697
2716
2698
2717
case PatternKind::Is: {
@@ -2722,12 +2741,14 @@ namespace {
2722
2741
subPattern,
2723
2742
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2724
2743
bindPatternVarsOneWay);
2744
+ if (!subPatternType)
2745
+ return None;
2725
2746
2726
2747
// NOTE: The order here is important! Pattern matching equality is
2727
2748
// not symmetric (we need to fix that either by using a different
2728
2749
// constraint, or actually making it symmetric).
2729
2750
CS.addConstraint (
2730
- ConstraintKind::Equal, castType, subPatternType,
2751
+ ConstraintKind::Equal, castType, * subPatternType,
2731
2752
locator.withPathElement (LocatorPathElt::PatternMatch (pattern)));
2732
2753
}
2733
2754
return setType (isType);
@@ -2791,6 +2812,9 @@ namespace {
2791
2812
TypeResolverContext::InExpression, patternMatchLoc);
2792
2813
}();
2793
2814
2815
+ if (!parentType)
2816
+ return None;
2817
+
2794
2818
// Perform member lookup into the parent's metatype.
2795
2819
Type parentMetaType = MetatypeType::get (parentType);
2796
2820
CS.addValueMemberConstraint (parentMetaType, enumPattern->getName (),
@@ -2818,13 +2842,13 @@ namespace {
2818
2842
// When there is a subpattern, the member will have function type,
2819
2843
// and we're matching the type of that subpattern to the parameter
2820
2844
// types.
2821
- Type subPatternType = getTypeForPattern (
2845
+ auto subPatternType = getTypeForPattern (
2822
2846
subPattern,
2823
2847
locator.withPathElement (LocatorPathElt::PatternMatch (subPattern)),
2824
2848
bindPatternVarsOneWay);
2825
2849
2826
2850
SmallVector<AnyFunctionType::Param, 4 > params;
2827
- decomposeTuple (subPatternType, params);
2851
+ decomposeTuple (* subPatternType, params);
2828
2852
2829
2853
// Remove parameter labels; they aren't used when matching cases,
2830
2854
// but outright conflicts will be checked during coercion.
@@ -2857,10 +2881,24 @@ namespace {
2857
2881
}
2858
2882
2859
2883
case PatternKind::Expr: {
2860
- // We generate constraints for ExprPatterns in a separate pass. For
2861
- // now, just create a type variable.
2862
- return setType (CS.createTypeVariable (CS.getConstraintLocator (locator),
2863
- TVO_CanBindToNoEscape));
2884
+ auto *EP = cast<ExprPattern>(pattern);
2885
+ Type patternTy = CS.createTypeVariable (CS.getConstraintLocator (locator),
2886
+ TVO_CanBindToNoEscape);
2887
+
2888
+ auto target = SyntacticElementTarget::forExprPattern (EP);
2889
+
2890
+ if (CS.preCheckTarget (target, /* replaceInvalidRefWithErrors=*/ true ,
2891
+ /* leaveClosureBodyUnchecked=*/ false )) {
2892
+ return None;
2893
+ }
2894
+ CS.setType (EP->getMatchVar (), patternTy);
2895
+
2896
+ if (CS.generateConstraints (target))
2897
+ return None;
2898
+
2899
+ CS.setTargetFor (EP, target);
2900
+ CS.setExprPatternFor (EP->getSubExpr (), EP);
2901
+ return setType (patternTy);
2864
2902
}
2865
2903
}
2866
2904
@@ -4299,10 +4337,14 @@ static bool generateInitPatternConstraints(ConstraintSystem &cs,
4299
4337
4300
4338
Type patternType;
4301
4339
if (auto pattern = target.getInitializationPattern ()) {
4302
- patternType = cs.generateConstraints (
4340
+ auto ty = cs.generateConstraints (
4303
4341
pattern, locator, target.shouldBindPatternVarsOneWay (),
4304
4342
target.getInitializationPatternBindingDecl (),
4305
4343
target.getInitializationPatternBindingIndex ());
4344
+ if (!ty)
4345
+ return true ;
4346
+
4347
+ patternType = *ty;
4306
4348
} else {
4307
4349
patternType = cs.createTypeVariable (locator, TVO_CanBindToNoEscape);
4308
4350
}
@@ -4461,7 +4503,7 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
4461
4503
// Collect constraints from the element pattern.
4462
4504
auto elementLocator = cs.getConstraintLocator (
4463
4505
sequenceExpr, ConstraintLocator::SequenceElementType);
4464
- Type initType =
4506
+ auto initType =
4465
4507
cs.generateConstraints (pattern, elementLocator,
4466
4508
target.shouldBindPatternVarsOneWay (), nullptr , 0 );
4467
4509
if (!initType)
@@ -4480,7 +4522,7 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
4480
4522
// resolving `optional object` constraint which is sometimes too eager.
4481
4523
cs.addConstraint (ConstraintKind::Conversion, nextType,
4482
4524
OptionalType::get (elementType), elementTypeLoc);
4483
- cs.addConstraint (ConstraintKind::Conversion, elementType, initType,
4525
+ cs.addConstraint (ConstraintKind::Conversion, elementType, * initType,
4484
4526
elementLocator);
4485
4527
}
4486
4528
@@ -4506,7 +4548,7 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
4506
4548
4507
4549
// Populate all of the information for a for-each loop.
4508
4550
forEachStmtInfo.elementType = elementType;
4509
- forEachStmtInfo.initType = initType;
4551
+ forEachStmtInfo.initType = * initType;
4510
4552
target.setPattern (pattern);
4511
4553
target.getForEachStmtInfo () = forEachStmtInfo;
4512
4554
return target;
@@ -4686,7 +4728,7 @@ bool ConstraintSystem::generateConstraints(
4686
4728
4687
4729
// Generate constraints to bind all of the internal declarations
4688
4730
// and verify the pattern.
4689
- Type patternType = generateConstraints (
4731
+ auto patternType = generateConstraints (
4690
4732
pattern, locator, /* shouldBindPatternVarsOneWay*/ true ,
4691
4733
target.getPatternBindingOfUninitializedVar (),
4692
4734
target.getIndexOfUninitializedVar ());
@@ -4715,25 +4757,13 @@ Expr *ConstraintSystem::generateConstraints(
4715
4757
return generateConstraintsFor (*this , expr, dc);
4716
4758
}
4717
4759
4718
- Type ConstraintSystem::generateConstraints (
4760
+ Optional< Type> ConstraintSystem::generateConstraints (
4719
4761
Pattern *pattern, ConstraintLocatorBuilder locator,
4720
4762
bool bindPatternVarsOneWay, PatternBindingDecl *patternBinding,
4721
4763
unsigned patternIndex) {
4722
4764
ConstraintGenerator cg (*this , nullptr );
4723
- auto ty = cg.getTypeForPattern (pattern, locator, bindPatternVarsOneWay,
4724
- patternBinding, patternIndex);
4725
- assert (ty);
4726
-
4727
- // Gather the ExprPatterns, and form a conjunction for their expressions.
4728
- SmallVector<ExprPattern *, 4 > exprPatterns;
4729
- pattern->forEachNode ([&](Pattern *P) {
4730
- if (auto *EP = dyn_cast<ExprPattern>(P))
4731
- exprPatterns.push_back (EP);
4732
- });
4733
- if (!exprPatterns.empty ())
4734
- generateConstraints (exprPatterns, getConstraintLocator (pattern));
4735
-
4736
- return ty;
4765
+ return cg.getTypeForPattern (pattern, locator, bindPatternVarsOneWay,
4766
+ patternBinding, patternIndex);
4737
4767
}
4738
4768
4739
4769
bool ConstraintSystem::generateConstraints (StmtCondition condition,
0 commit comments