Skip to content

Commit 1922ae9

Browse files
authored
Hoist static locals to module scope (#491)
1 parent 054611a commit 1922ae9

16 files changed

Lines changed: 227 additions & 184 deletions

‎cpp2rust/converter/converter.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,8 @@ bool Converter::VisitCompoundStmt(clang::CompoundStmt *stmt) {
13461346

13471347
bool Converter::VisitDeclStmt(clang::DeclStmt *stmt) {
13481348
for (auto *decl : stmt->decls()) {
1349-
if (clang::isa<clang::TagDecl>(decl)) {
1349+
auto *var = clang::dyn_cast<clang::VarDecl>(decl);
1350+
if (clang::isa<clang::TagDecl>(decl) || (var && var->isStaticLocal())) {
13501351
Buffer buf(*this);
13511352
Convert(decl);
13521353
hoisted_records_ += std::move(buf).str();

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ fn main_0() -> i32 {
6161
== 2)
6262
);
6363
assert!(({ (*obj_4.with(Value::clone).borrow()).p.clone() }.with(|__s| __s.value) == 42));
64-
thread_local!(
65-
static cache_5: Value<Box<[Ptr<Inner>]>> = Rc::new(RefCell::new(Box::new([
66-
(alpha_0.with(|v| v.as_pointer())),
67-
(beta_1.with(|v| v.as_pointer())),
68-
])));
69-
);
7064
assert!(
7165
(({
7266
let __idx = (0) as usize;
@@ -85,6 +79,12 @@ fn main_0() -> i32 {
8579
);
8680
return 0;
8781
}
82+
thread_local!(
83+
static cache_5: Value<Box<[Ptr<Inner>]>> = Rc::new(RefCell::new(Box::new([
84+
(alpha_0.with(|v| v.as_pointer())),
85+
(beta_1.with(|v| v.as_pointer())),
86+
])));
87+
);
8888
pub fn __cpp2rust_init_globals() {
8989
let _ = alpha_0.with(|_| ());
9090
let _ = beta_1.with(|_| ());

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,21 @@ thread_local!(
118118
])));
119119
);
120120
pub fn check_local_static_5() {
121-
thread_local!(
122-
static local_outer_6: Value<Outer> = Rc::new(RefCell::new(<Outer>::default()));
123-
);
124-
thread_local!(
125-
static local_fn_7: Value<FnPtr<fn(i32) -> i32>> =
126-
Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null()));
127-
);
128-
thread_local!(
129-
static local_p_8: Value<Ptr<i32>> = Rc::new(RefCell::new(Ptr::<i32>::null()));
130-
);
131121
assert!(({ (*local_outer_6.with(Value::clone).borrow()).p1.clone() }).is_null());
132122
assert!(({ (*local_outer_6.with(Value::clone).borrow()).fn_.clone() }).is_null());
133123
assert!((*local_fn_7.with(Value::clone).borrow()).is_null());
134124
assert!((*local_p_8.with(Value::clone).borrow()).is_null());
135125
}
126+
thread_local!(
127+
static local_outer_6: Value<Outer> = Rc::new(RefCell::new(<Outer>::default()));
128+
);
129+
thread_local!(
130+
static local_fn_7: Value<FnPtr<fn(i32) -> i32>> =
131+
Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null()));
132+
);
133+
thread_local!(
134+
static local_p_8: Value<Ptr<i32>> = Rc::new(RefCell::new(Ptr::<i32>::null()));
135+
);
136136
pub fn main() {
137137
__cpp2rust_init_globals();
138138
std::process::exit(main_0());

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
pub fn next_0() -> i32 {
10-
thread_local!(
11-
static counter_1: Value<i32> = Rc::new(RefCell::new(0));
12-
);
1310
return (*counter_1.with(Value::clone).borrow_mut()).prefix_inc();
1411
}
12+
thread_local!(
13+
static counter_1: Value<i32> = Rc::new(RefCell::new(0));
14+
);
1515
pub fn marker_2(mut tag: u8) -> u8 {
1616
return ((((tag as i32) << 3) | 2) as u8);
1717
}
@@ -74,14 +74,14 @@ thread_local!(
7474
pub static member_10: Value<i32> = Rc::new(RefCell::new(({ next_0() })));
7575
);
7676
pub fn local_static_12() -> i32 {
77-
thread_local!(
78-
static once_13: Value<i32> = Rc::new(RefCell::new(({ next_0() })));
79-
);
80-
thread_local!(
81-
static local_ctor_14: Value<Ctor> = Rc::new(RefCell::new(Ctor::new_2({ 3 })));
82-
);
8377
return (once_13.with(|rc| *rc.borrow()) + { (*local_ctor_14.with(Value::clone).borrow()).v });
8478
}
79+
thread_local!(
80+
static once_13: Value<i32> = Rc::new(RefCell::new(({ next_0() })));
81+
);
82+
thread_local!(
83+
static local_ctor_14: Value<Ctor> = Rc::new(RefCell::new(Ctor::new_2({ 3 })));
84+
);
8585
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)]
8686
#[byte_size(4)]
8787
pub struct Singleton {
@@ -93,9 +93,6 @@ impl Singleton {
9393
Self { hits: 0 }
9494
}
9595
pub fn instance() -> Ptr<Singleton> {
96-
thread_local!(
97-
static s_15: Value<Singleton> = Rc::new(RefCell::new(Singleton::new()));
98-
);
9996
return s_15.with(|v| v.as_pointer());
10097
}
10198
}
@@ -104,6 +101,9 @@ impl Default for Singleton {
104101
{ Singleton::new() }
105102
}
106103
}
104+
thread_local!(
105+
static s_15: Value<Singleton> = Rc::new(RefCell::new(Singleton::new()));
106+
);
107107
pub fn main() {
108108
__cpp2rust_init_globals();
109109
std::process::exit(main_0());

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
pub fn a_0() -> i32 {
10-
thread_local!(
11-
static i_1: Value<i32> = Rc::new(RefCell::new(1));
12-
);
1310
return i_1.with(|rc| *rc.borrow());
1411
}
12+
thread_local!(
13+
static i_1: Value<i32> = Rc::new(RefCell::new(1));
14+
);
1515
pub fn b_2() -> i32 {
16-
thread_local!(
17-
static i_3: Value<i32> = Rc::new(RefCell::new(2));
18-
);
1916
return i_3.with(|rc| *rc.borrow());
2017
}
18+
thread_local!(
19+
static i_3: Value<i32> = Rc::new(RefCell::new(2));
20+
);
2121
pub fn main() {
2222
__cpp2rust_init_globals();
2323
std::process::exit(main_0());

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

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,51 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
pub fn foo_0() -> i32 {
10-
thread_local!(
11-
static static_i_1: Value<i32> = Rc::new(RefCell::new(0_i32));
12-
);
13-
thread_local!(
14-
static static_f_2: Value<f32> = Rc::new(RefCell::new(0_f32));
15-
);
16-
thread_local!(
17-
static static_b_3: Value<bool> = Rc::new(RefCell::new(false));
18-
);
19-
thread_local!(
20-
static kX1_4: Value<i32> = Rc::new(RefCell::new(1));
21-
);
22-
thread_local!(
23-
static kX2_5: Value<i32> = Rc::new(RefCell::new(2));
24-
);
2510
kX1_4.with(|rc| *rc.borrow_mut() += 1);
2611
return ((kX1_4.with(|rc| *rc.borrow()) + kX2_5.with(|rc| *rc.borrow()))
2712
+ static_i_1.with(|rc| *rc.borrow()));
2813
}
14+
thread_local!(
15+
static static_i_1: Value<i32> = Rc::new(RefCell::new(0_i32));
16+
);
17+
thread_local!(
18+
static static_f_2: Value<f32> = Rc::new(RefCell::new(0_f32));
19+
);
20+
thread_local!(
21+
static static_b_3: Value<bool> = Rc::new(RefCell::new(false));
22+
);
23+
thread_local!(
24+
static kX1_4: Value<i32> = Rc::new(RefCell::new(1));
25+
);
26+
thread_local!(
27+
static kX2_5: Value<i32> = Rc::new(RefCell::new(2));
28+
);
29+
pub fn from_local_class_6() -> i32 {
30+
return (({ S_8Impl::get(&Rc::new(RefCell::new(<S_8>::default())).as_pointer()) })
31+
+ ({ S_8Impl::get(&Rc::new(RefCell::new(<S_8>::default())).as_pointer()) }));
32+
}
33+
thread_local!(
34+
static x_7: Value<i32> = Rc::new(RefCell::new(3));
35+
);
36+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
37+
#[byte_size(1)]
38+
pub struct S_8 {}
2939
pub fn main() {
3040
__cpp2rust_init_globals();
3141
std::process::exit(main_0());
3242
}
3343
fn main_0() -> i32 {
3444
assert!((((({ foo_0() }) + ({ foo_0() })) + ({ foo_0() })) == 15));
45+
assert!((({ from_local_class_6() }) == 9));
46+
assert!((({ from_local_class_6() }) == 13));
3547
return 0;
3648
}
49+
pub trait S_8Impl {
50+
fn get(&self) -> i32;
51+
}
52+
impl S_8Impl for Ptr<S_8> {
53+
fn get(&self) -> i32 {
54+
return (*x_7.with(Value::clone).borrow_mut()).prefix_inc();
55+
}
56+
}
3757
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,6 @@ fn main_0() -> i32 {
1414
let mut special: Ptr<i8> = Ptr::<i8>::from_string_literal(
1515
b"\x07\x08\t\n\x0b\x0c\r !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\xff",
1616
);
17-
thread_local!(
18-
static expected_0: Value<Box<[i8]>> = Rc::new(RefCell::new(Box::new([
19-
7_i8,
20-
8_i8,
21-
9_i8,
22-
10_i8,
23-
11_i8,
24-
12_i8,
25-
13_i8,
26-
32_i8,
27-
33_i8,
28-
34_i8,
29-
35_i8,
30-
36_i8,
31-
37_i8,
32-
38_i8,
33-
39_i8,
34-
40_i8,
35-
41_i8,
36-
42_i8,
37-
43_i8,
38-
44_i8,
39-
45_i8,
40-
46_i8,
41-
47_i8,
42-
58_i8,
43-
59_i8,
44-
60_i8,
45-
61_i8,
46-
62_i8,
47-
63_i8,
48-
64_i8,
49-
91_i8,
50-
92_i8,
51-
93_i8,
52-
94_i8,
53-
95_i8,
54-
96_i8,
55-
123_i8,
56-
124_i8,
57-
125_i8,
58-
126_i8,
59-
(b'\xff' as i8),
60-
])));
61-
);
6217
let mut i: i32 = 0;
6318
'loop_: while (i
6419
< (((::std::mem::size_of::<[i8; 41]>() as usize)
@@ -76,4 +31,49 @@ fn main_0() -> i32 {
7631
}
7732
return 0;
7833
}
34+
thread_local!(
35+
static expected_0: Value<Box<[i8]>> = Rc::new(RefCell::new(Box::new([
36+
7_i8,
37+
8_i8,
38+
9_i8,
39+
10_i8,
40+
11_i8,
41+
12_i8,
42+
13_i8,
43+
32_i8,
44+
33_i8,
45+
34_i8,
46+
35_i8,
47+
36_i8,
48+
37_i8,
49+
38_i8,
50+
39_i8,
51+
40_i8,
52+
41_i8,
53+
42_i8,
54+
43_i8,
55+
44_i8,
56+
45_i8,
57+
46_i8,
58+
47_i8,
59+
58_i8,
60+
59_i8,
61+
60_i8,
62+
61_i8,
63+
62_i8,
64+
63_i8,
65+
64_i8,
66+
91_i8,
67+
92_i8,
68+
93_i8,
69+
94_i8,
70+
95_i8,
71+
96_i8,
72+
123_i8,
73+
124_i8,
74+
125_i8,
75+
126_i8,
76+
(b'\xff' as i8),
77+
])));
78+
);
7979
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,6 @@ pub fn main() {
8888
std::process::exit(main_0());
8989
}
9090
fn main_0() -> i32 {
91-
thread_local!(
92-
static items_4: Value<Box<[Ptr<i8>]>> = Rc::new(RefCell::new(Box::new([
93-
Ptr::<i8>::from_string_literal(b"a"),
94-
Ptr::<i8>::from_string_literal(b"b"),
95-
Ptr::<i8>::from_string_literal(b"c"),
96-
])));
97-
);
9891
let p_list: Value<Branch> = <Value<Branch>>::default();
9992
(*p_list.borrow_mut()).choice = Choice_enum_C_LIST;
10093
(*p_list.borrow_mut()).index = 0;
@@ -165,4 +158,11 @@ fn main_0() -> i32 {
165158
);
166159
return 0;
167160
}
161+
thread_local!(
162+
static items_4: Value<Box<[Ptr<i8>]>> = Rc::new(RefCell::new(Box::new([
163+
Ptr::<i8>::from_string_literal(b"a"),
164+
Ptr::<i8>::from_string_literal(b"b"),
165+
Ptr::<i8>::from_string_literal(b"c"),
166+
])));
167+
);
168168
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ unsafe fn main_0() -> i32 {
4848
(((*(*std::cell::LazyCell::force_mut(&mut *&raw mut items_3))[(1) as usize]).value) == (2))
4949
);
5050
assert!((((*(*std::cell::LazyCell::force_mut(&mut *&raw mut obj_4)).p).value) == (42)));
51-
static mut cache_5: std::cell::LazyCell<[*mut Inner; 2]> =
52-
std::cell::LazyCell::new(|| unsafe {
53-
[
54-
(&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut alpha_0)) as *mut Inner),
55-
(&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut beta_1)) as *mut Inner),
56-
]
57-
});;
5851
assert!(
5952
(((*(*std::cell::LazyCell::force_mut(&mut *&raw mut cache_5))[(0) as usize]).value) == (1))
6053
);
@@ -63,6 +56,12 @@ unsafe fn main_0() -> i32 {
6356
);
6457
return 0;
6558
}
59+
static mut cache_5: std::cell::LazyCell<[*mut Inner; 2]> = std::cell::LazyCell::new(|| unsafe {
60+
[
61+
(&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut alpha_0)) as *mut Inner),
62+
(&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut beta_1)) as *mut Inner),
63+
]
64+
});
6665
pub unsafe fn __cpp2rust_init_globals() {
6766
std::cell::LazyCell::force(&*&raw const alpha_0);
6867
std::cell::LazyCell::force(&*&raw const beta_1);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ pub static mut static_foo_array_4: std::cell::LazyCell<[Foo; 2]> =
6868
]
6969
});
7070
pub unsafe fn check_local_static_5() {
71-
static mut local_outer_6: std::cell::LazyCell<Outer> =
72-
std::cell::LazyCell::new(|| unsafe { <Outer>::default() });;
73-
static mut local_fn_7: std::cell::LazyCell<Option<unsafe fn(i32) -> i32>> =
74-
std::cell::LazyCell::new(|| unsafe { None });;
75-
static mut local_p_8: std::cell::LazyCell<*mut i32> =
76-
std::cell::LazyCell::new(|| unsafe { std::ptr::null_mut() });;
7771
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut local_outer_6)).p1).is_null());
7872
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut local_outer_6)).fn_).is_none());
7973
assert!((*std::cell::LazyCell::force_mut(&mut *&raw mut local_fn_7)).is_none());
8074
assert!((*std::cell::LazyCell::force_mut(&mut *&raw mut local_p_8)).is_null());
8175
}
76+
static mut local_outer_6: std::cell::LazyCell<Outer> =
77+
std::cell::LazyCell::new(|| unsafe { <Outer>::default() });
78+
static mut local_fn_7: std::cell::LazyCell<Option<unsafe fn(i32) -> i32>> =
79+
std::cell::LazyCell::new(|| unsafe { None });
80+
static mut local_p_8: std::cell::LazyCell<*mut i32> =
81+
std::cell::LazyCell::new(|| unsafe { std::ptr::null_mut() });
8282
pub fn main() {
8383
unsafe {
8484
__cpp2rust_init_globals();

0 commit comments

Comments
 (0)