Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

gccrs#4697: Add assert_zero_valid

Link: Rust-GCC/gccrs#4697

Objective

This PR introduces the assert_zero_valid intrinsic to the compiler.

Changes Made

Properly implementing this intrinsic requires deep layout engine support to detect types that cannot safely be zero-initialized (e.g., references like &T or NonNull pointers). Since gccrs currently lacks this layout capability, it is implemented as a stub that always returns unit ().

Note: In the future, this intrinsic should inject a runtime panic during codegen if the type does not permit zero-initialization.

Why is this needed for alloc?

Internal structures within alloc (such as Box and Vec) use this intrinsic to ensure memory safety when creating zero-initialized data buffers.

Test Case

fn main() {
    unsafe {
        let _valid: i32 = std::mem::zeroed();
        println!("valid for i32");

        // this will abort because std::mem::zeroed uses assert_zero_valid
        let _invalid: &i32 = std::mem::zeroed();
    }
}