Skip to content

Commit 1c34f99

Browse files
committed
Add pointer comparison in ctor
1 parent 32c481d commit 1c34f99

3 files changed

Lines changed: 60 additions & 14 deletions

File tree

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct S {
1212
pub self__: Value<Ptr<S>>,
1313
}
1414
impl S {
15-
pub fn S(a: i32) -> Self {
15+
pub fn S1(a: i32) -> Self {
1616
let a: Value<i32> = Rc::new(RefCell::new(a));
1717
let __this: Value<S> = Rc::new(RefCell::new(Self {
1818
a_: Rc::new(RefCell::new((*a.borrow()))),
@@ -21,6 +21,21 @@ impl S {
2121
let this: Ptr<S> = __this.as_pointer();
2222
Rc::try_unwrap(__this).ok().unwrap().into_inner()
2323
}
24+
pub fn S2(a: i32, other: Ptr<S>) -> Self {
25+
let a: Value<i32> = Rc::new(RefCell::new(a));
26+
let other: Value<Ptr<S>> = Rc::new(RefCell::new(other));
27+
let __this: Value<S> = Rc::new(RefCell::new(Self {
28+
a_: Rc::new(RefCell::new((*a.borrow()))),
29+
self__: Rc::new(RefCell::new(Ptr::<S>::null())),
30+
}));
31+
let this: Ptr<S> = __this.as_pointer();
32+
if (this == (*other.borrow())) {
33+
(*(*this.upgrade().deref()).self__.borrow_mut()) = Ptr::<S>::null();
34+
} else {
35+
(*(*this.upgrade().deref()).self__.borrow_mut()) = (*other.borrow()).clone();
36+
}
37+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
38+
}
2439
}
2540
impl Clone for S {
2641
fn clone(&self) -> Self {
@@ -92,7 +107,7 @@ pub fn main() {
92107
std::process::exit(main_0());
93108
}
94109
fn main_0() -> i32 {
95-
let s: Value<S> = Rc::new(RefCell::new(S::S({ 1 })));
110+
let s: Value<S> = Rc::new(RefCell::new(S::S1({ 1 })));
96111
let ref_: Ptr<S> = ({ SImpl::returns_this_reference(&s.as_pointer()) });
97112
(*(*ref_.upgrade().deref()).a_.borrow_mut()).postfix_inc();
98113
assert!(((*(*s.borrow()).a_.borrow()) == 2));
@@ -122,22 +137,22 @@ fn main_0() -> i32 {
122137
assert!(((*(*d.borrow()).a_.borrow()) == 6));
123138
let cr: Ptr<S> = ({ SImpl::cref(&s.as_pointer()) });
124139
assert!(((*(*cr.upgrade().deref()).a_.borrow()) == 9));
125-
let t: Value<S> = Rc::new(RefCell::new(S::S({ 0 })));
140+
let t: Value<S> = Rc::new(RefCell::new(S::S1({ 0 })));
126141
assert!(
127142
({
128143
let _o: Ptr<S> = (s.as_pointer());
129144
SImpl::is(&s.as_pointer(), _o)
130145
})
131146
);
132147
assert!(!({ SImpl::is(&s.as_pointer(), (t.as_pointer()),) }));
133-
let p: Value<Ptr<S>> = Rc::new(RefCell::new(Ptr::alloc(S::S({ 1 }))));
148+
let p: Value<Ptr<S>> = Rc::new(RefCell::new(Ptr::alloc(S::S1({ 1 }))));
134149
let q: Value<Ptr<S>> = Rc::new(RefCell::new(
135150
({ SImpl::returns_this_pointer(&(*p.borrow())) }),
136151
));
137152
(*(*(*q.borrow()).upgrade().deref()).a_.borrow_mut()).postfix_inc();
138153
assert!(((*(*(*p.borrow()).upgrade().deref()).a_.borrow()) == 2));
139154
(*p.borrow()).delete();
140-
let h: Value<Ptr<S>> = Rc::new(RefCell::new(Ptr::alloc(S::S({ 5 }))));
155+
let h: Value<Ptr<S>> = Rc::new(RefCell::new(Ptr::alloc(S::S1({ 5 }))));
141156
({ SImpl::destroy(&(*h.borrow())) });
142157
({ SImpl::reset(&s.as_pointer()) });
143158
assert!(((*(*s.borrow()).a_.borrow()) == 0));
@@ -156,7 +171,7 @@ fn main_0() -> i32 {
156171
}) as i32)
157172
== (false as i32))
158173
);
159-
let other: Value<S> = Rc::new(RefCell::new(S::S({ 22 })));
174+
let other: Value<S> = Rc::new(RefCell::new(S::S1({ 22 })));
160175
assert!(
161176
((({ SImpl::copy_if_different(&s.as_pointer(), (other.as_pointer()),) }) as i32)
162177
== (true as i32))
@@ -166,6 +181,11 @@ fn main_0() -> i32 {
166181
let _lhs = (*(*s.borrow()).self__.borrow()).clone();
167182
_lhs == (*(*other.borrow()).self__.borrow()).clone()
168183
});
184+
let u: Value<S> = Rc::new(RefCell::new(S::S2({ 1 }, { (s.as_pointer()) })));
185+
assert!({
186+
let _lhs = (*(*u.borrow()).self__.borrow()).clone();
187+
_lhs == (s.as_pointer())
188+
});
169189
return 0;
170190
}
171191
pub trait SImpl {
@@ -222,7 +242,7 @@ impl SImpl for Ptr<S> {
222242
(*self).delete();
223243
}
224244
fn reset(&self) {
225-
(*self).write(S::S({ 0 }));
245+
(*self).write(S::S1({ 0 }));
226246
}
227247
fn copy_if_different_const(&self, other: Ptr<S>) -> bool {
228248
let other: Value<Ptr<S>> = Rc::new(RefCell::new(other));

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,27 @@ pub struct S {
1313
pub self__: *mut S,
1414
}
1515
impl S {
16-
pub unsafe fn S(mut a: i32) -> Self {
16+
pub unsafe fn S1(mut a: i32) -> Self {
1717
let mut __this = Self {
1818
a_: a,
1919
self__: std::ptr::null_mut(),
2020
};
2121
let this = &raw mut __this;
2222
__this
2323
}
24+
pub unsafe fn S2(mut a: i32, mut other: *mut S) -> Self {
25+
let mut __this = Self {
26+
a_: a,
27+
self__: std::ptr::null_mut(),
28+
};
29+
let this = &raw mut __this;
30+
if ((this) == (other)) {
31+
(*this).self__ = std::ptr::null_mut();
32+
} else {
33+
(*this).self__ = other;
34+
}
35+
__this
36+
}
2437
pub unsafe fn returns_this_reference(&mut self) -> *mut S {
2538
let this = self as *mut S;
2639
return &mut (*this) as *mut S;
@@ -68,7 +81,7 @@ impl S {
6881
}
6982
pub unsafe fn reset(&mut self) {
7083
let this = self as *mut S;
71-
(*this) = S::S({ 0 });
84+
(*this) = S::S1({ 0 });
7285
}
7386
pub unsafe fn copy_if_different_const(&mut self, mut other: *const S) -> bool {
7487
let this = self as *mut S;
@@ -111,7 +124,7 @@ pub fn main() {
111124
}
112125
}
113126
unsafe fn main_0() -> i32 {
114-
let mut s: S = S::S({ 1 });
127+
let mut s: S = S::S1({ 1 });
115128
let ref_: *mut S = (unsafe { S::returns_this_reference(&mut s) });
116129
(*ref_).a_.postfix_inc();
117130
assert!(((s.a_) == (2)));
@@ -133,20 +146,20 @@ unsafe fn main_0() -> i32 {
133146
assert!(((d.a_) == (6)));
134147
let cr: *const S = (unsafe { S::cref(&s) });
135148
assert!((((*cr).a_) == (9)));
136-
let mut t: S = S::S({ 0 });
149+
let mut t: S = S::S1({ 0 });
137150
assert!(
138151
(unsafe {
139152
let _o: *const S = (&mut s as *mut S).cast_const();
140153
S::is(&s, _o)
141154
})
142155
);
143156
assert!(!(unsafe { S::is(&s, (&mut t as *mut S).cast_const(),) }));
144-
let mut p: *mut S = (Box::leak(Box::new(S::S({ 1 }))) as *mut S);
157+
let mut p: *mut S = (Box::leak(Box::new(S::S1({ 1 }))) as *mut S);
145158
let mut q: *mut S = (unsafe { S::returns_this_pointer(&mut (*p)) });
146159
(*q).a_.postfix_inc();
147160
assert!((((*p).a_) == (2)));
148161
::std::mem::drop(Box::from_raw(p));
149-
let mut h: *mut S = (Box::leak(Box::new(S::S({ 5 }))) as *mut S);
162+
let mut h: *mut S = (Box::leak(Box::new(S::S1({ 5 }))) as *mut S);
150163
(unsafe { S::destroy(&mut (*h)) });
151164
(unsafe { S::reset(&mut s) });
152165
assert!(((s.a_) == (0)));
@@ -165,12 +178,14 @@ unsafe fn main_0() -> i32 {
165178
}) as i32)
166179
== (false as i32))
167180
);
168-
let mut other: S = S::S({ 22 });
181+
let mut other: S = S::S1({ 22 });
169182
assert!(
170183
(((unsafe { S::copy_if_different(&mut s, (&mut other as *mut S),) }) as i32)
171184
== (true as i32))
172185
);
173186
assert!(((s.a_) == (other.a_)));
174187
assert!(((s.self__) == (other.self__)));
188+
let mut u: S = S::S2({ 1 }, { (&mut s as *mut S) });
189+
assert!(((u.self__) == (&mut s as *mut S)));
175190
return 0;
176191
}

‎tests/unit/this.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ void bump(S *p);
66
struct S {
77
S(int a) : a_(a), self_(nullptr) {}
88

9+
S(int a, S *other) : a_(a) {
10+
if (this == other) {
11+
self_ = nullptr;
12+
} else {
13+
self_ = other;
14+
}
15+
}
16+
917
S &returns_this_reference() { return *this; }
1018

1119
S *returns_this_pointer() { return this; }
@@ -120,5 +128,8 @@ int main() {
120128
assert(s.a_ == other.a_);
121129
assert(s.self_ == other.self_);
122130

131+
S u(1, &s);
132+
assert(u.self_ == &s);
133+
123134
return 0;
124135
}

0 commit comments

Comments
 (0)