Skip to content

Commit 3359fdc

Browse files
committed
tests: Add codegen test for clone
1 parent dd57f20 commit 3359fdc

2 files changed

Lines changed: 58 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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
# Preset check
24+
echo "$asm" | grep -q "$FUNC.*:" || { echo "FAIL: $FUNC label missing"; fail=1; }
25+
26+
# ---- negative checks ----
27+
echo "$asm" | grep -q -E 'memcpy|memmove' && { echo "FAIL: references memcpy"; fail=1; }
28+
echo "$asm" | grep -q -E 'panic' && { echo "FAIL: references panic"; fail=1; }
29+
30+
# Extract the function body
31+
body=$(echo "$asm" | sed -n "/$FUNC.*:$/,/\.cfi_endproc/p")
32+
nlines=$(echo "$body" | wc -l | tr -d ' ')
33+
if [ "$nlines" -gt "$MAXBODY" ]; then
34+
echo "FAIL: body too long"
35+
fail=1
36+
fi
37+
38+
39+
if [ "${fail:-0}" = 1 ]; then
40+
echo "--- output (full) ---"
41+
echo "$asm"
42+
echo "--- end ---"
43+
exit 1
44+
else
45+
echo "--- output (body) ---"
46+
echo "$body"
47+
echo "--- end ---"
48+
fi
49+
50+
echo "PASS: function $FUNC"

0 commit comments

Comments
 (0)