Skip to content

Commit b0ff9cd

Browse files
committed
tests: Add codegen test for clone
1 parent 31b9199 commit b0ff9cd

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

tests/codegen/clone_copy.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
extern crate arrayvec;
2+
use arrayvec::ArrayVec;
3+
4+
#[unsafe(no_mangle)]
5+
#[inline(never)]
6+
pub fn test_subject(array: &ArrayVec<u8, 8>) -> ArrayVec<u8, 8> {
7+
array.clone()
8+
}

tests/codegen/justfile

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
root := "../.."
2+
3+
default: check
4+
5+
build-deps:
6+
cd {{root}} && cargo build --release
7+
8+
# Compile and inspect the asm for .clone() testcase
9+
# Require that it's very short (should be like a short struct copy)
10+
check: build-deps
11+
#!/usr/bin/env bash
12+
set -euo pipefail
13+
14+
ROOT={{root}}
15+
asm=$(rustc --edition 2024 -C opt-level=3 --crate-type lib \
16+
clone_copy.rs -L "$ROOT/target/release/deps" \
17+
--emit=asm -o -)
18+
19+
fail=
20+
21+
FUNC=test_subject
22+
MAXBODY=20
23+
24+
# Sanity check
25+
echo "$asm" | grep -q "$FUNC.*:" || { echo "FAIL: $FUNC label missing"; fail=1; }
26+
27+
# Assertions
28+
echo "$asm" | grep -q -E 'memcpy|memmove' && { echo "FAIL: references memcpy"; fail=1; }
29+
echo "$asm" | grep -q -E 'panic' && { echo "FAIL: references panic"; fail=1; }
30+
31+
# Extract the function body
32+
body=$(echo "$asm" | sed -n "/$FUNC.*:$/,/\.cfi_endproc/p")
33+
nlines=$(echo "$body" | wc -l | tr -d ' ')
34+
if [ "$nlines" -gt "$MAXBODY" ]; then
35+
echo "FAIL: body too long"
36+
fail=1
37+
fi
38+
39+
40+
if [ "${fail:-0}" = 1 ]; then
41+
echo "--- output (full) ---"
42+
echo "$asm"
43+
echo "--- end ---"
44+
exit 1
45+
else
46+
echo "--- output (body) ---"
47+
echo "$body"
48+
echo "--- end ---"
49+
fi
50+
51+
echo "PASS: function $FUNC"

0 commit comments

Comments
 (0)