Skip to content

Commit ac53a1e

Browse files
author
Gary Liu
committed
New test case to show the problem fixed by commit 61ce470
Fix new test case from commit fcaba99c81edcb8320856cb55367e1d8a9176675 Adding the review changes to #3193
1 parent 61ce470 commit ac53a1e

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

lib/SILGen/SILGenPattern.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,26 +1031,22 @@ void PatternMatchEmission::emitWildcardDispatch(ClauseMatrix &clauses,
10311031
bool hasGuard = guardExpr != nullptr;
10321032
assert(!hasGuard || !clauses[row].isIrrefutable());
10331033

1034-
auto stmt = clauses[row].getClientData<CaseStmt>();
1034+
auto stmt = clauses[row].getClientData<Stmt>();
1035+
assert(isa<CaseStmt>(stmt) || isa<CatchStmt>(stmt));
1036+
10351037
bool hasMultipleItems = false;
1036-
if (stmt->getKind() == StmtKind::Case) {
1037-
ArrayRef<CaseLabelItem> labelItems = stmt->getCaseLabelItems();
1038-
hasMultipleItems = labelItems.size() > 1;
1038+
if (auto *caseStmt = dyn_cast<CaseStmt>(stmt)) {
1039+
hasMultipleItems = caseStmt->getCaseLabelItems().size() > 1;
10391040
}
1040-
1041+
10411042
// Bind the rest of the patterns.
10421043
bindIrrefutablePatterns(clauses[row], args, !hasGuard, hasMultipleItems);
10431044

10441045
// Emit the guard branch, if it exists.
10451046
if (guardExpr) {
1046-
if (stmt->getKind() == StmtKind::Case) {
1047-
SGF.usingImplicitVariablesForPattern(clauses[row].getCasePattern(), stmt, [&]{
1048-
this->emitGuardBranch(guardExpr, guardExpr, failure);
1049-
});
1050-
} else {
1051-
assert(stmt->getKind() == StmtKind::Catch);
1052-
emitGuardBranch(guardExpr, guardExpr, failure);
1053-
}
1047+
SGF.usingImplicitVariablesForPattern(clauses[row].getCasePattern(), dyn_cast<CaseStmt>(stmt), [&]{
1048+
this->emitGuardBranch(guardExpr, guardExpr, failure);
1049+
});
10541050
}
10551051

10561052
// Enter the row.
@@ -2190,6 +2186,12 @@ class Lowering::PatternMatchContext {
21902186

21912187
void SILGenFunction::usingImplicitVariablesForPattern(Pattern *pattern, CaseStmt *stmt,
21922188
const llvm::function_ref<void(void)> &f) {
2189+
// Early exit for CatchStmt
2190+
if (!stmt) {
2191+
f();
2192+
return;
2193+
}
2194+
21932195
ArrayRef<CaseLabelItem> labelItems = stmt->getCaseLabelItems();
21942196
auto expectedPattern = labelItems[0].getPattern();
21952197

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: rm -rf %t && mkdir %t
2+
// RUN: %target-build-swift %s -o %t/a.out
3+
// RUN: %target-run %t/a.out | FileCheck %s
4+
// REQUIRES: executable_test
5+
6+
enum testError: ErrorProtocol {
7+
case C1
8+
case C2(Int, String)
9+
}
10+
11+
public func someFunc(_ str: String) throws -> String {
12+
if (str[str.startIndex] == "D") {
13+
throw testError.C2(2, str)
14+
}
15+
return str
16+
}
17+
18+
let testData: [String] = [
19+
"ABC",
20+
"DEF",
21+
]
22+
23+
public func testCatchWildcardDispatch(_ name: String...) throws {
24+
for dir in testData {
25+
do {
26+
print(try someFunc(dir))
27+
} catch .C2(let errno, _) as testError where errno == 2 {
28+
print(name)
29+
}
30+
}
31+
}
32+
33+
// CHECK: ABC
34+
// CHECK: ["TEST"]
35+
try testCatchWildcardDispatch("TEST")
36+

0 commit comments

Comments
 (0)