const-eval: allow constants to refer to mutable/external memory, but reject such constants as patterns by RalfJung · Pull Request #140942 · rust-lang/rust (original) (raw)

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

@RalfJung

@rustbot rustbot added S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

T-compiler

Relevant to the compiler team, which will review and decide on the PR/issue.

labels

May 12, 2025

@RalfJung RalfJung added T-lang

Relevant to the language team

S-waiting-on-team

DEPRECATED: Use the team-based variants `S-waiting-on-t-lang`, `S-waiting-on-t-compiler`, ...

I-lang-nominated

Nominated for discussion during a lang team meeting.

labels

May 12, 2025

RalfJung

RalfJung

@traviscross traviscross removed the T-compiler

Relevant to the compiler team, which will review and decide on the PR/issue.

label

May 13, 2025

@bors bors added the S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

label

Jun 26, 2025

@RalfJung

…reject such constants as patterns

@RalfJung

@RalfJung

@bors bors added S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

and removed S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

labels

Jun 26, 2025

bors added a commit that referenced this pull request

Jun 27, 2025

@bors

…rors

Rollup of 18 pull requests

Successful merges:

Failed merges:

r? @ghost @rustbot modify labels: rollup

rust-timer added a commit that referenced this pull request

Jun 27, 2025

@rust-timer

Rollup merge of #140942 - RalfJung:const-ref-to-mut, r=oli-obk

const-eval: allow constants to refer to mutable/external memory, but reject such constants as patterns

This fixes #140653 by accepting code such as this:

static FOO: AtomicU32 = AtomicU32::new(0);
const C: &'static AtomicU32 = &FOO;

This can be written entirely in safe code, so there can't really be anything wrong with it.

We also accept the much more questionable following code, since it looks very similar to the interpreter:

static mut FOO2: u32 = 0;
const C2: &'static u32 = unsafe { &mut FOO2 };

Using this without causing UB is at least very hard (the details are unclear since it is related to how the aliasing model deals with the staging of const-eval vs runtime code).

If a constant like C2 is used as a pattern, we emit an error:

error: constant BAD_PATTERN cannot be used as pattern
  --> $DIR/const_refs_to_static_fail.rs:30:9
   |
LL |         BAD_PATTERN => {},
   |         ^^^^^^^^^^^
   |
   = note: constants that reference mutable or external memory cannot be used as pattern

(If you somehow manage to build a pattern with constant C, you'd get the same error, but that should be impossible: we don't have a type that can be used in patterns and that has interior mutability.)

The same treatment is afforded for shared references to extern static, for the same reason: the const evaluation is entirely fine with it, we just can't build a pattern for it -- and when using interior mutability, this can be totally sound.

We do still not accept anything where there is an &mut in the final value of the const, as that should always require unsafe code and it's hard to imagine a sound use-case that would require this.

compiler-errors added a commit to compiler-errors/rust that referenced this pull request

Jun 27, 2025

@compiler-errors

… r=oli-obk

const checks for lifetime-extended temporaries: avoid 'top-level scope' terminology

This error recently got changed in rust-lang#140942 to use the terminology of "top-level scope", but after further discussion in rust-lang/reference#1865 it seems the reference will not be using that terminology after all. So let's also remove it from the compiler again, and let's focus on what actually happens with these temporaries: their lifetime is extended until the end of the program.

r? @oli-obk @traviscross

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request

Jun 27, 2025

@matthiaskrgr

… r=oli-obk

const checks for lifetime-extended temporaries: avoid 'top-level scope' terminology

This error recently got changed in rust-lang#140942 to use the terminology of "top-level scope", but after further discussion in rust-lang/reference#1865 it seems the reference will not be using that terminology after all. So let's also remove it from the compiler again, and let's focus on what actually happens with these temporaries: their lifetime is extended until the end of the program.

r? @oli-obk @traviscross

rust-timer added a commit that referenced this pull request

Jun 28, 2025

@rust-timer

