diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index b63c7fcb3..929c41c9d 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -4285,7 +4285,7 @@ std::string Converter::GetDefaultAsStringFallback(clang::QualType qual_type) { if (qual_type->isEnumeralType()) { auto enum_decl = qual_type->castAs()->getDecl(); if (enum_decl->enumerators().empty()) { - return std::string(1, token::kZero); + return GetDefaultAsString(enum_decl->getIntegerType()); } return EnumeratorName(*enum_decl->enumerator_begin()); } diff --git a/tests/unit/enum_underlying_type.cpp b/tests/unit/enum_underlying_type.cpp index 1dcec38c6..90d168ba5 100644 --- a/tests/unit/enum_underlying_type.cpp +++ b/tests/unit/enum_underlying_type.cpp @@ -13,6 +13,12 @@ enum Sized : uint16_t { SIZED_A = 1, SIZED_B = 0xFFFF }; enum class Scoped : int8_t { NEG = -2, POS = 2 }; +enum Empty : bool {}; + +struct HoldsEmpty { + Empty e{}; +}; + int main() { Flag flag = FLAG_ON; assert(flag); @@ -45,5 +51,10 @@ int main() { Scoped sc = Scoped::NEG; assert(static_cast(sc) == -2); assert(static_cast(Scoped::POS) == 2); + + HoldsEmpty he; + assert(!he.e); + Empty ev{}; + assert(ev == he.e); return 0; } diff --git a/tests/unit/out/refcount/enum_underlying_type.rs b/tests/unit/out/refcount/enum_underlying_type.rs index 050a10e76..dad2fce2e 100644 --- a/tests/unit/out/refcount/enum_underlying_type.rs +++ b/tests/unit/out/refcount/enum_underlying_type.rs @@ -24,6 +24,20 @@ pub const Sized_SIZED_B: Sized = 65535; pub type Scoped = i8; pub const Scoped_NEG: Scoped = -2; pub const Scoped_POS: Scoped = 2; +pub type Empty = bool; +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(1)] +pub struct HoldsEmpty { + #[offset(0)] + pub e: Empty, +} +impl Default for HoldsEmpty { + fn default() -> Self { + HoldsEmpty { + e: ::default(), + } + } +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -55,6 +69,10 @@ fn main_0() -> i32 { let mut sc: Scoped = Scoped_NEG; assert!(((sc as i32) == -2_i32)); assert!((((Scoped_POS as i8) as i32) == 2)); + let mut he: HoldsEmpty = ::default(); + assert!(!(he.e)); + let mut ev: Empty = ::default(); + assert!(((ev as i32) == (he.e as i32))); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/enum_underlying_type.rs b/tests/unit/out/unsafe/enum_underlying_type.rs index 99220ceee..dc5e727c1 100644 --- a/tests/unit/out/unsafe/enum_underlying_type.rs +++ b/tests/unit/out/unsafe/enum_underlying_type.rs @@ -24,6 +24,17 @@ pub const Sized_SIZED_B: Sized = 65535; pub type Scoped = i8; pub const Scoped_NEG: Scoped = -2; pub const Scoped_POS: Scoped = 2; +pub type Empty = bool; +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg)] +pub struct HoldsEmpty { + pub e: Empty, +} +impl Default for HoldsEmpty { + fn default() -> Self { + HoldsEmpty { e: false } + } +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -57,6 +68,10 @@ unsafe fn main_0() -> i32 { let mut sc: Scoped = Scoped_NEG; assert!(((sc as i32) == (-2_i32))); assert!((((Scoped_POS as i8) as i32) == (2))); + let mut he: HoldsEmpty = ::default(); + assert!(!(he.e)); + let mut ev: Empty = false; + assert!(((ev as i32) == (he.e as i32))); return 0; } pub unsafe fn __cpp2rust_init_globals() {}