-
Notifications
You must be signed in to change notification settings - Fork 4
Translate switch with default case in the middle #30
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
nunoplopes
merged 44 commits into
Cpp2Rust:master
from
lucic71:default-case-in-the-middle
Apr 28, 2026
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
9b26449
Move visited_cases in class scope
lucic71 c33fbee
Handle default as middle case
lucic71 0c143fe
Add switch tests
lucic71 614e78e
Fix nested switches
lucic71 c4880c0
Move helper switch/case functions in converter_lib
lucic71 2aa8cc8
Emit switch! macro on switch stmt with fallthrough
lucic71 5455888
Update tests
lucic71 ac4bb8b
Add impl for switch and goto_block macros
lucic71 2c48f4a
clang-format
lucic71 e184ee7
Add switch_break
lucic71 9f9ee3b
Add SwitchBreakRewriter and LoopControlForbidden
lucic71 41bf5a3
Replace break with switch_break in tests
lucic71 caf0176
Import switch_break in libcc2rs
lucic71 cb7c711
Add negative tests
lucic71 228f855
Add banner
lucic71 6558323
Add BreakTarget
lucic71 c7416fc
Delete switch_break
lucic71 88ffb77
Update tests
lucic71 2f6a163
Add GotoStateMachine and SwitchStateMachine
lucic71 d40aeea
Describe behavior of switch and goto_block
lucic71 f84dbd4
Rename any to should_emit
lucic71 10acf9d
Add switch interleaved with loops tests
lucic71 c90fb38
Fix naming
lucic71 b0b483c
Drop explicit fallthrough from test
lucic71 cf8d4f0
Delete libcc2rs-macros
lucic71 214a0b5
Delete libcc2rs-macros references
lucic71 0749c51
Drop support for reducible default case chains
lucic71 23765a0
Delete unused functions
lucic71 033337f
Rename to break_target
lucic71 88a6e91
Rename VisitSwitchCase to ConvertSwitchCaseCondition
lucic71 690f97e
Add EmitSwitchArm
lucic71 436fc75
Simplify PushBreakTarget
lucic71 f6b377f
Track current break target using a stack
lucic71 7bc6f88
Update tests
lucic71 5caae54
Merge branch 'break-target' into default-case-in-the-middle
lucic71 cc986ec
Delete the global switch test
lucic71 77a9bb7
Re-add body declaration
lucic71 e6a66fd
Update tests
lucic71 af549fe
Fail if translation-fail test successfully compiles
lucic71 0ccf03c
Merge branch 'break-target' into default-case-in-the-middle
lucic71 f9722cd
Update tests
lucic71 acdbd2b
Merge branch 'master' into default-case-in-the-middle
lucic71 f209f74
Update tests
lucic71 aa3013e
Merge branch 'master' into default-case-in-the-middle
lucic71 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| extern crate libcc2rs; | ||
| use libcc2rs::*; | ||
| use std::cell::RefCell; | ||
| use std::collections::BTreeMap; | ||
| use std::io::prelude::*; | ||
| use std::io::{Read, Seek, Write}; | ||
| use std::os::fd::AsFd; | ||
| use std::rc::{Rc, Weak}; | ||
| pub fn case_then_default_0(x: i32) -> i32 { | ||
| let x: Value<i32> = Rc::new(RefCell::new(x)); | ||
| let r: Value<i32> = Rc::new(RefCell::new(0)); | ||
| 'switch: { | ||
| let __match_cond = (*x.borrow()); | ||
| match __match_cond { | ||
| v if v == 2 => { | ||
| (*r.borrow_mut()) = 20; | ||
| break 'switch; | ||
| } | ||
| _ => { | ||
| (*r.borrow_mut()) = 10; | ||
| break 'switch; | ||
| } | ||
| } | ||
| }; | ||
| return (*r.borrow()); | ||
| } | ||
| pub fn main() { | ||
| std::process::exit(main_0()); | ||
| } | ||
| fn main_0() -> i32 { | ||
| assert!( | ||
| (({ | ||
| let _x: i32 = 1; | ||
| case_then_default_0(_x) | ||
| }) == 10) | ||
| ); | ||
| assert!( | ||
| (({ | ||
| let _x: i32 = 2; | ||
| case_then_default_0(_x) | ||
| }) == 20) | ||
| ); | ||
| assert!( | ||
| (({ | ||
| let _x: i32 = 99; | ||
| case_then_default_0(_x) | ||
| }) == 10) | ||
| ); | ||
| return 0; | ||
| } |
56 changes: 56 additions & 0 deletions
56
tests/unit/out/refcount/switch_cases_and_default_stacked.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| extern crate libcc2rs; | ||
| use libcc2rs::*; | ||
| use std::cell::RefCell; | ||
| use std::collections::BTreeMap; | ||
| use std::io::prelude::*; | ||
| use std::io::{Read, Seek, Write}; | ||
| use std::os::fd::AsFd; | ||
| use std::rc::{Rc, Weak}; | ||
| pub fn cases_and_default_stacked_0(x: i32) -> i32 { | ||
| let x: Value<i32> = Rc::new(RefCell::new(x)); | ||
| let r: Value<i32> = Rc::new(RefCell::new(0)); | ||
| 'switch: { | ||
| let __match_cond = (*x.borrow()); | ||
| match __match_cond { | ||
| v if v == 3 => { | ||
| (*r.borrow_mut()) = 3; | ||
| break 'switch; | ||
| } | ||
| _ => { | ||
| (*r.borrow_mut()) = 42; | ||
| break 'switch; | ||
| } | ||
| } | ||
| }; | ||
| return (*r.borrow()); | ||
| } | ||
| pub fn main() { | ||
| std::process::exit(main_0()); | ||
| } | ||
| fn main_0() -> i32 { | ||
| assert!( | ||
| (({ | ||
| let _x: i32 = 1; | ||
| cases_and_default_stacked_0(_x) | ||
| }) == 42) | ||
| ); | ||
| assert!( | ||
| (({ | ||
| let _x: i32 = 2; | ||
| cases_and_default_stacked_0(_x) | ||
| }) == 42) | ||
| ); | ||
| assert!( | ||
| (({ | ||
| let _x: i32 = 3; | ||
| cases_and_default_stacked_0(_x) | ||
| }) == 3) | ||
| ); | ||
| assert!( | ||
| (({ | ||
| let _x: i32 = 99; | ||
| cases_and_default_stacked_0(_x) | ||
| }) == 42) | ||
| ); | ||
| return 0; | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of
v == 1 || v == 2 || v == 3, can't you do:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's because of enums. A switch such as:
would get translated as:
I think we can just ignore the implicit enum to integer cast in order to allow this to work with | instead of ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, then let's keep it as-is. In C++ you can mix enums and integers.