Disallow hidden references to mutable static#124895
Conversation
|
Sorry, I can only review interpreter and Miri PRs. r? compiler |
|
r? @davidtwco |
This comment has been minimized.
This comment has been minimized.
ef8ab6d to
ac71600
Compare
This comment has been minimized.
This comment has been minimized.
ac71600 to
968dfbc
Compare
This comment has been minimized.
This comment has been minimized.
|
Marking as waiting on author since you've made this a draft. |
968dfbc to
7d2fd98
Compare
This comment has been minimized.
This comment has been minimized.
7d2fd98 to
72b1c9b
Compare
This comment has been minimized.
This comment has been minimized.
72b1c9b to
6467ccd
Compare
This comment has been minimized.
This comment has been minimized.
6467ccd to
abfd0a0
Compare
|
The Miri subtree was changed cc @rust-lang/miri Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
|
@rustbot ready |
|
Thanks, @traviscross! What would be an acceptable heuristic for determining from the method signature that the |
Sounds like a start. For |
|
☔ The latest upstream changes (presumably #128134) made this pull request unmergeable. Please resolve the merge conflicts. |
|
In terms of the heuristic, I'm trying to think up the maximally-pessimistic example of how the reference could leak. Perhaps it's something like this: use core::cell::Cell;
struct W<'w>(Cell<Option<&'w W<'w>>>);
impl<'w> W<'w> {
fn leak<'a: 'w>(&'a self) {
self.0.set(Some(self));
}
}
fn main() {
static mut X: W = W(Cell::new(None));
unsafe { X.leak() };
let leaked = unsafe { X.0.get().unwrap() };
_ = [leaked];
} |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Please change all of the FIXME(obeis) to not reference a specific username, since this is probably grouped under something better like FIXME(static_mut_refs).
Also, are all of the test changes still necessary? Do we really need to change STATIC += 1 to STATIC = STATIC + 1? If we're motivating users to make somewhat churn-y noop changes to their code, I really don't think this lint is complete yet. If not, then those should be reverted.
Same with x.is_some() to let Some(_) = X, and most of the other changes to tests.
Existing tests should not be changing static mut to static or to use atomics. In general, tests should be left alone, and have allow(static_mut_refs) added to the top. There's no reason to add FIXMEs to those -- some of these tests are explicitly using static muts to exercise their behavior in the compiler.
|
I was perhaps a bit too knee-jerky on the test changes, but they're really hard to separate out the changes from an initial glance. I would prefe us to separately in a follow-up PR (or maybe even a PR that lands before this one) that we could tweak some of the test to use |
compiler-errors
left a comment
There was a problem hiding this comment.
There are also some random stray files that need to be deleted:
compiler/rustc_lint_defs/src/lib.rs:11:5compiler/rustc_lint_defs/src/lib.rs:887:48
|
I'd personally rather we try to make this lint a bit less trigger-prone if possible. I'd be fine even if we implement it only for totally unconstrained late-bound receiver lifetimes where the lifetime does not show up in the return type or something like that. But that's my personal opinion; I guess if T-lang is fine with this amount of churn then whatever. |
|
r? @compiler-errors -- I'm not a good reviewer for this other than the basics. |
|
☔ The latest upstream changes (presumably #107251) made this pull request unmergeable. Please resolve the merge conflicts. |
|
@compiler-errors Done. |
I don't think this actually ever got fixed, but whatever. @bors r+ |
|
☀️ Test successful - checks-actions |
|
Finished benchmarking commit (5ba6db1): comparison URL. Overall result: ❌ regressions - no action needed@rustbot label: -perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)Results (secondary -1.8%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResults (secondary 7.1%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 767.803s -> 768.593s (0.10%) |
Closes #123060
Tracking: