makepad/libs/relax/compiler/tests/sroa_enums.rs
Admin b22a714840 makepad run: the Rust JIT (libs/relax) and makepad-remote
The JIT that runs makepad apps from source: lexer, front end, type checker,
lazy borrow checker (Gandalf; `--check-first` opts in), codegen, session and
warm/cold caches keyed by compiler stamp, provided crates bound late,
startup images, live patching. User-facing name is `makepad run` (binary
`makepad`, MAKEPAD_RUN_* env, ~/.makepad-run); libs/relax stays the internal
library name. `makepad check` reports rustc-style diagnostics with rustc
error codes and checks a whole workspace (`--workspace`). Runs on macOS,
Linux, Windows and Android. makepad-remote: one tool schema for apps and the
WM (app/1): grabs, captures, traces, frame profiles and tweaks.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-13 12:26:21 +02:00

60 lines
1.7 KiB
Rust

// Enum payloads with overlapping field kinds and returned tuples copied through a local.
// Run with `makepad test`; this also runs as an ordinary rustc test.
#[inline(always)]
fn option_payload(i: u32) -> Option<f64> {
if i % 3 == 0 { None } else { Some(i as f64 + 0.5) }
}
#[inline(always)]
fn result_payload(i: u32) -> Result<u32, (u8, u8)> {
if i % 2 == 0 { Ok(3 * i + 1) } else { Err((i as u8, (31 - i) as u8)) }
}
#[inline(never)]
fn returned_pair(i: u32) -> (u32, Option<char>) {
let ch = match i % 4 {
0 => Some('A'),
1 => Some('\u{03bb}'),
2 => Some('\u{1f642}'),
_ => None,
};
(5 * i + 7, ch)
}
#[inline(always)]
fn pair_through_slot(i: u32) -> (u32, Option<char>) {
let mut slot = returned_pair(i);
slot.0 += 1;
slot
}
#[inline(never)]
fn enum_checksum(n: u32) -> u64 {
let mut checksum = 0;
for i in 0..n {
checksum += match option_payload(i) {
Some(x) => (2.0 * x) as u64,
None => 11,
};
checksum += match result_payload(i) {
Ok(x) => x as u64,
Err((a, b)) => ((a as u64) << 8) | b as u64,
};
let pair = pair_through_slot(i);
checksum += pair.0 as u64;
checksum += match pair.1 {
Some(ch) => ch as u64,
None => 13,
};
}
checksum
}
#[test]
fn sroa_enum_and_sret_checksum() {
// i = 0..31: Option contributes 1024 - 341 + 11*11 = 804.
// Result contributes 736 + 256*256 + 240 = 66512.
// Tuple integers contribute 5*496 + 8*32 = 2736; characters contribute
// 8*(65 + 955 + 128578 + 13) = 1036888. Total: 1106940.
assert_eq!(enum_checksum(std::hint::black_box(32)), 1_106_940);
}