Skip to content

Commit 1c6b5ce

Browse files
authored
Fix Default for enums with bool underlying type (#504)
1 parent 87e09e3 commit 1c6b5ce

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4306,7 +4306,7 @@ std::string Converter::GetDefaultAsStringFallback(clang::QualType qual_type) {
43064306
if (qual_type->isEnumeralType()) {
43074307
auto enum_decl = qual_type->castAs<clang::EnumType>()->getDecl();
43084308
if (enum_decl->enumerators().empty()) {
4309-
return std::string(1, token::kZero);
4309+
return GetDefaultAsString(enum_decl->getIntegerType());
43104310
}
43114311
return EnumeratorName(*enum_decl->enumerator_begin());
43124312
}

‎tests/unit/enum_underlying_type.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ enum Sized : uint16_t { SIZED_A = 1, SIZED_B = 0xFFFF };
1313

1414
enum class Scoped : int8_t { NEG = -2, POS = 2 };
1515

16+
enum Empty : bool {};
17+
18+
struct HoldsEmpty {
19+
Empty e{};
20+
};
21+
1622
int main() {
1723
Flag flag = FLAG_ON;
1824
assert(flag);
@@ -45,5 +51,10 @@ int main() {
4551
Scoped sc = Scoped::NEG;
4652
assert(static_cast<int>(sc) == -2);
4753
assert(static_cast<int8_t>(Scoped::POS) == 2);
54+
55+
HoldsEmpty he;
56+
assert(!he.e);
57+
Empty ev{};
58+
assert(ev == he.e);
4859
return 0;
4960
}

‎tests/unit/out/refcount/enum_underlying_type.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ pub const Sized_SIZED_B: Sized = 65535;
2424
pub type Scoped = i8;
2525
pub const Scoped_NEG: Scoped = -2;
2626
pub const Scoped_POS: Scoped = 2;
27+
pub type Empty = bool;
28+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)]
29+
#[byte_size(1)]
30+
pub struct HoldsEmpty {
31+
#[offset(0)]
32+
pub e: Empty,
33+
}
34+
impl Default for HoldsEmpty {
35+
fn default() -> Self {
36+
HoldsEmpty {
37+
e: <Empty>::default(),
38+
}
39+
}
40+
}
2741
pub fn main() {
2842
__cpp2rust_init_globals();
2943
std::process::exit(main_0());
@@ -55,6 +69,10 @@ fn main_0() -> i32 {
5569
let mut sc: Scoped = Scoped_NEG;
5670
assert!(((sc as i32) == -2_i32));
5771
assert!((((Scoped_POS as i8) as i32) == 2));
72+
let mut he: HoldsEmpty = <HoldsEmpty>::default();
73+
assert!(!(he.e));
74+
let mut ev: Empty = <Empty>::default();
75+
assert!(((ev as i32) == (he.e as i32)));
5876
return 0;
5977
}
6078
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/enum_underlying_type.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ pub const Sized_SIZED_B: Sized = 65535;
2424
pub type Scoped = i8;
2525
pub const Scoped_NEG: Scoped = -2;
2626
pub const Scoped_POS: Scoped = 2;
27+
pub type Empty = bool;
28+
#[repr(C)]
29+
#[derive(Copy, Clone, VaArg, FnPtrArg)]
30+
pub struct HoldsEmpty {
31+
pub e: Empty,
32+
}
33+
impl Default for HoldsEmpty {
34+
fn default() -> Self {
35+
HoldsEmpty { e: false }
36+
}
37+
}
2738
pub fn main() {
2839
unsafe {
2940
__cpp2rust_init_globals();
@@ -57,6 +68,10 @@ unsafe fn main_0() -> i32 {
5768
let mut sc: Scoped = Scoped_NEG;
5869
assert!(((sc as i32) == (-2_i32)));
5970
assert!((((Scoped_POS as i8) as i32) == (2)));
71+
let mut he: HoldsEmpty = <HoldsEmpty>::default();
72+
assert!(!(he.e));
73+
let mut ev: Empty = false;
74+
assert!(((ev as i32) == (he.e as i32)));
6075
return 0;
6176
}
6277
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)