diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 1aa18fea3..198df44c7 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -968,6 +968,10 @@ std::string GetNamedDeclAsString(const clang::NamedDecl *decl) { // keyword -> keyword_ // keyword_ -> keyword__ // etc + if (!name.empty() && + std::ranges::all_of(name, [](char c) { return c == '_'; })) { + name += '_'; + } for (auto &keyword : rust_keywords) { if (!name.starts_with(keyword)) continue; diff --git a/tests/unit/out/refcount/union_basic.rs b/tests/unit/out/refcount/union_basic.rs index 6ccc5aaa7..5c5ed59d7 100644 --- a/tests/unit/out/refcount/union_basic.rs +++ b/tests/unit/out/refcount/union_basic.rs @@ -43,6 +43,28 @@ impl Default for empty { } } } +#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)] +#[byte_size(8)] +pub struct underscores { + #[offset(0)] + #[byte_size(8)] + __bytes: Value>, +} +impl underscores { + pub fn __(this: Ptr) -> Ptr { + this.reinterpret_cast() + } + pub fn ___(this: Ptr) -> Ptr { + this.reinterpret_cast() + } +} +impl Default for underscores { + fn default() -> Self { + underscores { + __bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))), + } + } +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -71,6 +93,14 @@ fn main_0() -> i32 { assert!(((basic::i(ru.clone()).read()) == 1065353216)); assert!(((pf.read()) == 1_f32)); assert!((((*buf.borrow())[(3) as usize] as i32) == 63)); + let us: Value = Rc::new(RefCell::new(::default())); + underscores::__(us.as_pointer()).write(5); + assert!(((underscores::__(us.as_pointer()).read()) == 5)); + underscores::___(us.as_pointer()).write(2.5_f64); + assert!(((underscores::___(us.as_pointer()).read()) == 2.5_f64)); + let mut __: i32 = 1; + let mut ___: i32 = 2; + assert!(((__ + ___) == 3)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/union_basic.rs b/tests/unit/out/unsafe/union_basic.rs index 870ea8b18..eb1de9e12 100644 --- a/tests/unit/out/unsafe/union_basic.rs +++ b/tests/unit/out/unsafe/union_basic.rs @@ -27,6 +27,17 @@ impl Default for empty { unsafe { std::mem::zeroed() } } } +#[repr(C)] +#[derive(Copy, Clone, FnPtrArg, VaArg)] +pub union underscores { + pub __: i32, + pub ___: f64, +} +impl Default for underscores { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -58,6 +69,14 @@ unsafe fn main_0() -> i32 { assert!((((*ru).i) == (1065353216))); assert!(((*pf) == (1_f32))); assert!(((buf[(3) as usize] as i32) == (63))); + let mut us: underscores = ::default(); + us.__ = 5; + assert!(((us.__) == (5))); + us.___ = 2.5_f64; + assert!(((us.___) == (2.5_f64))); + let mut __: i32 = 1; + let mut ___: i32 = 2; + assert!((((__) + (___)) == (3))); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/union_basic.cpp b/tests/unit/union_basic.cpp index c73c7673b..647a59edf 100644 --- a/tests/unit/union_basic.cpp +++ b/tests/unit/union_basic.cpp @@ -8,6 +8,11 @@ union basic { union empty {}; +union underscores { + int _; + double __; +}; + int main(void) { union basic u; union empty e; @@ -33,5 +38,15 @@ int main(void) { assert(*pf == 1.0f); assert(buf[3] == 0x3F); + union underscores us; + us._ = 5; + assert(us._ == 5); + us.__ = 2.5; + assert(us.__ == 2.5); + + int _ = 1; + int __ = 2; + assert(_ + __ == 3); + return 0; }