Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow statics pointing to mutable statics #121782

Merged
merged 2 commits into from
Mar 1, 2024

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Feb 29, 2024

Fixes #120450 for good. We can even simplify our checks: no need to specifically go looking for mutable references in const, we can just reject any reference that points to something mutable.

r? @oli-obk

@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 Feb 29, 2024
@rustbot
Copy link
Collaborator

rustbot commented Feb 29, 2024

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

{
throw_validation_failure!(self.path, ConstRefToMutable);
// In a const, everything must be completely immutable.
if matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { .. })) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we don't have any tests testing the promoted code path? Or is that rejected elsewhere even in unleash mode?

Copy link
Member Author

@RalfJung RalfJung Feb 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With promoteds we have to accept this or else we fail this recently added test.

We know from the internet that a promoted will never add new mutable memory, so all references to mutable memory must be shared refs as otherwise we'd hit the error above about a mutable ref to immutable memory. (But the shared refs may have interior mutability. We do complain if we find an UnsafeCell in an immutable location though.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤣

We know from the internet

(sorry, I know what you meant, but I literally lolled)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂

(For anyone else reading along, I meant "interner".)

@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung force-pushed the mutable-ref-in-static branch from 66268ec to a9596fb Compare February 29, 2024 08:34
@oli-obk
Copy link
Contributor

oli-obk commented Feb 29, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Feb 29, 2024

📌 Commit a9596fb has been approved by oli-obk

It is now in the queue for this repository.

@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-review Status: Awaiting review from the assignee but also interested parties. labels Feb 29, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 29, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#121326 (Detect empty leading where clauses on type aliases)
 - rust-lang#121464 (rustc: Fix wasm64 metadata object files)
 - rust-lang#121681 (Safe Transmute: Revise safety analysis)
 - rust-lang#121753 (Add proper cfg to keep only one AlignmentEnum definition for different target_pointer_widths)
 - rust-lang#121782 (allow statics pointing to mutable statics)
 - rust-lang#121798 (Fix links in rustc doc)
 - rust-lang#121806 (add const test for ptr::metadata)
 - rust-lang#121809 (Remove doc aliases to PATH)

r? `@ghost`
`@rustbot` modify labels: rollup
@workingjubilee
Copy link
Member

I am thinking about this change from a teaching perspective. I have a small question: If I am understanding this correctly, previously we had "const contexts" which use a set of const evaluation rules to produce a value, and this basically cleaves the world somewhat into "const contexts" and "static contexts", where the rules are nominally shared but some final values become permitted that were not sensical previously. Yes?

@bors bors merged commit 255fdcc into rust-lang:master Mar 1, 2024
11 checks passed
@rustbot rustbot added this to the 1.78.0 milestone Mar 1, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 1, 2024
Rollup merge of rust-lang#121782 - RalfJung:mutable-ref-in-static, r=oli-obk

allow statics pointing to mutable statics

Fixes rust-lang#120450 for good. We can even simplify our checks: no need to specifically go looking for mutable references in const, we can just reject any reference that points to something mutable.

r? `@oli-obk`
@RalfJung
Copy link
Member Author

RalfJung commented Mar 1, 2024

const and static always had a bunch of subtle differences, so this distinction is not entirely new. I would still call them all "const contexts" but there are some particularities that only apply to some const contexts.

For example, const/const fn are not allowed to mention statics, but static is allowed to mention other statics (and even itself).

@RalfJung RalfJung deleted the mutable-ref-in-static branch March 1, 2024 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mutable reference protected by a mutex in static context is considered UB
6 participants