Skip to content

Commit 0749c51

Browse files
committed
Drop support for reducible default case chains
1 parent 214a0b5 commit 0749c51

5 files changed

Lines changed: 178 additions & 1041 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,9 +1140,8 @@ bool Converter::VisitCXXForRangeStmtIndexBased(clang::CXXForRangeStmt *stmt,
11401140
}
11411141

11421142
bool Converter::VisitBreakStmt([[maybe_unused]] clang::BreakStmt *stmt) {
1143-
if (break_target_stack_.isRegularSwitch()) {
1144-
StrCat(keyword::kBreak);
1145-
StrCat("'switch");
1143+
if (break_target_stack_.isSwitch()) {
1144+
StrCat(keyword::kBreak, "'switch");
11461145
return false;
11471146
}
11481147
StrCat(keyword::kBreak);
@@ -2652,20 +2651,12 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
26522651
auto *body = clang::dyn_cast<clang::CompoundStmt>(stmt->getBody());
26532652
assert(body);
26542653

2655-
bool has_fallthrough = SwitchHasFallthrough(stmt);
2656-
2657-
if (has_fallthrough) {
2658-
StrCat("switch!(match ", ToString(stmt->getCond()), " {");
2659-
} else {
2660-
StrCat("'switch: {");
2661-
StrCat(std::format("let __match_cond = {};", ToString(stmt->getCond())));
2662-
StrCat("match __match_cond");
2663-
StrCat("{");
2664-
}
2654+
StrCat("'switch: {");
2655+
StrCat(std::format("let __match_cond = {};", ToString(stmt->getCond())));
2656+
StrCat("match __match_cond");
2657+
StrCat("{");
26652658

2666-
PushBreakTarget push(break_target_stack_, has_fallthrough
2667-
? BreakTarget::FallthroughSwitch
2668-
: BreakTarget::RegularSwitch);
2659+
PushBreakTarget push(break_target_stack_, BreakTarget::Switch);
26692660

26702661
clang::SwitchCase *default_case = nullptr;
26712662
for (auto *sc : GetTopLevelSwitchCases(stmt)) {
@@ -2691,12 +2682,8 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
26912682
StrCat(R"( _ => {})");
26922683
}
26932684

2694-
if (has_fallthrough) {
2695-
StrCat("})");
2696-
} else {
2697-
StrCat("}");
2698-
StrCat("}");
2699-
}
2685+
StrCat("}");
2686+
StrCat("}");
27002687
return false;
27012688
}
27022689

cpp2rust/converter/converter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,13 +464,13 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
464464
clang::FunctionDecl *curr_function_ = nullptr;
465465
bool in_function_formals_ = false;
466466

467-
enum class BreakTarget { Loop, RegularSwitch, FallthroughSwitch };
467+
enum class BreakTarget { Loop, Switch };
468468
class BreakTargetStack {
469469
public:
470470
void push(BreakTarget t) { stack_.push(t); }
471471
void pop() { stack_.pop(); }
472-
bool isRegularSwitch() const {
473-
return !stack_.empty() && stack_.top() == BreakTarget::RegularSwitch;
472+
bool isSwitch() const {
473+
return !stack_.empty() && stack_.top() == BreakTarget::Switch;
474474
}
475475

476476
private:

0 commit comments

Comments
 (0)