Skip to content

Dereferencing raw pointer to invalid data? #216

Closed
@RalfJung

Description

@RalfJung

When I dereference a raw pointer but do not actually load any data, such as in &raw const *x, is there any validity requirement on the data it points to?

According to the reference, the answer is "no". And indeed if the answer is "yes", it becomes impossible I think to do field-by-field initialization of a struct even with raw pointers. So "no" likely is the only possible answer.

This would mean that given a well-aligned, non-dangling x: *const !, doing &raw const *x is okay. Also, given a well-aligned, non-dangling x: *const (i32, !), doing &raw const (*x).0 is okay. In the second case, whether the resulting ptr is actually usable relates to the layout of structs with uninhabited fields -- I recall a discussion with (I think) @rkruppe about this? There were some proposals on the table to make such structs zero-sized, but they caused problems and IIRC the conclusion was that that's probably not possible.

To be sure about all of this, we should find a way to get that officially blessed by the lang team. Is there some reasonable place in the rustc repo or so where we could document this, and then FCP? Or do we need an RFC?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-validityTopic: Related to validity invariants

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions