Skip to content

Commit 7fb2011

Browse files
committed
Fix nested switches
1 parent e302514 commit 7fb2011

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ bool Converter::VisitCXXForRangeStmtIndexBased(clang::CXXForRangeStmt *stmt,
10991099

11001100
bool Converter::VisitBreakStmt([[maybe_unused]] clang::BreakStmt *stmt) {
11011101
StrCat(keyword::kBreak);
1102-
if (break_with_explicit_label_) {
1102+
if (switch_depth_ > 0) {
11031103
StrCat("'switch");
11041104
}
11051105
return false;
@@ -2665,7 +2665,7 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
26652665
StrCat("match __match_cond");
26662666
StrCat("{");
26672667

2668-
break_with_explicit_label_ = true;
2668+
++switch_depth_;
26692669

26702670
clang::SwitchCase *default_case = nullptr;
26712671
for (auto *sc : GetTopLevelSwitchCases(stmt)) {
@@ -2691,7 +2691,7 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
26912691
StrCat(R"( _ => {})");
26922692
}
26932693

2694-
break_with_explicit_label_ = false;
2694+
--switch_depth_;
26952695

26962696
StrCat("}");
26972697
StrCat("}");

cpp2rust/converter/converter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
459459
clang::ASTContext &ctx_;
460460
clang::FunctionDecl *curr_function_ = nullptr;
461461
bool in_function_formals_ = false;
462-
bool break_with_explicit_label_ = false;
462+
int switch_depth_ = 0;
463463
std::stack<clang::Expr *> curr_for_inc_;
464464
std::stack<clang::QualType> curr_init_type_;
465465

0 commit comments

Comments
 (0)