1use std::fmt::{Debug, Formatter};
2use std::ops::Range;
3
4use rustc_abi::{FieldIdx, VariantIdx};
5use rustc_data_structures::captures::Captures;
6use rustc_data_structures::fx::{FxHashMap, FxIndexSet, StdEntry};
7use rustc_data_structures::stack::ensure_sufficient_stack;
8use rustc_index::IndexVec;
9use rustc_index::bit_set::DenseBitSet;
10use rustc_middle::mir::tcx::PlaceTy;
11use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
12use rustc_middle::mir::*;
13use rustc_middle::ty::{self, Ty, TyCtxt};
14use tracing::debug;
15
16use crate::JoinSemiLattice;
17use crate::lattice::{HasBottom, HasTop};
18
19rustc_index::newtype_index!(
20 pub struct PlaceIndex {}
25);
26
27rustc_index::newtype_index!(
28 struct ValueIndex {}
32);
33
34#[derive(PartialEq, Eq, Debug)]
36pub struct StateData<V> {
37 bottom: V,
38 map: FxHashMap<ValueIndex, V>,
40}
41
42impl<V: HasBottom> StateData<V> {
43 fn new() -> StateData<V> {
44 StateData { bottom: V::BOTTOM, map: FxHashMap::default() }
45 }
46
47 fn get(&self, idx: ValueIndex) -> &V {
48 self.map.get(&idx).unwrap_or(&self.bottom)
49 }
50
51 fn insert(&mut self, idx: ValueIndex, elem: V) {
52 if elem.is_bottom() {
53 self.map.remove(&idx);
54 } else {
55 self.map.insert(idx, elem);
56 }
57 }
58}
59
60impl<V: Clone> Clone for StateData<V> {
61 fn clone(&self) -> Self {
62 StateData { bottom: self.bottom.clone(), map: self.map.clone() }
63 }
64
65 fn clone_from(&mut self, source: &Self) {
66 self.map.clone_from(&source.map)
67 }
68}
69
70impl<V: JoinSemiLattice + Clone> JoinSemiLattice for StateData<V> {
71 fn join(&mut self, other: &Self) -> bool {
72 let mut changed = false;
73 #[allow(rustc::potential_query_instability)]
74 for (i, v) in other.map.iter() {
75 match self.map.entry(*i) {
76 StdEntry::Vacant(e) => {
77 e.insert(v.clone());
78 changed = true
79 }
80 StdEntry::Occupied(e) => changed |= e.into_mut().join(v),
81 }
82 }
83 changed
84 }
85}
86
87#[derive(PartialEq, Eq, Debug)]
98pub enum State<V> {
99 Unreachable,
100 Reachable(StateData<V>),
101}
102
103impl<V: Clone> Clone for State<V> {
104 fn clone(&self) -> Self {
105 match self {
106 Self::Reachable(x) => Self::Reachable(x.clone()),
107 Self::Unreachable => Self::Unreachable,
108 }
109 }
110
111 fn clone_from(&mut self, source: &Self) {
112 match (&mut *self, source) {
113 (Self::Reachable(x), Self::Reachable(y)) => {
114 x.clone_from(&y);
115 }
116 _ => *self = source.clone(),
117 }
118 }
119}
120
121impl<V: Clone + HasBottom> State<V> {
122 pub fn new_reachable() -> State<V> {
123 State::Reachable(StateData::new())
124 }
125
126 pub fn all_bottom(&self) -> bool {
127 match self {
128 State::Unreachable => false,
129 State::Reachable(ref values) =>
130 {
131 #[allow(rustc::potential_query_instability)]
132 values.map.values().all(V::is_bottom)
133 }
134 }
135 }
136
137 pub fn is_reachable(&self) -> bool {
138 matches!(self, State::Reachable(_))
139 }
140
141 pub fn flood_with(&mut self, place: PlaceRef<'_>, map: &Map<'_>, value: V) {
143 self.flood_with_tail_elem(place, None, map, value)
144 }
145
146 pub fn flood(&mut self, place: PlaceRef<'_>, map: &Map<'_>)
148 where
149 V: HasTop,
150 {
151 self.flood_with(place, map, V::TOP)
152 }
153
154 fn flood_discr_with(&mut self, place: PlaceRef<'_>, map: &Map<'_>, value: V) {
156 self.flood_with_tail_elem(place, Some(TrackElem::Discriminant), map, value)
157 }
158
159 pub fn flood_discr(&mut self, place: PlaceRef<'_>, map: &Map<'_>)
161 where
162 V: HasTop,
163 {
164 self.flood_discr_with(place, map, V::TOP)
165 }
166
167 pub fn flood_with_tail_elem(
175 &mut self,
176 place: PlaceRef<'_>,
177 tail_elem: Option<TrackElem>,
178 map: &Map<'_>,
179 value: V,
180 ) {
181 let State::Reachable(values) = self else { return };
182 map.for_each_aliasing_place(place, tail_elem, &mut |vi| values.insert(vi, value.clone()));
183 }
184
185 fn insert_idx(&mut self, target: PlaceIndex, result: ValueOrPlace<V>, map: &Map<'_>) {
190 match result {
191 ValueOrPlace::Value(value) => self.insert_value_idx(target, value, map),
192 ValueOrPlace::Place(source) => self.insert_place_idx(target, source, map),
193 }
194 }
195
196 pub fn insert_value_idx(&mut self, target: PlaceIndex, value: V, map: &Map<'_>) {
201 let State::Reachable(values) = self else { return };
202 if let Some(value_index) = map.places[target].value_index {
203 values.insert(value_index, value)
204 }
205 }
206
207 pub fn insert_place_idx(&mut self, target: PlaceIndex, source: PlaceIndex, map: &Map<'_>) {
215 let State::Reachable(values) = self else { return };
216
217 if let Some(target_value) = map.places[target].value_index {
221 if let Some(source_value) = map.places[source].value_index {
222 values.insert(target_value, values.get(source_value).clone());
223 }
224 }
225 for target_child in map.children(target) {
226 let projection = map.places[target_child].proj_elem.unwrap();
228 if let Some(source_child) = map.projections.get(&(source, projection)) {
229 self.insert_place_idx(target_child, *source_child, map);
230 }
231 }
232 }
233
234 pub fn assign(&mut self, target: PlaceRef<'_>, result: ValueOrPlace<V>, map: &Map<'_>)
236 where
237 V: HasTop,
238 {
239 self.flood(target, map);
240 if let Some(target) = map.find(target) {
241 self.insert_idx(target, result, map);
242 }
243 }
244
245 pub fn assign_discr(&mut self, target: PlaceRef<'_>, result: ValueOrPlace<V>, map: &Map<'_>)
247 where
248 V: HasTop,
249 {
250 self.flood_discr(target, map);
251 if let Some(target) = map.find_discr(target) {
252 self.insert_idx(target, result, map);
253 }
254 }
255
256 pub fn try_get(&self, place: PlaceRef<'_>, map: &Map<'_>) -> Option<V> {
258 let place = map.find(place)?;
259 self.try_get_idx(place, map)
260 }
261
262 pub fn try_get_discr(&self, place: PlaceRef<'_>, map: &Map<'_>) -> Option<V> {
264 let place = map.find_discr(place)?;
265 self.try_get_idx(place, map)
266 }
267
268 pub fn try_get_len(&self, place: PlaceRef<'_>, map: &Map<'_>) -> Option<V> {
270 let place = map.find_len(place)?;
271 self.try_get_idx(place, map)
272 }
273
274 pub fn try_get_idx(&self, place: PlaceIndex, map: &Map<'_>) -> Option<V> {
276 match self {
277 State::Reachable(values) => {
278 map.places[place].value_index.map(|v| values.get(v).clone())
279 }
280 State::Unreachable => None,
281 }
282 }
283
284 pub fn get(&self, place: PlaceRef<'_>, map: &Map<'_>) -> V
288 where
289 V: HasBottom + HasTop,
290 {
291 match self {
292 State::Reachable(_) => self.try_get(place, map).unwrap_or(V::TOP),
293 State::Unreachable => V::BOTTOM,
295 }
296 }
297
298 pub fn get_discr(&self, place: PlaceRef<'_>, map: &Map<'_>) -> V
302 where
303 V: HasBottom + HasTop,
304 {
305 match self {
306 State::Reachable(_) => self.try_get_discr(place, map).unwrap_or(V::TOP),
307 State::Unreachable => V::BOTTOM,
309 }
310 }
311
312 pub fn get_len(&self, place: PlaceRef<'_>, map: &Map<'_>) -> V
316 where
317 V: HasBottom + HasTop,
318 {
319 match self {
320 State::Reachable(_) => self.try_get_len(place, map).unwrap_or(V::TOP),
321 State::Unreachable => V::BOTTOM,
323 }
324 }
325
326 pub fn get_idx(&self, place: PlaceIndex, map: &Map<'_>) -> V
330 where
331 V: HasBottom + HasTop,
332 {
333 match self {
334 State::Reachable(values) => {
335 map.places[place].value_index.map(|v| values.get(v).clone()).unwrap_or(V::TOP)
336 }
337 State::Unreachable => {
338 V::BOTTOM
340 }
341 }
342 }
343}
344
345impl<V: JoinSemiLattice + Clone> JoinSemiLattice for State<V> {
346 fn join(&mut self, other: &Self) -> bool {
347 match (&mut *self, other) {
348 (_, State::Unreachable) => false,
349 (State::Unreachable, _) => {
350 *self = other.clone();
351 true
352 }
353 (State::Reachable(this), State::Reachable(ref other)) => this.join(other),
354 }
355 }
356}
357
358#[derive(Debug)]
365pub struct Map<'tcx> {
366 locals: IndexVec<Local, Option<PlaceIndex>>,
367 projections: FxHashMap<(PlaceIndex, TrackElem), PlaceIndex>,
368 places: IndexVec<PlaceIndex, PlaceInfo<'tcx>>,
369 value_count: usize,
370 inner_values: IndexVec<PlaceIndex, Range<usize>>,
372 inner_values_buffer: Vec<ValueIndex>,
373}
374
375impl<'tcx> Map<'tcx> {
376 pub fn new(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, value_limit: Option<usize>) -> Self {
382 let mut map = Self {
383 locals: IndexVec::from_elem(None, &body.local_decls),
384 projections: FxHashMap::default(),
385 places: IndexVec::new(),
386 value_count: 0,
387 inner_values: IndexVec::new(),
388 inner_values_buffer: Vec::new(),
389 };
390 let exclude = excluded_locals(body);
391 map.register(tcx, body, exclude, value_limit);
392 debug!("registered {} places ({} nodes in total)", map.value_count, map.places.len());
393 map
394 }
395
396 #[tracing::instrument(level = "trace", skip(self, tcx, body))]
398 fn register(
399 &mut self,
400 tcx: TyCtxt<'tcx>,
401 body: &Body<'tcx>,
402 exclude: DenseBitSet<Local>,
403 value_limit: Option<usize>,
404 ) {
405 for (local, decl) in body.local_decls.iter_enumerated() {
407 if exclude.contains(local) {
408 continue;
409 }
410
411 debug_assert!(self.locals[local].is_none());
413 let place = self.places.push(PlaceInfo::new(decl.ty, None));
414 self.locals[local] = Some(place);
415 }
416
417 let mut collector =
419 PlaceCollector { tcx, body, map: self, assignments: Default::default() };
420 collector.visit_body(body);
421 let PlaceCollector { mut assignments, .. } = collector;
422
423 let mut num_places = 0;
435 while num_places < self.places.len() {
436 num_places = self.places.len();
437
438 for assign in 0.. {
439 let Some(&(lhs, rhs)) = assignments.get_index(assign) else { break };
440
441 let mut child = self.places[lhs].first_child;
443 while let Some(lhs_child) = child {
444 let PlaceInfo { ty, proj_elem, next_sibling, .. } = self.places[lhs_child];
445 let rhs_child =
446 self.register_place(ty, rhs, proj_elem.expect("child is not a projection"));
447 assignments.insert((lhs_child, rhs_child));
448 child = next_sibling;
449 }
450
451 let mut child = self.places[rhs].first_child;
453 while let Some(rhs_child) = child {
454 let PlaceInfo { ty, proj_elem, next_sibling, .. } = self.places[rhs_child];
455 let lhs_child =
456 self.register_place(ty, lhs, proj_elem.expect("child is not a projection"));
457 assignments.insert((lhs_child, rhs_child));
458 child = next_sibling;
459 }
460 }
461 }
462 drop(assignments);
463
464 let typing_env = body.typing_env(tcx);
466 for place_info in self.places.iter_mut() {
467 if let Some(value_limit) = value_limit
469 && self.value_count >= value_limit
470 {
471 break;
472 }
473
474 if let Ok(ty) = tcx.try_normalize_erasing_regions(typing_env, place_info.ty) {
475 place_info.ty = ty;
476 }
477
478 assert!(place_info.value_index.is_none());
480 if let Ok(layout) = tcx.layout_of(typing_env.as_query_input(place_info.ty))
481 && layout.backend_repr.is_scalar()
482 {
483 place_info.value_index = Some(self.value_count.into());
484 self.value_count += 1;
485 }
486 }
487
488 self.inner_values_buffer = Vec::with_capacity(self.value_count);
492 self.inner_values = IndexVec::from_elem(0..0, &self.places);
493 for local in body.local_decls.indices() {
494 if let Some(place) = self.locals[local] {
495 self.cache_preorder_invoke(place);
496 }
497 }
498
499 for opt_place in self.locals.iter_mut() {
501 if let Some(place) = *opt_place
502 && self.inner_values[place].is_empty()
503 {
504 *opt_place = None;
505 }
506 }
507 #[allow(rustc::potential_query_instability)]
508 self.projections.retain(|_, child| !self.inner_values[*child].is_empty());
509 }
510
511 #[tracing::instrument(level = "trace", skip(self), ret)]
512 fn register_place(&mut self, ty: Ty<'tcx>, base: PlaceIndex, elem: TrackElem) -> PlaceIndex {
513 *self.projections.entry((base, elem)).or_insert_with(|| {
514 let next = self.places.push(PlaceInfo::new(ty, Some(elem)));
515 self.places[next].next_sibling = self.places[base].first_child;
516 self.places[base].first_child = Some(next);
517 next
518 })
519 }
520
521 fn cache_preorder_invoke(&mut self, root: PlaceIndex) {
524 let start = self.inner_values_buffer.len();
525 if let Some(vi) = self.places[root].value_index {
526 self.inner_values_buffer.push(vi);
527 }
528
529 let mut next_child = self.places[root].first_child;
531 while let Some(child) = next_child {
532 ensure_sufficient_stack(|| self.cache_preorder_invoke(child));
533 next_child = self.places[child].next_sibling;
534 }
535
536 let end = self.inner_values_buffer.len();
537 self.inner_values[root] = start..end;
538 }
539}
540
541struct PlaceCollector<'a, 'tcx> {
542 tcx: TyCtxt<'tcx>,
543 body: &'a Body<'tcx>,
544 map: &'a mut Map<'tcx>,
545 assignments: FxIndexSet<(PlaceIndex, PlaceIndex)>,
546}
547
548impl<'tcx> PlaceCollector<'_, 'tcx> {
549 #[tracing::instrument(level = "trace", skip(self))]
550 fn register_place(&mut self, place: Place<'tcx>) -> Option<PlaceIndex> {
551 let mut place_index = self.map.locals[place.local]?;
553 let mut ty = PlaceTy::from_ty(self.body.local_decls[place.local].ty);
554 tracing::trace!(?place_index, ?ty);
555
556 if let ty::Ref(_, ref_ty, _) | ty::RawPtr(ref_ty, _) = ty.ty.kind()
557 && let ty::Slice(..) = ref_ty.kind()
558 {
559 self.map.register_place(self.tcx.types.usize, place_index, TrackElem::DerefLen);
560 } else if ty.ty.is_enum() {
561 let discriminant_ty = ty.ty.discriminant_ty(self.tcx);
562 self.map.register_place(discriminant_ty, place_index, TrackElem::Discriminant);
563 }
564
565 for proj in place.projection {
566 let track_elem = proj.try_into().ok()?;
567 ty = ty.projection_ty(self.tcx, proj);
568 place_index = self.map.register_place(ty.ty, place_index, track_elem);
569 tracing::trace!(?proj, ?place_index, ?ty);
570
571 if let ty::Ref(_, ref_ty, _) | ty::RawPtr(ref_ty, _) = ty.ty.kind()
572 && let ty::Slice(..) = ref_ty.kind()
573 {
574 self.map.register_place(self.tcx.types.usize, place_index, TrackElem::DerefLen);
575 } else if ty.ty.is_enum() {
576 let discriminant_ty = ty.ty.discriminant_ty(self.tcx);
577 self.map.register_place(discriminant_ty, place_index, TrackElem::Discriminant);
578 }
579 }
580
581 Some(place_index)
582 }
583}
584
585impl<'tcx> Visitor<'tcx> for PlaceCollector<'_, 'tcx> {
586 #[tracing::instrument(level = "trace", skip(self))]
587 fn visit_place(&mut self, place: &Place<'tcx>, ctxt: PlaceContext, _: Location) {
588 if !ctxt.is_use() {
589 return;
590 }
591
592 self.register_place(*place);
593 }
594
595 fn visit_assign(&mut self, lhs: &Place<'tcx>, rhs: &Rvalue<'tcx>, location: Location) {
596 self.super_assign(lhs, rhs, location);
597
598 match rhs {
599 Rvalue::Use(Operand::Move(rhs) | Operand::Copy(rhs)) | Rvalue::CopyForDeref(rhs) => {
600 let Some(lhs) = self.register_place(*lhs) else { return };
601 let Some(rhs) = self.register_place(*rhs) else { return };
602 self.assignments.insert((lhs, rhs));
603 }
604 Rvalue::Aggregate(kind, fields) => {
605 let Some(mut lhs) = self.register_place(*lhs) else { return };
606 match **kind {
607 AggregateKind::Adt(_, _, _, _, Some(_)) => return,
609 AggregateKind::Adt(_, variant, _, _, None) => {
610 let ty = self.map.places[lhs].ty;
611 if ty.is_enum() {
612 lhs = self.map.register_place(ty, lhs, TrackElem::Variant(variant));
613 }
614 }
615 AggregateKind::RawPtr(..)
616 | AggregateKind::Array(_)
617 | AggregateKind::Tuple
618 | AggregateKind::Closure(..)
619 | AggregateKind::Coroutine(..)
620 | AggregateKind::CoroutineClosure(..) => {}
621 }
622 for (index, field) in fields.iter_enumerated() {
623 if let Some(rhs) = field.place()
624 && let Some(rhs) = self.register_place(rhs)
625 {
626 let lhs = self.map.register_place(
627 self.map.places[rhs].ty,
628 lhs,
629 TrackElem::Field(index),
630 );
631 self.assignments.insert((lhs, rhs));
632 }
633 }
634 }
635 _ => {}
636 }
637 }
638}
639
640impl<'tcx> Map<'tcx> {
641 pub fn apply(&self, place: PlaceIndex, elem: TrackElem) -> Option<PlaceIndex> {
643 self.projections.get(&(place, elem)).copied()
644 }
645
646 fn find_extra(
648 &self,
649 place: PlaceRef<'_>,
650 extra: impl IntoIterator<Item = TrackElem>,
651 ) -> Option<PlaceIndex> {
652 let mut index = *self.locals[place.local].as_ref()?;
653
654 for &elem in place.projection {
655 index = self.apply(index, elem.try_into().ok()?)?;
656 }
657 for elem in extra {
658 index = self.apply(index, elem)?;
659 }
660
661 Some(index)
662 }
663
664 pub fn find(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
666 self.find_extra(place, [])
667 }
668
669 pub fn find_discr(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
671 self.find_extra(place, [TrackElem::Discriminant])
672 }
673
674 pub fn find_len(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
676 self.find_extra(place, [TrackElem::DerefLen])
677 }
678
679 fn children(
681 &self,
682 parent: PlaceIndex,
683 ) -> impl Iterator<Item = PlaceIndex> + Captures<'_> + Captures<'tcx> {
684 Children::new(self, parent)
685 }
686
687 fn for_each_aliasing_place(
695 &self,
696 place: PlaceRef<'_>,
697 tail_elem: Option<TrackElem>,
698 f: &mut impl FnMut(ValueIndex),
699 ) {
700 if place.is_indirect_first_projection() {
701 return;
703 }
704 let Some(mut index) = self.locals[place.local] else {
705 return;
707 };
708 let elems = place.projection.iter().map(|&elem| elem.try_into()).chain(tail_elem.map(Ok));
709 for elem in elems {
710 if let Some(vi) = self.places[index].value_index {
712 f(vi);
713 }
714
715 let Ok(elem) = elem else { return };
716 let sub = self.apply(index, elem);
717 if let TrackElem::Variant(..) | TrackElem::Discriminant = elem {
718 self.for_each_variant_sibling(index, sub, f);
720 }
721 if let Some(sub) = sub {
722 index = sub
723 } else {
724 return;
725 }
726 }
727 self.for_each_value_inside(index, f);
728 }
729
730 fn for_each_variant_sibling(
732 &self,
733 parent: PlaceIndex,
734 preserved_child: Option<PlaceIndex>,
735 f: &mut impl FnMut(ValueIndex),
736 ) {
737 for sibling in self.children(parent) {
738 let elem = self.places[sibling].proj_elem;
739 if let Some(TrackElem::Variant(..) | TrackElem::Discriminant) = elem
742 && Some(sibling) != preserved_child
744 {
745 self.for_each_value_inside(sibling, f);
746 }
747 }
748 }
749
750 fn for_each_value_inside(&self, root: PlaceIndex, f: &mut impl FnMut(ValueIndex)) {
752 let range = self.inner_values[root].clone();
753 let values = &self.inner_values_buffer[range];
754 for &v in values {
755 f(v)
756 }
757 }
758
759 pub fn for_each_projection_value<O>(
761 &self,
762 root: PlaceIndex,
763 value: O,
764 project: &mut impl FnMut(TrackElem, &O) -> Option<O>,
765 f: &mut impl FnMut(PlaceIndex, &O),
766 ) {
767 if self.inner_values[root].is_empty() {
769 return;
770 }
771
772 if self.places[root].value_index.is_some() {
773 f(root, &value)
774 }
775
776 for child in self.children(root) {
777 let elem = self.places[child].proj_elem.unwrap();
778 if let Some(value) = project(elem, &value) {
779 self.for_each_projection_value(child, value, project, f);
780 }
781 }
782 }
783}
784
785#[derive(Debug)]
790struct PlaceInfo<'tcx> {
791 ty: Ty<'tcx>,
793
794 value_index: Option<ValueIndex>,
796
797 proj_elem: Option<TrackElem>,
799
800 first_child: Option<PlaceIndex>,
802
803 next_sibling: Option<PlaceIndex>,
805}
806
807impl<'tcx> PlaceInfo<'tcx> {
808 fn new(ty: Ty<'tcx>, proj_elem: Option<TrackElem>) -> Self {
809 Self { ty, next_sibling: None, first_child: None, proj_elem, value_index: None }
810 }
811}
812
813struct Children<'a, 'tcx> {
814 map: &'a Map<'tcx>,
815 next: Option<PlaceIndex>,
816}
817
818impl<'a, 'tcx> Children<'a, 'tcx> {
819 fn new(map: &'a Map<'tcx>, parent: PlaceIndex) -> Self {
820 Self { map, next: map.places[parent].first_child }
821 }
822}
823
824impl Iterator for Children<'_, '_> {
825 type Item = PlaceIndex;
826
827 fn next(&mut self) -> Option<Self::Item> {
828 match self.next {
829 Some(child) => {
830 self.next = self.map.places[child].next_sibling;
831 Some(child)
832 }
833 None => None,
834 }
835 }
836}
837
838#[derive(Debug)]
840pub enum ValueOrPlace<V> {
841 Value(V),
842 Place(PlaceIndex),
843}
844
845impl<V: HasTop> ValueOrPlace<V> {
846 pub const TOP: Self = ValueOrPlace::Value(V::TOP);
847}
848
849#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
853pub enum TrackElem {
854 Field(FieldIdx),
855 Variant(VariantIdx),
856 Discriminant,
857 DerefLen,
859}
860
861impl<V, T> TryFrom<ProjectionElem<V, T>> for TrackElem {
862 type Error = ();
863
864 fn try_from(value: ProjectionElem<V, T>) -> Result<Self, Self::Error> {
865 match value {
866 ProjectionElem::Field(field, _) => Ok(TrackElem::Field(field)),
867 ProjectionElem::Downcast(_, idx) => Ok(TrackElem::Variant(idx)),
868 _ => Err(()),
869 }
870 }
871}
872
873pub fn iter_fields<'tcx>(
875 ty: Ty<'tcx>,
876 tcx: TyCtxt<'tcx>,
877 typing_env: ty::TypingEnv<'tcx>,
878 mut f: impl FnMut(Option<VariantIdx>, FieldIdx, Ty<'tcx>),
879) {
880 match ty.kind() {
881 ty::Tuple(list) => {
882 for (field, ty) in list.iter().enumerate() {
883 f(None, field.into(), ty);
884 }
885 }
886 ty::Adt(def, args) => {
887 if def.is_union() {
888 return;
889 }
890 for (v_index, v_def) in def.variants().iter_enumerated() {
891 let variant = if def.is_struct() { None } else { Some(v_index) };
892 for (f_index, f_def) in v_def.fields.iter().enumerate() {
893 let field_ty = f_def.ty(tcx, args);
894 let field_ty = tcx
895 .try_normalize_erasing_regions(typing_env, field_ty)
896 .unwrap_or_else(|_| tcx.erase_regions(field_ty));
897 f(variant, f_index.into(), field_ty);
898 }
899 }
900 }
901 ty::Closure(_, args) => {
902 iter_fields(args.as_closure().tupled_upvars_ty(), tcx, typing_env, f);
903 }
904 ty::Coroutine(_, args) => {
905 iter_fields(args.as_coroutine().tupled_upvars_ty(), tcx, typing_env, f);
906 }
907 ty::CoroutineClosure(_, args) => {
908 iter_fields(args.as_coroutine_closure().tupled_upvars_ty(), tcx, typing_env, f);
909 }
910 _ => (),
911 }
912}
913
914pub fn excluded_locals(body: &Body<'_>) -> DenseBitSet<Local> {
916 struct Collector {
917 result: DenseBitSet<Local>,
918 }
919
920 impl<'tcx> Visitor<'tcx> for Collector {
921 fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
922 if (context.is_borrow()
923 || context.is_address_of()
924 || context.is_drop()
925 || context == PlaceContext::MutatingUse(MutatingUseContext::AsmOutput))
926 && !place.is_indirect()
927 {
928 self.result.insert(place.local);
931 }
932 }
933 }
934
935 let mut collector = Collector { result: DenseBitSet::new_empty(body.local_decls.len()) };
936 collector.visit_body(body);
937 collector.result
938}
939
940fn debug_with_context_rec<V: Debug + Eq + HasBottom>(
941 place: PlaceIndex,
942 place_str: &str,
943 new: &StateData<V>,
944 old: Option<&StateData<V>>,
945 map: &Map<'_>,
946 f: &mut Formatter<'_>,
947) -> std::fmt::Result {
948 if let Some(value) = map.places[place].value_index {
949 match old {
950 None => writeln!(f, "{}: {:?}", place_str, new.get(value))?,
951 Some(old) => {
952 if new.get(value) != old.get(value) {
953 writeln!(f, "\u{001f}-{}: {:?}", place_str, old.get(value))?;
954 writeln!(f, "\u{001f}+{}: {:?}", place_str, new.get(value))?;
955 }
956 }
957 }
958 }
959
960 for child in map.children(place) {
961 let info_elem = map.places[child].proj_elem.unwrap();
962 let child_place_str = match info_elem {
963 TrackElem::Discriminant => {
964 format!("discriminant({place_str})")
965 }
966 TrackElem::Variant(idx) => {
967 format!("({place_str} as {idx:?})")
968 }
969 TrackElem::Field(field) => {
970 if place_str.starts_with('*') {
971 format!("({}).{}", place_str, field.index())
972 } else {
973 format!("{}.{}", place_str, field.index())
974 }
975 }
976 TrackElem::DerefLen => {
977 format!("Len(*{})", place_str)
978 }
979 };
980 debug_with_context_rec(child, &child_place_str, new, old, map, f)?;
981 }
982
983 Ok(())
984}
985
986pub fn debug_with_context<V: Debug + Eq + HasBottom>(
987 new: &StateData<V>,
988 old: Option<&StateData<V>>,
989 map: &Map<'_>,
990 f: &mut Formatter<'_>,
991) -> std::fmt::Result {
992 for (local, place) in map.locals.iter_enumerated() {
993 if let Some(place) = place {
994 debug_with_context_rec(*place, &format!("{local:?}"), new, old, map, f)?;
995 }
996 }
997 Ok(())
998}