2020-11-23 12:51:04 +00:00
|
|
|
#![warn(clippy::declare_interior_mutable_const)]
|
|
|
|
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cell::Cell;
|
|
|
|
use std::fmt::Display;
|
2024-08-24 21:34:45 +00:00
|
|
|
use std::ptr;
|
2020-11-23 12:51:04 +00:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
use std::sync::Once;
|
|
|
|
|
2023-07-02 12:35:19 +00:00
|
|
|
const ATOMIC: AtomicUsize = AtomicUsize::new(5); //~ ERROR: interior mutable
|
|
|
|
const CELL: Cell<usize> = Cell::new(6); //~ ERROR: interior mutable
|
2020-11-23 12:51:04 +00:00
|
|
|
const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
|
2023-07-02 12:35:19 +00:00
|
|
|
//~^ ERROR: interior mutable
|
2020-11-23 12:51:04 +00:00
|
|
|
|
|
|
|
macro_rules! declare_const {
|
|
|
|
($name:ident: $ty:ty = $e:expr) => {
|
|
|
|
const $name: $ty = $e;
|
2024-02-27 14:25:18 +00:00
|
|
|
//~^ ERROR: interior mutable
|
2020-11-23 12:51:04 +00:00
|
|
|
};
|
|
|
|
}
|
2024-02-27 14:25:18 +00:00
|
|
|
declare_const!(_ONCE: Once = Once::new());
|
2020-11-23 12:51:04 +00:00
|
|
|
|
|
|
|
// const ATOMIC_REF: &AtomicUsize = &AtomicUsize::new(7); // This will simply trigger E0492.
|
|
|
|
|
|
|
|
const INTEGER: u8 = 8;
|
|
|
|
const STRING: String = String::new();
|
|
|
|
const STR: &str = "012345";
|
|
|
|
const COW: Cow<str> = Cow::Borrowed("abcdef");
|
2023-07-02 12:35:19 +00:00
|
|
|
// note: a const item of Cow is used in the `postgres` package.
|
2020-11-23 12:51:04 +00:00
|
|
|
|
|
|
|
const NO_ANN: &dyn Display = &70;
|
|
|
|
|
|
|
|
static STATIC_TUPLE: (AtomicUsize, String) = (ATOMIC, STRING);
|
2023-07-02 12:35:19 +00:00
|
|
|
// there should be no lints on the line above line
|
2020-11-23 12:51:04 +00:00
|
|
|
|
2022-07-28 17:08:22 +00:00
|
|
|
mod issue_8493 {
|
|
|
|
use std::cell::Cell;
|
|
|
|
|
|
|
|
thread_local! {
|
|
|
|
static _BAR: Cell<i32> = const { Cell::new(0) };
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! issue_8493 {
|
|
|
|
() => {
|
2023-07-02 12:35:19 +00:00
|
|
|
const _BAZ: Cell<usize> = Cell::new(0);
|
2022-07-28 17:08:22 +00:00
|
|
|
static _FOOBAR: () = {
|
|
|
|
thread_local! {
|
|
|
|
static _VAR: Cell<i32> = const { Cell::new(0) };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-02-27 14:25:18 +00:00
|
|
|
issue_8493!();
|
2022-06-30 08:50:09 +00:00
|
|
|
}
|
|
|
|
|
2024-08-24 21:34:45 +00:00
|
|
|
#[repr(C, align(8))]
|
|
|
|
struct NoAtomic(usize);
|
|
|
|
#[repr(C, align(8))]
|
|
|
|
struct WithAtomic(AtomicUsize);
|
|
|
|
|
|
|
|
const fn with_non_null() -> *const WithAtomic {
|
|
|
|
const NO_ATOMIC: NoAtomic = NoAtomic(0);
|
|
|
|
(&NO_ATOMIC as *const NoAtomic).cast()
|
|
|
|
}
|
|
|
|
const WITH_ATOMIC: *const WithAtomic = with_non_null();
|
|
|
|
|
|
|
|
struct Generic<T>(T);
|
|
|
|
impl<T> Generic<T> {
|
|
|
|
const RAW_POINTER: *const Cell<T> = ptr::null();
|
|
|
|
}
|
|
|
|
|
2020-11-23 12:51:04 +00:00
|
|
|
fn main() {}
|