Skip to content

Commit e81c2f9

Browse files
committed
transpile: In Case translation, do not skip over initial cast
1 parent cec7848 commit e81c2f9

5 files changed

Lines changed: 12 additions & 19 deletions

c2rust-transpile/src/cfg/mod.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,23 +1857,16 @@ impl CfgBuilder {
18571857
self.add_wip_block(wip, Jump(this_label.clone()));
18581858

18591859
// Case
1860-
let resolved = translator.ast_context.unwrap_cast_expr(case_expr);
1861-
let branch = match translator.ast_context.index(resolved).kind {
1862-
CExprKind::Literal(..) | CExprKind::ConstantExpr(_, _, Some(_)) => translator
1863-
.convert_expr(ctx.const_().pattern().used(), resolved, None)
1864-
.ok()
1865-
.and_then(WithStmts::to_pure_expr)
1866-
.and_then(|expr| expr_to_pat(*expr)),
1867-
_ => None,
1868-
};
1869-
1870-
let pat = match branch {
1871-
Some(pat) => pat,
1872-
None => match cie {
1860+
let expr = translator
1861+
.convert_expr(ctx.const_().pattern().used(), case_expr, None)
1862+
.ok()
1863+
.and_then(WithStmts::to_pure_expr);
1864+
let pat = expr
1865+
.and_then(|expr| expr_to_pat(*expr))
1866+
.unwrap_or_else(|| match cie {
18731867
ConstIntExpr::U(n) => mk().lit_pat(mk().int_unsuffixed_lit(n)),
18741868
ConstIntExpr::I(n) => mk().lit_pat(mk().int_unsuffixed_lit(n)),
1875-
},
1876-
};
1869+
});
18771870

18781871
self.switch_expr_cases
18791872
.last_mut()

c2rust-transpile/tests/snapshots/snapshots__transpile@enums.c.2021.clang15.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub unsafe extern "C" fn test_enums() {
5050
let mut wrong_enum_constant: ::core::ffi::c_int =
5151
(e.0 == Bar0.0 as ::core::ffi::c_uint) as ::core::ffi::c_int;
5252
match foo.0 {
53-
0 | 1 | 2 | 3 | 42 | 4294967254 | _ => {}
53+
0 | FOO1_MACRO | 2 | 3 | 42 | 4294967254 | _ => {}
5454
}
5555
match bar.0 {
5656
0 | BAR1_MACRO | 2 | 3 | -1 | 42 | -42 | _ => {}

c2rust-transpile/tests/snapshots/snapshots__transpile@enums.c.2024.clang15.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub unsafe extern "C" fn test_enums() {
5151
let mut wrong_enum_constant: ::core::ffi::c_int =
5252
(e.0 == Bar0.0 as ::core::ffi::c_uint) as ::core::ffi::c_int;
5353
match foo.0 {
54-
0 | 1 | 2 | 3 | 42 | 4294967254 | _ => {}
54+
0 | FOO1_MACRO | 2 | 3 | 42 | 4294967254 | _ => {}
5555
}
5656
match bar.0 {
5757
0 | BAR1_MACRO | 2 | 3 | -1 | 42 | -42 | _ => {}

c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.2021.clang15.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub unsafe extern "C" fn ZSTD_dParam_getBounds(mut dParam: ZSTD_dParameter) -> :
2525
bounds = 1 as ::core::ffi::c_int;
2626
return bounds;
2727
}
28-
1000 => {
28+
ZSTD_d_format => {
2929
bounds = 5 as ::core::ffi::c_int;
3030
return bounds;
3131
}

c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.2024.clang15.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub unsafe extern "C" fn ZSTD_dParam_getBounds(mut dParam: ZSTD_dParameter) -> :
2626
bounds = 1 as ::core::ffi::c_int;
2727
return bounds;
2828
}
29-
1000 => {
29+
ZSTD_d_format => {
3030
bounds = 5 as ::core::ffi::c_int;
3131
return bounds;
3232
}

0 commit comments

Comments
 (0)