Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4553,17 +4553,22 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {

PushBrace match_brace(*this);

// switch! keeps source order for fallthrough and reorders only dispatch.
const SwitchArm *default_arm = nullptr;
for (const auto &arm : arms) {
if (arm.is_default_case) {
default_arm = &arm;
continue;
if (!needs_switch_macro) {
continue;
}
}
EmitSwitchArm(arm, /*is_default=*/false);
EmitSwitchArm(arm, arm.is_default_case);
}

if (default_arm) {
EmitSwitchArm(*default_arm, /*is_default=*/true);
if (!needs_switch_macro) {
EmitSwitchArm(*default_arm, /*is_default=*/true);
}
} else {
StrCat(R"( _ => {})");
}
Expand Down
9 changes: 8 additions & 1 deletion libcc2rs-macros/src/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,14 @@ impl SwitchStateMachine {

fn build_dispatch_arm(&self, user_arms: &[Arm], label: &Lifetime, state: &Ident) -> Arm {
let cond = &self.condition;
let case_arms = self.cases.iter().map(|c| {
// Dispatch default last, without changing the body order used for fallthrough.
let is_default = |c: &&DispatchCase| c.guard.is_none() && matches!(c.pat, Pat::Wild(_));
let cases = self
.cases
.iter()
.filter(|c| !is_default(c))
.chain(self.cases.iter().filter(is_default));
let case_arms = cases.map(|c| {
let target_pos = user_arms
.iter()
.position(|a| a.label == c.target)
Expand Down
20 changes: 20 additions & 0 deletions libcc2rs-macros/tests/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,26 @@ fn switch_default_case() {
assert_eq!(v, 2);
}

#[test]
fn switch_nonfinal_default() {
for (input, expected) in [(0, 1), (1, 32), (2, 2)] {
let mut out = 0;
switch!(match input {
_ if input == 0 => {
out = 1;
break;
}
_ => {
out = 3;
}
0 | 2 => {
out = out * 10 + 2;
}
});
assert_eq!(out, expected);
}
}

#[test]
fn switch_or_pattern() {
for x in [1, 2, 5] {
Expand Down
52 changes: 52 additions & 0 deletions tests/unit/out/refcount/switch_default_fallthrough.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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 default_chain_0(mut x: i32) -> i32 {
let mut r: i32 = 0;
switch!(match x {
__v if __v == 2 => {
r = 2;
}
_ => {
r = { ((r * 10) + 9) };
}
__v if __v == 1 => {
r = { ((r * 10) + 1) };
}
});
return r;
}
pub fn default_break_1(mut x: i32) -> i32 {
let mut r: i32 = 0;
switch!(match x {
_ => {
r = 9;
break;
}
__v if __v == 1 => {
r = 1;
break;
}
__v if __v == 2 => {
r = 2;
}
});
return r;
}
pub fn main() {
__cpp2rust_init_globals();
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
assert!((({ default_chain_0(99,) }) == 91));
assert!((({ default_chain_0(2,) }) == 291));
assert!((({ default_chain_0(1,) }) == 1));
assert!((({ default_break_1(2,) }) == 2));
return 0;
}
pub fn __cpp2rust_init_globals() {}
54 changes: 54 additions & 0 deletions tests/unit/out/unsafe/switch_default_fallthrough.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub unsafe fn default_chain_0(mut x: i32) -> i32 {
let mut r: i32 = 0;
switch!(match x {
__v if __v == 2 => {
r = 2;
}
_ => {
r = (((r) * (10)) + (9));
}
__v if __v == 1 => {
r = (((r) * (10)) + (1));
}
});
return r;
}
pub unsafe fn default_break_1(mut x: i32) -> i32 {
let mut r: i32 = 0;
switch!(match x {
_ => {
r = 9;
break;
}
__v if __v == 1 => {
r = 1;
break;
}
__v if __v == 2 => {
r = 2;
}
});
return r;
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
assert!(((unsafe { default_chain_0(99,) }) == (91)));
assert!(((unsafe { default_chain_0(2,) }) == (291)));
assert!(((unsafe { default_chain_0(1,) }) == (1)));
assert!(((unsafe { default_break_1(2,) }) == (2)));
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
37 changes: 37 additions & 0 deletions tests/unit/switch_default_fallthrough.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <cassert>

int default_chain(int x) {
int r = 0;
switch (x) {
case 2:
r = 2;
default:
r = r * 10 + 9;
case 1:
r = r * 10 + 1;
}
return r;
}

int default_break(int x) {
int r = 0;
switch (x) {
default:
r = 9;
break;
case 1:
r = 1;
break;
case 2:
r = 2;
}
return r;
}

int main() {
assert(default_chain(99) == 91);
assert(default_chain(2) == 291);
assert(default_chain(1) == 1);
assert(default_break(2) == 2);
return 0;
}
Loading