Closed as duplicate
Description
Please forgive if redundant - I did a search but could not find relevant discussion.
I work on embedded Rust and we use ZSTs and traits for hardware abstraction layers (HAL).
If we change call sites to accept HAL objects by reference (to support unit tests with stateful HALs), I noticed Rust will occupy an argument passing slot even though &ZST
could also be zero-sized.
Notice the differences in https://gcc.godbolt.org/z/s6ocP1bds
Reproducing example here:
#![no_std]
struct MyDevice;
#[unsafe(no_mangle)]
pub fn zst_by_copy(z: MyDevice, a: usize, b: usize, c: usize) -> usize {
a + b + c
}
#[unsafe(no_mangle)]
pub fn zst_by_reference(z: &MyDevice, a: usize, b: usize, c: usize) -> usize {
a + b + c
}
zst_by_copy:
add a0, a0, a1
add a0, a0, a2
ret
zst_by_reference:
add a0, a2, a1
add a0, a0, a3
ret
Is there a fundamental reason or is this simply a missed optimization opportunity?