Skip to content

Commit 0bc29ce

Browse files
committed
Add regression test for const_item_interior_mutations deref FP
1 parent 1d8f9c5 commit 0bc29ce

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/150157>
2+
//
3+
// We shouldn't lint on user types, including through deref.
4+
5+
//@ check-pass
6+
7+
use std::cell::Cell;
8+
use std::ops::Deref;
9+
10+
// Cut down version of the issue reproducer without the thread local to just a Deref
11+
pub struct LocalKey<T> {
12+
inner: T,
13+
}
14+
15+
impl<T> Deref for LocalKey<T> {
16+
type Target = T;
17+
18+
fn deref(&self) -> &Self::Target {
19+
&self.inner
20+
}
21+
}
22+
23+
const LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
24+
25+
fn main() {
26+
let count = LOCAL_COUNT.get();
27+
//~^ WARN mutation of an interior mutable `const`
28+
LOCAL_COUNT.set(count);
29+
//~^ WARN mutation of an interior mutable `const`
30+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
warning: mutation of an interior mutable `const` item with call to `get`
2+
--> $DIR/const-item-interior-mutations-const-deref.rs:26:17
3+
|
4+
LL | let count = LOCAL_COUNT.get();
5+
| -----------^^^^^^
6+
| |
7+
| `LOCAL_COUNT` is a interior mutable `const` item of type `LocalKey<Cell<usize>>`
8+
|
9+
= note: each usage of a `const` item creates a new temporary
10+
= note: only the temporaries and never the original `const LOCAL_COUNT` will be modified
11+
= help: for more details on interior mutability see <https://doc.rust-lang.org/reference/interior-mutability.html>
12+
= note: `#[warn(const_item_interior_mutations)]` on by default
13+
help: for a shared instance of `LOCAL_COUNT`, consider making it a `static` item instead
14+
|
15+
LL - const LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
16+
LL + static LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
17+
|
18+
19+
warning: mutation of an interior mutable `const` item with call to `set`
20+
--> $DIR/const-item-interior-mutations-const-deref.rs:28:5
21+
|
22+
LL | LOCAL_COUNT.set(count);
23+
| -----------^^^^^^^^^^^
24+
| |
25+
| `LOCAL_COUNT` is a interior mutable `const` item of type `LocalKey<Cell<usize>>`
26+
|
27+
= note: each usage of a `const` item creates a new temporary
28+
= note: only the temporaries and never the original `const LOCAL_COUNT` will be modified
29+
= help: for more details on interior mutability see <https://doc.rust-lang.org/reference/interior-mutability.html>
30+
help: for a shared instance of `LOCAL_COUNT`, consider making it a `static` item instead
31+
|
32+
LL - const LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
33+
LL + static LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
34+
|
35+
36+
warning: 2 warnings emitted
37+

0 commit comments

Comments
 (0)