Skip to content

Fix static_mut_refs lint check logic#159168

Open
cclfmht wants to merge 3 commits into
rust-lang:mainfrom
cclfmht:fix/issue-158735
Open

Fix static_mut_refs lint check logic#159168
cclfmht wants to merge 3 commits into
rust-lang:mainfrom
cclfmht:fix/issue-158735

Conversation

@cclfmht

@cclfmht cclfmht commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

This PR fixes issue #158735.

Previously, the lint might suggested using interior mutable type even if it the compiler already decides that the referenced type is interior mutable, while it didn't give such suggestion when the referenced type is considered non-interior mutable. This commit refined the logic as follow: if the referenced type is not interior mutable, then suggests using types with interior mutability; otherwise, suggests removing mut if the reference is a shared reference. Note that in the latter case, compiler might be silent if the span of the static mut definition is not appropriate for suggestions (e.g., comes from macro expansion).

r? @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 Jul 12, 2026
@rustbot

rustbot commented Jul 12, 2026

Copy link
Copy Markdown
Collaborator

RalfJung is not on the review rotation at the moment.
They may take a while to respond.

| ^^^^^^^^^ mutable reference to mutable static
|
= note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>

@RalfJung RalfJung Jul 12, 2026

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 don't think this is the suggestion we want to give here -- certainly not the only one. In many cases, using raw pointers is an equally viable solution.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In the subdiagnostic part it also suggests user to rewrite &mut to &raw mut for using raw pointers, so I think we provide 2 solutions here.

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.

That just means we're sending mixed signals, where we're saying both "do A" and "do B".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Understood. Then it would be appropriate to remove that suggestion for explicit referencing (&a or &mut a) cases like this.

@RalfJung

Copy link
Copy Markdown
Member

r? @estebank for the code part of this
(This is a follow-up to #151362)

@rustbot rustbot assigned estebank and unassigned RalfJung Jul 12, 2026
cclfmht added 3 commits July 12, 2026 19:00
Previously, the lint might suggested using interior mutable type even if
it the compiler already decides that the referenced type is interior
mutable, while it didn't give such suggestion when the referenced type is
considered non-interior mutable. This commit refined the logic as follow:
if the referenced type is not interior mutable, then suggests using types
with interior mutability; otherwise, suggests removing `mut` if the
reference is a *shared* reference. Note that in the latter case, compiler
might be silent if the span of the `static mut` definition is not
appropriate for suggestions (e.g., comes from macro expansion).
For cases like `&a` or `&mut a`, we only leave a subdiagnostic to suggest
user to use raw borrow operators. Using raw pointers in such cases are
also equally viable solution but it's not appropriate to suggest using
interior mutable types at the same time.
@cclfmht cclfmht force-pushed the fix/issue-158735 branch from d1eb366 to 1e65341 Compare July 12, 2026 11:24
@rustbot

rustbot commented Jul 12, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@estebank

Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors

rust-bors Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 1e65341 has been approved by estebank

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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 Jul 14, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 15, 2026
Fix static_mut_refs lint check logic

This PR fixes issue rust-lang#158735.

Previously, the lint might suggested using interior mutable type even if it the compiler already decides that the referenced type is interior mutable, while it didn't give such suggestion when the referenced type is considered non-interior mutable. This commit refined the logic as follow: if the referenced type is not interior mutable, then suggests using types with interior mutability; otherwise, suggests removing `mut` if the reference is a *shared* reference. Note that in the latter case, compiler might be silent if the span of the `static mut` definition is not appropriate for suggestions (e.g., comes from macro expansion).

r? @RalfJung
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.

4 participants