Translate switch with default case in the middle#30
Merged
nunoplopes merged 44 commits intoCpp2Rust:masterfrom Apr 28, 2026
Merged
Translate switch with default case in the middle#30nunoplopes merged 44 commits intoCpp2Rust:masterfrom
nunoplopes merged 44 commits intoCpp2Rust:masterfrom
Conversation
Rename visited_cases to visited_switch_cases_ and move it in class scope. VisitSwitchStmt always resets the set of visited switch cases. The old implementation was buggy because it saved reused SwitchCase pointers across translation units, making contain() return true for switches declared in other TUs.
C++ allows:
switch (x) {
default:
...
case 1:
...
}
In rust, default needs to be always on the last position, otherwise,
all values of x, even 1, will hit the default arm. Hence, the above C++
exmaple becomes:
match x {
1 => ...
_ => ...
}
As such, the algorithm for translating the cases becomes:
for (case: GetTopLevelSwitchCases()) {
if (ChainContainsDefault(case)) {
defer the conversion for the end
}
Convert(case)
Convert(GetSwitchArmBody(case))
}
Convert(deferred default)
ChainContainsDefault traverses the stacked case statements starting from
a top level case. For example:
case 2:
case 3:
default:
...
is deferred for the end of the match arms and is translated as:
_ => {}
i.e., drop the case 2, case 3 from the output, only convert as if it was
only default.
nunoplopes
reviewed
Apr 27, 2026
| } else { | ||
| StrCat("_ => {"); | ||
| } | ||
| StrCat("v if v == "); |
Contributor
There was a problem hiding this comment.
instead of v == 1 || v == 2 || v == 3, can't you do:
match x {
1 | 2 | 3 => ...
Contributor
Author
There was a problem hiding this comment.
It's because of enums. A switch such as:
enum E {
A,
B,
C
};
int main() {
enum E e = A;
switch (e) {
case A:
case B:
break;
case C:
break;
}
return 0;
}would get translated as:
match e {
// (E::A as u32) is an expression, not a pattern, so it's not allowed to be chained using |
(E::A as u32) | (E::B as u32) => {
break;
}
v if v == (E::C as u32) => {
break;
}
_ => {}
}I think we can just ignore the implicit enum to integer cast in order to allow this to work with | instead of ||
Contributor
There was a problem hiding this comment.
Ok, then let's keep it as-is. In C++ you can mix enums and integers.
Contributor
|
Please rebase; the branch has conflicts. |
Contributor
Author
Done |
Contributor
|
CI failed. |
7e175d1 to
aa3013e
Compare
Contributor
Author
Fixed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR handles code as:
In rust, the default arm has to be the last arm, otherwise, arms following the default one will be ignored, making this assertion fail
assert(default_middle(2) == 2). Hence, the rust translation becomes:This is achieved by iterating over all top-level case statements in VisitSwitchStmt and deferring the translation of the default arm until every othter arm is translated. A top-level case statement is defined as:
Furthermore, chains of case statements that contain a default statement are reduced to default, effectively making the above code translate as: