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
2 changes: 1 addition & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4285,7 +4285,7 @@ std::string Converter::GetDefaultAsStringFallback(clang::QualType qual_type) {
if (qual_type->isEnumeralType()) {
auto enum_decl = qual_type->castAs<clang::EnumType>()->getDecl();
if (enum_decl->enumerators().empty()) {
return std::string(1, token::kZero);
return GetDefaultAsString(enum_decl->getIntegerType());
}
return EnumeratorName(*enum_decl->enumerator_begin());
}
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/enum_underlying_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -45,5 +51,10 @@ int main() {
Scoped sc = Scoped::NEG;
assert(static_cast<int>(sc) == -2);
assert(static_cast<int8_t>(Scoped::POS) == 2);

HoldsEmpty he;
assert(!he.e);
Empty ev{};
assert(ev == he.e);
return 0;
}
18 changes: 18 additions & 0 deletions tests/unit/out/refcount/enum_underlying_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: <Empty>::default(),
}
}
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand Down Expand Up @@ -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 = <HoldsEmpty>::default();
assert!(!(he.e));
let mut ev: Empty = <Empty>::default();
assert!(((ev as i32) == (he.e as i32)));
return 0;
}
pub fn __cpp2rust_init_globals() {}
15 changes: 15 additions & 0 deletions tests/unit/out/unsafe/enum_underlying_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 = <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() {}
Loading