Skip to content

Commit 5a80f33

Browse files
authored
Merge pull request #3193 from linux-on-ibm-z/master-s390x
2 parents 0f5156a + ac53a1e commit 5a80f33

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

lib/SILGen/SILGenPattern.cpp

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

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

10411045
// Emit the guard branch, if it exists.
10421046
if (guardExpr) {
1043-
SGF.usingImplicitVariablesForPattern(clauses[row].getCasePattern(), stmt, [&]{
1047+
SGF.usingImplicitVariablesForPattern(clauses[row].getCasePattern(), dyn_cast<CaseStmt>(stmt), [&]{
10441048
this->emitGuardBranch(guardExpr, guardExpr, failure);
10451049
});
10461050
}
@@ -2182,6 +2186,12 @@ class Lowering::PatternMatchContext {
21822186

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

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)