2023-04-23 06:03:09 -05:00
|
|
|
//@aux-build:helper.rs
|
2020-11-23 06:51:04 -06:00
|
|
|
|
2023-07-02 07:35:19 -05:00
|
|
|
#![deny(clippy::borrow_interior_mutable_const)]
|
2020-11-23 06:51:04 -06:00
|
|
|
#![allow(clippy::declare_interior_mutable_const)]
|
|
|
|
|
|
|
|
// this file (mostly) replicates its `declare` counterpart. Please see it for more discussions.
|
|
|
|
|
|
|
|
extern crate helper;
|
|
|
|
|
|
|
|
use std::cell::Cell;
|
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
|
|
|
|
enum OptionalCell {
|
|
|
|
Unfrozen(Cell<bool>),
|
|
|
|
Frozen,
|
|
|
|
}
|
|
|
|
|
|
|
|
const UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(true));
|
|
|
|
const FROZEN_VARIANT: OptionalCell = OptionalCell::Frozen;
|
|
|
|
|
|
|
|
fn borrow_optional_cell() {
|
2023-07-02 07:35:19 -05:00
|
|
|
let _ = &UNFROZEN_VARIANT; //~ ERROR: interior mutability
|
2020-11-23 06:51:04 -06:00
|
|
|
let _ = &FROZEN_VARIANT;
|
|
|
|
}
|
|
|
|
|
|
|
|
trait AssocConsts {
|
|
|
|
const TO_BE_UNFROZEN_VARIANT: OptionalCell;
|
|
|
|
const TO_BE_FROZEN_VARIANT: OptionalCell;
|
|
|
|
|
|
|
|
const DEFAULTED_ON_UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
|
|
|
|
const DEFAULTED_ON_FROZEN_VARIANT: OptionalCell = OptionalCell::Frozen;
|
|
|
|
|
|
|
|
fn function() {
|
|
|
|
// This is the "suboptimal behavior" mentioned in `is_value_unfrozen`
|
|
|
|
// caused by a similar reason to unfrozen types without any default values
|
|
|
|
// get linted even if it has frozen variants'.
|
2023-07-02 07:35:19 -05:00
|
|
|
let _ = &Self::TO_BE_FROZEN_VARIANT; //~ ERROR: interior mutability
|
2020-11-23 06:51:04 -06:00
|
|
|
|
|
|
|
// The lint ignores default values because an impl of this trait can set
|
|
|
|
// an unfrozen variant to `DEFAULTED_ON_FROZEN_VARIANT` and use the default impl for `function`.
|
2023-07-02 07:35:19 -05:00
|
|
|
let _ = &Self::DEFAULTED_ON_FROZEN_VARIANT; //~ ERROR: interior mutability
|
2020-11-23 06:51:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AssocConsts for u64 {
|
|
|
|
const TO_BE_UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
|
|
|
|
const TO_BE_FROZEN_VARIANT: OptionalCell = OptionalCell::Frozen;
|
|
|
|
|
|
|
|
fn function() {
|
2023-07-02 07:35:19 -05:00
|
|
|
let _ = &<Self as AssocConsts>::TO_BE_UNFROZEN_VARIANT; //~ ERROR: interior mutability
|
2020-11-23 06:51:04 -06:00
|
|
|
let _ = &<Self as AssocConsts>::TO_BE_FROZEN_VARIANT;
|
2023-07-02 07:35:19 -05:00
|
|
|
let _ = &Self::DEFAULTED_ON_UNFROZEN_VARIANT; //~ ERROR: interior mutability
|
2020-11-23 06:51:04 -06:00
|
|
|
let _ = &Self::DEFAULTED_ON_FROZEN_VARIANT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait AssocTypes {
|
|
|
|
type ToBeUnfrozen;
|
|
|
|
|
|
|
|
const TO_BE_UNFROZEN_VARIANT: Option<Self::ToBeUnfrozen>;
|
|
|
|
const TO_BE_FROZEN_VARIANT: Option<Self::ToBeUnfrozen>;
|
|
|
|
|
|
|
|
// there's no need to test here because it's the exactly same as `trait::AssocTypes`
|
|
|
|
fn function();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AssocTypes for u64 {
|
|
|
|
type ToBeUnfrozen = AtomicUsize;
|
|
|
|
|
2023-07-02 07:35:19 -05:00
|
|
|
const TO_BE_UNFROZEN_VARIANT: Option<Self::ToBeUnfrozen> = Some(Self::ToBeUnfrozen::new(4));
|
2020-11-23 06:51:04 -06:00
|
|
|
const TO_BE_FROZEN_VARIANT: Option<Self::ToBeUnfrozen> = None;
|
|
|
|
|
|
|
|
fn function() {
|
2023-07-02 07:35:19 -05:00
|
|
|
let _ = &<Self as AssocTypes>::TO_BE_UNFROZEN_VARIANT; //~ ERROR: interior mutability
|
2020-11-23 06:51:04 -06:00
|
|
|
let _ = &<Self as AssocTypes>::TO_BE_FROZEN_VARIANT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum BothOfCellAndGeneric<T> {
|
|
|
|
Unfrozen(Cell<*const T>),
|
|
|
|
Generic(*const T),
|
|
|
|
Frozen(usize),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> BothOfCellAndGeneric<T> {
|
2023-07-02 07:35:19 -05:00
|
|
|
const UNFROZEN_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Unfrozen(Cell::new(std::ptr::null()));
|
|
|
|
const GENERIC_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Generic(std::ptr::null());
|
2020-11-23 06:51:04 -06:00
|
|
|
const FROZEN_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Frozen(5);
|
|
|
|
|
|
|
|
fn function() {
|
2023-07-02 07:35:19 -05:00
|
|
|
let _ = &Self::UNFROZEN_VARIANT; //~ ERROR: interior mutability
|
|
|
|
let _ = &Self::GENERIC_VARIANT; //~ ERROR: interior mutability
|
2020-11-23 06:51:04 -06:00
|
|
|
let _ = &Self::FROZEN_VARIANT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// constants defined in foreign crates
|
2023-07-02 07:35:19 -05:00
|
|
|
let _ = &helper::WRAPPED_PRIVATE_UNFROZEN_VARIANT; //~ ERROR: interior mutability
|
2020-11-23 06:51:04 -06:00
|
|
|
let _ = &helper::WRAPPED_PRIVATE_FROZEN_VARIANT;
|
|
|
|
}
|