Skip to content

Commit f363de3

Browse files
committed
Add template specialization with in-class initializer test
1 parent 3051af8 commit f363de3

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎tests/unit/in_class_field_initializer.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ struct S {
1313
Inner d;
1414
};
1515

16+
template <typename T> struct Boxed {
17+
T v = T();
18+
int tag = 7;
19+
Boxed(T x, int t) : v(x), tag(t) {}
20+
};
21+
1622
int main() {
1723
S s;
1824
assert(s.a == 1);
@@ -21,5 +27,8 @@ int main() {
2127
assert(s.c.y == 4);
2228
assert(s.d.x == 3);
2329
assert(s.d.y == 4);
30+
Boxed<int> boxed(5, 9);
31+
assert(boxed.v == 5);
32+
assert(boxed.tag == 9);
2433
return 0;
2534
};

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,54 @@ impl ByteRepr for S {
9393
}
9494
}
9595
}
96+
#[derive()]
97+
pub struct Boxed_int_ {
98+
pub v: Value<i32>,
99+
pub tag: Value<i32>,
100+
}
101+
impl Boxed_int_ {
102+
pub fn Boxed_int_(x: i32, t: i32) -> Self {
103+
let x: Value<i32> = Rc::new(RefCell::new(x));
104+
let t: Value<i32> = Rc::new(RefCell::new(t));
105+
let mut this = Self {
106+
v: Rc::new(RefCell::new((*x.borrow()))),
107+
tag: Rc::new(RefCell::new((*t.borrow()))),
108+
};
109+
this
110+
}
111+
}
112+
impl Clone for Boxed_int_ {
113+
fn clone(&self) -> Self {
114+
let mut this = Self {
115+
v: Rc::new(RefCell::new((*self.v.borrow()))),
116+
tag: Rc::new(RefCell::new((*self.tag.borrow()))),
117+
};
118+
this
119+
}
120+
}
121+
impl Default for Boxed_int_ {
122+
fn default() -> Self {
123+
Boxed_int_ {
124+
v: <Value<i32>>::default(),
125+
tag: <Value<i32>>::default(),
126+
}
127+
}
128+
}
129+
impl ByteRepr for Boxed_int_ {
130+
fn byte_size() -> usize {
131+
8
132+
}
133+
fn to_bytes(&self, buf: &mut [u8]) {
134+
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
135+
(*self.tag.borrow()).to_bytes(&mut buf[4..8]);
136+
}
137+
fn from_bytes(buf: &[u8]) -> Self {
138+
Self {
139+
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
140+
tag: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))),
141+
}
142+
}
143+
}
96144
pub fn main() {
97145
std::process::exit(main_0());
98146
}
@@ -104,5 +152,8 @@ fn main_0() -> i32 {
104152
assert!(((*(*(*s.borrow()).c.borrow()).y.borrow()) == 4));
105153
assert!(((*(*(*s.borrow()).d.borrow()).x.borrow()) == 3));
106154
assert!(((*(*(*s.borrow()).d.borrow()).y.borrow()) == 4));
155+
let boxed: Value<Boxed_int_> = Rc::new(RefCell::new(Boxed_int_::Boxed_int_({ 5 }, { 9 })));
156+
assert!(((*(*boxed.borrow()).v.borrow()) == 5));
157+
assert!(((*(*boxed.borrow()).tag.borrow()) == 9));
107158
return 0;
108159
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ impl Default for S {
3535
}
3636
}
3737
}
38+
#[repr(C)]
39+
#[derive(Copy, Clone)]
40+
pub struct Boxed_int_ {
41+
pub v: i32,
42+
pub tag: i32,
43+
}
44+
impl Boxed_int_ {
45+
pub unsafe fn Boxed_int_(mut x: i32, mut t: i32) -> Self {
46+
let mut this = Self { v: x, tag: t };
47+
this
48+
}
49+
}
50+
impl Default for Boxed_int_ {
51+
fn default() -> Self {
52+
Boxed_int_ {
53+
v: 0_i32,
54+
tag: 0_i32,
55+
}
56+
}
57+
}
3858
pub fn main() {
3959
unsafe {
4060
std::process::exit(main_0() as i32);
@@ -48,5 +68,8 @@ unsafe fn main_0() -> i32 {
4868
assert!(((s.c.y) == (4)));
4969
assert!(((s.d.x) == (3)));
5070
assert!(((s.d.y) == (4)));
71+
let mut boxed: Boxed_int_ = Boxed_int_::Boxed_int_({ 5 }, { 9 });
72+
assert!(((boxed.v) == (5)));
73+
assert!(((boxed.tag) == (9)));
5174
return 0;
5275
}

0 commit comments

Comments
 (0)