2023-07-02 07:35:19 -05:00
|
|
|
#![deny(clippy::borrow_interior_mutable_const)]
|
2022-01-27 08:12:45 -06:00
|
|
|
#![allow(clippy::declare_interior_mutable_const, clippy::needless_borrow)]
|
2020-09-08 16:59:43 -05:00
|
|
|
#![allow(const_item_mutation)]
|
2018-06-06 10:20:22 -05:00
|
|
|
|
|
|
|
use std::borrow::Cow;
|
2020-08-28 09:10:16 -05:00
|
|
|
use std::cell::{Cell, UnsafeCell};
|
2018-06-06 10:20:22 -05:00
|
|
|
use std::fmt::Display;
|
2018-12-09 16:26:16 -06:00
|
|
|
use std::sync::Once;
|
2024-09-24 04:58:04 -05:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2018-06-06 10:20:22 -05:00
|
|
|
|
2020-01-26 21:14:11 -06:00
|
|
|
const ATOMIC: AtomicUsize = AtomicUsize::new(5);
|
|
|
|
const CELL: Cell<usize> = Cell::new(6);
|
2024-07-11 18:41:48 -05:00
|
|
|
const ATOMIC_TUPLE: ([AtomicUsize; 1], Option<Box<AtomicUsize>>, u8) = ([ATOMIC], None, 7);
|
2018-06-06 10:20:22 -05:00
|
|
|
const INTEGER: u8 = 8;
|
|
|
|
const STRING: String = String::new();
|
|
|
|
const STR: &str = "012345";
|
|
|
|
const COW: Cow<str> = Cow::Borrowed("abcdef");
|
2019-05-30 01:23:47 -05:00
|
|
|
const NO_ANN: &dyn Display = &70;
|
2018-06-06 10:20:22 -05:00
|
|
|
static STATIC_TUPLE: (AtomicUsize, String) = (ATOMIC, STRING);
|
|
|
|
const ONCE_INIT: Once = Once::new();
|
|
|
|
|
2020-11-23 06:51:04 -06:00
|
|
|
// This is just a pointer that can be safely dereferenced,
|
2020-08-28 09:10:16 -05:00
|
|
|
// it's semantically the same as `&'static T`;
|
|
|
|
// but it isn't allowed to make a static reference from an arbitrary integer value at the moment.
|
|
|
|
// For more information, please see the issue #5918.
|
|
|
|
pub struct StaticRef<T> {
|
|
|
|
ptr: *const T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> StaticRef<T> {
|
|
|
|
/// Create a new `StaticRef` from a raw pointer
|
|
|
|
///
|
|
|
|
/// ## Safety
|
|
|
|
///
|
|
|
|
/// Callers must pass in a reference to statically allocated memory which
|
|
|
|
/// does not overlap with other values.
|
|
|
|
pub const unsafe fn new(ptr: *const T) -> StaticRef<T> {
|
|
|
|
StaticRef { ptr }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> std::ops::Deref for StaticRef<T> {
|
|
|
|
type Target = T;
|
|
|
|
|
2022-12-15 21:06:21 -06:00
|
|
|
fn deref(&self) -> &T {
|
2020-08-28 09:10:16 -05:00
|
|
|
unsafe { &*self.ptr }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// use a tuple to make sure referencing a field behind a pointer isn't linted.
|
|
|
|
const CELL_REF: StaticRef<(UnsafeCell<u32>,)> = unsafe { StaticRef::new(std::ptr::null()) };
|
|
|
|
|
2018-06-06 10:20:22 -05:00
|
|
|
fn main() {
|
2023-07-02 07:35:19 -05:00
|
|
|
ATOMIC.store(1, Ordering::SeqCst); //~ ERROR: interior mutability
|
|
|
|
assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR: interior mutability
|
2018-06-06 10:20:22 -05:00
|
|
|
|
|
|
|
let _once = ONCE_INIT;
|
2023-07-02 07:35:19 -05:00
|
|
|
let _once_ref = &ONCE_INIT; //~ ERROR: interior mutability
|
|
|
|
let _once_ref_2 = &&ONCE_INIT; //~ ERROR: interior mutability
|
|
|
|
let _once_ref_4 = &&&&ONCE_INIT; //~ ERROR: interior mutability
|
|
|
|
let _once_mut = &mut ONCE_INIT; //~ ERROR: interior mutability
|
2018-06-06 10:20:22 -05:00
|
|
|
let _atomic_into_inner = ATOMIC.into_inner();
|
|
|
|
// these should be all fine.
|
|
|
|
let _twice = (ONCE_INIT, ONCE_INIT);
|
|
|
|
let _ref_twice = &(ONCE_INIT, ONCE_INIT);
|
|
|
|
let _ref_once = &(ONCE_INIT, ONCE_INIT).0;
|
|
|
|
let _array_twice = [ONCE_INIT, ONCE_INIT];
|
|
|
|
let _ref_array_twice = &[ONCE_INIT, ONCE_INIT];
|
|
|
|
let _ref_array_once = &[ONCE_INIT, ONCE_INIT][0];
|
|
|
|
|
|
|
|
// referencing projection is still bad.
|
2023-07-02 07:35:19 -05:00
|
|
|
let _ = &ATOMIC_TUPLE; //~ ERROR: interior mutability
|
|
|
|
let _ = &ATOMIC_TUPLE.0; //~ ERROR: interior mutability
|
|
|
|
let _ = &(&&&&ATOMIC_TUPLE).0; //~ ERROR: interior mutability
|
|
|
|
let _ = &ATOMIC_TUPLE.0[0]; //~ ERROR: interior mutability
|
|
|
|
let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ ERROR: interior mutability
|
2018-06-06 10:20:22 -05:00
|
|
|
let _ = &ATOMIC_TUPLE.2;
|
|
|
|
let _ = (&&&&ATOMIC_TUPLE).0;
|
|
|
|
let _ = (&&&&ATOMIC_TUPLE).2;
|
|
|
|
let _ = ATOMIC_TUPLE.0;
|
2023-07-02 07:35:19 -05:00
|
|
|
let _ = ATOMIC_TUPLE.0[0]; //~ ERROR: interior mutability
|
2018-06-06 10:20:22 -05:00
|
|
|
let _ = ATOMIC_TUPLE.1.into_iter();
|
|
|
|
let _ = ATOMIC_TUPLE.2;
|
2018-12-09 16:26:16 -06:00
|
|
|
let _ = &{ ATOMIC_TUPLE };
|
2018-06-06 10:20:22 -05:00
|
|
|
|
2023-07-02 07:35:19 -05:00
|
|
|
CELL.set(2); //~ ERROR: interior mutability
|
|
|
|
assert_eq!(CELL.get(), 6); //~ ERROR: interior mutability
|
2018-06-06 10:20:22 -05:00
|
|
|
|
|
|
|
assert_eq!(INTEGER, 8);
|
|
|
|
assert!(STRING.is_empty());
|
|
|
|
|
|
|
|
let a = ATOMIC;
|
|
|
|
a.store(4, Ordering::SeqCst);
|
|
|
|
assert_eq!(a.load(Ordering::SeqCst), 4);
|
|
|
|
|
|
|
|
STATIC_TUPLE.0.store(3, Ordering::SeqCst);
|
|
|
|
assert_eq!(STATIC_TUPLE.0.load(Ordering::SeqCst), 3);
|
|
|
|
assert!(STATIC_TUPLE.1.is_empty());
|
|
|
|
|
|
|
|
assert_eq!(NO_ANN.to_string(), "70"); // should never lint this.
|
2020-08-28 09:10:16 -05:00
|
|
|
|
|
|
|
let _ = &CELL_REF.0;
|
2018-06-06 10:20:22 -05:00
|
|
|
}
|