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#4582: Add min_align_of

Link: Rust-GCC/gccrs#4582

Definition:

#![allow(unused)]
fn main() {
#[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")]
pub fn min_align_of<T>() -> usize;
}

Objective

This PR implements the min_align_of compiler intrinsic to resolve the minimum alignment of a compile-time sized type.

Changes Made

Added a handler function that leverages the GCC type alignment infrastructure. It calculates the alignment requirement of a given type and returns it as a constant integer expression (size_type_node).

Why is this needed for alloc?

The alloc crate relies heavily on this intrinsic to correctly calculate memory alignment constraints when allocating memory blocks.

Test Case

use std::mem::align_of as min_align_of; // new API is align_of

fn main() {
    let align_u16 = min_align_of::<u16>(); // returns 2
    let align_u32 = min_align_of::<i32>(); // returns 4

    println!("align of u16: {align_u16}");
    println!("align of u32: {align_u32}");
}

The intrinsic successfully evaluates the alignments of u16 and i32 at compile-time and returns 2 and 4 respectively.