Skip to content

Conversation

@traviscross
Copy link
Contributor

@traviscross traviscross commented Nov 16, 2025

We had said that the dereference operator could not be used with raw pointers in a constant expression. However, that restriction has been lifted. First, in Rust 1.58, we stabilized
const_raw_ptr_deref.

rust-lang/rust#89551

This allowed for dereferencing immutable raw pointers in a constant expression. Then, in Rust 1.83, we stabilized const_mut_refs and const_refs_to_cell.

rust-lang/rust#129195

That allowed for:

  • Mentioning &mut types.
  • Creating &mut and *mut values.
  • Creating &T and *const T values where T contains interior mutability.
  • Dereferencing &mut and *mut values (both for reads and writes).

Let's remove the stated restriction on dereferencing raw pointers in a constant expression and add examples.

cc @ehuss @RalfJung


I noticed this when double-checking that we didn't need to do anything for:

cc @rust-lang/fls

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Nov 16, 2025
@traviscross traviscross force-pushed the TC/remove-restriction-on-dereferencing-pointers-in-const branch from 8c89538 to 3dab34b Compare November 16, 2025 08:27
We had said that the dereference operator could not be used with raw
pointers in a constant expression.  However, that restriction has been
lifted.  First, in Rust 1.58, we stabilized `const_raw_ptr_deref`.

rust-lang/rust#89551

This allowed for dereferencing immutable raw pointers in a constant
expression.  Then, in Rust 1.83, we stabilized `const_mut_refs` and
`const_refs_to_cell`.

rust-lang/rust#129195

That allowed for:

- Mentioning `&mut` types.
- Creating `&mut` and `*mut` values.
- Creating `&T` and `*const T` values where `T` contains interior
  mutability.
- Dereferencing `&mut` and `*mut` values (both for reads and writes).

Let's remove the stated restriction on dereferencing raw pointers in a
constant expression and add examples.
To define what's allowed in a constant expression, we have a list that
starts with the text:

> The following expressions are constant expressions...

Correspondingly, most of the items in this list are stated in terms of
being a "this expression" or a "that expression".  However, for deref,
we had instead said "the dereference operator".  Even though the
`expr.deref` section is currently titled "the dereference operator",
it seems more clear and consistent in the context of this list to talk
about dereference expressions, so let's do that.  These are
expressions, after all, The grammar production is named
`DereferenceExpression`.

(There is one other place in the list that we talk about operators:
"built-in negation, arithmetic, logical, comparison, or lazy boolean
operators used on integer and floating point types, `bool`, and
`char`."  Perhaps we'll reword that one later.  It stands out a bit
less than this one did.)
@traviscross traviscross force-pushed the TC/remove-restriction-on-dereferencing-pointers-in-const branch from 3dab34b to 71865f1 Compare November 16, 2025 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: The marked PR is awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants