Repository: rust-lang/rust
File: compiler/rustc_target/src/callconv/mod.rs (Rust)

Code before:
        // `cast_to()` creates a `CastTarget` with empty `attrs`, losing type validity
        // information. Restore `noundef` from the original layout where provable.
        for arg in self.args.iter_mut().chain(iter::once(&mut self.ret)) {
            if let PassMode::Cast { ref mut cast, .. } = arg.mode {
                if layout_is_noundef(arg.layout, cx) {
                    cast.attrs.set(ArgAttribute::NoUndef);
                }
            }
        }
    }
}

/// Determines whether `layout` contains no uninit bytes (no padding, no unions),
/// using only the computed layout.
///
/// Conservative: returns `false` for anything it cannot prove fully initialized,
/// including multi-variant enums and SIMD vectors.
// FIXME: extend to multi-variant enums (per-variant padding analysis needed).
fn layout_is_noundef<'a, Ty, C>(layout: TyAndLayout<'a, Ty>, cx: &C) -> bool
where
    Ty: TyAbiInterface<'a, C> + Copy,
    C: HasDataLayout,
{
    match layout.backend_repr {
        BackendRepr::Scalar(scalar) => !scalar.is_uninit_valid(),
        BackendRepr::ScalarPair(..) => fields_cover_layout(layout, cx),
        BackendRepr::Memory { .. } => match layout.fields {
            FieldsShape::Primitive | FieldsShape::Union(_) => false,
            // Array elements are at stride offsets with no inter-element gaps.
            FieldsShape::Array { stride: _, count } => {
                count == 0 || layout_is_noundef(layout.field(cx, 0), cx)
            }
            FieldsShape::Arbitrary { .. } => {
                // With `Variants::Multiple`, `layout.fields` only covers shared
                // bytes (niche/discriminant); per-variant data is absent, so
                // full coverage cannot be proven.
                matches!(layout.variants, Variants::Single { .. })
                    && fields_cover_layout(layout, cx)
            }
        },
        BackendRepr::SimdVector { .. } | BackendRepr::ScalableVector { .. } => false,
    }
}

/// Checks that the fields of `layout` contiguously cover bytes `0..layout.size`
/// with no padding gaps, and each field is recursively `layout_is_noundef`.
fn fields_cover_layout<'a, Ty, C>(layout: TyAndLayout<'a, Ty>, cx: &C) -> bool
where
    Ty: TyAbiInterface<'a, C> + Copy,
    C: HasDataLayout,
{

Review comment (performance):
This seems unnecessarly inefficient. It could check the two scalar fields directly rather than recursing the type structure. See [here](https://github.com/rust-lang/rust/blob/5a76a60d016d7ed5cd5fcecc131010dbce8148e0/compiler/rustc_const_eval/src/interpret/place.rs#L898-L903) for a similar check.

Code after:
                    // code, so we pass things indirectly even there. See #139029 for more on that.
                    if spec.simd_types_indirect {
                        arg.make_indirect();
                    }
                }

                _ => {}
            }
        }
    }
}

/// Determines whether `layout` contains no uninit bytes (no padding, no unions),
/// using only the computed layout.
///
/// Conservative: returns `false` for anything it cannot prove fully initialized,
/// including multi-variant enums and SIMD vectors.
// FIXME: extend to multi-variant enums (per-variant padding analysis needed).
fn layout_is_noundef<'a, Ty, C>(layout: TyAndLayout<'a, Ty>, cx: &C) -> bool
where
    Ty: TyAbiInterface<'a, C> + Copy,
    C: HasDataLayout,
{
    match layout.backend_repr {
        BackendRepr::Scalar(scalar) => !scalar.is_uninit_valid(),
        BackendRepr::ScalarPair(s1, s2) => {
            !s1.is_uninit_valid()
                && !s2.is_uninit_valid()
                // Ensure there is no padding.
                && s1.size(cx) + s2.size(cx) == layout.size
        }
        BackendRepr::Memory { .. } => match layout.fields {
            FieldsShape::Primitive | FieldsShape::Union(_) => false,
            // Array elements are at stride offsets with no inter-element gaps.
            FieldsShape::Array { stride: _, count } => {
                count == 0 || layout_is_noundef(layout.field(cx, 0), cx)
            }
            FieldsShape::Arbitrary { .. } => {
                // With `Variants::Multiple`, `layout.fields` only covers shared
                // bytes (niche/discriminant); per-variant data is absent, so
                // full coverage cannot be proven.
                matches!(layout.variants, Variants::Single { .. }) && fields_are_noundef(layout, cx)
            }
        },
        BackendRepr::SimdVector { .. } | BackendRepr::ScalableVector { .. } => false,
    }
}

/// Returns `true` if the fields of `layout` contiguously cover bytes `0..layout.size`
/// with no padding gaps and each field is recursively `layout_is_noundef`.
fn fields_are_noundef<'a, Ty, C>(layout: TyAndLayout<'a, Ty>, cx: &C) -> bool