Skip to content

Clarify &UnsafeCell<T>&T access #157544

@kpreid

Description

@kpreid

Location (URL)

https://doc.rust-lang.org/stable/std/cell/struct.UnsafeCell.html

Summary

The documentation has extensive explanation of how to obtain and use an *mut T or &mut T from UnsafeCell<T> and use that pointer to mutate the cell. However, it says nothing explicitly about the means to, and the safety considerations of, obtaining &Ts which derive from and overlap with an &UnsafeCell<T>, or any other situations involving immutable references. For example, if one is working with bulk data, one might have these questions:

  • Is it sound to cast &UnsafeCell<[T]> to &[UnsafeCell<T>]?

    The implementation of Cell::as_slice_of_cells() implies that it is, so it would be desirable to have the more general property documented.

  • Is it sound to cast &[UnsafeCell<T>] to &UnsafeCell<[T]>?

  • Is it sound to cast &[UnsafeCell<T>] to &[T], provided no mutation occurs while the reference is in use?

    UCG says this is not yet definitively OK because SB rejects it but the library documentation should at least explicitly caveat this.

@rustbot label +T-libs +T-opsem

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.T-opsemRelevant to the opsem team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions