Skip to content

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

Closed

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.

Activity

  1. added
    A-lifetimesArea: Lifetimes / regions
    A-raw-pointersArea: raw pointers, MaybeUninit, NonNull
    T-langRelevant to the language team
    T-libs-api[DEPRECATED; DO NOT USE]
    on Dec 20, 2020
  2. added 2 commits that reference this issue on Mar 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-raw-pointersArea: raw pointers, MaybeUninit, NonNullT-langRelevant to the language teamT-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions