]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_dataflow/src/value_analysis.rs
Flood with bottom for Deinit, StorageLive and StorageDead
[rust.git] / compiler / rustc_mir_dataflow / src / value_analysis.rs
1 //! This module provides a framework on top of the normal MIR dataflow framework to simplify the
2 //! implementation of analyses that track the values stored in places of interest.
3 //!
4 //! The default methods of [`ValueAnalysis`] (prefixed with `super_` instead of `handle_`)
5 //! provide some behavior that should be valid for all abstract domains that are based only on the
6 //! value stored in a certain place. On top of these default rules, an implementation should
7 //! override some of the `handle_` methods. For an example, see `ConstAnalysis`.
8 //!
9 //! An implementation must also provide a [`Map`]. Before the anaylsis begins, all places that
10 //! should be tracked during the analysis must be registered. Currently, the projections of these
11 //! places may only contain derefs, fields and downcasts (otherwise registration fails). During the
12 //! analysis, no new places can be registered.
13 //!
14 //! Note that if you want to track values behind references, you have to register the dereferenced
15 //! place. For example: Assume `let x = (0, 0)` and that we want to propagate values from `x.0` and
16 //! `x.1` also through the assignment `let y = &x`. In this case, we should register `x.0`, `x.1`,
17 //! `(*y).0` and `(*y).1`.
18 //!
19 //!
20 //! # Correctness
21 //!
22 //! Warning: This is a semi-formal attempt to argue for the correctness of this analysis. If you
23 //! find any weak spots, let me know! Recommended reading: Abstract Interpretation. We will use the
24 //! term "place" to refer to a place expression (like `mir::Place`), and we will call the
25 //! underlying entity "object". For instance, `*_1` and `*_2` are not the same place, but depending
26 //! on the value of `_1` and `_2`, they could refer to the same object. Also, the same place can
27 //! refer to different objects during execution. If `_1` is reassigned, then `*_1` may refer to
28 //! different objects before and after assignment. Additionally, when saying "access to a place",
29 //! what we really mean is "access to an object denoted by arbitrary projections of that place".
30 //!
31 //! In the following, we will assume a constant propagation analysis. Our analysis is correct if
32 //! every transfer function is correct. This is the case if for every pair (f, f#) and abstract
33 //! state s, we have f(y(s)) <= y(f#(s)), where s is a mapping from tracked place to top, bottom or
34 //! a constant. Since pointers (and mutable references) are not tracked, but can be used to change
35 //! values in the concrete domain, f# must assume that all places that can be affected in this way
36 //! for a given program point are already marked with top in s (otherwise many assignments and
37 //! function calls would have no choice but to mark all tracked places with top). This leads us to
38 //! an invariant: For all possible program points where there could possibly exist means of mutable
39 //! access to a tracked place (in the concrete domain), this place must be assigned to top (in the
40 //! abstract domain). The concretization function y can be defined as expected for the constant
41 //! propagation analysis, although the concrete state of course contains all kinds of non-tracked
42 //! data. However, by the invariant above, no mutable access to tracked places that are not marked
43 //! with top may be introduced.
44 //!
45 //! Note that we (at least currently) do not differentiate between "this place may assume different
46 //! values" and "a pointer to this place escaped the analysis". However, we still want to handle
47 //! assignments to constants as usual for f#. This adds an assumption: Whenever we have an
48 //! assignment that is captured by the analysis, all mutable access to the underlying place (which
49 //! is not observable by the analysis) must be invalidated. This is (hopefully) covered by Stacked
50 //! Borrows.
51 //!
52 //! To be continued...
53
54 use std::fmt::{Debug, Formatter};
55
56 use rustc_data_structures::fx::FxHashMap;
57 use rustc_index::vec::IndexVec;
58 use rustc_middle::mir::tcx::PlaceTy;
59 use rustc_middle::mir::*;
60 use rustc_middle::ty::{self, Ty, TyCtxt};
61 use rustc_target::abi::VariantIdx;
62
63 use crate::{
64     fmt::DebugWithContext, lattice::FlatSet, Analysis, AnalysisDomain, CallReturnPlaces,
65     JoinSemiLattice, SwitchIntEdgeEffects,
66 };
67
68 pub trait ValueAnalysis<'tcx> {
69     /// For each place of interest, the analysis tracks a value of the given type.
70     type Value: Clone + JoinSemiLattice + HasBottom + HasTop;
71
72     const NAME: &'static str;
73
74     fn map(&self) -> &Map;
75
76     fn handle_statement(&self, statement: &Statement<'tcx>, state: &mut State<Self::Value>) {
77         self.super_statement(statement, state)
78     }
79
80     fn super_statement(&self, statement: &Statement<'tcx>, state: &mut State<Self::Value>) {
81         match &statement.kind {
82             StatementKind::Assign(box (place, rvalue)) => {
83                 self.handle_assign(*place, rvalue, state);
84             }
85             StatementKind::SetDiscriminant { .. } => {
86                 // Could treat this as writing a constant to a pseudo-place.
87                 // But discriminants are currently not tracked, so we do nothing.
88                 // Related: https://github.com/rust-lang/unsafe-code-guidelines/issues/84
89             }
90             StatementKind::Intrinsic(box intrinsic) => {
91                 self.handle_intrinsic(intrinsic, state);
92             }
93             StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
94                 // We can flood with bottom here, because `StorageLive` makes the local
95                 // uninitialized, and `StorageDead` makes it UB to access.
96                 state.flood_with(Place::from(*local).as_ref(), self.map(), Self::Value::bottom());
97             }
98             StatementKind::Deinit(box place) => {
99                 // The bottom states denotes uninitialized values.
100                 state.flood_with(place.as_ref(), self.map(), Self::Value::bottom());
101             }
102             StatementKind::Nop
103             | StatementKind::Retag(..)
104             | StatementKind::FakeRead(..)
105             | StatementKind::Coverage(..)
106             | StatementKind::AscribeUserType(..) => (),
107         }
108     }
109
110     fn handle_intrinsic(
111         &self,
112         intrinsic: &NonDivergingIntrinsic<'tcx>,
113         state: &mut State<Self::Value>,
114     ) {
115         self.super_intrinsic(intrinsic, state);
116     }
117
118     fn super_intrinsic(
119         &self,
120         intrinsic: &NonDivergingIntrinsic<'tcx>,
121         state: &mut State<Self::Value>,
122     ) {
123         match intrinsic {
124             NonDivergingIntrinsic::Assume(..) => {
125                 // Could use this, but ignoring it is sound.
126             }
127             NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping { dst, .. }) => {
128                 if let Some(place) = dst.place() {
129                     state.flood(place.as_ref(), self.map());
130                 }
131             }
132         }
133     }
134
135     fn handle_assign(
136         &self,
137         target: Place<'tcx>,
138         rvalue: &Rvalue<'tcx>,
139         state: &mut State<Self::Value>,
140     ) {
141         self.super_assign(target, rvalue, state)
142     }
143
144     fn super_assign(
145         &self,
146         target: Place<'tcx>,
147         rvalue: &Rvalue<'tcx>,
148         state: &mut State<Self::Value>,
149     ) {
150         let result = self.handle_rvalue(rvalue, state);
151         state.assign(target.as_ref(), result, self.map());
152     }
153
154     fn handle_rvalue(
155         &self,
156         rvalue: &Rvalue<'tcx>,
157         state: &mut State<Self::Value>,
158     ) -> ValueOrPlaceOrRef<Self::Value> {
159         self.super_rvalue(rvalue, state)
160     }
161
162     fn super_rvalue(
163         &self,
164         rvalue: &Rvalue<'tcx>,
165         state: &mut State<Self::Value>,
166     ) -> ValueOrPlaceOrRef<Self::Value> {
167         match rvalue {
168             Rvalue::Use(operand) => self.handle_operand(operand, state).into(),
169             Rvalue::Ref(_, BorrowKind::Shared, place) => self
170                 .map()
171                 .find(place.as_ref())
172                 .map(ValueOrPlaceOrRef::Ref)
173                 .unwrap_or(ValueOrPlaceOrRef::Unknown),
174             Rvalue::Ref(_, _, place) | Rvalue::AddressOf(_, place) => {
175                 state.flood(place.as_ref(), self.map());
176                 ValueOrPlaceOrRef::Unknown
177             }
178             Rvalue::CopyForDeref(place) => {
179                 self.handle_operand(&Operand::Copy(*place), state).into()
180             }
181             _ => ValueOrPlaceOrRef::Unknown,
182         }
183     }
184
185     fn handle_operand(
186         &self,
187         operand: &Operand<'tcx>,
188         state: &mut State<Self::Value>,
189     ) -> ValueOrPlace<Self::Value> {
190         self.super_operand(operand, state)
191     }
192
193     fn super_operand(
194         &self,
195         operand: &Operand<'tcx>,
196         state: &mut State<Self::Value>,
197     ) -> ValueOrPlace<Self::Value> {
198         match operand {
199             Operand::Constant(box constant) => {
200                 ValueOrPlace::Value(self.handle_constant(constant, state))
201             }
202             Operand::Copy(place) | Operand::Move(place) => {
203                 // Do we want to handle moves differently? Could flood place with bottom.
204                 self.map()
205                     .find(place.as_ref())
206                     .map(ValueOrPlace::Place)
207                     .unwrap_or(ValueOrPlace::Unknown)
208             }
209         }
210     }
211
212     fn handle_constant(
213         &self,
214         constant: &Constant<'tcx>,
215         state: &mut State<Self::Value>,
216     ) -> Self::Value {
217         self.super_constant(constant, state)
218     }
219
220     fn super_constant(
221         &self,
222         _constant: &Constant<'tcx>,
223         _state: &mut State<Self::Value>,
224     ) -> Self::Value {
225         Self::Value::top()
226     }
227
228     /// The effect of a successful function call return should not be
229     /// applied here, see [`Analysis::apply_terminator_effect`].
230     fn handle_terminator(&self, terminator: &Terminator<'tcx>, state: &mut State<Self::Value>) {
231         self.super_terminator(terminator, state)
232     }
233
234     fn super_terminator(&self, terminator: &Terminator<'tcx>, state: &mut State<Self::Value>) {
235         match &terminator.kind {
236             TerminatorKind::Call { .. } | TerminatorKind::InlineAsm { .. } => {
237                 // Effect is applied by `handle_call_return`.
238             }
239             TerminatorKind::Drop { place, .. } => {
240                 // Place can still be accessed after drop, and drop has mutable access to it.
241                 state.flood(place.as_ref(), self.map());
242             }
243             TerminatorKind::DropAndReplace { .. } | TerminatorKind::Yield { .. } => {
244                 // They would have an effect, but are not allowed in this phase.
245                 bug!("encountered disallowed terminator");
246             }
247             _ => {
248                 // The other terminators can be ignored.
249             }
250         }
251     }
252
253     fn handle_call_return(
254         &self,
255         return_places: CallReturnPlaces<'_, 'tcx>,
256         state: &mut State<Self::Value>,
257     ) {
258         self.super_call_return(return_places, state)
259     }
260
261     fn super_call_return(
262         &self,
263         return_places: CallReturnPlaces<'_, 'tcx>,
264         state: &mut State<Self::Value>,
265     ) {
266         return_places.for_each(|place| {
267             state.flood(place.as_ref(), self.map());
268         })
269     }
270
271     fn handle_switch_int(
272         &self,
273         discr: &Operand<'tcx>,
274         apply_edge_effects: &mut impl SwitchIntEdgeEffects<State<Self::Value>>,
275     ) {
276         self.super_switch_int(discr, apply_edge_effects)
277     }
278
279     fn super_switch_int(
280         &self,
281         _discr: &Operand<'tcx>,
282         _apply_edge_effects: &mut impl SwitchIntEdgeEffects<State<Self::Value>>,
283     ) {
284     }
285
286     fn wrap(self) -> ValueAnalysisWrapper<Self>
287     where
288         Self: Sized,
289     {
290         ValueAnalysisWrapper(self)
291     }
292 }
293
294 pub struct ValueAnalysisWrapper<T>(pub T);
295
296 impl<'tcx, T: ValueAnalysis<'tcx>> AnalysisDomain<'tcx> for ValueAnalysisWrapper<T> {
297     type Domain = State<T::Value>;
298
299     type Direction = crate::Forward;
300
301     const NAME: &'static str = T::NAME;
302
303     fn bottom_value(&self, _body: &Body<'tcx>) -> Self::Domain {
304         State(StateData::Unreachable)
305     }
306
307     fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
308         // The initial state maps all tracked places of argument projections to âŠ¤ and the rest to âŠ¥.
309         // This utilizes that reading from an uninitialized place is UB.
310         assert!(matches!(state.0, StateData::Unreachable));
311         let values = IndexVec::from_elem_n(T::Value::bottom(), self.0.map().value_count);
312         *state = State(StateData::Reachable(values));
313         for arg in body.args_iter() {
314             state.flood(PlaceRef { local: arg, projection: &[] }, self.0.map());
315         }
316     }
317 }
318
319 impl<'tcx, T> Analysis<'tcx> for ValueAnalysisWrapper<T>
320 where
321     T: ValueAnalysis<'tcx>,
322 {
323     fn apply_statement_effect(
324         &self,
325         state: &mut Self::Domain,
326         statement: &Statement<'tcx>,
327         _location: Location,
328     ) {
329         if state.is_reachable() {
330             self.0.handle_statement(statement, state);
331         }
332     }
333
334     fn apply_terminator_effect(
335         &self,
336         state: &mut Self::Domain,
337         terminator: &Terminator<'tcx>,
338         _location: Location,
339     ) {
340         if state.is_reachable() {
341             self.0.handle_terminator(terminator, state);
342         }
343     }
344
345     fn apply_call_return_effect(
346         &self,
347         state: &mut Self::Domain,
348         _block: BasicBlock,
349         return_places: crate::CallReturnPlaces<'_, 'tcx>,
350     ) {
351         if state.is_reachable() {
352             self.0.handle_call_return(return_places, state)
353         }
354     }
355
356     fn apply_switch_int_edge_effects(
357         &self,
358         _block: BasicBlock,
359         discr: &Operand<'tcx>,
360         apply_edge_effects: &mut impl SwitchIntEdgeEffects<Self::Domain>,
361     ) {
362         // FIXME: Dataflow framework provides no access to current state here.
363         self.0.handle_switch_int(discr, apply_edge_effects)
364     }
365 }
366
367 rustc_index::newtype_index!(
368     /// This index uniquely identifies a place.
369     ///
370     /// Not every place has a `PlaceIndex`, and not every `PlaceIndex` correspondends to a tracked
371     /// place. However, every tracked place and all places along its projection have a `PlaceIndex`.
372     pub struct PlaceIndex {}
373 );
374
375 rustc_index::newtype_index!(
376     /// This index uniquely identifies a tracked place and therefore a slot in [`State`].
377     ///
378     /// It is an implementation detail of this module.
379     struct ValueIndex {}
380 );
381
382 /// See [`State`].
383 #[derive(PartialEq, Eq, Clone, Debug)]
384 enum StateData<V> {
385     Reachable(IndexVec<ValueIndex, V>),
386     Unreachable,
387 }
388
389 /// The dataflow state for an instance of [`ValueAnalysis`].
390 ///
391 /// Every instance specifies a lattice that represents the possible values of a single tracked
392 /// place. If we call this lattice `V` and set set of tracked places `P`, then a [`State`] is an
393 /// element of `{unreachable} âˆª (P -> V)`. This again forms a lattice, where the bottom element is
394 /// `unreachable` and the top element is the mapping `p â†¦ âŠ¤`. Note that the mapping `p â†¦ âŠ¥` is not
395 /// the bottom element (because joining an unreachable and any other reachable state yields a
396 /// reachable state). All operations on unreachable states are ignored.
397 ///
398 /// Flooding means assigning a value (by default `⊤`) to all tracked projections of a given place.
399 #[derive(PartialEq, Eq, Clone, Debug)]
400 pub struct State<V>(StateData<V>);
401
402 impl<V: Clone + HasTop> State<V> {
403     pub fn is_reachable(&self) -> bool {
404         matches!(&self.0, StateData::Reachable(_))
405     }
406
407     pub fn mark_unreachable(&mut self) {
408         self.0 = StateData::Unreachable;
409     }
410
411     pub fn flood_all(&mut self) {
412         self.flood_all_with(V::top())
413     }
414
415     pub fn flood_all_with(&mut self, value: V) {
416         let StateData::Reachable(values) = &mut self.0 else { return };
417         values.raw.fill(value);
418     }
419
420     pub fn flood_with(&mut self, place: PlaceRef<'_>, map: &Map, value: V) {
421         if let Some(root) = map.find(place) {
422             self.flood_idx_with(root, map, value);
423         }
424     }
425
426     pub fn flood(&mut self, place: PlaceRef<'_>, map: &Map) {
427         self.flood_with(place, map, V::top())
428     }
429
430     pub fn flood_idx_with(&mut self, place: PlaceIndex, map: &Map, value: V) {
431         let StateData::Reachable(values) = &mut self.0 else { return };
432         map.preorder_invoke(place, &mut |place| {
433             if let Some(vi) = map.places[place].value_index {
434                 values[vi] = value.clone();
435             }
436         });
437     }
438
439     pub fn flood_idx(&mut self, place: PlaceIndex, map: &Map) {
440         self.flood_idx_with(place, map, V::top())
441     }
442
443     /// This method assumes that the given places are not overlapping, and that we can therefore
444     /// copy all entries one after another.
445     pub fn assign_place_idx(&mut self, target: PlaceIndex, source: PlaceIndex, map: &Map) {
446         let StateData::Reachable(values) = &mut self.0 else { return };
447         // If both places are tracked, we copy the value to the target. If the target is tracked,
448         // but the source is not, we have to invalidate the value in target. If the target is not
449         // tracked, then we don't have to do anything.
450         if let Some(target_value) = map.places[target].value_index {
451             if let Some(source_value) = map.places[source].value_index {
452                 values[target_value] = values[source_value].clone();
453             } else {
454                 values[target_value] = V::top();
455             }
456         }
457         for target_child in map.children(target) {
458             // Try to find corresponding child and recurse. Reasoning is similar as above.
459             let projection = map.places[target_child].proj_elem.unwrap();
460             if let Some(source_child) = map.projections.get(&(source, projection)) {
461                 self.assign_place_idx(target_child, *source_child, map);
462             } else {
463                 self.flood_idx(target_child, map);
464             }
465         }
466     }
467
468     pub fn assign(&mut self, target: PlaceRef<'_>, result: ValueOrPlaceOrRef<V>, map: &Map) {
469         if let Some(target) = map.find(target) {
470             self.assign_idx(target, result, map);
471         } else {
472             // We don't track this place nor any projections, assignment can be ignored.
473         }
474     }
475
476     pub fn assign_idx(&mut self, target: PlaceIndex, result: ValueOrPlaceOrRef<V>, map: &Map) {
477         match result {
478             ValueOrPlaceOrRef::Value(value) => {
479                 // First flood the target place in case we also track any projections (although
480                 // this scenario is currently not well-supported by the API).
481                 self.flood_idx(target, map);
482                 let StateData::Reachable(values) = &mut self.0 else { return };
483                 if let Some(value_index) = map.places[target].value_index {
484                     values[value_index] = value;
485                 }
486             }
487             ValueOrPlaceOrRef::Place(source) => self.assign_place_idx(target, source, map),
488             ValueOrPlaceOrRef::Ref(source) => {
489                 let StateData::Reachable(values) = &mut self.0 else { return };
490                 if let Some(value_index) = map.places[target].value_index {
491                     values[value_index] = V::top();
492                 }
493                 if let Some(target_deref) = map.apply_elem(target, ProjElem::Deref) {
494                     self.assign_place_idx(target_deref, source, map);
495                 }
496             }
497             ValueOrPlaceOrRef::Unknown => {
498                 self.flood_idx(target, map);
499             }
500         }
501     }
502
503     pub fn get(&self, place: PlaceRef<'_>, map: &Map) -> V {
504         map.find(place).map(|place| self.get_idx(place, map)).unwrap_or(V::top())
505     }
506
507     pub fn get_idx(&self, place: PlaceIndex, map: &Map) -> V {
508         match &self.0 {
509             StateData::Reachable(values) => {
510                 map.places[place].value_index.map(|v| values[v].clone()).unwrap_or(V::top())
511             }
512             StateData::Unreachable => V::top(),
513         }
514     }
515 }
516
517 impl<V: JoinSemiLattice + Clone> JoinSemiLattice for State<V> {
518     fn join(&mut self, other: &Self) -> bool {
519         match (&mut self.0, &other.0) {
520             (_, StateData::Unreachable) => false,
521             (StateData::Unreachable, _) => {
522                 *self = other.clone();
523                 true
524             }
525             (StateData::Reachable(this), StateData::Reachable(other)) => this.join(other),
526         }
527     }
528 }
529
530 #[derive(Debug)]
531 pub struct Map {
532     locals: IndexVec<Local, Option<PlaceIndex>>,
533     projections: FxHashMap<(PlaceIndex, ProjElem), PlaceIndex>,
534     places: IndexVec<PlaceIndex, PlaceInfo>,
535     value_count: usize,
536 }
537
538 impl Map {
539     pub fn new() -> Self {
540         Self {
541             locals: IndexVec::new(),
542             projections: FxHashMap::default(),
543             places: IndexVec::new(),
544             value_count: 0,
545         }
546     }
547
548     /// Register all places with suitable types up to a certain derefence depth (to prevent cycles).
549     pub fn register_with_filter<'tcx>(
550         &mut self,
551         tcx: TyCtxt<'tcx>,
552         source: &impl HasLocalDecls<'tcx>,
553         max_derefs: u32,
554         mut filter: impl FnMut(Ty<'tcx>) -> bool,
555     ) {
556         let mut projection = Vec::new();
557         for (local, decl) in source.local_decls().iter_enumerated() {
558             self.register_with_filter_rec(
559                 tcx,
560                 max_derefs,
561                 local,
562                 &mut projection,
563                 decl.ty,
564                 &mut filter,
565             );
566         }
567     }
568
569     fn register_with_filter_rec<'tcx>(
570         &mut self,
571         tcx: TyCtxt<'tcx>,
572         max_derefs: u32,
573         local: Local,
574         projection: &mut Vec<PlaceElem<'tcx>>,
575         ty: Ty<'tcx>,
576         filter: &mut impl FnMut(Ty<'tcx>) -> bool,
577     ) {
578         if filter(ty) {
579             // This might fail if `ty` is not scalar.
580             let _ = self.register_with_ty(local, projection, ty);
581         }
582         if max_derefs > 0 {
583             if let Some(ty::TypeAndMut { ty, .. }) = ty.builtin_deref(false) {
584                 projection.push(PlaceElem::Deref);
585                 self.register_with_filter_rec(tcx, max_derefs - 1, local, projection, ty, filter);
586                 projection.pop();
587             }
588         }
589         iter_fields(ty, tcx, |variant, field, ty| {
590             if variant.is_some() {
591                 // Downcasts are currently not supported.
592                 return;
593             }
594             projection.push(PlaceElem::Field(field, ty));
595             self.register_with_filter_rec(tcx, max_derefs, local, projection, ty, filter);
596             projection.pop();
597         });
598     }
599
600     fn make_place<'tcx>(
601         &mut self,
602         local: Local,
603         projection: &[PlaceElem<'tcx>],
604     ) -> Result<PlaceIndex, ()> {
605         // Get the base index of the local.
606         let mut index =
607             *self.locals.get_or_insert_with(local, || self.places.push(PlaceInfo::new(None)));
608
609         // Apply the projection.
610         for &elem in projection {
611             match elem {
612                 PlaceElem::Downcast(..) => return Err(()),
613                 _ => (),
614             }
615             let elem = elem.try_into()?;
616             index = *self.projections.entry((index, elem)).or_insert_with(|| {
617                 // Prepend new child to the linked list.
618                 let next = self.places.push(PlaceInfo::new(Some(elem)));
619                 self.places[next].next_sibling = self.places[index].first_child;
620                 self.places[index].first_child = Some(next);
621                 next
622             });
623         }
624
625         Ok(index)
626     }
627
628     pub fn register<'tcx>(
629         &mut self,
630         local: Local,
631         projection: &[PlaceElem<'tcx>],
632         decls: &impl HasLocalDecls<'tcx>,
633         tcx: TyCtxt<'tcx>,
634     ) -> Result<(), ()> {
635         projection
636             .iter()
637             .fold(PlaceTy::from_ty(decls.local_decls()[local].ty), |place_ty, &elem| {
638                 place_ty.projection_ty(tcx, elem)
639             });
640
641         let place_ty = Place::ty_from(local, projection, decls, tcx);
642         if place_ty.variant_index.is_some() {
643             return Err(());
644         }
645         self.register_with_ty(local, projection, place_ty.ty)
646     }
647
648     fn register_with_ty<'tcx>(
649         &mut self,
650         local: Local,
651         projection: &[PlaceElem<'tcx>],
652         ty: Ty<'tcx>,
653     ) -> Result<(), ()> {
654         if !ty.is_scalar() {
655             // Currently, only scalar types are allowed, because they are atomic
656             // and therefore do not require invalidation of parent places.
657             return Err(());
658         }
659
660         let place = self.make_place(local, projection)?;
661
662         // Allocate a value slot if it doesn't have one.
663         if self.places[place].value_index.is_none() {
664             self.places[place].value_index = Some(self.value_count.into());
665             self.value_count += 1;
666         }
667
668         Ok(())
669     }
670
671     pub fn apply_elem(&self, place: PlaceIndex, elem: ProjElem) -> Option<PlaceIndex> {
672         self.projections.get(&(place, elem)).copied()
673     }
674
675     pub fn find(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
676         let mut index = *self.locals.get(place.local)?.as_ref()?;
677
678         for &elem in place.projection {
679             index = self.apply_elem(index, elem.try_into().ok()?)?;
680         }
681
682         Some(index)
683     }
684
685     pub fn children(&self, parent: PlaceIndex) -> impl Iterator<Item = PlaceIndex> + '_ {
686         Children::new(self, parent)
687     }
688
689     pub fn preorder_invoke(&self, root: PlaceIndex, f: &mut impl FnMut(PlaceIndex)) {
690         f(root);
691         for child in self.children(root) {
692             self.preorder_invoke(child, f);
693         }
694     }
695 }
696
697 #[derive(Debug)]
698 struct PlaceInfo {
699     next_sibling: Option<PlaceIndex>,
700     first_child: Option<PlaceIndex>,
701     /// The projection used to go from parent to this node (only None for root).
702     proj_elem: Option<ProjElem>,
703     value_index: Option<ValueIndex>,
704 }
705
706 impl PlaceInfo {
707     fn new(proj_elem: Option<ProjElem>) -> Self {
708         Self { next_sibling: None, first_child: None, proj_elem, value_index: None }
709     }
710 }
711
712 struct Children<'a> {
713     map: &'a Map,
714     next: Option<PlaceIndex>,
715 }
716
717 impl<'a> Children<'a> {
718     fn new(map: &'a Map, parent: PlaceIndex) -> Self {
719         Self { map, next: map.places[parent].first_child }
720     }
721 }
722
723 impl<'a> Iterator for Children<'a> {
724     type Item = PlaceIndex;
725
726     fn next(&mut self) -> Option<Self::Item> {
727         match self.next {
728             Some(child) => {
729                 self.next = self.map.places[child].next_sibling;
730                 Some(child)
731             }
732             None => None,
733         }
734     }
735 }
736
737 // FIXME: See if we can get rid of `Unknown`.
738 pub enum ValueOrPlace<V> {
739     Value(V),
740     Place(PlaceIndex),
741     Unknown,
742 }
743
744 pub enum ValueOrPlaceOrRef<V> {
745     Value(V),
746     Place(PlaceIndex),
747     Ref(PlaceIndex),
748     Unknown,
749 }
750
751 impl<V> From<ValueOrPlace<V>> for ValueOrPlaceOrRef<V> {
752     fn from(x: ValueOrPlace<V>) -> Self {
753         match x {
754             ValueOrPlace::Value(value) => ValueOrPlaceOrRef::Value(value),
755             ValueOrPlace::Place(place) => ValueOrPlaceOrRef::Place(place),
756             ValueOrPlace::Unknown => ValueOrPlaceOrRef::Unknown,
757         }
758     }
759 }
760
761 pub trait HasBottom {
762     fn bottom() -> Self;
763 }
764
765 pub trait HasTop {
766     fn top() -> Self;
767 }
768
769 impl<V> HasBottom for FlatSet<V> {
770     fn bottom() -> Self {
771         Self::Bottom
772     }
773 }
774
775 impl<V> HasTop for FlatSet<V> {
776     fn top() -> Self {
777         Self::Top
778     }
779 }
780
781 /// Currently, we only track places through deref and field projections.
782 ///
783 /// For now, downcast is not allowed due to aliasing between variants (see #101168).
784 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
785 pub enum ProjElem {
786     Deref,
787     Field(Field),
788 }
789
790 impl<V, T> TryFrom<ProjectionElem<V, T>> for ProjElem {
791     type Error = ();
792
793     fn try_from(value: ProjectionElem<V, T>) -> Result<Self, Self::Error> {
794         match value {
795             ProjectionElem::Deref => Ok(ProjElem::Deref),
796             ProjectionElem::Field(field, _) => Ok(ProjElem::Field(field)),
797             _ => Err(()),
798         }
799     }
800 }
801
802 fn iter_fields<'tcx>(
803     ty: Ty<'tcx>,
804     tcx: TyCtxt<'tcx>,
805     mut f: impl FnMut(Option<VariantIdx>, Field, Ty<'tcx>),
806 ) {
807     match ty.kind() {
808         ty::Tuple(list) => {
809             for (field, ty) in list.iter().enumerate() {
810                 f(None, field.into(), ty);
811             }
812         }
813         ty::Adt(def, substs) => {
814             for (v_index, v_def) in def.variants().iter_enumerated() {
815                 for (f_index, f_def) in v_def.fields.iter().enumerate() {
816                     let field_ty = f_def.ty(tcx, substs);
817                     let field_ty = tcx
818                         .try_normalize_erasing_regions(ty::ParamEnv::reveal_all(), field_ty)
819                         .unwrap_or(field_ty);
820                     f(Some(v_index), f_index.into(), field_ty);
821                 }
822             }
823         }
824         ty::Closure(_, substs) => {
825             iter_fields(substs.as_closure().tupled_upvars_ty(), tcx, f);
826         }
827         _ => (),
828     }
829 }
830
831 fn debug_with_context_rec<V: Debug + Eq>(
832     place: PlaceIndex,
833     place_str: &str,
834     new: &IndexVec<ValueIndex, V>,
835     old: Option<&IndexVec<ValueIndex, V>>,
836     map: &Map,
837     f: &mut Formatter<'_>,
838 ) -> std::fmt::Result {
839     if let Some(value) = map.places[place].value_index {
840         match old {
841             None => writeln!(f, "{}: {:?}", place_str, new[value])?,
842             Some(old) => {
843                 if new[value] != old[value] {
844                     writeln!(f, "\u{001f}-{}: {:?}", place_str, old[value])?;
845                     writeln!(f, "\u{001f}+{}: {:?}", place_str, new[value])?;
846                 }
847             }
848         }
849     }
850
851     for child in map.children(place) {
852         let info_elem = map.places[child].proj_elem.unwrap();
853         let child_place_str = match info_elem {
854             ProjElem::Deref => format!("*{}", place_str),
855             ProjElem::Field(field) => {
856                 if place_str.starts_with("*") {
857                     format!("({}).{}", place_str, field.index())
858                 } else {
859                     format!("{}.{}", place_str, field.index())
860                 }
861             }
862         };
863         debug_with_context_rec(child, &child_place_str, new, old, map, f)?;
864     }
865
866     Ok(())
867 }
868
869 fn debug_with_context<V: Debug + Eq>(
870     new: &IndexVec<ValueIndex, V>,
871     old: Option<&IndexVec<ValueIndex, V>>,
872     map: &Map,
873     f: &mut Formatter<'_>,
874 ) -> std::fmt::Result {
875     for (local, place) in map.locals.iter_enumerated() {
876         if let Some(place) = place {
877             debug_with_context_rec(*place, &format!("{:?}", local), new, old, map, f)?;
878         }
879     }
880     Ok(())
881 }
882
883 impl<'tcx, T> DebugWithContext<ValueAnalysisWrapper<T>> for State<T::Value>
884 where
885     T: ValueAnalysis<'tcx>,
886     T::Value: Debug,
887 {
888     fn fmt_with(&self, ctxt: &ValueAnalysisWrapper<T>, f: &mut Formatter<'_>) -> std::fmt::Result {
889         match &self.0 {
890             StateData::Reachable(values) => debug_with_context(values, None, ctxt.0.map(), f),
891             StateData::Unreachable => write!(f, "unreachable"),
892         }
893     }
894
895     fn fmt_diff_with(
896         &self,
897         old: &Self,
898         ctxt: &ValueAnalysisWrapper<T>,
899         f: &mut Formatter<'_>,
900     ) -> std::fmt::Result {
901         match (&self.0, &old.0) {
902             (StateData::Reachable(this), StateData::Reachable(old)) => {
903                 debug_with_context(this, Some(old), ctxt.0.map(), f)
904             }
905             _ => Ok(()), // Consider printing something here.
906         }
907     }
908 }