Skip to content

Support Linux on z as a Swift platform: Part 2 #3193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions lib/SILGen/SILGenPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,16 +1031,20 @@ void PatternMatchEmission::emitWildcardDispatch(ClauseMatrix &clauses,
bool hasGuard = guardExpr != nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please squash this down to the original patch

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. All my changes are squashed into one.

assert(!hasGuard || !clauses[row].isIrrefutable());

auto stmt = clauses[row].getClientData<CaseStmt>();
ArrayRef<CaseLabelItem> labelItems = stmt->getCaseLabelItems();
bool hasMultipleItems = labelItems.size() > 1;

auto stmt = clauses[row].getClientData<Stmt>();
assert(isa<CaseStmt>(stmt) || isa<CatchStmt>(stmt));

bool hasMultipleItems = false;
if (auto *caseStmt = dyn_cast<CaseStmt>(stmt)) {
hasMultipleItems = caseStmt->getCaseLabelItems().size() > 1;
}

// Bind the rest of the patterns.
bindIrrefutablePatterns(clauses[row], args, !hasGuard, hasMultipleItems);

// Emit the guard branch, if it exists.
if (guardExpr) {
SGF.usingImplicitVariablesForPattern(clauses[row].getCasePattern(), stmt, [&]{
SGF.usingImplicitVariablesForPattern(clauses[row].getCasePattern(), dyn_cast<CaseStmt>(stmt), [&]{
this->emitGuardBranch(guardExpr, guardExpr, failure);
});
}
Expand Down Expand Up @@ -2182,6 +2186,12 @@ class Lowering::PatternMatchContext {

void SILGenFunction::usingImplicitVariablesForPattern(Pattern *pattern, CaseStmt *stmt,
const llvm::function_ref<void(void)> &f) {
// Early exit for CatchStmt
if (!stmt) {
f();
return;
}

ArrayRef<CaseLabelItem> labelItems = stmt->getCaseLabelItems();
auto expectedPattern = labelItems[0].getPattern();

Expand Down
36 changes: 36 additions & 0 deletions test/Interpreter/wildcard_dispatch_on_catch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// RUN: rm -rf %t && mkdir %t
// RUN: %target-build-swift %s -o %t/a.out
// RUN: %target-run %t/a.out | FileCheck %s
// REQUIRES: executable_test

enum testError: ErrorProtocol {
case C1
case C2(Int, String)
}

public func someFunc(_ str: String) throws -> String {
if (str[str.startIndex] == "D") {
throw testError.C2(2, str)
}
return str
}

let testData: [String] = [
"ABC",
"DEF",
]

public func testCatchWildcardDispatch(_ name: String...) throws {
for dir in testData {
do {
print(try someFunc(dir))
} catch .C2(let errno, _) as testError where errno == 2 {
print(name)
}
}
}

// CHECK: ABC
// CHECK: ["TEST"]
try testCatchWildcardDispatch("TEST")