Skip to content

Commit 5053380

Browse files
authored
Merge pull request #32624 from xedin/rdar-64844584-5.3
[5.3][CSGen] Handle incorrect patterns (e.g. referencing unknown types)
2 parents 8ebc214 + 7aa283d commit 5053380

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

lib/Sema/CSGen.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,12 +2321,18 @@ namespace {
23212321

23222322
return setType(ParenType::get(CS.getASTContext(), underlyingType));
23232323
}
2324-
case PatternKind::Var:
2324+
case PatternKind::Var: {
2325+
auto *subPattern = cast<VarPattern>(pattern)->getSubPattern();
2326+
auto type = getTypeForPattern(subPattern, locator, externalPatternType,
2327+
bindPatternVarsOneWay);
2328+
2329+
if (!type)
2330+
return Type();
2331+
23252332
// Var doesn't affect the type.
2326-
return setType(
2327-
getTypeForPattern(
2328-
cast<VarPattern>(pattern)->getSubPattern(), locator,
2329-
externalPatternType, bindPatternVarsOneWay));
2333+
return setType(type);
2334+
}
2335+
23302336
case PatternKind::Any: {
23312337
return setType(
23322338
CS.createTypeVariable(CS.getConstraintLocator(locator),
@@ -4235,7 +4241,9 @@ static bool generateInitPatternConstraints(
42354241
pattern, locator, target.shouldBindPatternVarsOneWay(),
42364242
target.getInitializationPatternBindingDecl(),
42374243
target.getInitializationPatternBindingIndex());
4238-
assert(patternType && "All patterns have a type");
4244+
4245+
if (!patternType)
4246+
return true;
42394247

42404248
if (auto wrappedVar = target.getInitializationWrappedVar()) {
42414249
// Add an equal constraint between the pattern type and the

test/Constraints/patterns.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,14 @@ func rdar63510989() {
495495
// expected-warning@-1 {{immutable value 'v' was never used; consider replacing with '_' or removing it}}
496496
}
497497
}
498+
499+
// rdar://problem/64157451 - compiler crash when using undefined type in pattern
500+
func rdar64157451() {
501+
enum E {
502+
case foo(Int)
503+
}
504+
505+
func test(e: E) {
506+
if case .foo(let v as DoeNotExist) = e {} // expected-error {{cannot find type 'DoeNotExist' in scope}}
507+
}
508+
}

0 commit comments

Comments
 (0)