Skip to content

Commit 2fdf59a

Browse files
committed
fix out-of-line operator=
1 parent 290bc3e commit 2fdf59a

9 files changed

Lines changed: 262 additions & 3 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ include(AddClang)
3737
if (NOT CMAKE_CXX_COMPILER MATCHES "clang" AND NOT DEFINED ENV{CODEQL_EXTRACTOR_CPP_ROOT})
3838
set(CMAKE_CXX_COMPILER "${CLANG_CMAKE_DIR}/../../../bin/clang++")
3939
endif()
40+
if (CMAKE_CXX_COMPILER MATCHES "clang")
41+
# libstdc++ 15 asks for loops to be unrolled (#pragma GCC unroll in
42+
# std::find_if), which Clang warns about when it can't, depending on its
43+
# version and on inlining.
44+
set(CMAKE_CXX_FLAGS "-Wno-pass-failed ${CMAKE_CXX_FLAGS}")
45+
endif()
4046
# Exported for the testsuite, which builds the original programs with it
4147
set(CPP2RUST_CXX_COMPILER "${CMAKE_CXX_COMPILER}" CACHE INTERNAL "")
4248

‎cpp2rust/converter/converter.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6436,6 +6436,11 @@ std::string Converter::GetMappedAsString(clang::Expr *expr, clang::Expr **args,
64366436
}
64376437

64386438
// The arguments borrowed through pointers are borrowed for the whole call.
6439+
// The borrows made while converting the rule wrap its statements too, which
6440+
// weren't put in a block if they were to be in the block of the lets.
6441+
if (let_block && !mut_borrows_.empty()) {
6442+
result = '{' + result + '}';
6443+
}
64396444
for (auto &[place, name] : std::ranges::reverse_view(mut_borrows_)) {
64406445
result = std::format("{}.with_mut(|{}| {})", place, name, result);
64416446
}

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2130,8 +2130,9 @@ bool IsMethodOnPtr(const clang::CXXMethodDecl *method) {
21302130
method->getParent()->isLambda()) {
21312131
return false;
21322132
}
2133+
// Members defaulted out of line are translated from their definitions.
21332134
if (auto *definition = method->getDefinition();
2134-
definition && definition->isDefaulted() &&
2135+
definition && definition->isDefaulted() && !method->isUserProvided() &&
21352136
!IsComparisonOperator(method)) {
21362137
return false;
21372138
}

‎tests/unit/copy_assign.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <cassert>
2+
#include <vector>
23

34
static int assigns = 0;
45

@@ -58,6 +59,15 @@ struct HasCopyCounted {
5859
CopyCounted arr[2];
5960
};
6061

