1 //! This module provides a framework on top of the normal MIR dataflow framework to simplify the
2 //! implementation of analyses that track information about the values stored in certain places.
3 //! We are using the term "place" here to refer to a `mir::Place` (a place expression) instead of
4 //! an `interpret::Place` (a memory location).
6 //! The default methods of [`ValueAnalysis`] (prefixed with `super_` instead of `handle_`)
7 //! provide some behavior that should be valid for all abstract domains that are based only on the
8 //! value stored in a certain place. On top of these default rules, an implementation should
9 //! override some of the `handle_` methods. For an example, see `ConstAnalysis`.
11 //! An implementation must also provide a [`Map`]. Before the analysis begins, all places that
12 //! should be tracked during the analysis must be registered. During the analysis, no new places
13 //! can be registered. The [`State`] can be queried to retrieve the abstract value stored for a
14 //! certain place by passing the map.
16 //! This framework is currently experimental. Originally, it supported shared references and enum
17 //! variants. However, it was discovered that both of these were unsound, and especially references
18 //! had subtle but serious issues. In the future, they could be added back in, but we should clarify
19 //! the rules for optimizations that rely on the aliasing model first.
24 //! - The bottom state denotes uninitialized memory. Because we are only doing a sound approximation
25 //! of the actual execution, we can also use this state for places where access would be UB.
27 //! - The assignment logic in `State::assign_place_idx` assumes that the places are non-overlapping,
28 //! or identical. Note that this refers to place expressions, not memory locations.
30 //! - Currently, places that have their reference taken cannot be tracked. Although this would be
31 //! possible, it has to rely on some aliasing model, which we are not ready to commit to yet.
32 //! Because of that, we can assume that the only way to change the value behind a tracked place is
33 //! by direct assignment.
35 use std::fmt::{Debug, Formatter};
37 use rustc_data_structures::fx::FxHashMap;
38 use rustc_index::vec::IndexVec;
39 use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
40 use rustc_middle::mir::*;
41 use rustc_middle::ty::{self, Ty, TyCtxt};
42 use rustc_target::abi::VariantIdx;
44 use crate::lattice::{HasBottom, HasTop};
46 fmt::DebugWithContext, Analysis, AnalysisDomain, CallReturnPlaces, JoinSemiLattice,
50 pub trait ValueAnalysis<'tcx> {
51 /// For each place of interest, the analysis tracks a value of the given type.
52 type Value: Clone + JoinSemiLattice + HasBottom + HasTop;
54 const NAME: &'static str;
56 fn map(&self) -> ⤅
58 fn handle_statement(&self, statement: &Statement<'tcx>, state: &mut State<Self::Value>) {
59 self.super_statement(statement, state)
62 fn super_statement(&self, statement: &Statement<'tcx>, state: &mut State<Self::Value>) {
63 match &statement.kind {
64 StatementKind::Assign(box (place, rvalue)) => {
65 self.handle_assign(*place, rvalue, state);
67 StatementKind::SetDiscriminant { .. } => {
68 // Could treat this as writing a constant to a pseudo-place.
69 // But discriminants are currently not tracked, so we do nothing.
70 // Related: https://github.com/rust-lang/unsafe-code-guidelines/issues/84
72 StatementKind::Intrinsic(box intrinsic) => {
73 self.handle_intrinsic(intrinsic, state);
75 StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
76 // StorageLive leaves the local in an uninitialized state.
77 // StorageDead makes it UB to access the local afterwards.
78 state.flood_with(Place::from(*local).as_ref(), self.map(), Self::Value::bottom());
80 StatementKind::Deinit(box place) => {
81 // Deinit makes the place uninitialized.
82 state.flood_with(place.as_ref(), self.map(), Self::Value::bottom());
84 StatementKind::Retag(..) => {
85 // We don't track references.
88 | StatementKind::FakeRead(..)
89 | StatementKind::Coverage(..)
90 | StatementKind::AscribeUserType(..) => (),
96 intrinsic: &NonDivergingIntrinsic<'tcx>,
97 state: &mut State<Self::Value>,
99 self.super_intrinsic(intrinsic, state);
104 intrinsic: &NonDivergingIntrinsic<'tcx>,
105 state: &mut State<Self::Value>,
108 NonDivergingIntrinsic::Assume(..) => {
109 // Could use this, but ignoring it is sound.
111 NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping { dst, .. }) => {
112 if let Some(place) = dst.place() {
113 state.flood(place.as_ref(), self.map());
122 rvalue: &Rvalue<'tcx>,
123 state: &mut State<Self::Value>,
125 self.super_assign(target, rvalue, state)
131 rvalue: &Rvalue<'tcx>,
132 state: &mut State<Self::Value>,
134 let result = self.handle_rvalue(rvalue, state);
135 state.assign(target.as_ref(), result, self.map());
140 rvalue: &Rvalue<'tcx>,
141 state: &mut State<Self::Value>,
142 ) -> ValueOrPlace<Self::Value> {
143 self.super_rvalue(rvalue, state)
148 rvalue: &Rvalue<'tcx>,
149 state: &mut State<Self::Value>,
150 ) -> ValueOrPlace<Self::Value> {
152 Rvalue::Use(operand) => self.handle_operand(operand, state),
153 Rvalue::CopyForDeref(place) => self.handle_operand(&Operand::Copy(*place), state),
154 Rvalue::Ref(..) | Rvalue::AddressOf(..) => {
155 // We don't track such places.
159 | Rvalue::ThreadLocalRef(..)
162 | Rvalue::BinaryOp(..)
163 | Rvalue::CheckedBinaryOp(..)
164 | Rvalue::NullaryOp(..)
165 | Rvalue::UnaryOp(..)
166 | Rvalue::Discriminant(..)
167 | Rvalue::Aggregate(..)
168 | Rvalue::ShallowInitBox(..) => {
169 // No modification is possible through these r-values.
177 operand: &Operand<'tcx>,
178 state: &mut State<Self::Value>,
179 ) -> ValueOrPlace<Self::Value> {
180 self.super_operand(operand, state)
185 operand: &Operand<'tcx>,
186 state: &mut State<Self::Value>,
187 ) -> ValueOrPlace<Self::Value> {
189 Operand::Constant(box constant) => {
190 ValueOrPlace::Value(self.handle_constant(constant, state))
192 Operand::Copy(place) | Operand::Move(place) => {
193 // On move, we would ideally flood the place with bottom. But with the current
194 // framework this is not possible (similar to `InterpCx::eval_operand`).
196 .find(place.as_ref())
197 .map(ValueOrPlace::Place)
198 .unwrap_or(ValueOrPlace::top())
205 constant: &Constant<'tcx>,
206 state: &mut State<Self::Value>,
208 self.super_constant(constant, state)
213 _constant: &Constant<'tcx>,
214 _state: &mut State<Self::Value>,
219 /// The effect of a successful function call return should not be
220 /// applied here, see [`Analysis::apply_terminator_effect`].
221 fn handle_terminator(&self, terminator: &Terminator<'tcx>, state: &mut State<Self::Value>) {
222 self.super_terminator(terminator, state)
225 fn super_terminator(&self, terminator: &Terminator<'tcx>, _state: &mut State<Self::Value>) {
226 match &terminator.kind {
227 TerminatorKind::Call { .. } | TerminatorKind::InlineAsm { .. } => {
228 // Effect is applied by `handle_call_return`.
230 TerminatorKind::Drop { .. } => {
231 // We don't track dropped places.
233 TerminatorKind::DropAndReplace { .. } | TerminatorKind::Yield { .. } => {
234 // They would have an effect, but are not allowed in this phase.
235 bug!("encountered disallowed terminator");
237 TerminatorKind::Goto { .. }
238 | TerminatorKind::SwitchInt { .. }
239 | TerminatorKind::Resume
240 | TerminatorKind::Abort
241 | TerminatorKind::Return
242 | TerminatorKind::Unreachable
243 | TerminatorKind::Assert { .. }
244 | TerminatorKind::GeneratorDrop
245 | TerminatorKind::FalseEdge { .. }
246 | TerminatorKind::FalseUnwind { .. } => {
247 // These terminators have no effect on the analysis.
252 fn handle_call_return(
254 return_places: CallReturnPlaces<'_, 'tcx>,
255 state: &mut State<Self::Value>,
257 self.super_call_return(return_places, state)
260 fn super_call_return(
262 return_places: CallReturnPlaces<'_, 'tcx>,
263 state: &mut State<Self::Value>,
265 return_places.for_each(|place| {
266 state.flood(place.as_ref(), self.map());
270 fn handle_switch_int(
272 discr: &Operand<'tcx>,
273 apply_edge_effects: &mut impl SwitchIntEdgeEffects<State<Self::Value>>,
275 self.super_switch_int(discr, apply_edge_effects)
280 _discr: &Operand<'tcx>,
281 _apply_edge_effects: &mut impl SwitchIntEdgeEffects<State<Self::Value>>,
285 fn wrap(self) -> ValueAnalysisWrapper<Self>
289 ValueAnalysisWrapper(self)
293 pub struct ValueAnalysisWrapper<T>(pub T);
295 impl<'tcx, T: ValueAnalysis<'tcx>> AnalysisDomain<'tcx> for ValueAnalysisWrapper<T> {
296 type Domain = State<T::Value>;
298 type Direction = crate::Forward;
300 const NAME: &'static str = T::NAME;
302 fn bottom_value(&self, _body: &Body<'tcx>) -> Self::Domain {
303 State(StateData::Unreachable)
306 fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
307 // The initial state maps all tracked places of argument projections to ⊤ and the rest to ⊥.
308 assert!(matches!(state.0, StateData::Unreachable));
309 let values = IndexVec::from_elem_n(T::Value::bottom(), self.0.map().value_count);
310 *state = State(StateData::Reachable(values));
311 for arg in body.args_iter() {
312 state.flood(PlaceRef { local: arg, projection: &[] }, self.0.map());
317 impl<'tcx, T> Analysis<'tcx> for ValueAnalysisWrapper<T>
319 T: ValueAnalysis<'tcx>,
321 fn apply_statement_effect(
323 state: &mut Self::Domain,
324 statement: &Statement<'tcx>,
327 if state.is_reachable() {
328 self.0.handle_statement(statement, state);
332 fn apply_terminator_effect(
334 state: &mut Self::Domain,
335 terminator: &Terminator<'tcx>,
338 if state.is_reachable() {
339 self.0.handle_terminator(terminator, state);
343 fn apply_call_return_effect(
345 state: &mut Self::Domain,
347 return_places: crate::CallReturnPlaces<'_, 'tcx>,
349 if state.is_reachable() {
350 self.0.handle_call_return(return_places, state)
354 fn apply_switch_int_edge_effects(
357 discr: &Operand<'tcx>,
358 apply_edge_effects: &mut impl SwitchIntEdgeEffects<Self::Domain>,
360 // FIXME: Dataflow framework provides no access to current state here.
361 self.0.handle_switch_int(discr, apply_edge_effects)
365 rustc_index::newtype_index!(
366 /// This index uniquely identifies a place.
368 /// Not every place has a `PlaceIndex`, and not every `PlaceIndex` correspondends to a tracked
369 /// place. However, every tracked place and all places along its projection have a `PlaceIndex`.
370 pub struct PlaceIndex {}
373 rustc_index::newtype_index!(
374 /// This index uniquely identifies a tracked place and therefore a slot in [`State`].
376 /// It is an implementation detail of this module.
381 #[derive(PartialEq, Eq, Debug)]
383 Reachable(IndexVec<ValueIndex, V>),
387 impl<V: Clone> Clone for StateData<V> {
388 fn clone(&self) -> Self {
390 Self::Reachable(x) => Self::Reachable(x.clone()),
391 Self::Unreachable => Self::Unreachable,
395 fn clone_from(&mut self, source: &Self) {
396 match (&mut *self, source) {
397 (Self::Reachable(x), Self::Reachable(y)) => {
398 // We go through `raw` here, because `IndexVec` currently has a naive `clone_from`.
399 x.raw.clone_from(&y.raw);
401 _ => *self = source.clone(),
406 /// The dataflow state for an instance of [`ValueAnalysis`].
408 /// Every instance specifies a lattice that represents the possible values of a single tracked
409 /// place. If we call this lattice `V` and set set of tracked places `P`, then a [`State`] is an
410 /// element of `{unreachable} ∪ (P -> V)`. This again forms a lattice, where the bottom element is
411 /// `unreachable` and the top element is the mapping `p ↦ ⊤`. Note that the mapping `p ↦ ⊥` is not
412 /// the bottom element (because joining an unreachable and any other reachable state yields a
413 /// reachable state). All operations on unreachable states are ignored.
415 /// Flooding means assigning a value (by default `⊤`) to all tracked projections of a given place.
416 #[derive(PartialEq, Eq, Debug)]
417 pub struct State<V>(StateData<V>);
419 impl<V: Clone> Clone for State<V> {
420 fn clone(&self) -> Self {
424 fn clone_from(&mut self, source: &Self) {
425 self.0.clone_from(&source.0);
429 impl<V: Clone + HasTop + HasBottom> State<V> {
430 pub fn is_reachable(&self) -> bool {
431 matches!(&self.0, StateData::Reachable(_))
434 pub fn mark_unreachable(&mut self) {
435 self.0 = StateData::Unreachable;
438 pub fn flood_all(&mut self) {
439 self.flood_all_with(V::top())
442 pub fn flood_all_with(&mut self, value: V) {
443 let StateData::Reachable(values) = &mut self.0 else { return };
444 values.raw.fill(value);
447 pub fn flood_with(&mut self, place: PlaceRef<'_>, map: &Map, value: V) {
448 if let Some(root) = map.find(place) {
449 self.flood_idx_with(root, map, value);
453 pub fn flood(&mut self, place: PlaceRef<'_>, map: &Map) {
454 self.flood_with(place, map, V::top())
457 pub fn flood_idx_with(&mut self, place: PlaceIndex, map: &Map, value: V) {
458 let StateData::Reachable(values) = &mut self.0 else { return };
459 map.preorder_invoke(place, &mut |place| {
460 if let Some(vi) = map.places[place].value_index {
461 values[vi] = value.clone();
466 pub fn flood_idx(&mut self, place: PlaceIndex, map: &Map) {
467 self.flood_idx_with(place, map, V::top())
470 /// Copies `source` to `target`, including all tracked places beneath.
472 /// If `target` contains a place that is not contained in `source`, it will be overwritten with
473 /// Top. Also, because this will copy all entries one after another, it may only be used for
474 /// places that are non-overlapping or identical.
475 pub fn assign_place_idx(&mut self, target: PlaceIndex, source: PlaceIndex, map: &Map) {
476 let StateData::Reachable(values) = &mut self.0 else { return };
478 // If both places are tracked, we copy the value to the target. If the target is tracked,
479 // but the source is not, we have to invalidate the value in target. If the target is not
480 // tracked, then we don't have to do anything.
481 if let Some(target_value) = map.places[target].value_index {
482 if let Some(source_value) = map.places[source].value_index {
483 values[target_value] = values[source_value].clone();
485 values[target_value] = V::top();
488 for target_child in map.children(target) {
489 // Try to find corresponding child and recurse. Reasoning is similar as above.
490 let projection = map.places[target_child].proj_elem.unwrap();
491 if let Some(source_child) = map.projections.get(&(source, projection)) {
492 self.assign_place_idx(target_child, *source_child, map);
494 self.flood_idx(target_child, map);
499 pub fn assign(&mut self, target: PlaceRef<'_>, result: ValueOrPlace<V>, map: &Map) {
500 if let Some(target) = map.find(target) {
501 self.assign_idx(target, result, map);
503 // We don't track this place nor any projections, assignment can be ignored.
507 pub fn assign_idx(&mut self, target: PlaceIndex, result: ValueOrPlace<V>, map: &Map) {
509 ValueOrPlace::Value(value) => {
510 // First flood the target place in case we also track any projections (although
511 // this scenario is currently not well-supported by the API).
512 self.flood_idx(target, map);
513 let StateData::Reachable(values) = &mut self.0 else { return };
514 if let Some(value_index) = map.places[target].value_index {
515 values[value_index] = value;
518 ValueOrPlace::Place(source) => self.assign_place_idx(target, source, map),
522 /// Retrieve the value stored for a place, or ⊤ if it is not tracked.
523 pub fn get(&self, place: PlaceRef<'_>, map: &Map) -> V {
524 map.find(place).map(|place| self.get_idx(place, map)).unwrap_or(V::top())
527 /// Retrieve the value stored for a place index, or ⊤ if it is not tracked.
528 pub fn get_idx(&self, place: PlaceIndex, map: &Map) -> V {
530 StateData::Reachable(values) => {
531 map.places[place].value_index.map(|v| values[v].clone()).unwrap_or(V::top())
533 StateData::Unreachable => {
534 // Because this is unreachable, we can return any value we want.
541 impl<V: JoinSemiLattice + Clone> JoinSemiLattice for State<V> {
542 fn join(&mut self, other: &Self) -> bool {
543 match (&mut self.0, &other.0) {
544 (_, StateData::Unreachable) => false,
545 (StateData::Unreachable, _) => {
546 *self = other.clone();
549 (StateData::Reachable(this), StateData::Reachable(other)) => this.join(other),
554 /// Partial mapping from [`Place`] to [`PlaceIndex`], where some places also have a [`ValueIndex`].
556 /// This data structure essentially maintains a tree of places and their projections. Some
557 /// additional bookkeeping is done, to speed up traversal over this tree:
558 /// - For iteration, every [`PlaceInfo`] contains an intrusive linked list of its children.
559 /// - To directly get the child for a specific projection, there is a `projections` map.
562 locals: IndexVec<Local, Option<PlaceIndex>>,
563 projections: FxHashMap<(PlaceIndex, TrackElem), PlaceIndex>,
564 places: IndexVec<PlaceIndex, PlaceInfo>,
571 locals: IndexVec::new(),
572 projections: FxHashMap::default(),
573 places: IndexVec::new(),
578 /// Returns a map that only tracks places whose type passes the filter.
580 /// This is currently the only way to create a [`Map`]. The way in which the tracked places are
581 /// chosen is an implementation detail and may not be relied upon (other than that their type
582 /// passes the filter).
583 #[instrument(skip_all, level = "debug")]
584 pub fn from_filter<'tcx>(
587 filter: impl FnMut(Ty<'tcx>) -> bool,
589 let mut map = Self::new();
590 let exclude = excluded_locals(body);
591 map.register_with_filter(tcx, body, filter, &exclude);
592 debug!("registered {} places ({} nodes in total)", map.value_count, map.places.len());
596 /// Register all non-excluded places that pass the filter.
597 fn register_with_filter<'tcx>(
601 mut filter: impl FnMut(Ty<'tcx>) -> bool,
602 exclude: &IndexVec<Local, bool>,
604 // We use this vector as stack, pushing and popping projections.
605 let mut projection = Vec::new();
606 for (local, decl) in body.local_decls.iter_enumerated() {
608 self.register_with_filter_rec(tcx, local, &mut projection, decl.ty, &mut filter);
613 /// Potentially register the (local, projection) place and its fields, recursively.
615 /// Invariant: The projection must only contain fields.
616 fn register_with_filter_rec<'tcx>(
620 projection: &mut Vec<PlaceElem<'tcx>>,
622 filter: &mut impl FnMut(Ty<'tcx>) -> bool,
624 // Note: The framework supports only scalars for now.
625 if filter(ty) && ty.is_scalar() {
626 // We know that the projection only contains trackable elements.
627 let place = self.make_place(local, projection).unwrap();
629 // Allocate a value slot if it doesn't have one.
630 if self.places[place].value_index.is_none() {
631 self.places[place].value_index = Some(self.value_count.into());
632 self.value_count += 1;
636 // Recurse with all fields of this place.
637 iter_fields(ty, tcx, |variant, field, ty| {
638 if variant.is_some() {
639 // Downcasts are currently not supported.
642 projection.push(PlaceElem::Field(field, ty));
643 self.register_with_filter_rec(tcx, local, projection, ty, filter);
648 /// Tries to add the place to the map, without allocating a value slot.
650 /// Can fail if the projection contains non-trackable elements.
654 projection: &[PlaceElem<'tcx>],
655 ) -> Result<PlaceIndex, ()> {
656 // Get the base index of the local.
658 *self.locals.get_or_insert_with(local, || self.places.push(PlaceInfo::new(None)));
660 // Apply the projection.
661 for &elem in projection {
662 let elem = elem.try_into()?;
663 index = *self.projections.entry((index, elem)).or_insert_with(|| {
664 // Prepend new child to the linked list.
665 let next = self.places.push(PlaceInfo::new(Some(elem)));
666 self.places[next].next_sibling = self.places[index].first_child;
667 self.places[index].first_child = Some(next);
675 /// Returns the number of tracked places, i.e., those for which a value can be stored.
676 pub fn tracked_places(&self) -> usize {
680 /// Applies a single projection element, yielding the corresponding child.
681 pub fn apply(&self, place: PlaceIndex, elem: TrackElem) -> Option<PlaceIndex> {
682 self.projections.get(&(place, elem)).copied()
685 /// Locates the given place, if it exists in the tree.
686 pub fn find(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
687 let mut index = *self.locals.get(place.local)?.as_ref()?;
689 for &elem in place.projection {
690 index = self.apply(index, elem.try_into().ok()?)?;
696 /// Iterate over all direct children.
697 pub fn children(&self, parent: PlaceIndex) -> impl Iterator<Item = PlaceIndex> + '_ {
698 Children::new(self, parent)
701 /// Invoke a function on the given place and all descendants.
702 pub fn preorder_invoke(&self, root: PlaceIndex, f: &mut impl FnMut(PlaceIndex)) {
704 for child in self.children(root) {
705 self.preorder_invoke(child, f);
710 /// This is the information tracked for every [`PlaceIndex`] and is stored by [`Map`].
712 /// Together, `first_child` and `next_sibling` form an intrusive linked list, which is used to
713 /// model a tree structure (a replacement for a member like `children: Vec<PlaceIndex>`).
716 /// We store a [`ValueIndex`] if and only if the placed is tracked by the analysis.
717 value_index: Option<ValueIndex>,
719 /// The projection used to go from parent to this node (only None for root).
720 proj_elem: Option<TrackElem>,
722 /// The left-most child.
723 first_child: Option<PlaceIndex>,
725 /// Index of the sibling to the right of this node.
726 next_sibling: Option<PlaceIndex>,
730 fn new(proj_elem: Option<TrackElem>) -> Self {
731 Self { next_sibling: None, first_child: None, proj_elem, value_index: None }
735 struct Children<'a> {
737 next: Option<PlaceIndex>,
740 impl<'a> Children<'a> {
741 fn new(map: &'a Map, parent: PlaceIndex) -> Self {
742 Self { map, next: map.places[parent].first_child }
746 impl<'a> Iterator for Children<'a> {
747 type Item = PlaceIndex;
749 fn next(&mut self) -> Option<Self::Item> {
752 self.next = self.map.places[child].next_sibling;
760 /// Used as the result of an operand or r-value.
761 pub enum ValueOrPlace<V> {
766 impl<V: HasTop> ValueOrPlace<V> {
767 pub fn top() -> Self {
768 ValueOrPlace::Value(V::top())
772 /// The set of projection elements that can be used by a tracked place.
774 /// Although only field projections are currently allowed, this could change in the future.
775 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
780 impl<V, T> TryFrom<ProjectionElem<V, T>> for TrackElem {
783 fn try_from(value: ProjectionElem<V, T>) -> Result<Self, Self::Error> {
785 ProjectionElem::Field(field, _) => Ok(TrackElem::Field(field)),
791 /// Invokes `f` on all direct fields of `ty`.
792 fn iter_fields<'tcx>(
795 mut f: impl FnMut(Option<VariantIdx>, Field, Ty<'tcx>),
799 for (field, ty) in list.iter().enumerate() {
800 f(None, field.into(), ty);
803 ty::Adt(def, substs) => {
807 for (v_index, v_def) in def.variants().iter_enumerated() {
808 let variant = if def.is_struct() { None } else { Some(v_index) };
809 for (f_index, f_def) in v_def.fields.iter().enumerate() {
810 let field_ty = f_def.ty(tcx, substs);
812 .try_normalize_erasing_regions(ty::ParamEnv::reveal_all(), field_ty)
813 .unwrap_or(field_ty);
814 f(variant, f_index.into(), field_ty);
818 ty::Closure(_, substs) => {
819 iter_fields(substs.as_closure().tupled_upvars_ty(), tcx, f);
825 /// Returns all locals with projections that have their reference or address taken.
826 fn excluded_locals<'tcx>(body: &Body<'tcx>) -> IndexVec<Local, bool> {
828 result: IndexVec<Local, bool>,
831 impl<'tcx> Visitor<'tcx> for Collector {
832 fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
833 if context.is_borrow()
834 || context.is_address_of()
836 || context == PlaceContext::MutatingUse(MutatingUseContext::AsmOutput)
838 // A pointer to a place could be used to access other places with the same local,
839 // hence we have to exclude the local completely.
840 self.result[place.local] = true;
845 let mut collector = Collector { result: IndexVec::from_elem(false, &body.local_decls) };
846 collector.visit_body(body);
850 /// This is used to visualize the dataflow analysis.
851 impl<'tcx, T> DebugWithContext<ValueAnalysisWrapper<T>> for State<T::Value>
853 T: ValueAnalysis<'tcx>,
856 fn fmt_with(&self, ctxt: &ValueAnalysisWrapper<T>, f: &mut Formatter<'_>) -> std::fmt::Result {
858 StateData::Reachable(values) => debug_with_context(values, None, ctxt.0.map(), f),
859 StateData::Unreachable => write!(f, "unreachable"),
866 ctxt: &ValueAnalysisWrapper<T>,
867 f: &mut Formatter<'_>,
868 ) -> std::fmt::Result {
869 match (&self.0, &old.0) {
870 (StateData::Reachable(this), StateData::Reachable(old)) => {
871 debug_with_context(this, Some(old), ctxt.0.map(), f)
873 _ => Ok(()), // Consider printing something here.
878 fn debug_with_context_rec<V: Debug + Eq>(
881 new: &IndexVec<ValueIndex, V>,
882 old: Option<&IndexVec<ValueIndex, V>>,
884 f: &mut Formatter<'_>,
885 ) -> std::fmt::Result {
886 if let Some(value) = map.places[place].value_index {
888 None => writeln!(f, "{}: {:?}", place_str, new[value])?,
890 if new[value] != old[value] {
891 writeln!(f, "\u{001f}-{}: {:?}", place_str, old[value])?;
892 writeln!(f, "\u{001f}+{}: {:?}", place_str, new[value])?;
898 for child in map.children(place) {
899 let info_elem = map.places[child].proj_elem.unwrap();
900 let child_place_str = match info_elem {
901 TrackElem::Field(field) => {
902 if place_str.starts_with('*') {
903 format!("({}).{}", place_str, field.index())
905 format!("{}.{}", place_str, field.index())
909 debug_with_context_rec(child, &child_place_str, new, old, map, f)?;
915 fn debug_with_context<V: Debug + Eq>(
916 new: &IndexVec<ValueIndex, V>,
917 old: Option<&IndexVec<ValueIndex, V>>,
919 f: &mut Formatter<'_>,
920 ) -> std::fmt::Result {
921 for (local, place) in map.locals.iter_enumerated() {
922 if let Some(place) = place {
923 debug_with_context_rec(*place, &format!("{:?}", local), new, old, map, f)?;