Skip to content

ptr::read unable to assume slice is always not null #106369

Closed
@AnsonYeung

Description

@AnsonYeung

Expects the following function to always return true since it's undefined behaviour otherwise.

use std::ptr;

pub unsafe fn test(s: &Vec<&[i32]>, idx: usize) -> bool {
    unsafe { Some(ptr::read(s.as_ptr().add(idx))).is_some() }
}

Actual result:

example::test:
        mov     rax, qword ptr [rdi + 8]
        shl     rsi, 4
        cmp     qword ptr [rax + rsi], 0
        setne   al
        ret

https://rust.godbolt.org/z/xzcTr15Yj
Causes #106368

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationI-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