Skip to content

Commit a382072

Browse files
committed
transpile: Propagate cast of EnumConstant to its own integral type
1 parent f999097 commit a382072

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

c2rust-transpile/src/translator/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3601,6 +3601,18 @@ impl<'c> Translation<'c> {
36013601
if self.enum_constant_matches_type(target_type_id.ctype, decl_id) {
36023602
return true;
36033603
}
3604+
3605+
let source_enum_id = self.ast_context.parents[&decl_id];
3606+
let source_integral_type_id = self.enum_integral_type(source_enum_id);
3607+
let target_type_resolved_id = self
3608+
.ast_context
3609+
.resolve_type_id_no_typedef(target_type_id.ctype);
3610+
3611+
// Likewise, if we are casting to the inner integral type of the enum, then
3612+
// translate the enum constant directly as that.
3613+
if target_type_resolved_id == source_integral_type_id.ctype {
3614+
return true;
3615+
}
36043616
}
36053617
}
36063618

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ pub unsafe extern "C" fn test_enums() {
4444
bar = Bar(bar.0 - 2 as ::core::ffi::c_int);
4545
let mut e: Foo = Foo1;
4646
let mut enum_enum: ::core::ffi::c_int = (e.0 == foo.0) as ::core::ffi::c_int;
47-
let mut enum_constant: ::core::ffi::c_int =
48-
(e.0 == Foo0.0 as ::core::ffi::c_int as ::core::ffi::c_uint) as ::core::ffi::c_int;
47+
let mut enum_constant: ::core::ffi::c_int = (e.0 == Foo0.0) as ::core::ffi::c_int;
4948
let mut wrong_enum_enum: ::core::ffi::c_int =
5049
(e.0 == bar.0 as ::core::ffi::c_uint) as ::core::ffi::c_int;
5150
let mut wrong_enum_constant: ::core::ffi::c_int =

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ pub unsafe extern "C" fn test_enums() {
4545
bar = Bar(bar.0 - 2 as ::core::ffi::c_int);
4646
let mut e: Foo = Foo1;
4747
let mut enum_enum: ::core::ffi::c_int = (e.0 == foo.0) as ::core::ffi::c_int;
48-
let mut enum_constant: ::core::ffi::c_int =
49-
(e.0 == Foo0.0 as ::core::ffi::c_int as ::core::ffi::c_uint) as ::core::ffi::c_int;
48+
let mut enum_constant: ::core::ffi::c_int = (e.0 == Foo0.0) as ::core::ffi::c_int;
5049
let mut wrong_enum_enum: ::core::ffi::c_int =
5150
(e.0 == bar.0 as ::core::ffi::c_uint) as ::core::ffi::c_int;
5251
let mut wrong_enum_constant: ::core::ffi::c_int =

0 commit comments

Comments
 (0)