62+
// The assignment operator of a field called with the member call syntax.
63+
struct ExplicitOperatorCall {
64+
std::vector<int> w;
65+
ExplicitOperatorCall &operator=(const ExplicitOperatorCall &o) {
66+
w.operator=(o.w);
67+
return *this;
68+
}
69+
};
70+
6171
int main() {
6272
Partial a(1, 100), b(2, 200), c(3, 300);
6373
a = b;
@@ -115,5 +125,9 @@ int main() {
115125
hc1 = hc2;
116126
assert(hc1.c.v == 4 && hc1.arr[0].v == 5 && hc1.arr[1].v == 6);
117127
assert(copies == 0);
128+
ExplicitOperatorCall e1, e2;
129+
e1.w.push_back(5);
130+
e2 = e1;
131+
assert(e2.w.size() == 1 && e2.w[0] == 5 && e1.w.size() == 1);
118132
return 0;
119133
}

‎tests/unit/copy_move_defaulted.cpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ struct OutOfLineDefaulted {
159159
OutOfLineDefaulted::OutOfLineDefaulted() = default;
160160
OutOfLineDefaulted::OutOfLineDefaulted(const OutOfLineDefaulted &) = default;
161161

162+
// Assignment operators defaulted out of line.
163+
struct OutOfLineAssign {
164+
SeesThis s;
165+
int v = 1;
166+
std::vector<int> w;
167+
OutOfLineAssign &operator=(const OutOfLineAssign &);
168+
OutOfLineAssign &operator=(OutOfLineAssign &&);
169+
};
170+
OutOfLineAssign &OutOfLineAssign::operator=(const OutOfLineAssign &) = default;
171+
OutOfLineAssign &OutOfLineAssign::operator=(OutOfLineAssign &&) = default;
172+
162173
int main() {
163174
Explicit a(1);
164175
Explicit b = a;
@@ -288,6 +299,16 @@ int main() {
288299
o1.v = 2;
289300
OutOfLineDefaulted o2(o1);
290301
assert(o2.v == 2 && o2.x.self == &o2.x);
302+
OutOfLineAssign a1, a2, a3;
303+
a1.v = 3;
304+
a1.w.push_back(4);
305+
a2 = a1;
306+
assert(a2.v == 3 && a2.w.size() == 1 && a1.w.size() == 1);
307+
assert(a2.s.self == &a2.s);
308+
a3 = std::move(a2);
309+
assert(a3.v == 3 && a3.w.size() == 1 && a3.s.self == &a3.s);
310+
OutOfLineAssign &r = (a1 = a3);
311+
assert(&r == &a1);
291312
}
292313
return 0;
293314
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,35 @@ impl Default for HasCopyCounted {
186186
}
187187
}
188188
}
189+
#[derive(
190+
Clone,
191+
Record,
192+
ByteRepr,
193+
ZeroInitialization,
194+
VaArg,
195+
FnPtrArg,
196+
Destructible,
197+
CopyConstructible,
198+
Movable,
199+
Default,
200+
)]
201+
#[byte_size(24)]
202+
pub struct ExplicitOperatorCall {
203+
#[offset(0)]
204+
#[byte_size(24)]
205+
pub w: Vec<i32>,
206+
}
207+
impl CopyAssignable for ExplicitOperatorCall {
208+
fn copy_assign(this: &Ptr<Self>, o: Ptr<ExplicitOperatorCall>) -> Ptr<ExplicitOperatorCall> {
209+
{
210+
let mut __tmp_0 = { o.with(|__s| __s.w.clone()) };
211+
field!((*this), w).with_mut(|__b0| {
212+
(*__b0) = __tmp_0;
213+
})
214+
};
215+
return (*this).clone();
216+
}
217+
}
189218
pub fn main() {
190219
__cpp2rust_init_globals();
191220
libcc2rs::cpp2rust_exit(main_0());
@@ -354,6 +383,20 @@ fn main_0() -> i32 {
354383
&& (array_elem!(hc1.as_pointer(), arr, 1).with(|__s| __s.v) == 6)
355384
);
356385
assert!((copies_1.with(|rc| *rc.borrow()) == 0));
386+
let e1: Value<ExplicitOperatorCall> = Rc::new(RefCell::new(<ExplicitOperatorCall>::default()));
387+
let e2: Value<ExplicitOperatorCall> = Rc::new(RefCell::new(<ExplicitOperatorCall>::default()));
388+
{
389+
let __a1 = 5;
390+
(*e1.borrow_mut()).w.push(__a1)
391+
};
392+
({ CopyAssignable::copy_assign(&e2.as_pointer(), e1.as_pointer()) });
393+
assert!(
394+
({
395+
(((*e2.borrow()).w.len() == 1_usize)
396+
&& ((vec_elem!(e2.as_pointer(), w, 0_usize).read()) == 5))
397+
&& ((*e1.borrow()).w.len() == 1_usize)
398+
})
399+
);
357400
return 0;
358401
}
359402
pub trait CopyCountedImpl {

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

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,52 @@ impl Default for OutOfLineDefaulted {
485485
}
486486
}
487487
impl OutOfLineDefaulted {}
488+
#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, Destructible)]
489+
#[byte_size(40)]
490+
pub struct OutOfLineAssign {
491+
#[offset(0)]
492+
#[byte_size(8)]
493+
pub s: SeesThis,
494+
#[offset(8)]
495+
pub v: i32,
496+
#[offset(16)]
497+
#[byte_size(24)]
498+
pub w: Vec<i32>,
499+
}
500+
impl Default for OutOfLineAssign {
501+
fn default() -> Self {
502+
{
503+
OutOfLineAssign {
504+
s: <SeesThis>::default(),
505+
v: 1,
506+
w: Default::default(),
507+
}
508+
}
509+
}
510+
}
511+
impl CopyAssignable for OutOfLineAssign {
512+
fn copy_assign(this: &Ptr<Self>, _a0: Ptr<OutOfLineAssign>) -> Ptr<OutOfLineAssign> {
513+
({ CopyAssignable::copy_assign(&field_ptr!((*this), s), field_ptr!(_a0, s)) });
514+
field!((*this), v).write(_a0.with(|__s| __s.v));
515+
{
516+
let mut __tmp_0 = { _a0.with(|__s| __s.w.clone()) };
517+
field!((*this), w).with_mut(|__b0| {
518+
(*__b0) = __tmp_0;
519+
})
520+
};
521+
return (*this).clone();
522+
}
523+
}
524+
impl MoveAssignable for OutOfLineAssign {
525+
fn move_assign(this: &Ptr<Self>, _a0: Ptr<OutOfLineAssign>) -> Ptr<OutOfLineAssign> {
526+
({ MoveAssignable::move_assign(&field_ptr!((*this), s), field_ptr!(_a0, s)) });
527+
field!((*this), v).write({ (*_a0.upgrade().deref()).v });
528+
field!((*this), w).with_mut(|__b0| {
529+
(*__b0) = field!(_a0, w).with_mut(std::mem::take);
530+
});
531+
return (*this).clone();
532+
}
533+
}
488534
pub fn main() {
489535
__cpp2rust_init_globals();
490536
libcc2rs::cpp2rust_exit(main_0());
@@ -643,7 +689,7 @@ fn main_0() -> i32 {
643689
(*bufs.borrow_mut()).push(__a1)
644690
};
645691
{
646-
let mut __tmp_0 = {
692+
let mut __tmp_1 = {
647693
{
648694
let __v: Value<Buffer> = Rc::new(RefCell::new(<Buffer>::zero_init()));
649695
MoveConstructible::move_constructor(&__v.as_pointer(), {
@@ -652,7 +698,7 @@ fn main_0() -> i32 {
652698
Rc::try_unwrap(__v).ok().unwrap().into_inner()
653699
}
654700
};
655-
(*bufs.borrow_mut()).push(__tmp_0);
701+
(*bufs.borrow_mut()).push(__tmp_1);
656702
};
657703
assert!(
658704
({
@@ -820,6 +866,34 @@ fn main_0() -> i32 {
820866
({ (*o2.borrow()).v } == 2)
821867
&& ({ { (*o2.borrow()).x.self_.clone() } } == { (field_ptr!(o2.as_pointer(), x)) })
822868
);
869+
let a1: Value<OutOfLineAssign> = Rc::new(RefCell::new(<OutOfLineAssign>::default()));
870+
let a2: Value<OutOfLineAssign> = Rc::new(RefCell::new(<OutOfLineAssign>::default()));
871+
let a3: Value<OutOfLineAssign> = Rc::new(RefCell::new(<OutOfLineAssign>::default()));
872+
(*a1.borrow_mut()).v = 3;
873+
{
874+
let __a1 = 4;
875+
(*a1.borrow_mut()).w.push(__a1)
876+
};
877+
({ CopyAssignable::copy_assign(&a2.as_pointer(), a1.as_pointer()) });
878+
assert!(
879+
({
880+
(({ (*a2.borrow()).v } == 3) && ((*a2.borrow()).w.len() == 1_usize))
881+
&& ((*a1.borrow()).w.len() == 1_usize)
882+
})
883+
);
884+
assert!(({ { (*a2.borrow()).s.self_.clone() } } == { (field_ptr!(a2.as_pointer(), s)) }));
885+
({ MoveAssignable::move_assign(&a3.as_pointer(), a2.as_pointer()) });
886+
assert!(
887+
({
888+
(({ (*a3.borrow()).v } == 3) && ((*a3.borrow()).w.len() == 1_usize))
889+
&& ({ { (*a3.borrow()).s.self_.clone() } } == {
890+
(field_ptr!(a3.as_pointer(), s))
891+
})
892+
})
893+
);
894+
let r: Ptr<OutOfLineAssign> =
895+
({ CopyAssignable::copy_assign(&a1.as_pointer(), a3.as_pointer()) });
896+
assert!(({ (r).clone() } == { (a1.as_pointer()) }));
823897
}
824898
return 0;
825899
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,28 @@ pub struct HasCopyCounted {
180180
pub c: CopyCounted,
181181
pub arr: [CopyCounted; 2],
182182
}
183+
#[repr(C)]
184+
#[derive(
185+
Clone,
186+
VaArg,
187+
FnPtrArg,
188+
ZeroInitializationUnsafe,
189+
DestructibleUnsafe,
190+
CopyConstructibleUnsafe,
191+
MovableUnsafe,
192+
Default,
193+
)]
194+
pub struct ExplicitOperatorCall {
195+
pub w: Vec<i32>,
196+
}
197+
impl CopyAssignableUnsafe for ExplicitOperatorCall {
198+
unsafe fn copy_assign(&mut self, o: *const ExplicitOperatorCall) -> *mut ExplicitOperatorCall {
199+
{
200+
self.w = (*o).w.clone();
201+
};
202+
return &mut (*(self as *mut ExplicitOperatorCall));
203+
}
204+
}
183205
pub fn main() {
184206
unsafe {
185207
__cpp2rust_init_globals();
@@ -341,6 +363,16 @@ unsafe fn main_0() -> i32 {
341363
&& ((hc1.arr[(1) as usize].v) == (6))
342364
);
343365
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_1)) == (0)));
366+
let mut e1: ExplicitOperatorCall = <ExplicitOperatorCall>::default();
367+
let mut e2: ExplicitOperatorCall = <ExplicitOperatorCall>::default();
368+
{
369+
let __a1 = 5;
370+
e1.w.push(__a1)
371+
};
372+
(unsafe { CopyAssignableUnsafe::copy_assign(&mut e2, &e1) });
373+
assert!(
374+
(((e2.w.len()) == (1_usize)) && ((e2.w[(0_usize)]) == (5))) && ((e1.w.len()) == (1_usize))
375+
);
344376
return 0;
345377
}
346378
pub unsafe fn __cpp2rust_init_globals() {

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,50 @@ impl Default for OutOfLineDefaulted {
454454
}
455455
}
456456
impl OutOfLineDefaulted {}
457+
#[repr(C)]
458+
#[derive(FnPtrArg, ZeroInitializationUnsafe, DestructibleUnsafe)]
459+
pub struct OutOfLineAssign {
460+
pub s: SeesThis,
461+
pub v: i32,
462+
pub w: Vec<i32>,
463+
}
464+
impl Default for OutOfLineAssign {
465+
fn default() -> Self {
466+
unsafe {
467+
OutOfLineAssign {
468+
s: <SeesThis>::default(),
469+
v: 1,
470+
w: Default::default(),
471+
}
472+
}
473+
}
474+
}
475+
impl CopyAssignableUnsafe for OutOfLineAssign {
476+
unsafe fn copy_assign(&mut self, _a0: *const OutOfLineAssign) -> *mut OutOfLineAssign {
477+
(unsafe {
478+
let _arg0: *const SeesThis = &(*_a0).s;
479+
CopyAssignableUnsafe::copy_assign(&mut self.s, _arg0)
480+
});
481+
self.v = (*_a0).v;
482+
{
483+
self.w = (*_a0).w.clone();
484+
};
485+
return &mut (*(self as *mut OutOfLineAssign));
486+
}
487+
}
488+
impl MoveAssignableUnsafe for OutOfLineAssign {
489+
unsafe fn move_assign(&mut self, _a0: *mut OutOfLineAssign) -> *mut OutOfLineAssign {
490+
(unsafe {
491+
let _arg0: *mut SeesThis = &mut (*_a0).s;
492+
MoveAssignableUnsafe::move_assign(&mut self.s, _arg0)
493+
});
494+
self.v = (*_a0).v;
495+
{
496+
self.w = std::mem::take(&mut (*_a0).w);
497+
};
498+
return &mut (*(self as *mut OutOfLineAssign));
499+
}
500+
}
457501
pub fn main() {
458502
unsafe {
459503
__cpp2rust_init_globals();
@@ -777,6 +821,25 @@ unsafe fn main_0() -> i32 {
777821
let mut o2: OutOfLineDefaulted = <OutOfLineDefaulted>::zero_init();
778822
CopyConstructibleUnsafe::copy_constructor(&mut o2, { &o1 });
779823
assert!(((o2.v) == (2)) && ((o2.x.self_) == (&mut o2.x as *mut CtorSeesThis)));
824+
let mut a1: OutOfLineAssign = <OutOfLineAssign>::default();
825+
let mut a2: OutOfLineAssign = <OutOfLineAssign>::default();
826+
let mut a3: OutOfLineAssign = <OutOfLineAssign>::default();
827+
a1.v = 3;
828+
{
829+
let __a1 = 4;
830+
a1.w.push(__a1)
831+
};
832+
(unsafe { CopyAssignableUnsafe::copy_assign(&mut a2, &a1) });
833+
assert!((((a2.v) == (3)) && ((a2.w.len()) == (1_usize))) && ((a1.w.len()) == (1_usize)));
834+
assert!(((a2.s.self_) == (&mut a2.s as *mut SeesThis)));
835+
(unsafe { MoveAssignableUnsafe::move_assign(&mut a3, &mut a2) });
836+
assert!(
837+
(((a3.v) == (3)) && ((a3.w.len()) == (1_usize)))
838+
&& ((a3.s.self_) == (&mut a3.s as *mut SeesThis))
839+
);
840+
let r: *mut OutOfLineAssign =
841+
&mut (*(unsafe { CopyAssignableUnsafe::copy_assign(&mut a1, &a3) }));
842+
assert!(((r) == (&mut a1 as *mut OutOfLineAssign)));
780843
}
781844
return 0;
782845
}

0 commit comments

Comments
 (0)