Fix static_mut_refs lint check logic#159168
Conversation
|
|
| | ^^^^^^^^^ 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> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
That just means we're sending mixed signals, where we're saying both "do A" and "do B".
There was a problem hiding this comment.
Understood. Then it would be appropriate to remove that suggestion for explicit referencing (&a or &mut a) cases like this.
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.
d1eb366 to
1e65341
Compare
|
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. |
|
@bors r+ |
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
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
mutif the reference is a shared reference. Note that in the latter case, compiler might be silent if the span of thestatic mutdefinition is not appropriate for suggestions (e.g., comes from macro expansion).r? @RalfJung