-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Lint at deny-by-default
against references to static mut
#128794
Comments
As someone who was previously pushing for some hard errors here, this makes sense to me.1 So long as Footnotes
|
Over here, @RalfJung brought up an interesting point:
Following that line of argument, it seems in that world we might want to go beyond just the known cases like I find myself drawn to that as being probably the consistent and right thing to do. That could suggest that we start by not linting against these cases where references aren't created at all in the MIR. Then later work could do more to avoid linting against other cases where such a proof is feasible. What do people think? |
@rustbot labels +I-lang-nominated Nominating to discuss that matter in case we don't manage to fully work it out asynchronously. |
The issue description has now been updated with a concrete proposal for how to proceed here. We can discuss that next week, and if appropriate, decide on that or on some other proposal via FCP. That proposal is:
We had ended the 2024-08-07 meeting with an apparent meeting consensus, but later discussion has indicated that there was not in fact a meeting of the minds among all members, so please disregard any earlier discussion to that effect. Lang is still undecided on the proper handling of #124895. |
@rfcbot fcp merge We discussed this in the meeting today; we had consensus on the plan above. We're going to FCP this, but please take our meeting consensus as enough to proceed with implementation here. |
Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
I still think as I did above (#128794 (comment)), so good by me. We're essentially approving the "intent to lint on references to static mut as footguns", and I don't think we need to be too strict about exactly all the details -- a huge advantage of it being a lint is that we can always tweak it around the edges later. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
This change was made in: |
We had earlier decided to disallow references to
static mut
in:static mut
[Edition Idea] #114447Later, we decided to disallow hidden references to mutable statics also:
Since then, some interesting cases have come up in:
These interesting cases revolve around how we desugar syntax for equality, comparison, compound assignment, and indexing. There are two aspects.
One, though
PartialEq::eq
,PartialOrd::lt
,AddAssign::add_assign
,Index::index
, andIndexMut::index_mut
all take references, for primitive types, as implemented today in lowering to MIR, these operations don't generate references at all. E.g., no references at all are created here:This behavior is documented for compound assignment expressions, but not otherwise (as far as I can tell).
Two, even if acting on wrapper types where a reference would be generated, the signatures of these methods guarantee that the reference is not leaked and is short-lived. That is, there's no possible sound implementation of
AddAssign::add_assign
, for any type, that would cause this:...to have any additional UB as compared with:
...which raises the question of why we would want to treat these differently, and why we would want to encourage people to write the latter rather than the former.
In the 2024-08-07 triage meeting, we discussed whether making references to mutable statics a hard error actually met our bar for hard errors. In other cases, we've chosen that only when doing so would make the language definition in a future edition simpler. Otherwise, we've stopped at a deny-by-default lint.
We discussed how, if we made this a lint rather than a hard error, we might have more room to maneuver.
This is what the comment by @scottmcm below is in reply to. However, we did not reach an actual consensus in the meeting.
Also, we did not discuss in the meeting the second aspect described above about how short-lived references are UB-equivalent to accesses.
Proposal
On the table in this issue is the following proposal:
static_mut_ref
lint todeny-by-default
in Rust 2024.deny-by-default
in other cases like this.static_mut_ref
to lint against "hidden references" (that is, references created by virtue of autoref).deny-by-default
lint rather than a hard error would be used in Rust 2024.static mut
.static mut
. There's no reason for us to push people away from these patterns and into other patterns that have no more or less potential for UB.cc @obeis @RalfJung @rust-lang/lang
Tracking:
The text was updated successfully, but these errors were encountered: