Skip to content

Commit 4b91ff7

Browse files
authored
Merge pull request #31604 from xedin/rdar-60534522
[ConstraintSystem] Always verify computed/resolved pattern types befo…
2 parents 1560669 + f22ca72 commit 4b91ff7

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
@@ -2259,6 +2259,8 @@ namespace {
22592259

22602260
switch (pattern->getKind()) {
22612261
case PatternKind::Paren: {
2262+
auto *paren = cast<ParenPattern>(pattern);
2263+
22622264
// Parentheses don't affect the canonical type, but record them as
22632265
// type sugar.
22642266
if (externalPatternType &&
@@ -2267,13 +2269,14 @@ namespace {
22672269
->getUnderlyingType();
22682270
}
22692271

2270-
return setType(
2271-
ParenType::get(
2272-
CS.getASTContext(),
2273-
getTypeForPattern(
2274-
cast<ParenPattern>(pattern)->getSubPattern(), locator,
2275-
externalPatternType,
2276-
bindPatternVarsOneWay)));
2272+
auto underlyingType =
2273+
getTypeForPattern(paren->getSubPattern(), locator,
2274+
externalPatternType, bindPatternVarsOneWay);
2275+
2276+
if (!underlyingType)
2277+
return Type();
2278+
2279+
return setType(ParenType::get(CS.getASTContext(), underlyingType));
22772280
}
22782281
case PatternKind::Var:
22792282
// Var doesn't affect the type.
@@ -2378,10 +2381,14 @@ namespace {
23782381

23792382
Type type = TypeChecker::typeCheckPattern(contextualPattern);
23802383

2384+
if (!type)
2385+
return Type();
2386+
23812387
// Look through reference storage types.
23822388
type = type->getReferenceStorageReferent();
23832389

23842390
Type openedType = CS.openUnboundGenericType(type, locator);
2391+
assert(openedType);
23852392

23862393
auto *subPattern = cast<TypedPattern>(pattern)->getSubPattern();
23872394
// Determine the subpattern type. It will be convertible to the
@@ -2391,6 +2398,9 @@ namespace {
23912398
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
23922399
openedType, bindPatternVarsOneWay);
23932400

2401+
if (!subPatternType)
2402+
return Type();
2403+
23942404
CS.addConstraint(
23952405
ConstraintKind::Conversion, subPatternType, openedType,
23962406
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
@@ -2433,6 +2443,10 @@ namespace {
24332443
eltPattern,
24342444
locator.withPathElement(LocatorPathElt::PatternMatch(eltPattern)),
24352445
externalEltType, bindPatternVarsOneWay);
2446+
2447+
if (!eltTy)
2448+
return Type();
2449+
24362450
tupleTypeElts.push_back(TupleTypeElt(eltTy, tupleElt.getLabel()));
24372451
}
24382452

@@ -2460,6 +2474,9 @@ namespace {
24602474
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
24612475
externalPatternType, bindPatternVarsOneWay);
24622476

2477+
if (!subPatternType)
2478+
return Type();
2479+
24632480
return setType(OptionalType::get(subPatternType));
24642481
}
24652482

@@ -2468,16 +2485,25 @@ namespace {
24682485

24692486
Type castType =
24702487
resolveTypeReferenceInExpression(isPattern->getCastTypeLoc());
2488+
2489+
if (!castType)
2490+
return Type();
2491+
24712492
castType = CS.openUnboundGenericType(
24722493
castType,
24732494
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
24742495

2496+
assert(castType);
2497+
24752498
auto *subPattern = isPattern->getSubPattern();
24762499
Type subPatternType = getTypeForPattern(
24772500
subPattern,
24782501
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
24792502
castType, bindPatternVarsOneWay);
24802503

2504+
if (!subPatternType)
2505+
return Type();
2506+
24812507
// Make sure we can cast from the subpattern type to the type we're
24822508
// checking; if it's impossible, fail.
24832509
CS.addConstraint(
@@ -2510,11 +2536,16 @@ namespace {
25102536
Type parentType =
25112537
resolveTypeReferenceInExpression(enumPattern->getParentType());
25122538

2539+
if (!parentType)
2540+
return Type();
2541+
25132542
parentType = CS.openUnboundGenericType(
25142543
parentType, CS.getConstraintLocator(
25152544
locator, {LocatorPathElt::PatternMatch(pattern),
25162545
ConstraintLocator::ParentType}));
25172546

2547+
assert(parentType);
2548+
25182549
// Perform member lookup into the parent's metatype.
25192550
Type parentMetaType = MetatypeType::get(parentType);
25202551
CS.addValueMemberConstraint(
@@ -2547,6 +2578,10 @@ namespace {
25472578
// types.
25482579
Type subPatternType = getTypeForPattern(
25492580
subPattern, locator, Type(), bindPatternVarsOneWay);
2581+
2582+
if (!subPatternType)
2583+
return Type();
2584+
25502585
SmallVector<AnyFunctionType::Param, 4> params;
25512586
AnyFunctionType::decomposeInput(subPatternType, params);
25522587

0 commit comments

Comments
 (0)