Skip to content

Commit 039e051

Browse files
Preserve switch fallthrough around non-final default arms (#685)
Valid C++ switches can silently execute the wrong branches when `default` is not last, in either output model. The fix also prevents a final case without a redundant `break` from falling into an earlier default. Keep switch-macro bodies in source order and defer the unguarded wildcard only in the generated dispatch match. Dispatch still jumps to the original body label; ordinary fallthrough then enters the next source body. Guarded wildcard arms retain their original priority. --- Made by [Perfloop](https://app.perfloop.ai). Human sponsor: [Tomás Senart](https://github.com/tsenart). Signed-off-by: Perfloop Agent <agent@perfloop.ai>
1 parent c8becea commit 039e051

6 files changed

Lines changed: 179 additions & 4 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4554,17 +4554,22 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
45544554

45554555
PushBrace match_brace(*this);
45564556

4557+
// switch! keeps source order for fallthrough and reorders only dispatch.
45574558
const SwitchArm *default_arm = nullptr;
45584559
for (const auto &arm : arms) {
45594560
if (arm.is_default_case) {
45604561
default_arm = &arm;
4561-
continue;
4562+
if (!needs_switch_macro) {
4563+
continue;
4564+
}
45624565
}
4563-
EmitSwitchArm(arm, /*is_default=*/false);
4566+
EmitSwitchArm(arm, arm.is_default_case);
45644567
}
45654568

45664569
if (default_arm) {
4567-
EmitSwitchArm(*default_arm, /*is_default=*/true);
4570+
if (!needs_switch_macro) {
4571+
EmitSwitchArm(*default_arm, /*is_default=*/true);
4572+
}
45684573
} else {
45694574
StrCat(R"( _ => {})");
45704575
}

‎libcc2rs-macros/src/state_machine.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,14 @@ impl SwitchStateMachine {
240240

241241
fn build_dispatch_arm(&self, user_arms: &[Arm], label: &Lifetime, state: &Ident) -> Arm {
242242
let cond = &self.condition;
243-
let case_arms = self.cases.iter().map(|c| {
243+
// Dispatch default last, without changing the body order used for fallthrough.
244+
let is_default = |c: &&DispatchCase| c.guard.is_none() && matches!(c.pat, Pat::Wild(_));
245+
let cases = self
246+
.cases
247+
.iter()
248+
.filter(|c| !is_default(c))
249+
.chain(self.cases.iter().filter(is_default));
250+
let case_arms = cases.map(|c| {
244251
let target_pos = user_arms
245252
.iter()
246253
.position(|a| a.label == c.target)

‎libcc2rs-macros/tests/control_flow.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,26 @@ fn switch_default_case() {
106106
assert_eq!(v, 2);
107107
}
108108

109+
#[test]
110+
fn switch_nonfinal_default() {
111+
for (input, expected) in [(0, 1), (1, 32), (2, 2)] {
112+
let mut out = 0;
113+
switch!(match input {
114+
_ if input == 0 => {
115+
out = 1;
116+
break;
117+
}
118+
_ => {
119+
out = 3;
120+
}
121+
0 | 2 => {
122+
out = out * 10 + 2;
123+
}
124+
});
125+
assert_eq!(out, expected);
126+
}
127+
}
128+
109129
#[test]
110130
fn switch_or_pattern() {
111131
for x in [1, 2, 5] {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn default_chain_0(mut x: i32) -> i32 {
10+
let mut r: i32 = 0;
11+
switch!(match x {
12+
__v if __v == 2 => {
13+
r = 2;
14+
}
15+
_ => {
16+
r = { ((r * 10) + 9) };
17+
}
18+
__v if __v == 1 => {
19+
r = { ((r * 10) + 1) };
20+
}
21+
});
22+
return r;
23+
}
24+
pub fn default_break_1(mut x: i32) -> i32 {
25+
let mut r: i32 = 0;
26+
switch!(match x {
27+
_ => {
28+
r = 9;
29+
break;
30+
}
31+
__v if __v == 1 => {
32+
r = 1;
33+
break;
34+
}
35+
__v if __v == 2 => {
36+
r = 2;
37+
}
38+
});
39+
return r;
40+
}
41+
pub fn main() {
42+
__cpp2rust_init_globals();
43+
libcc2rs::cpp2rust_exit(main_0());
44+
}
45+
fn main_0() -> i32 {
46+
assert!((({ default_chain_0(99,) }) == 91));
47+
assert!((({ default_chain_0(2,) }) == 291));
48+
assert!((({ default_chain_0(1,) }) == 1));
49+
assert!((({ default_break_1(2,) }) == 2));
50+
return 0;
51+
}
52+
pub fn __cpp2rust_init_globals() {}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn default_chain_0(mut x: i32) -> i32 {
10+
let mut r: i32 = 0;
11+
switch!(match x {
12+
__v if __v == 2 => {
13+
r = 2;
14+
}
15+
_ => {
16+
r = (((r) * (10)) + (9));
17+
}
18+
__v if __v == 1 => {
19+
r = (((r) * (10)) + (1));
20+
}
21+
});
22+
return r;
23+
}
24+
pub unsafe fn default_break_1(mut x: i32) -> i32 {
25+
let mut r: i32 = 0;
26+
switch!(match x {
27+
_ => {
28+
r = 9;
29+
break;
30+
}
31+
__v if __v == 1 => {
32+
r = 1;
33+
break;
34+
}
35+
__v if __v == 2 => {
36+
r = 2;
37+
}
38+
});
39+
return r;
40+
}
41+
pub fn main() {
42+
unsafe {
43+
__cpp2rust_init_globals();
44+
libcc2rs::cpp2rust_exit(main_0() as i32);
45+
}
46+
}
47+
unsafe fn main_0() -> i32 {
48+
assert!(((unsafe { default_chain_0(99,) }) == (91)));
49+
assert!(((unsafe { default_chain_0(2,) }) == (291)));
50+
assert!(((unsafe { default_chain_0(1,) }) == (1)));
51+
assert!(((unsafe { default_break_1(2,) }) == (2)));
52+
return 0;
53+
}
54+
pub unsafe fn __cpp2rust_init_globals() {}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <cassert>
2+
3+
int default_chain(int x) {
4+
int r = 0;
5+
switch (x) {
6+
case 2:
7+
r = 2;
8+
default:
9+
r = r * 10 + 9;
10+
case 1:
11+
r = r * 10 + 1;
12+
}
13+
return r;
14+
}
15+
16+
int default_break(int x) {
17+
int r = 0;
18+
switch (x) {
19+
default:
20+
r = 9;
21+
break;
22+
case 1:
23+
r = 1;
24+
break;
25+
case 2:
26+
r = 2;
27+
}
28+
return r;
29+
}
30+
31+
int main() {
32+
assert(default_chain(99) == 91);
33+
assert(default_chain(2) == 291);
34+
assert(default_chain(1) == 1);
35+
assert(default_break(2) == 2);
36+
return 0;
37+
}

0 commit comments

Comments
 (0)