Add regression test for const_item_interior_mutations deref FP

(cherry picked from commit 0bc29cec45)
This commit is contained in:
Urgau
2025-12-19 17:55:19 +01:00
committed by Josh Stone
parent 9f8bfd6b4a
commit 0aa11b5dd2
2 changed files with 67 additions and 0 deletions
@@ -0,0 +1,30 @@
// Regression test for <https://github.com/rust-lang/rust/issues/150157>
//
// We shouldn't lint on user types, including through deref.
//@ check-pass
use std::cell::Cell;
use std::ops::Deref;
// Cut down version of the issue reproducer without the thread local to just a Deref
pub struct LocalKey<T> {
inner: T,
}
impl<T> Deref for LocalKey<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
const LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
fn main() {
let count = LOCAL_COUNT.get();
//~^ WARN mutation of an interior mutable `const`
LOCAL_COUNT.set(count);
//~^ WARN mutation of an interior mutable `const`
}
@@ -0,0 +1,37 @@
warning: mutation of an interior mutable `const` item with call to `get`
--> $DIR/const-item-interior-mutations-const-deref.rs:26:17
|
LL | let count = LOCAL_COUNT.get();
| -----------^^^^^^
| |
| `LOCAL_COUNT` is a interior mutable `const` item of type `LocalKey<Cell<usize>>`
|
= note: each usage of a `const` item creates a new temporary
= note: only the temporaries and never the original `const LOCAL_COUNT` will be modified
= help: for more details on interior mutability see <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: `#[warn(const_item_interior_mutations)]` on by default
help: for a shared instance of `LOCAL_COUNT`, consider making it a `static` item instead
|
LL - const LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
LL + static LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
|
warning: mutation of an interior mutable `const` item with call to `set`
--> $DIR/const-item-interior-mutations-const-deref.rs:28:5
|
LL | LOCAL_COUNT.set(count);
| -----------^^^^^^^^^^^
| |
| `LOCAL_COUNT` is a interior mutable `const` item of type `LocalKey<Cell<usize>>`
|
= note: each usage of a `const` item creates a new temporary
= note: only the temporaries and never the original `const LOCAL_COUNT` will be modified
= help: for more details on interior mutability see <https://doc.rust-lang.org/reference/interior-mutability.html>
help: for a shared instance of `LOCAL_COUNT`, consider making it a `static` item instead
|
LL - const LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
LL + static LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
|
warning: 2 warnings emitted