Skip to content

Commit 4024fe3

Browse files
authored
Fix variables named _ (#502)
1 parent 9835ca9 commit 4024fe3

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,10 @@ std::string GetNamedDeclAsString(const clang::NamedDecl *decl) {
968968
// keyword -> keyword_
969969
// keyword_ -> keyword__
970970
// etc
971+
if (!name.empty() &&
972+
std::ranges::all_of(name, [](char c) { return c == '_'; })) {
973+
name += '_';
974+
}
971975
for (auto &keyword : rust_keywords) {
972976
if (!name.starts_with(keyword))
973977
continue;

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ impl Default for empty {
4343
}
4444
}
4545
}
46+
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
47+
#[byte_size(8)]
48+
pub struct underscores {
49+
#[offset(0)]
50+
#[byte_size(8)]
51+
__bytes: Value<Box<[u8]>>,
52+
}
53+
impl underscores {
54+
pub fn __(this: Ptr<Self>) -> Ptr<i32> {
55+
this.reinterpret_cast()
56+
}
57+
pub fn ___(this: Ptr<Self>) -> Ptr<f64> {
58+
this.reinterpret_cast()
59+
}
60+
}
61+
impl Default for underscores {
62+
fn default() -> Self {
63+
underscores {
64+
__bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))),
65+
}
66+
}
67+
}
4668
pub fn main() {
4769
__cpp2rust_init_globals();
4870
std::process::exit(main_0());
@@ -71,6 +93,14 @@ fn main_0() -> i32 {
7193
assert!(((basic::i(ru.clone()).read()) == 1065353216));
7294
assert!(((pf.read()) == 1_f32));
7395
assert!((((*buf.borrow())[(3) as usize] as i32) == 63));
96+
let us: Value<underscores> = Rc::new(RefCell::new(<underscores>::default()));
97+
underscores::__(us.as_pointer()).write(5);
98+
assert!(((underscores::__(us.as_pointer()).read()) == 5));
99+
underscores::___(us.as_pointer()).write(2.5_f64);
100+
assert!(((underscores::___(us.as_pointer()).read()) == 2.5_f64));
101+
let mut __: i32 = 1;
102+
let mut ___: i32 = 2;
103+
assert!(((__ + ___) == 3));
74104
return 0;
75105
}
76106
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ impl Default for empty {
2727
unsafe { std::mem::zeroed() }
2828
}
2929
}
30+
#[repr(C)]
31+
#[derive(Copy, Clone, FnPtrArg, VaArg)]
32+
pub union underscores {
33+
pub __: i32,
34+
pub ___: f64,
35+
}
36+
impl Default for underscores {
37+
fn default() -> Self {
38+
unsafe { std::mem::zeroed() }
39+
}
40+
}
3041
pub fn main() {
3142
unsafe {
3243
__cpp2rust_init_globals();
@@ -58,6 +69,14 @@ unsafe fn main_0() -> i32 {
5869
assert!((((*ru).i) == (1065353216)));
5970
assert!(((*pf) == (1_f32)));
6071
assert!(((buf[(3) as usize] as i32) == (63)));
72+
let mut us: underscores = <underscores>::default();
73+
us.__ = 5;
74+
assert!(((us.__) == (5)));
75+
us.___ = 2.5_f64;
76+
assert!(((us.___) == (2.5_f64)));
77+
let mut __: i32 = 1;
78+
let mut ___: i32 = 2;
79+
assert!((((__) + (___)) == (3)));
6180
return 0;
6281
}
6382
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/union_basic.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ union basic {
88

99
union empty {};
1010

11+
union underscores {
12+
int _;
13+
double __;
14+
};
15+
1116
int main(void) {
1217
union basic u;
1318
union empty e;
@@ -33,5 +38,15 @@ int main(void) {
3338
assert(*pf == 1.0f);
3439
assert(buf[3] == 0x3F);
3540

41+
union underscores us;
42+
us._ = 5;
43+
assert(us._ == 5);
44+
us.__ = 2.5;
45+
assert(us.__ == 2.5);
46+
47+
int _ = 1;
48+
int __ = 2;
49+
assert(_ + __ == 3);
50+
3651
return 0;
3752
}

0 commit comments

Comments
 (0)