Skip to content

Commit 1eb3254

Browse files
authored
Dedup template specializations (#628)
`main` and `b_count_local` both specialise `count` with a local type called S. This PR fixes the collision in the generated Rust code.
1 parent 6c0f20c commit 1eb3254

6 files changed

Lines changed: 94 additions & 25 deletions

File tree

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,10 @@ static std::string GetLexicalSpecializationID(const clang::Decl *decl) {
10391039
clang::dyn_cast<clang::VarTemplateSpecializationDecl>(decl)) {
10401040
id += GetMangledName(var);
10411041
}
1042+
if (const auto *fn = clang::dyn_cast<clang::FunctionDecl>(decl);
1043+
fn && fn->getTemplateSpecializationArgs()) {
1044+
id += GetMangledName(fn);
1045+
}
10421046
if (const auto *self =
10431047
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(decl)) {
10441048
id += GetMangledTypeName(self);

‎tests/std/basic/basic.def.odr/p15-function-template/a.cpp‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
#include "../../../support/asserts-dtor-count-at-exit.h"
44
#include "h.h"
55

6-
AssertsDtorCountAtExit check{2};
6+
AssertsDtorCountAtExit check{4};
77

88
int main() {
99
assert(LifetimeTracked::ctorCount == 0);
10+
struct S {};
11+
assert(count<S>() == 1);
12+
assert(b_count_local() == 1);
13+
assert(count<S>() == 2);
1014
assert(count<int>() == 1);
1115
assert(b_count_int() == 2);
1216
assert(count<int>() == 3);
13-
assert(LifetimeTracked::ctorCount == 1);
17+
assert(LifetimeTracked::ctorCount == 3);
1418
assert(count<long>() == 1);
1519
assert(b_count_long() == 2);
16-
assert(LifetimeTracked::ctorCount == 2);
20+
assert(LifetimeTracked::ctorCount == 4);
1721
return 0;
1822
}

‎tests/std/basic/basic.def.odr/p15-function-template/b.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33
int b_count_int() { return count<int>(); }
44

55
int b_count_long() { return count<long>(); }
6+
7+
int b_count_local() {
8+
struct S {};
9+
return count<S>();
10+
}

‎tests/std/basic/basic.def.odr/p15-function-template/h.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ template <class T> int count() {
1414

1515
int b_count_int();
1616
int b_count_long();
17+
18+
int b_count_local();

‎tests/std/basic/basic.def.odr/p15-function-template/out/refcount/p15-function-template.rs‎

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ pub fn count_4() -> i32 {
7979
pub fn count_7() -> i32 {
8080
return (*n_9.with(Value::clone).borrow_mut()).prefix_inc();
8181
}
82+
pub fn count_10() -> i32 {
83+
return (*n_12.with(Value::clone).borrow_mut()).prefix_inc();
84+
}
8285
thread_local!(
8386
static t_5: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked::new()));
8487
);
@@ -92,8 +95,14 @@ thread_local!(
9295
static n_9: Value<i32> = Rc::new(RefCell::new(0));
9396
);
9497
thread_local!(
95-
pub static check_10: Value<AssertsDtorCountAtExit> =
96-
Rc::new(RefCell::new(AssertsDtorCountAtExit { expected: 2 }));
98+
static t_11: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked::new()));
99+
);
100+
thread_local!(
101+
static n_12: Value<i32> = Rc::new(RefCell::new(0));
102+
);
103+
thread_local!(
104+
pub static check_13: Value<AssertsDtorCountAtExit> =
105+
Rc::new(RefCell::new(AssertsDtorCountAtExit { expected: 4 }));
97106
);
98107
pub fn main() {
99108
__cpp2rust_init_globals();
@@ -102,20 +111,41 @@ pub fn main() {
102111
fn main_0() -> i32 {
103112
assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0));
104113
assert!((({ count_4() }) == 1));
105-
assert!((({ b_count_int_11() }) == 2));
106-
assert!((({ count_4() }) == 3));
107-
assert!((ctorCount_0.with(|rc| *rc.borrow()) == 1));
114+
assert!((({ b_count_local_15() }) == 1));
115+
assert!((({ count_4() }) == 2));
108116
assert!((({ count_7() }) == 1));
109-
assert!((({ b_count_long_12() }) == 2));
110-
assert!((ctorCount_0.with(|rc| *rc.borrow()) == 2));
117+
assert!((({ b_count_int_16() }) == 2));
118+
assert!((({ count_7() }) == 3));
119+
assert!((ctorCount_0.with(|rc| *rc.borrow()) == 3));
120+
assert!((({ count_10() }) == 1));
121+
assert!((({ b_count_long_17() }) == 2));
122+
assert!((ctorCount_0.with(|rc| *rc.borrow()) == 4));
111123
return 0;
112124
}
113-
pub fn b_count_int_11() -> i32 {
114-
return ({ count_4() });
125+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
126+
#[byte_size(1)]
127+
pub struct S_14 {}
128+
pub fn count_18() -> i32 {
129+
return (*n_20.with(Value::clone).borrow_mut()).prefix_inc();
115130
}
116-
pub fn b_count_long_12() -> i32 {
131+
thread_local!(
132+
static t_19: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked::new()));
133+
);
134+
thread_local!(
135+
static n_20: Value<i32> = Rc::new(RefCell::new(0));
136+
);
137+
pub fn b_count_int_16() -> i32 {
117138
return ({ count_7() });
118139
}
140+
pub fn b_count_long_17() -> i32 {
141+
return ({ count_10() });
142+
}
143+
pub fn b_count_local_15() -> i32 {
144+
return ({ count_18() });
145+
}
146+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
147+
#[byte_size(1)]
148+
pub struct S_21 {}
119149
pub trait AssertsDtorCountAtExitImpl {
120150
fn destructor(&self);
121151
}
@@ -147,5 +177,5 @@ pub fn __cpp2rust_init_globals() {
147177
let _ = copyCount_1.with(|_| ());
148178
let _ = dtorCount_2.with(|_| ());
149179
let _ = useCount_3.with(|_| ());
150-
let _ = check_10.with(|_| ());
180+
let _ = check_13.with(|_| ());
151181
}

