Skip to content

Commit caab4f4

Browse files
committed
[CSGen] Handle incorrect patterns (e.g. referencing unknown types)
Currently `generateInitPatternConstraints` assumes that all patterns have types but it's not the case for patterns that e.g. reference unknown types or have other structural issues. Let's fail `generateInitPatternConstraints` if constraint generation fails. Resolves: rdar://problem/64157451
1 parent 1715021 commit caab4f4

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

lib/Sema/CSGen.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,12 +2282,17 @@ namespace {
22822282

22832283
return setType(ParenType::get(CS.getASTContext(), underlyingType));
22842284
}
2285-
case PatternKind::Var:
2285+
case PatternKind::Var: {
2286+
auto *subPattern = cast<VarPattern>(pattern)->getSubPattern();
2287+
auto type = getTypeForPattern(subPattern, locator, externalPatternType,
2288+
bindPatternVarsOneWay);
2289+
2290+
if (!type)
2291+
return Type();
2292+
22862293
// Var doesn't affect the type.
2287-
return setType(
2288-
getTypeForPattern(
2289-
cast<VarPattern>(pattern)->getSubPattern(), locator,
2290-
externalPatternType, bindPatternVarsOneWay));
2294+
return setType(type);
2295+
}
22912296

22922297
case PatternKind::Any: {
22932298
return setType(
@@ -3918,7 +3923,9 @@ static bool generateInitPatternConstraints(
39183923
pattern, locator, target.shouldBindPatternVarsOneWay(),
39193924
target.getInitializationPatternBindingDecl(),
39203925
target.getInitializationPatternBindingIndex());
3921-
assert(patternType && "All patterns have a type");
3926+
3927+
if (!patternType)
3928+
return true;
39223929

39233930
if (auto wrappedVar = target.getInitializationWrappedVar()) {
39243931
Type propertyType = generateWrappedPropertyTypeConstraints(

test/Constraints/patterns.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,14 @@ func rdar63510989() {
507507
// expected-warning@-1 {{immutable value 'v' was never used; consider replacing with '_' or removing it}}
508508
}
509509
}
510+
511+
// rdar://problem/64157451 - compiler crash when using undefined type in pattern
512+
func rdar64157451() {
513+
enum E {
514+
case foo(Int)
515+
}
516+
517+
func test(e: E) {
518+
if case .foo(let v as DoeNotExist) = e {} // expected-error {{cannot find type 'DoeNotExist' in scope}}
519+
}
520+
}

0 commit comments

Comments
 (0)