Rollup merge of #143092 - RalfJung:const-check-lifetime-ext, r=oli-obk

const checks for lifetime-extended temporaries: avoid 'top-level scope' terminology

This error recently got changed in #140942 to use the terminology of "top-level scope", but after further discussion in rust-lang/reference#1865 it seems the reference will not be using that terminology after all. So let's also remove it from the compiler again, and let's focus on what actually happens with these temporaries: their lifetime is extended until the end of the program.

r? @oli-obk @traviscross

github-actions bot pushed a commit to rust-lang/miri that referenced this pull request

Jun 28, 2025

@matthiaskrgr

const checks for lifetime-extended temporaries: avoid 'top-level scope' terminology

This error recently got changed in rust-lang/rust#140942 to use the terminology of "top-level scope", but after further discussion in rust-lang/reference#1865 it seems the reference will not be using that terminology after all. So let's also remove it from the compiler again, and let's focus on what actually happens with these temporaries: their lifetime is extended until the end of the program.

r? @oli-obk @traviscross

github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request

Jul 4, 2025

@bors

@traviscross traviscross added the relnotes

Marks issues that should be documented in the release notes of the next release.

label

Jul 18, 2025

wip-sync pushed a commit to NetBSD/pkgsrc-wip that referenced this pull request

Sep 20, 2025

@he32

Pkgsrc changes:

Upstream changes relative to 1.89.0:

Version 1.90 (2025-09-18)

Language

Compiler

Platform Support

Refer to Rust's platform support page for more information on Rust's tiered platform support.

Libraries

Stabilized APIs

These previously stable APIs are now stable in const contexts:

Cargo

Rustdoc

Compatibility Notes

tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request

Sep 24, 2025

@tmeijn

This MR contains the following updates:

Package Update Change
rust minor 1.89.0 -> 1.90.0

MR created with the help of el-capitano/tools/renovate-bot.

Proposed changes to behavior should be submitted there as MRs.


Release Notes

rust-lang/rust (rust)

v1.90.0

Compare Source

===========================

Language

Compiler

Platform Support

Refer to Rust's platform support page for more information on Rust's tiered platform support.

Libraries

Stabilized APIs

These previously stable APIs are now stable in const contexts:

Cargo

Rustdoc

Compatibility Notes


Configuration

📅 Schedule: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.

Rebasing: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 Ignore: Close this MR and you won't be reminded about this update again.



This MR has been generated by Renovate Bot.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request

Oct 18, 2025

@he32

Pkgsrc changes:

Upstream changes:

Version 1.90 (2025-09-18)

Language

Compiler

Platform Support

Refer to Rust's platform support page for more information on Rust's tiered platform support.

Libraries

Stabilized APIs

These previously stable APIs are now stable in const contexts:

Cargo

Rustdoc

Compatibility Notes

Version 1.89.0 (2025-08-07)

Language

Compiler

Platform Support

Refer to Rust's platform support page for more information on Rust's tiered platform support.

Libraries

Stabilized APIs

These previously stable APIs are now stable in const contexts:

Cargo

Rustdoc

Compatibility Notes

Internal Changes

These changes do not affect any public interfaces of Rust, but they represent significant improvements to the performance or internals of rustc and related tools.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request

Oct 23, 2025

@he32

Pkgsrc changes:

Upstream changes:

Version 1.90 (2025-09-18)

Language

Compiler

Platform Support

Refer to Rust's platform support page for more information on Rust's tiered platform support.

Libraries

Stabilized APIs

These previously stable APIs are now stable in const contexts:

Cargo

Rustdoc

Compatibility Notes

Version 1.89.0 (2025-08-07)

Language

Compiler

Platform Support

Refer to Rust's platform support page for more information on Rust's tiered platform support.

Libraries

Stabilized APIs

These previously stable APIs are now stable in const contexts:

Cargo

Rustdoc

Compatibility Notes

Internal Changes

These changes do not affect any public interfaces of Rust, but they represent significant improvements to the performance or internals of rustc and related tools.