‎tests/std/basic/basic.def.odr/p15-function-template/out/unsafe/p15-function-template.rs‎

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,20 @@ pub unsafe fn count_4() -> i32 {
7676
pub unsafe fn count_7() -> i32 {
7777
return (*std::cell::LazyCell::force_mut(&mut *&raw mut n_9)).prefix_inc();
7878
}
79+
pub unsafe fn count_10() -> i32 {
80+
return (*std::cell::LazyCell::force_mut(&mut *&raw mut n_12)).prefix_inc();
81+
}
7982
static mut t_5: std::cell::LazyCell<LifetimeTracked> =
8083
std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() });
8184
static mut n_6: std::cell::LazyCell<i32> = std::cell::LazyCell::new(|| unsafe { 0 });
8285
static mut t_8: std::cell::LazyCell<LifetimeTracked> =
8386
std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() });
8487
static mut n_9: std::cell::LazyCell<i32> = std::cell::LazyCell::new(|| unsafe { 0 });
85-
pub static mut check_10: std::cell::LazyCell<AssertsDtorCountAtExit> =
86-
std::cell::LazyCell::new(|| unsafe { AssertsDtorCountAtExit { expected: 2 } });
88+
static mut t_11: std::cell::LazyCell<LifetimeTracked> =
89+
std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() });
90+
static mut n_12: std::cell::LazyCell<i32> = std::cell::LazyCell::new(|| unsafe { 0 });
91+
pub static mut check_13: std::cell::LazyCell<AssertsDtorCountAtExit> =
92+
std::cell::LazyCell::new(|| unsafe { AssertsDtorCountAtExit { expected: 4 } });
8793
pub fn main() {
8894
unsafe {
8995
__cpp2rust_init_globals();
@@ -93,24 +99,42 @@ pub fn main() {
9399
unsafe fn main_0() -> i32 {
94100
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (0)));
95101
assert!(((unsafe { count_4() }) == (1)));
96-
assert!(((unsafe { b_count_int_11() }) == (2)));
97-
assert!(((unsafe { count_4() }) == (3)));
98-
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (1)));
102+
assert!(((unsafe { b_count_local_15() }) == (1)));
103+
assert!(((unsafe { count_4() }) == (2)));
99104
assert!(((unsafe { count_7() }) == (1)));
100-
assert!(((unsafe { b_count_long_12() }) == (2)));
101-
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (2)));
105+
assert!(((unsafe { b_count_int_16() }) == (2)));
106+
assert!(((unsafe { count_7() }) == (3)));
107+
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (3)));
108+
assert!(((unsafe { count_10() }) == (1)));
109+
assert!(((unsafe { b_count_long_17() }) == (2)));
110+
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (4)));
102111
return 0;
103112
}
104-
pub unsafe fn b_count_int_11() -> i32 {
105-
return (unsafe { count_4() });
113+
#[repr(C)]
114+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
115+
pub struct S_14 {}
116+
pub unsafe fn count_18() -> i32 {
117+
return (*std::cell::LazyCell::force_mut(&mut *&raw mut n_20)).prefix_inc();
106118
}
107-
pub unsafe fn b_count_long_12() -> i32 {
119+
static mut t_19: std::cell::LazyCell<LifetimeTracked> =
120+
std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() });
121+
static mut n_20: std::cell::LazyCell<i32> = std::cell::LazyCell::new(|| unsafe { 0 });
122+
pub unsafe fn b_count_int_16() -> i32 {
108123
return (unsafe { count_7() });
109124
}
125+
pub unsafe fn b_count_long_17() -> i32 {
126+
return (unsafe { count_10() });
127+
}
128+
pub unsafe fn b_count_local_15() -> i32 {
129+
return (unsafe { count_18() });
130+
}
131+
#[repr(C)]
132+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
133+
pub struct S_21 {}
110134
pub unsafe fn __cpp2rust_init_globals() {
111135
std::cell::LazyCell::force(&*&raw const ctorCount_0);
112136
std::cell::LazyCell::force(&*&raw const copyCount_1);
113137
std::cell::LazyCell::force(&*&raw const dtorCount_2);
114138
std::cell::LazyCell::force(&*&raw const useCount_3);
115-
std::cell::LazyCell::force(&*&raw const check_10);
139+
std::cell::LazyCell::force(&*&raw const check_13);
116140
}

0 commit comments

Comments
 (0)