From cd0b2559d17fd5b7c0fbc8d01275bb3fd6b99f55 Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Sun, 16 Nov 2025 07:51:49 +0000 Subject: [PATCH 1/2] Remove restriction on dereferencing pointers in const 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`. https://github.com/rust-lang/rust/pull/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`. https://github.com/rust-lang/rust/pull/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. --- src/const_eval.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/const_eval.md b/src/const_eval.md index 04146adf6..35b148533 100644 --- a/src/const_eval.md +++ b/src/const_eval.md @@ -203,7 +203,23 @@ r[const-eval.const-expr.borrows] > See [issue #143129](https://github.com/rust-lang/rust/issues/143129) for more details. r[const-eval.const-expr.deref] -* The [dereference operator] except for raw pointers. +* The [dereference operator]. + + ```rust,no_run + # use core::cell::UnsafeCell; + const _: u8 = unsafe { + let x: *mut u8 = &raw mut *&mut 0; + // ^^^^^^^ + // Dereference of mutable reference. + *x = 1; // Dereference of mutable pointer. + *(x as *const u8) // Dereference of constant pointer. + }; + const _: u8 = unsafe { + let x = &UnsafeCell::new(0); + *x.get() = 1; // Mutation of interior mutable value. + *x.get() + }; + ``` r[const-eval.const-expr.group] From 0659e98417197390ee16d304c51a27773b62c3af Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Sun, 16 Nov 2025 08:08:10 +0000 Subject: [PATCH 2/2] Say "dereference expressions" in constant exprs list 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.) --- src/const_eval.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/const_eval.md b/src/const_eval.md index 35b148533..71f392dc3 100644 --- a/src/const_eval.md +++ b/src/const_eval.md @@ -203,7 +203,7 @@ r[const-eval.const-expr.borrows] > See [issue #143129](https://github.com/rust-lang/rust/issues/143129) for more details. r[const-eval.const-expr.deref] -* The [dereference operator]. +* [Dereference expressions]. ```rust,no_run # use core::cell::UnsafeCell; @@ -320,8 +320,8 @@ The types of a const function's parameters and return type are restricted to tho [constant expressions]: #constant-expressions [constants]: items/constant-items.md [Const parameters]: items/generics.md -[dereference expression]: expressions/operator-expr.md#the-dereference-operator -[dereference operator]: expressions/operator-expr.md#the-dereference-operator +[dereference expression]: expr.deref +[dereference expressions]: expr.deref [destructors]: destructors.md [enum discriminants]: items/enumerations.md#discriminants [expression statements]: statements.md#expression-statements