rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
99.03k stars 12.79k forks source link

Tracking issue for release notes of #129195: Stabilize `&mut` (and `*mut`) as well as `&Cell` (and `*const Cell`) in const #130362

Open rustbot opened 2 months ago

rustbot commented 2 months ago

This issue tracks the release notes text for #129195.

Steps

Release notes text

The responsible team for the underlying change should edit this section to replace the automatically generated link with a succinct description of what changed, drawing upon text proposed by the author (either in discussion or through direct editing).

# Language
- [Stabilize `&mut`, `*mut`, `&Cell`, and `*const Cell` in const.](https://github.com/rust-lang/rust/pull/129195)

[!TIP] Use the previous releases categories to help choose which one(s) to use. The category will be de-duplicated with all the other ones by the release team.

More than one section can be included if needed.

Release blog section

If the change is notable enough for inclusion in the blog post, the responsible team should add content to this section. Otherwise leave it empty.

cc @RalfJung, @fee1-dead -- origin issue/PR authors and assignees for starting to draft text

RalfJung commented 2 months ago

I am hoping there'll be a bunch of const fn API stabilizations as well to go with this (t-libs-api FCP has been proposed in a bunch of tracking issues). Then we can have one section in the release notes about creating mutable references / raw pointers and doing stuff with them.

RalfJung commented 2 months ago

So here's a draft for the release notes section for this. Cc @rust-lang/wg-const-eval

New const capabilities

This release includes several large extensions to what code running in const contexts can do. This refers to all code that the compiler has to evaluate at compile-time: the initial value of const and static items, array lengths, enum discriminant values, const generic arguments, and functions callable from such contexts (const fn).

Mutable references and pointers. It is now possible to use mutable references in const contexts:

const fn inc(x: &mut i32) {
    *x += 1;
}

const C: i32 = {
    let mut c = 41;
    inc(&mut c);
    c
};

Mutable raw pointers and interior mutability are also supported:

use std::cell::UnsafeCell;

const C: i32 = {
    let c = UnsafeCell::new(41);
    unsafe { *c.get() += 1 };
    c.into_inner()
};

However, mutable references and pointers can only be used inside the computation of a constant, they cannot become a part of the final value of the constant:

const C: &mut i32 = &mut 4;
// error[E0764]: mutable references are not allowed in the final value of constants

This release also ships with a whole bag of new functions that are now stable in const contexts: TODO put a list here, or just reference the list at the end of the release notes?

This unblocks an entire new category of code to be executed inside const contexts, and we are excited to see how the Rust ecosystem will make use of this!