Skip to content

Commit bd55523

Browse files
Make substring field test independent of std::string layout
The field-receiver case kept a std::string by value in a struct, so the refcount expected output recorded libstdc++'s 32-byte layout and failed on libc++, where std::string is 24 bytes. Reach the string through a reference member instead: substr still takes its receiver from a struct field, and the emitted layout is only the pointer-sized field. Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
1 parent 51b87b5 commit bd55523

3 files changed

Lines changed: 23 additions & 65 deletions

File tree

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

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,12 @@ use std::io::prelude::*;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
9-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign)]
10-
#[byte_size(32)]
9+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
10+
#[byte_size(8)]
1111
pub struct Text {
1212
#[offset(0)]
13-
#[byte_size(32)]
14-
pub value: Vec<i8>,
15-
}
16-
impl Text {
17-
pub fn move_from(_a0: Ptr<Text>) -> Self {
18-
Self {
19-
value: field!(_a0, value).with_mut(std::mem::take),
20-
}
21-
}
22-
}
23-
impl Default for Text {
24-
fn default() -> Self {
25-
{ Text { value: vec![0] } }
26-
}
13+
#[byte_size(8)]
14+
pub value: Ptr<Vec<i8>>,
2715
}
2816
pub fn receiver_0(s: Ptr<Vec<i8>>, calls: Ptr<i32>) -> Ptr<Vec<i8>> {
2917
calls.with_mut(|__v| __v.prefix_inc());
@@ -181,16 +169,17 @@ fn main_0() -> i32 {
181169
}),
182170
});
183171
println!("empty: {}", ((*out.borrow()).len() - 1));
172+
let field: Value<Vec<i8>> = Rc::new(RefCell::new({
173+
let mut __bytes = Ptr::<i8>::from_string_literal(b"hello").to_c_bytes();
174+
__bytes.push(0);
175+
__bytes
176+
}));
184177
let text: Value<Text> = Rc::new(RefCell::new(Text {
185-
value: {
186-
let mut __bytes = Ptr::<i8>::from_string_literal(b"hello").to_c_bytes();
187-
__bytes.push(0);
188-
__bytes
189-
},
178+
value: field.as_pointer(),
190179
}));
191180
out.as_pointer().write(
192181
match (
193-
field_ref!(text.as_pointer(), value).as_pointer(),
182+
({ (*text.borrow()).value.clone() }).clone(),
194183
1_usize,
195184
2_usize,
196185
) {
@@ -206,7 +195,7 @@ fn main_0() -> i32 {
206195
println!(
207196
"field: {} {}",
208197
out.decay(),
209-
field_ref!(text.as_pointer(), value).decay()
198+
{ (*text.borrow()).value.clone() }.decay()
210199
);
211200
let strings: Value<Vec<Vec<i8>>> = Rc::new(RefCell::new(Vec::from([
212201
{
@@ -239,13 +228,4 @@ fn main_0() -> i32 {
239228
println!("element: {} {}", out.decay(), index);
240229
return 0;
241230
}
242-
pub trait TextImpl {
243-
fn move_assign(&self, _a0: Ptr<Text>) -> Ptr<Text>;
244-
}
245-
impl TextImpl for Ptr<Text> {
246-
fn move_assign(&self, _a0: Ptr<Text>) -> Ptr<Text> {
247-
field!((*self), value).write({ { (*_a0.upgrade().deref()).value.clone() } });
248-
return (*self).clone();
249-
}
250-
}
251231
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,9 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
99
#[repr(C)]
10-
#[derive(Clone, VaArg, FnPtrArg, MoveCtorUnsafe, MoveAssignUnsafe)]
10+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
1111
pub struct Text {
12-
pub value: Vec<libc::c_char>,
13-
}
14-
impl Text {
15-
pub unsafe fn move_from(_a0: *mut Text) -> Self {
16-
let mut this = Self {
17-
value: std::mem::take(&mut (*_a0).value),
18-
};
19-
this
20-
}
21-
pub unsafe fn move_assign(&mut self, _a0: *mut Text) -> *mut Text {
22-
self.value = ((*_a0).value).clone();
23-
return &mut (*(self as *mut Text));
24-
}
25-
}
26-
impl Default for Text {
27-
fn default() -> Self {
28-
unsafe { Text { value: vec![0] } }
29-
}
12+
pub value: *mut Vec<libc::c_char>,
3013
}
3114
pub unsafe fn receiver_0(s: *mut Vec<libc::c_char>, calls: *mut i32) -> *mut Vec<libc::c_char> {
3215
(*calls).prefix_inc();
@@ -206,18 +189,12 @@ unsafe fn main_0() -> i32 {
206189
(out.len() - 1),
207190
)
208191
});
209-
let mut text: Text = Text {
210-
value: {
211-
let s = c"hello".as_ptr();
212-
std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1)
213-
.to_vec()
214-
},
192+
let mut field: Vec<libc::c_char> = {
193+
let s = c"hello".as_ptr();
194+
std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1).to_vec()
215195
};
216-
out = match (
217-
(&mut text.value as *const Vec<libc::c_char>),
218-
1_usize,
219-
2_usize,
220-
) {
196+
let mut text: Text = Text { value: &mut field };
197+
out = match ((text.value as *const Vec<libc::c_char>), 1_usize, 2_usize) {
221198
(__source, __pos, __count) => {
222199
let __s = &*__source;
223200
let mut __tmp1 = __s[__pos..::std::cmp::min(__pos + __count, __s.len() - 1)].to_vec();
@@ -229,7 +206,7 @@ unsafe fn main_0() -> i32 {
229206
libc::printf(
230207
c"field: %s %s\n".as_ptr() as *const libc::c_char,
231208
(out.as_ptr()),
232-
(text.value.as_ptr()),
209+
((*text.value).as_ptr()),
233210
)
234211
});
235212
let mut strings: Vec<Vec<libc::c_char>> = Vec::from([

‎tests/unit/string_substr.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <vector>
55

66
struct Text {
7-
std::string value;
7+
std::string &value;
88
};
99

1010
std::string &receiver(std::string &s, int &calls) {
@@ -44,7 +44,8 @@ int main() {
4444
out = s.substr(s.size(), 0);
4545
printf("empty: %zu\n", out.size());
4646

47-
Text text{"hello"};
47+
std::string field = "hello";
48+
Text text{field};
4849
out = text.value.substr(1, 2);
4950
printf("field: %s %s\n", out.c_str(), text.value.c_str());
5051
std::vector<std::string> strings = {"hello", "world"};

0 commit comments

Comments
 (0)