rustc_const_eval/check_consts/
resolver.rs1use std::fmt;
6use std::marker::PhantomData;
7
8use rustc_index::bit_set::MixedBitSet;
9use rustc_middle::mir::visit::Visitor;
10use rustc_middle::mir::{
11 self, BasicBlock, CallReturnPlaces, Local, Location, Statement, StatementKind, TerminatorEdges,
12};
13use rustc_mir_dataflow::fmt::DebugWithContext;
14use rustc_mir_dataflow::{Analysis, JoinSemiLattice};
15
16use super::{ConstCx, Qualif, qualifs};
17
18struct TransferFunction<'mir, 'tcx, Q> {
26 ccx: &'mir ConstCx<'mir, 'tcx>,
27 state: &'mir mut State,
28 _qualif: PhantomData<Q>,
29}
30
31impl<'mir, 'tcx, Q> TransferFunction<'mir, 'tcx, Q>
32where
33 Q: Qualif,
34{
35 fn new(ccx: &'mir ConstCx<'mir, 'tcx>, state: &'mir mut State) -> Self {
36 TransferFunction { ccx, state, _qualif: PhantomData }
37 }
38
39 fn initialize_state(&mut self) {
40 self.state.qualif.clear();
41 self.state.borrow.clear();
42
43 for arg in self.ccx.body.args_iter() {
44 let arg_ty = self.ccx.body.local_decls[arg].ty;
45 if Q::in_any_value_of_ty(self.ccx, arg_ty) {
46 self.state.qualif.insert(arg);
47 }
48 }
49 }
50
51 fn assign_qualif_direct(&mut self, place: &mir::Place<'tcx>, mut value: bool) {
52 debug_assert!(!place.is_indirect());
53
54 if !value {
55 for (base, _elem) in place.iter_projections() {
56 let base_ty = base.ty(self.ccx.body, self.ccx.tcx);
57 if base_ty.ty.is_union() && Q::in_any_value_of_ty(self.ccx, base_ty.ty) {
58 value = true;
59 break;
60 }
61 }
62 }
63
64 match (value, place.as_ref()) {
65 (true, mir::PlaceRef { local, .. }) => {
66 self.state.qualif.insert(local);
67 }
68
69 (false, mir::PlaceRef { local: _, projection: &[] }) => {
74 }
76
77 _ => {}
78 }
79 }
80
81 fn apply_call_return_effect(
82 &mut self,
83 _block: BasicBlock,
84 return_places: CallReturnPlaces<'_, 'tcx>,
85 ) {
86 return_places.for_each(|place| {
87 let return_ty = place.ty(self.ccx.body, self.ccx.tcx).ty;
90 let qualif = Q::in_any_value_of_ty(self.ccx, return_ty);
91
92 if !place.is_indirect() {
93 self.assign_qualif_direct(&place, qualif);
94 }
95 });
96 }
97
98 fn address_of_allows_mutation(&self) -> bool {
99 true
102 }
103
104 fn ref_allows_mutation(&self, kind: mir::BorrowKind, place: mir::Place<'tcx>) -> bool {
105 match kind {
106 mir::BorrowKind::Mut { .. } => true,
107 mir::BorrowKind::Shared | mir::BorrowKind::Fake(_) => {
108 self.shared_borrow_allows_mutation(place)
109 }
110 }
111 }
112
113 fn shared_borrow_allows_mutation(&self, place: mir::Place<'tcx>) -> bool {
123 !place.ty(self.ccx.body, self.ccx.tcx).ty.is_freeze(self.ccx.tcx, self.ccx.typing_env)
124 }
125}
126
127impl<'tcx, Q> Visitor<'tcx> for TransferFunction<'_, 'tcx, Q>
128where
129 Q: Qualif,
130{
131 fn visit_operand(&mut self, operand: &mir::Operand<'tcx>, location: Location) {
132 self.super_operand(operand, location);
133
134 if !Q::IS_CLEARED_ON_MOVE {
135 return;
136 }
137
138 if let mir::Operand::Move(place) = operand
141 && let Some(local) = place.as_local()
142 {
143 if !self.state.borrow.contains(local) {
147 self.state.qualif.remove(local);
148 }
149 }
150 }
151
152 fn visit_assign(
153 &mut self,
154 place: &mir::Place<'tcx>,
155 rvalue: &mir::Rvalue<'tcx>,
156 location: Location,
157 ) {
158 let qualif =
159 qualifs::in_rvalue::<Q, _>(self.ccx, &mut |l| self.state.qualif.contains(l), rvalue);
160 if !place.is_indirect() {
161 self.assign_qualif_direct(place, qualif);
162 }
163
164 self.super_assign(place, rvalue, location);
167 }
168
169 fn visit_rvalue(&mut self, rvalue: &mir::Rvalue<'tcx>, location: Location) {
170 self.super_rvalue(rvalue, location);
171
172 match rvalue {
173 mir::Rvalue::RawPtr(_mt, borrowed_place) => {
174 if !borrowed_place.is_indirect() && self.address_of_allows_mutation() {
175 let place_ty = borrowed_place.ty(self.ccx.body, self.ccx.tcx).ty;
176 if Q::in_any_value_of_ty(self.ccx, place_ty) {
177 self.state.qualif.insert(borrowed_place.local);
178 self.state.borrow.insert(borrowed_place.local);
179 }
180 }
181 }
182
183 mir::Rvalue::Ref(_, kind, borrowed_place) => {
184 if !borrowed_place.is_indirect() && self.ref_allows_mutation(*kind, *borrowed_place)
185 {
186 let place_ty = borrowed_place.ty(self.ccx.body, self.ccx.tcx).ty;
187 if Q::in_any_value_of_ty(self.ccx, place_ty) {
188 self.state.qualif.insert(borrowed_place.local);
189 self.state.borrow.insert(borrowed_place.local);
190 }
191 }
192 }
193
194 mir::Rvalue::Cast(..)
195 | mir::Rvalue::ShallowInitBox(..)
196 | mir::Rvalue::Use(..)
197 | mir::Rvalue::CopyForDeref(..)
198 | mir::Rvalue::ThreadLocalRef(..)
199 | mir::Rvalue::Repeat(..)
200 | mir::Rvalue::BinaryOp(..)
201 | mir::Rvalue::NullaryOp(..)
202 | mir::Rvalue::UnaryOp(..)
203 | mir::Rvalue::Discriminant(..)
204 | mir::Rvalue::Aggregate(..)
205 | mir::Rvalue::WrapUnsafeBinder(..) => {}
206 }
207 }
208
209 fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
210 match statement.kind {
211 StatementKind::StorageDead(local) => {
212 self.state.qualif.remove(local);
213 self.state.borrow.remove(local);
214 }
215 _ => self.super_statement(statement, location),
216 }
217 }
218
219 fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) {
220 self.super_terminator(terminator, location);
226 }
227}
228
229pub(super) struct FlowSensitiveAnalysis<'mir, 'tcx, Q> {
231 ccx: &'mir ConstCx<'mir, 'tcx>,
232 _qualif: PhantomData<Q>,
233}
234
235impl<'mir, 'tcx, Q> FlowSensitiveAnalysis<'mir, 'tcx, Q>
236where
237 Q: Qualif,
238{
239 pub(super) fn new(_: Q, ccx: &'mir ConstCx<'mir, 'tcx>) -> Self {
240 FlowSensitiveAnalysis { ccx, _qualif: PhantomData }
241 }
242
243 fn transfer_function(&self, state: &'mir mut State) -> TransferFunction<'mir, 'tcx, Q> {
244 TransferFunction::<Q>::new(self.ccx, state)
245 }
246}
247
248#[derive(Debug, PartialEq, Eq)]
249pub(super) struct State {
252 pub qualif: MixedBitSet<Local>,
254 pub borrow: MixedBitSet<Local>,
257}
258
259impl Clone for State {
260 fn clone(&self) -> Self {
261 State { qualif: self.qualif.clone(), borrow: self.borrow.clone() }
262 }
263
264 fn clone_from(&mut self, other: &Self) {
267 self.qualif.clone_from(&other.qualif);
268 self.borrow.clone_from(&other.borrow);
269 }
270}
271
272impl State {
273 #[inline]
274 pub(super) fn contains(&self, local: Local) -> bool {
275 self.qualif.contains(local)
276 }
277}
278
279impl<C> DebugWithContext<C> for State {
280 fn fmt_with(&self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
281 f.write_str("qualif: ")?;
282 self.qualif.fmt_with(ctxt, f)?;
283 f.write_str(" borrow: ")?;
284 self.borrow.fmt_with(ctxt, f)?;
285 Ok(())
286 }
287
288 fn fmt_diff_with(&self, old: &Self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
289 if self == old {
290 return Ok(());
291 }
292
293 if self.qualif != old.qualif {
294 f.write_str("qualif: ")?;
295 self.qualif.fmt_diff_with(&old.qualif, ctxt, f)?;
296 f.write_str("\n")?;
297 }
298
299 if self.borrow != old.borrow {
300 f.write_str("borrow: ")?;
301 self.qualif.fmt_diff_with(&old.borrow, ctxt, f)?;
302 f.write_str("\n")?;
303 }
304
305 Ok(())
306 }
307}
308
309impl JoinSemiLattice for State {
310 fn join(&mut self, other: &Self) -> bool {
311 self.qualif.join(&other.qualif) || self.borrow.join(&other.borrow)
312 }
313}
314
315impl<'tcx, Q> Analysis<'tcx> for FlowSensitiveAnalysis<'_, 'tcx, Q>
316where
317 Q: Qualif,
318{
319 type Domain = State;
320
321 const NAME: &'static str = Q::ANALYSIS_NAME;
322
323 fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
324 State {
325 qualif: MixedBitSet::new_empty(body.local_decls.len()),
326 borrow: MixedBitSet::new_empty(body.local_decls.len()),
327 }
328 }
329
330 fn initialize_start_block(&self, _body: &mir::Body<'tcx>, state: &mut Self::Domain) {
331 self.transfer_function(state).initialize_state();
332 }
333
334 fn apply_primary_statement_effect(
335 &mut self,
336 state: &mut Self::Domain,
337 statement: &mir::Statement<'tcx>,
338 location: Location,
339 ) {
340 self.transfer_function(state).visit_statement(statement, location);
341 }
342
343 fn apply_primary_terminator_effect<'mir>(
344 &mut self,
345 state: &mut Self::Domain,
346 terminator: &'mir mir::Terminator<'tcx>,
347 location: Location,
348 ) -> TerminatorEdges<'mir, 'tcx> {
349 self.transfer_function(state).visit_terminator(terminator, location);
350 terminator.edges()
351 }
352
353 fn apply_call_return_effect(
354 &mut self,
355 state: &mut Self::Domain,
356 block: BasicBlock,
357 return_places: CallReturnPlaces<'_, 'tcx>,
358 ) {
359 self.transfer_function(state).apply_call_return_effect(block, return_places)
360 }
361}