Skip to content

Commit d268f7b

Browse files
authored
Merge pull request #31614 from xedin/rdar-60534522-5.3
[5.3][ConstraintSystem] Always verify computed/resolved pattern types befo…
2 parents cc42617 + a04ad04 commit d268f7b

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

lib/Sema/CSGen.cpp

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,8 @@ namespace {
22542254

22552255
switch (pattern->getKind()) {
22562256
case PatternKind::Paren: {
2257+
auto *paren = cast<ParenPattern>(pattern);
2258+
22572259
// Parentheses don't affect the canonical type, but record them as
22582260
// type sugar.
22592261
if (externalPatternType &&
@@ -2262,13 +2264,14 @@ namespace {
22622264
->getUnderlyingType();
22632265
}
22642266

2265-
return setType(
2266-
ParenType::get(
2267-
CS.getASTContext(),
2268-
getTypeForPattern(
2269-
cast<ParenPattern>(pattern)->getSubPattern(), locator,
2270-
externalPatternType,
2271-
bindPatternVarsOneWay)));
2267+
auto underlyingType =
2268+
getTypeForPattern(paren->getSubPattern(), locator,
2269+
externalPatternType, bindPatternVarsOneWay);
2270+
2271+
if (!underlyingType)
2272+
return Type();
2273+
2274+
return setType(ParenType::get(CS.getASTContext(), underlyingType));
22722275
}
22732276
case PatternKind::Var:
22742277
// Var doesn't affect the type.
@@ -2370,10 +2373,14 @@ namespace {
23702373

23712374
Type type = TypeChecker::typeCheckPattern(contextualPattern);
23722375

2376+
if (!type)
2377+
return Type();
2378+
23732379
// Look through reference storage types.
23742380
type = type->getReferenceStorageReferent();
23752381

23762382
Type openedType = CS.openUnboundGenericType(type, locator);
2383+
assert(openedType);
23772384

23782385
auto *subPattern = cast<TypedPattern>(pattern)->getSubPattern();
23792386
// Determine the subpattern type. It will be convertible to the
@@ -2383,6 +2390,9 @@ namespace {
23832390
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
23842391
Type(), bindPatternVarsOneWay);
23852392

2393+
if (!subPatternType)
2394+
return Type();
2395+
23862396
CS.addConstraint(
23872397
ConstraintKind::Conversion, subPatternType, openedType,
23882398
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
@@ -2425,6 +2435,10 @@ namespace {
24252435
eltPattern,
24262436
locator.withPathElement(LocatorPathElt::PatternMatch(eltPattern)),
24272437
externalEltType, bindPatternVarsOneWay);
2438+
2439+
if (!eltTy)
2440+
return Type();
2441+
24282442
tupleTypeElts.push_back(TupleTypeElt(eltTy, tupleElt.getLabel()));
24292443
}
24302444

@@ -2452,6 +2466,9 @@ namespace {
24522466
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
24532467
externalPatternType, bindPatternVarsOneWay);
24542468

2469+
if (!subPatternType)
2470+
return Type();
2471+
24552472
return setType(OptionalType::get(subPatternType));
24562473
}
24572474

@@ -2460,16 +2477,25 @@ namespace {
24602477

24612478
Type castType =
24622479
resolveTypeReferenceInExpression(isPattern->getCastTypeLoc());
2480+
2481+
if (!castType)
2482+
return Type();
2483+
24632484
castType = CS.openUnboundGenericType(
24642485
castType,
24652486
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
24662487

2488+
assert(castType);
2489+
24672490
auto *subPattern = isPattern->getSubPattern();
24682491
Type subPatternType = getTypeForPattern(
24692492
subPattern,
24702493
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
24712494
castType, bindPatternVarsOneWay);
24722495

2496+
if (!subPatternType)
2497+
return Type();
2498+
24732499
// Make sure we can cast from the subpattern type to the type we're
24742500
// checking; if it's impossible, fail.
24752501
CS.addConstraint(
@@ -2502,11 +2528,16 @@ namespace {
25022528
Type parentType =
25032529
resolveTypeReferenceInExpression(enumPattern->getParentType());
25042530

2531+
if (!parentType)
2532+
return Type();
2533+
25052534
parentType = CS.openUnboundGenericType(
25062535
parentType, CS.getConstraintLocator(
25072536
locator, {LocatorPathElt::PatternMatch(pattern),
25082537
ConstraintLocator::ParentType}));
25092538

2539+
assert(parentType);
2540+
25102541
// Perform member lookup into the parent's metatype.
25112542
Type parentMetaType = MetatypeType::get(parentType);
25122543
CS.addValueMemberConstraint(
@@ -2539,6 +2570,10 @@ namespace {
25392570
// types.
25402571
Type subPatternType = getTypeForPattern(
25412572
subPattern, locator, Type(), bindPatternVarsOneWay);
2573+
2574+
if (!subPatternType)
2575+
return Type();
2576+
25422577
SmallVector<AnyFunctionType::Param, 4> params;
25432578
AnyFunctionType::decomposeInput(subPatternType, params);
25442579

0 commit comments

Comments
 (0)