Skip to content

NonNull's as_ref / as_mut don't have arbitrary lifetimes as stated #80183

Closed
@Zenithsiz

Description

@Zenithsiz

The docs for NonNull::as_ref mention:

You must enforce Rust's aliasing rules, since the returned lifetime 'a is arbitrarily chosen and does not necessarily reflect the actual lifetime of the data. [...]

Which makes sense, the pointer doesn't carry a lifetime with it, so we need to decide on it. However, the implementation actually looks like this (with implicit lifetimes shown for clarity):

pub unsafe fn as_ref<'a>(&'a self) -> &'a T {
    // ...
}

This makes it so the reference constructed has the same lifetime as the pointer variable, which is likely not what is intended.

The same occurs for all of these methods currently:

Additionally, these unstable methods with #![feature(ptr_as_uninit)] (#75402) also have the same problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-raw-pointersArea: raw pointers, MaybeUninit, NonNullT-langRelevant to the language team, which will review and decide on the PR/issue.T-libs-apiRelevant to the library API 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