Skip to content

Commit b2ec6f8

Browse files
committed
wip test
1 parent 999d239 commit b2ec6f8

4 files changed

+806
-0
lines changed
+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#![deny(clippy::borrow_interior_mutable_const)]
2+
#![allow(
3+
clippy::declare_interior_mutable_const,
4+
clippy::out_of_bounds_indexing,
5+
const_item_mutation
6+
)]
7+
8+
use core::cell::{Cell, UnsafeCell};
9+
use core::ops::{Deref, Index};
10+
11+
trait ConstDefault {
12+
const DEFAULT: Self;
13+
}
14+
impl ConstDefault for u32 {
15+
const DEFAULT: Self = 0;
16+
}
17+
impl<T: ConstDefault> ConstDefault for Cell<T> {
18+
const DEFAULT: Self = Cell::new(T::DEFAULT);
19+
}
20+
21+
fn main() {
22+
{
23+
const C: String = String::new();
24+
let _ = C;
25+
let _ = &C;
26+
let _ = C.len();
27+
let _ = &*C;
28+
}
29+
{
30+
const C: UnsafeCell<u32> = UnsafeCell::new(0);
31+
let _ = C;
32+
let _ = &C; //~ borrow_interior_mutable_const
33+
let _ = C.into_inner();
34+
let _ = C.get(); //~ borrow_interior_mutable_const
35+
}
36+
{
37+
const C: Cell<u32> = Cell::new(0);
38+
let _ = C;
39+
let _ = &C; //~ borrow_interior_mutable_const
40+
let _ = &mut C; //~ borrow_interior_mutable_const
41+
let _ = C.into_inner();
42+
43+
let local = C;
44+
C.swap(&local) //~ borrow_interior_mutable_const
45+
}
46+
{
47+
const C: [(Cell<u32>,); 1] = [(Cell::new(0),)];
48+
let _ = C;
49+
let _ = &C; //~ borrow_interior_mutable_const
50+
let _ = &C[0]; //~ borrow_interior_mutable_const
51+
let _ = &C[0].0; //~ borrow_interior_mutable_const
52+
C[0].0.set(1); //~ borrow_interior_mutable_const
53+
}
54+
{
55+
struct S(Cell<u32>);
56+
impl S {
57+
const C: Self = Self(Cell::new(0));
58+
}
59+
impl Deref for S {
60+
type Target = Cell<u32>;
61+
fn deref(&self) -> &Self::Target {
62+
&self.0
63+
}
64+
}
65+
let _ = S::C;
66+
let _ = S::C.0;
67+
let _ = &S::C; //~ borrow_interior_mutable_const
68+
let _ = &S::C.0; //~ borrow_interior_mutable_const
69+
S::C.set(1); //~ borrow_interior_mutable_const
70+
let _ = &*S::C; //~ borrow_interior_mutable_const
71+
(*S::C).set(1); //~ borrow_interior_mutable_const
72+
}
73+
{
74+
enum E {
75+
Cell(Cell<u32>),
76+
Other,
77+
}
78+
const CELL: E = E::Cell(Cell::new(0));
79+
const OTHER: E = E::Other;
80+
81+
let _ = CELL;
82+
let _ = &CELL; //~ borrow_interior_mutable_const
83+
let E::Cell(_) = CELL else {
84+
return;
85+
};
86+
87+
let _ = OTHER;
88+
let _ = &OTHER;
89+
let E::Cell(ref _x) = OTHER else {
90+
return;
91+
};
92+
}
93+
{
94+
struct S<T> {
95+
cell: (Cell<T>, u32),
96+
other: Option<T>,
97+
}
98+
impl<T: ConstDefault + Copy> S<T> {
99+
const C: Self = Self {
100+
cell: (Cell::<T>::DEFAULT, 0),
101+
other: Some(T::DEFAULT),
102+
};
103+
104+
fn f() {
105+
let _ = Self::C;
106+
let _ = &Self::C; //~ borrow_interior_mutable_const
107+
let _ = Self::C.other;
108+
let _ = &Self::C.other;
109+
let _ = &Self::C.cell; //~ borrow_interior_mutable_const
110+
let _ = &Self::C.cell.0; //~ borrow_interior_mutable_const
111+
Self::C.cell.0.set(T::DEFAULT); //~ borrow_interior_mutable_const
112+
let _ = &Self::C.cell.1;
113+
}
114+
}
115+
}
116+
{
117+
trait T {
118+
const VALUE: Option<Cell<u32>> = Some(Cell::new(0));
119+
}
120+
impl T for u32 {}
121+
impl T for i32 {
122+
const VALUE: Option<Cell<u32>> = None;
123+
}
124+
125+
let _ = &u32::VALUE; //~ borrow_interior_mutable_const
126+
let _ = &i32::VALUE;
127+
}
128+
{
129+
trait Trait<T: ConstDefault> {
130+
type T<U: ConstDefault>: ConstDefault;
131+
const VALUE: Option<Self::T<T>> = Some(Self::T::<T>::DEFAULT);
132+
}
133+
impl<T: ConstDefault> Trait<T> for u32 {
134+
type T<U: ConstDefault> = Cell<U>;
135+
}
136+
impl<T: ConstDefault> Trait<T> for i32 {
137+
type T<U: ConstDefault> = Cell<U>;
138+
const VALUE: Option<Cell<T>> = None;
139+
}
140+
141+
fn f<T: ConstDefault>() {
142+
let _ = &<u32 as Trait<T>>::VALUE; //~ borrow_interior_mutable_const
143+
let _ = &<i32 as Trait<T>>::VALUE;
144+
}
145+
}
146+
{
147+
struct S([Option<Cell<u32>>; 2]);
148+
impl Index<usize> for S {
149+
type Output = Option<Cell<u32>>;
150+
fn index(&self, idx: usize) -> &Self::Output {
151+
&self.0[idx]
152+
}
153+
}
154+
155+
const C: S = S([Some(Cell::new(0)), None]);
156+
let _ = &C; //~ borrow_interior_mutable_const
157+
let _ = &C[0]; //~ borrow_interior_mutable_const
158+
let _ = &C.0[0]; //~ borrow_interior_mutable_const
159+
let _ = &C.0[1];
160+
}
161+
{
162+
const C: [Option<Cell<u32>>; 2] = [None, None];
163+
let _ = &C[0];
164+
let _ = &C[1];
165+
let _ = &C[2];
166+
167+
fn f(i: usize) {
168+
let _ = &C[i];
169+
}
170+
}
171+
{
172+
const C: [Option<Cell<u32>>; 2] = [None, Some(Cell::new(0))];
173+
let _ = &C[0];
174+
let _ = &C[1]; //~ borrow_interior_mutable_const
175+
let _ = &C[2];
176+
177+
fn f(i: usize) {
178+
let _ = &C[i]; //~ borrow_interior_mutable_const
179+
}
180+
}
181+
}

0 commit comments

Comments
 (0)