Skip to content

make more slice mutable ref getters rustc_no_writable#157816

Open
RalfJung wants to merge 3 commits into
rust-lang:mainfrom
RalfJung:rustc_no_writable
Open

make more slice mutable ref getters rustc_no_writable#157816
RalfJung wants to merge 3 commits into
rust-lang:mainfrom
RalfJung:rustc_no_writable

Conversation

@RalfJung

Copy link
Copy Markdown
Member

This makes https://rust.godbolt.org/z/TreEYqfW8 work.

Based on and blocked on #157202
Cc @quiode @JoJoDeveloping

quiode and others added 3 commits June 12, 2026 08:55
* add #[rustc_no_writable] to slice::get_unchecked_mut
* add #[rustc_no_writable] to slice::get_mut
* add unchecked_mut miri test
@rustbot

rustbot commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

miri is developed in its own repository. If possible, consider making this change to rust-lang/miri instead.

cc @rust-lang/miri

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 12, 2026
@rustbot

rustbot commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

r? @clarfonthey

rustbot has assigned @clarfonthey.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 10 candidates
  • Random selection from Mark-Simulacrum, clarfonthey, jhpratt, joboet

@@ -185,6 +226,7 @@ const unsafe impl<T> SliceIndex<[T]> for usize {
}

#[inline]
#[rustc_no_writable]

@RalfJung RalfJung Jun 12, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It'd be nice if we could just add the attribute to the trait rather than each impl... but I am not sure how to implement that and it is not the usual behavior for such attributes.

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How exactly would that work? Just apply to the abstract method and then assume each implementation should have it applied?

That feels kind of unprecedented, since even attributes like #[inline] don't support that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it makes sense because of the conceptual alignment between what SliceIndex is for and what the attribute is trying to express.

I think #[track_caller] can work like this, based on what I see in should_inherit_track_caller in the compiler.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Huh, I had no idea you were able to use #[track_caller] in that way, and it turns out you're right: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=c726a3e5c00af55c75daaa67bf6533fc

Semantically, it makes sense, just, I had no idea that we had precedent for this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

After catching up on the semantics of this attribute, at least from my understanding, the presence or absence of this attribute is always safe?

We don't really distinguish unsafe attributes (to my knowledge) for internal attributes, at least at the moment, but I guess the main issue with such a thing is if it would have noticeable effects that the method implementations would have to account for, e.g., such that those implementations would be best marked as unsafe or similar.

I guess this technically doesn't matter in this particular review, just trying to grasp a bit better how this would affect methods like this. I'm not sure if this necessarily changes any compiler semantics besides emitting an LLVM flag, e.g., if a future borrow checker would take this attribute into account.

@RalfJung RalfJung Jun 13, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The attribute affects whether code has UB, but on its own (i.e. in otherwise safe code) the presence or absence of the attribute can never cause UB. Furthermore, adding the attribute can never introduce UB to a program, but removing the attribute can introduce UB if surrounding unsafe code relied on the presence of the attribute. By default, our codegen backend entirely ignores the attribute; it takes a -Z flag (or Miri) for the attribute to do anything.

This is not meant to be taken into account by a borrow checker, just by LLVM and Miri. For now, it is just an experiment. If the experiment goes well, we could consider a more proper (e.g. type-based) solution.

@clarfonthey

Copy link
Copy Markdown
Contributor

@rustbot blocked

(since this is explicitly blocked)

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-blocked Status: Blocked on something else such as an RFC or other implementation work. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants