Skip to content

Variables with rustc_layout_scalar_valid_range are not assumed to be in their range #82224

Closed
@Soveu

Description

@Soveu

I have noticed that for example NonZero integers are not assumed to be non-zero.

pub fn leading_zeros(x: NonZeroU32) -> u32 {
    //unsafe { std::intrinsics::assume(x.get() != 0) };
    x.get().leading_zeros()
}

pub fn is_zero(x: NonZeroU32) -> bool {
    //unsafe { std::intrinsics::assume(x.get() != 0) };
    x.get() == 0
}

example::leading_zeros:
        test    edi, edi     ; check if it is zero
        je      .LBB1_2
        bsr     eax, edi
        xor     eax, 31
        ret
.LBB1_2:
        mov     eax, 32
        ret

example::is_zero:
        test    edi, edi     ; check if it is zero
        sete    al
        ret

(it also affects division)

Manually adding intrinsics::assume fixes that issue, but this cannot be used in const functions.
Related to #79114 and #79134

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-layoutArea: Memory layout of typesA-mir-optArea: MIR optimizationsC-enhancementCategory: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions