Skip to content

Commit

Permalink
[SILGenPattern] Silence redundant unreachable enum case warning (#33336)
Browse files Browse the repository at this point in the history
  • Loading branch information
theblixguy committed Aug 22, 2020
1 parent 83b2ebe commit 6a7b38d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
23 changes: 19 additions & 4 deletions lib/SILGen/SILGenPattern.cpp
Expand Up @@ -1038,20 +1038,35 @@ void PatternMatchEmission::emitDispatch(ClauseMatrix &clauses, ArgArray args,
SGF.eraseBasicBlock(contBB);
return;
}

// Otherwise, if there is no fallthrough, then the next row is
// unreachable: emit a dead code diagnostic.
// unreachable: emit a dead code diagnostic if:
// 1) It's for a 'default' case (since Space Engine already handles
// unreachable enum case patterns) or it's for a enum case which
// has expression patterns since redundancy checking for such
// patterns isn't sufficiently done by the Space Engine.
// 2) It's for a case statement in a do-catch.
if (!clauses[firstRow].hasFallthroughTo()) {
SourceLoc Loc;
bool isDefault = false;
bool isParentDoCatch = false;
bool caseHasExprPattern = false;
if (auto *S = clauses[firstRow].getClientData<Stmt>()) {
Loc = S->getStartLoc();
if (auto *CS = dyn_cast<CaseStmt>(S))
if (auto *CS = dyn_cast<CaseStmt>(S)) {
caseHasExprPattern = llvm::any_of(
CS->getCaseLabelItems(), [&](const CaseLabelItem item) {
return item.getPattern()->getKind() == PatternKind::Expr;
});
isParentDoCatch = CS->getParentKind() == CaseParentKind::DoCatch;
isDefault = CS->isDefault();
}
} else {
Loc = clauses[firstRow].getCasePattern()->getStartLoc();
}
SGF.SGM.diagnose(Loc, diag::unreachable_case, isDefault);
if (isParentDoCatch || isDefault || caseHasExprPattern) {
SGF.SGM.diagnose(Loc, diag::unreachable_case, isDefault);
}
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions test/SILGen/unreachable_code.swift
Expand Up @@ -67,8 +67,7 @@ func testUnreachableCase1(a : Tree) {
case let Leaf:
_ = Leaf
return
case .Branch(_): // expected-warning {{case will never be executed}}
// expected-warning@-1 {{case is already handled by previous patterns; consider removing it}}
case .Branch(_): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return
}
}
Expand All @@ -87,8 +86,7 @@ func testUnreachableCase3(a : Tree) {
switch a {
case _:
break
case .Branch(_): // expected-warning {{case will never be executed}}
// expected-warning@-1 {{case is already handled by previous patterns; consider removing it}}
case .Branch(_): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return
}
}
Expand Down Expand Up @@ -137,3 +135,14 @@ func sr6141() {
return;
bar?.append("x") // expected-warning{{code after 'return' will never be executed}}
}

func testUnreachableCatchClause() {
enum ErrorEnum: Error { case someError }
do {
throw ErrorEnum.someError
} catch let error {
print(error)
} catch ErrorEnum.someError { // expected-warning {{case will never be executed}}
print("some error")
}
}

0 comments on commit 6a7b38d

Please sign in to comment.