Skip to content

Document whether dereferencing a raw pointer requires alignment #62416

Closed
@ExpHP

Description

@ExpHP

I am referring to the following operation, built into the language:

let x: *const u32 = unimplemented!();

unsafe { *x }

I checked all over:

The pointer primitive docs don't mention alignment.

The std::ptr module states:

Valid raw pointers as defined above are not necessarily properly aligned (where "proper" alignment is defined by the pointee type, i.e., *const T must be aligned to mem::align_of::<T>()). However, most functions require their arguments to be properly aligned, and will explicitly state this requirement in their documentation. Notable exceptions to this are read_unaligned and write_unaligned.

This is clearly banking on the fact that every function is documented, but raw pointer dereference is a built-in operation and therefore has no documentation aside from the (previously mentioned) pointer primitive page.

I checked The Rust Programming Language, Second Edition, one of the top google results (note: the google result is actually for 1.30.0). The string "align" does not appear.

I checked The Rust Reference. Nothing.


With no mention of alignment literally anywhere, you would think: Surely it must not be required, then, right?

Well, I checked the issue tracker, and according to this issue it sounds like alignment is required.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions