-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve projections during internal mutability analysis
- Loading branch information
1 parent
d79f862
commit 58d4834
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#![deny(clippy::borrow_interior_mutable_const)] | ||
#![deny(clippy::declare_interior_mutable_const)] | ||
|
||
// Inspired by https://github.com/rust-lang/rust/pull/130543#issuecomment-2364828139 | ||
|
||
use std::cell::UnsafeCell; | ||
|
||
trait Trait { | ||
type Assoc; | ||
} | ||
|
||
type Assoc<T> = <T as Trait>::Assoc; | ||
|
||
impl Trait for u8 { | ||
type Assoc = UnsafeCell<u8>; | ||
} | ||
|
||
impl Trait for () { | ||
type Assoc = (); | ||
} | ||
|
||
enum MaybeMutable { | ||
Mutable(Assoc<u8>), | ||
Immutable(Assoc<()>), | ||
} | ||
|
||
const CELL: Assoc<u8> = UnsafeCell::new(0); //~ ERROR: interior mutable | ||
const UNIT: Assoc<()> = (); | ||
const MUTABLE: MaybeMutable = MaybeMutable::Mutable(CELL); //~ ERROR: interior mutable | ||
const IMMUTABLE: MaybeMutable = MaybeMutable::Immutable(UNIT); | ||
|
||
fn print_ref<T>(t: &T) { | ||
let p: *const T = t; | ||
println!("{p:p}") | ||
} | ||
|
||
fn main() { | ||
print_ref(&CELL); //~ ERROR: interior mutability | ||
print_ref(&UNIT); | ||
print_ref(&MUTABLE); //~ ERROR: interior mutability | ||
print_ref(&IMMUTABLE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
error: a `const` item should not be interior mutable | ||
--> tests/ui/borrow_interior_mutable_const/projections.rs:27:1 | ||
| | ||
LL | const CELL: Assoc<u8> = UnsafeCell::new(0); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider making this `Sync` so that it can go in a static item or using a `thread_local` | ||
note: the lint level is defined here | ||
--> tests/ui/borrow_interior_mutable_const/projections.rs:2:9 | ||
| | ||
LL | #![deny(clippy::declare_interior_mutable_const)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: a `const` item should not be interior mutable | ||
--> tests/ui/borrow_interior_mutable_const/projections.rs:29:1 | ||
| | ||
LL | const MUTABLE: MaybeMutable = MaybeMutable::Mutable(CELL); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider making this `Sync` so that it can go in a static item or using a `thread_local` | ||
|
||
error: a `const` item with interior mutability should not be borrowed | ||
--> tests/ui/borrow_interior_mutable_const/projections.rs:38:16 | ||
| | ||
LL | print_ref(&CELL); | ||
| ^^^^ | ||
| | ||
= help: assign this const to a local or static variable, and use the variable here | ||
note: the lint level is defined here | ||
--> tests/ui/borrow_interior_mutable_const/projections.rs:1:9 | ||
| | ||
LL | #![deny(clippy::borrow_interior_mutable_const)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: a `const` item with interior mutability should not be borrowed | ||
--> tests/ui/borrow_interior_mutable_const/projections.rs:40:16 | ||
| | ||
LL | print_ref(&MUTABLE); | ||
| ^^^^^^^ | ||
| | ||
= help: assign this const to a local or static variable, and use the variable here | ||
|
||
error: aborting due to 4 previous errors | ||
|