5 //! There are two visitors, one for immutable and one for mutable references,
6 //! but both are generated by the following macro. The code is written according
7 //! to the following conventions:
9 //! - introduce a `visit_foo` and a `super_foo` method for every MIR type
10 //! - `visit_foo`, by default, calls `super_foo`
11 //! - `super_foo`, by default, destructures the `foo` and calls `visit_foo`
13 //! This allows you as a user to override `visit_foo` for types are
14 //! interested in, and invoke (within that method) call
15 //! `self.super_foo` to get the default behavior. Just as in an OO
16 //! language, you should never call `super` methods ordinarily except
17 //! in that circumstance.
19 //! For the most part, we do not destructure things external to the
20 //! MIR, e.g., types, spans, etc, but simply visit them and stop. This
21 //! avoids duplication with other visitors like `TypeFoldable`.
25 //! The code is written in a very deliberate style intended to minimize
26 //! the chance of things being overlooked. You'll notice that we always
27 //! use pattern matching to reference fields and we ensure that all
28 //! matches are exhaustive.
30 //! For example, the `super_basic_block_data` method begins like this:
33 //! fn super_basic_block_data(&mut self,
34 //! block: BasicBlock,
35 //! data: & $($mutability)? BasicBlockData<'tcx>) {
36 //! let BasicBlockData {
42 //! for statement in statements {
43 //! self.visit_statement(block, statement);
50 //! Here we used `let BasicBlockData { <fields> } = *data` deliberately,
51 //! rather than writing `data.statements` in the body. This is because if one
52 //! adds a new field to `BasicBlockData`, one will be forced to revise this code,
53 //! and hence one will (hopefully) invoke the correct visit methods (if any).
55 //! For this to work, ALL MATCHES MUST BE EXHAUSTIVE IN FIELDS AND VARIANTS.
56 //! That means you never write `..` to skip over fields, nor do you write `_`
57 //! to skip over variants in a `match`.
59 //! The only place that `_` is acceptable is to match a field (or
60 //! variant argument) that does not require visiting, as in
61 //! `is_cleanup` above.
64 use crate::ty::subst::SubstsRef;
65 use crate::ty::{CanonicalUserTypeAnnotation, Ty};
68 macro_rules! make_mir_visitor {
69 ($visitor_trait_name:ident, $($mutability:ident)?) => {
70 pub trait $visitor_trait_name<'tcx> {
71 // Override these, and call `self.super_xxx` to revert back to the
76 body: &$($mutability)? Body<'tcx>,
78 self.super_body(body);
81 fn visit_basic_block_data(&mut self,
83 data: & $($mutability)? BasicBlockData<'tcx>) {
84 self.super_basic_block_data(block, data);
87 fn visit_source_scope_data(&mut self,
88 scope_data: & $($mutability)? SourceScopeData<'tcx>) {
89 self.super_source_scope_data(scope_data);
92 fn visit_statement(&mut self,
93 statement: & $($mutability)? Statement<'tcx>,
95 self.super_statement(statement, location);
98 fn visit_assign(&mut self,
99 place: & $($mutability)? Place<'tcx>,
100 rvalue: & $($mutability)? Rvalue<'tcx>,
101 location: Location) {
102 self.super_assign(place, rvalue, location);
105 fn visit_terminator(&mut self,
106 terminator: & $($mutability)? Terminator<'tcx>,
107 location: Location) {
108 self.super_terminator(terminator, location);
111 fn visit_assert_message(&mut self,
112 msg: & $($mutability)? AssertMessage<'tcx>,
113 location: Location) {
114 self.super_assert_message(msg, location);
117 fn visit_rvalue(&mut self,
118 rvalue: & $($mutability)? Rvalue<'tcx>,
119 location: Location) {
120 self.super_rvalue(rvalue, location);
123 fn visit_operand(&mut self,
124 operand: & $($mutability)? Operand<'tcx>,
125 location: Location) {
126 self.super_operand(operand, location);
129 fn visit_ascribe_user_ty(&mut self,
130 place: & $($mutability)? Place<'tcx>,
131 variance: & $($mutability)? ty::Variance,
132 user_ty: & $($mutability)? UserTypeProjection,
133 location: Location) {
134 self.super_ascribe_user_ty(place, variance, user_ty, location);
137 fn visit_coverage(&mut self,
138 coverage: & $($mutability)? Coverage,
139 location: Location) {
140 self.super_coverage(coverage, location);
143 fn visit_retag(&mut self,
144 kind: & $($mutability)? RetagKind,
145 place: & $($mutability)? Place<'tcx>,
146 location: Location) {
147 self.super_retag(kind, place, location);
150 fn visit_place(&mut self,
151 place: & $($mutability)? Place<'tcx>,
152 context: PlaceContext,
153 location: Location) {
154 self.super_place(place, context, location);
157 visit_place_fns!($($mutability)?);
159 fn visit_constant(&mut self,
160 constant: & $($mutability)? Constant<'tcx>,
161 location: Location) {
162 self.super_constant(constant, location);
165 fn visit_span(&mut self,
166 span: & $($mutability)? Span) {
167 self.super_span(span);
170 fn visit_source_info(&mut self,
171 source_info: & $($mutability)? SourceInfo) {
172 self.super_source_info(source_info);
175 fn visit_ty(&mut self,
176 ty: $(& $mutability)? Ty<'tcx>,
181 fn visit_user_type_projection(
183 ty: & $($mutability)? UserTypeProjection,
185 self.super_user_type_projection(ty);
188 fn visit_user_type_annotation(
190 index: UserTypeAnnotationIndex,
191 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
193 self.super_user_type_annotation(index, ty);
196 fn visit_region(&mut self,
197 region: & $($mutability)? ty::Region<'tcx>,
199 self.super_region(region);
202 fn visit_const(&mut self,
203 constant: & $($mutability)? &'tcx ty::Const<'tcx>,
205 self.super_const(constant);
208 fn visit_substs(&mut self,
209 substs: & $($mutability)? SubstsRef<'tcx>,
211 self.super_substs(substs);
214 fn visit_local_decl(&mut self,
216 local_decl: & $($mutability)? LocalDecl<'tcx>) {
217 self.super_local_decl(local, local_decl);
220 fn visit_var_debug_info(&mut self,
221 var_debug_info: & $($mutability)* VarDebugInfo<'tcx>) {
222 self.super_var_debug_info(var_debug_info);
225 fn visit_local(&mut self,
226 _local: & $($mutability)? Local,
227 _context: PlaceContext,
228 _location: Location) {
231 fn visit_source_scope(&mut self,
232 scope: & $($mutability)? SourceScope) {
233 self.super_source_scope(scope);
236 // The `super_xxx` methods comprise the default behavior and are
237 // not meant to be overridden.
241 body: &$($mutability)? Body<'tcx>,
243 let span = body.span;
244 if let Some(gen) = &$($mutability)? body.generator {
245 if let Some(yield_ty) = &$($mutability)? gen.yield_ty {
248 TyContext::YieldTy(SourceInfo::outermost(span))
253 // for best performance, we want to use an iterator rather
254 // than a for-loop, to avoid calling `body::Body::invalidate` for
256 macro_rules! basic_blocks {
257 (mut) => (body.basic_blocks_mut().iter_enumerated_mut());
258 () => (body.basic_blocks().iter_enumerated());
260 for (bb, data) in basic_blocks!($($mutability)?) {
261 self.visit_basic_block_data(bb, data);
264 for scope in &$($mutability)? body.source_scopes {
265 self.visit_source_scope_data(scope);
269 &$($mutability)? body.return_ty(),
270 TyContext::ReturnTy(SourceInfo::outermost(body.span))
273 for local in body.local_decls.indices() {
274 self.visit_local_decl(local, & $($mutability)? body.local_decls[local]);
277 macro_rules! type_annotations {
278 (mut) => (body.user_type_annotations.iter_enumerated_mut());
279 () => (body.user_type_annotations.iter_enumerated());
282 for (index, annotation) in type_annotations!($($mutability)?) {
283 self.visit_user_type_annotation(
288 for var_debug_info in &$($mutability)? body.var_debug_info {
289 self.visit_var_debug_info(var_debug_info);
292 self.visit_span(&$($mutability)? body.span);
294 for const_ in &$($mutability)? body.required_consts {
295 let location = START_BLOCK.start_location();
296 self.visit_constant(const_, location);
300 fn super_basic_block_data(&mut self,
302 data: & $($mutability)? BasicBlockData<'tcx>) {
310 for statement in statements {
311 let location = Location { block, statement_index: index };
312 self.visit_statement(statement, location);
316 if let Some(terminator) = terminator {
317 let location = Location { block, statement_index: index };
318 self.visit_terminator(terminator, location);
322 fn super_source_scope_data(
324 scope_data: & $($mutability)? SourceScopeData<'tcx>,
326 let SourceScopeData {
330 inlined_parent_scope,
334 self.visit_span(span);
335 if let Some(parent_scope) = parent_scope {
336 self.visit_source_scope(parent_scope);
338 if let Some((callee, callsite_span)) = inlined {
339 let location = START_BLOCK.start_location();
341 self.visit_span(callsite_span);
343 let ty::Instance { def: callee_def, substs: callee_substs } = callee;
345 ty::InstanceDef::Item(_def_id) => {}
347 ty::InstanceDef::Intrinsic(_def_id) |
348 ty::InstanceDef::VtableShim(_def_id) |
349 ty::InstanceDef::ReifyShim(_def_id) |
350 ty::InstanceDef::Virtual(_def_id, _) |
351 ty::InstanceDef::ClosureOnceShim { call_once: _def_id, track_caller: _ } |
352 ty::InstanceDef::DropGlue(_def_id, None) => {}
354 ty::InstanceDef::FnPtrShim(_def_id, ty) |
355 ty::InstanceDef::DropGlue(_def_id, Some(ty)) |
356 ty::InstanceDef::CloneShim(_def_id, ty) => {
357 // FIXME(eddyb) use a better `TyContext` here.
358 self.visit_ty(ty, TyContext::Location(location));
361 self.visit_substs(callee_substs, location);
363 if let Some(inlined_parent_scope) = inlined_parent_scope {
364 self.visit_source_scope(inlined_parent_scope);
368 fn super_statement(&mut self,
369 statement: & $($mutability)? Statement<'tcx>,
370 location: Location) {
376 self.visit_source_info(source_info);
378 StatementKind::Assign(
379 box(ref $($mutability)? place, ref $($mutability)? rvalue)
381 self.visit_assign(place, rvalue, location);
383 StatementKind::FakeRead(box (_, place)) => {
386 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
390 StatementKind::SetDiscriminant { place, .. } => {
393 PlaceContext::MutatingUse(MutatingUseContext::Store),
397 StatementKind::StorageLive(local) => {
400 PlaceContext::NonUse(NonUseContext::StorageLive),
404 StatementKind::StorageDead(local) => {
407 PlaceContext::NonUse(NonUseContext::StorageDead),
411 StatementKind::Retag(kind, place) => {
412 self.visit_retag(kind, place, location);
414 StatementKind::AscribeUserType(
415 box(ref $($mutability)? place, ref $($mutability)? user_ty),
418 self.visit_ascribe_user_ty(place, variance, user_ty, location);
420 StatementKind::Coverage(coverage) => {
426 StatementKind::CopyNonOverlapping(box crate::mir::CopyNonOverlapping{
427 ref $($mutability)? src,
428 ref $($mutability)? dst,
429 ref $($mutability)? count,
431 self.visit_operand(src, location);
432 self.visit_operand(dst, location);
433 self.visit_operand(count, location)
435 StatementKind::Nop => {}
439 fn super_assign(&mut self,
440 place: &$($mutability)? Place<'tcx>,
441 rvalue: &$($mutability)? Rvalue<'tcx>,
442 location: Location) {
445 PlaceContext::MutatingUse(MutatingUseContext::Store),
448 self.visit_rvalue(rvalue, location);
451 fn super_terminator(&mut self,
452 terminator: &$($mutability)? Terminator<'tcx>,
453 location: Location) {
454 let Terminator { source_info, kind } = terminator;
456 self.visit_source_info(source_info);
458 TerminatorKind::Goto { .. } |
459 TerminatorKind::Resume |
460 TerminatorKind::Abort |
461 TerminatorKind::GeneratorDrop |
462 TerminatorKind::Unreachable |
463 TerminatorKind::FalseEdge { .. } |
464 TerminatorKind::FalseUnwind { .. } => {
467 TerminatorKind::Return => {
468 // `return` logically moves from the return place `_0`. Note that the place
469 // cannot be changed by any visitor, though.
470 let $($mutability)? local = RETURN_PLACE;
472 & $($mutability)? local,
473 PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
480 "`MutVisitor` tried to mutate return place of `return` terminator"
484 TerminatorKind::SwitchInt {
489 self.visit_operand(discr, location);
490 self.visit_ty(switch_ty, TyContext::Location(location));
493 TerminatorKind::Drop {
500 PlaceContext::MutatingUse(MutatingUseContext::Drop),
505 TerminatorKind::DropAndReplace {
513 PlaceContext::MutatingUse(MutatingUseContext::Drop),
516 self.visit_operand(value, location);
519 TerminatorKind::Call {
527 self.visit_operand(func, location);
529 self.visit_operand(arg, location);
531 if let Some((destination, _)) = destination {
534 PlaceContext::MutatingUse(MutatingUseContext::Call),
540 TerminatorKind::Assert {
547 self.visit_operand(cond, location);
548 self.visit_assert_message(msg, location);
551 TerminatorKind::Yield {
557 self.visit_operand(value, location);
560 PlaceContext::MutatingUse(MutatingUseContext::Yield),
565 TerminatorKind::InlineAsm {
575 InlineAsmOperand::In { value, .. } => {
576 self.visit_operand(value, location);
578 InlineAsmOperand::Out { place: Some(place), .. } => {
581 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
585 InlineAsmOperand::InOut { in_value, out_place, .. } => {
586 self.visit_operand(in_value, location);
587 if let Some(out_place) = out_place {
590 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
595 InlineAsmOperand::Const { value }
596 | InlineAsmOperand::SymFn { value } => {
597 self.visit_constant(value, location);
599 InlineAsmOperand::Out { place: None, .. }
600 | InlineAsmOperand::SymStatic { def_id: _ } => {}
607 fn super_assert_message(&mut self,
608 msg: & $($mutability)? AssertMessage<'tcx>,
609 location: Location) {
610 use crate::mir::AssertKind::*;
612 BoundsCheck { len, index } => {
613 self.visit_operand(len, location);
614 self.visit_operand(index, location);
616 Overflow(_, l, r) => {
617 self.visit_operand(l, location);
618 self.visit_operand(r, location);
620 OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
621 self.visit_operand(op, location);
623 ResumedAfterReturn(_) | ResumedAfterPanic(_) => {
629 fn super_rvalue(&mut self,
630 rvalue: & $($mutability)? Rvalue<'tcx>,
631 location: Location) {
633 Rvalue::Use(operand) => {
634 self.visit_operand(operand, location);
637 Rvalue::Repeat(value, _) => {
638 self.visit_operand(value, location);
641 Rvalue::ThreadLocalRef(_) => {}
643 Rvalue::Ref(r, bk, path) => {
644 self.visit_region(r, location);
646 BorrowKind::Shared => PlaceContext::NonMutatingUse(
647 NonMutatingUseContext::SharedBorrow
649 BorrowKind::Shallow => PlaceContext::NonMutatingUse(
650 NonMutatingUseContext::ShallowBorrow
652 BorrowKind::Unique => PlaceContext::NonMutatingUse(
653 NonMutatingUseContext::UniqueBorrow
655 BorrowKind::Mut { .. } =>
656 PlaceContext::MutatingUse(MutatingUseContext::Borrow),
658 self.visit_place(path, ctx, location);
661 Rvalue::AddressOf(m, path) => {
663 Mutability::Mut => PlaceContext::MutatingUse(
664 MutatingUseContext::AddressOf
666 Mutability::Not => PlaceContext::NonMutatingUse(
667 NonMutatingUseContext::AddressOf
670 self.visit_place(path, ctx, location);
673 Rvalue::Len(path) => {
676 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
681 Rvalue::Cast(_cast_kind, operand, ty) => {
682 self.visit_operand(operand, location);
683 self.visit_ty(ty, TyContext::Location(location));
686 Rvalue::BinaryOp(_bin_op, box(lhs, rhs))
687 | Rvalue::CheckedBinaryOp(_bin_op, box(lhs, rhs)) => {
688 self.visit_operand(lhs, location);
689 self.visit_operand(rhs, location);
692 Rvalue::UnaryOp(_un_op, op) => {
693 self.visit_operand(op, location);
696 Rvalue::Discriminant(place) => {
699 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
704 Rvalue::NullaryOp(_op, ty) => {
705 self.visit_ty(ty, TyContext::Location(location));
708 Rvalue::Aggregate(kind, operands) => {
709 let kind = &$($mutability)? **kind;
711 AggregateKind::Array(ty) => {
712 self.visit_ty(ty, TyContext::Location(location));
714 AggregateKind::Tuple => {
723 self.visit_substs(substs, location);
725 AggregateKind::Closure(
729 self.visit_substs(closure_substs, location);
731 AggregateKind::Generator(
736 self.visit_substs(generator_substs, location);
740 for operand in operands {
741 self.visit_operand(operand, location);
745 Rvalue::ShallowInitBox(operand, ty) => {
746 self.visit_operand(operand, location);
747 self.visit_ty(ty, TyContext::Location(location));
752 fn super_operand(&mut self,
753 operand: & $($mutability)? Operand<'tcx>,
754 location: Location) {
756 Operand::Copy(place) => {
759 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
763 Operand::Move(place) => {
766 PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
770 Operand::Constant(constant) => {
771 self.visit_constant(constant, location);
776 fn super_ascribe_user_ty(&mut self,
777 place: & $($mutability)? Place<'tcx>,
778 _variance: & $($mutability)? ty::Variance,
779 user_ty: & $($mutability)? UserTypeProjection,
780 location: Location) {
783 PlaceContext::NonUse(NonUseContext::AscribeUserTy),
786 self.visit_user_type_projection(user_ty);
789 fn super_coverage(&mut self,
790 _coverage: & $($mutability)? Coverage,
791 _location: Location) {
794 fn super_retag(&mut self,
795 _kind: & $($mutability)? RetagKind,
796 place: & $($mutability)? Place<'tcx>,
797 location: Location) {
800 PlaceContext::MutatingUse(MutatingUseContext::Retag),
805 fn super_local_decl(&mut self,
807 local_decl: & $($mutability)? LocalDecl<'tcx>) {
818 self.visit_ty(ty, TyContext::LocalDecl {
820 source_info: *source_info,
822 if let Some(user_ty) = user_ty {
823 for (user_ty, _) in & $($mutability)? user_ty.contents {
824 self.visit_user_type_projection(user_ty);
827 self.visit_source_info(source_info);
830 fn super_var_debug_info(&mut self,
831 var_debug_info: & $($mutability)? VarDebugInfo<'tcx>) {
838 self.visit_source_info(source_info);
839 let location = START_BLOCK.start_location();
841 VarDebugInfoContents::Const(c) => self.visit_constant(c, location),
842 VarDebugInfoContents::Place(place) =>
845 PlaceContext::NonUse(NonUseContext::VarDebugInfo),
851 fn super_source_scope(&mut self,
852 _scope: & $($mutability)? SourceScope) {
855 fn super_constant(&mut self,
856 constant: & $($mutability)? Constant<'tcx>,
857 location: Location) {
864 self.visit_span(span);
865 drop(user_ty); // no visit method for this
867 ConstantKind::Ty(ct) => self.visit_const(ct, location),
868 ConstantKind::Val(_, t) => self.visit_ty(t, TyContext::Location(location)),
872 fn super_span(&mut self, _span: & $($mutability)? Span) {
875 fn super_source_info(&mut self, source_info: & $($mutability)? SourceInfo) {
881 self.visit_span(span);
882 self.visit_source_scope(scope);
885 fn super_user_type_projection(
887 _ty: & $($mutability)? UserTypeProjection,
891 fn super_user_type_annotation(
893 _index: UserTypeAnnotationIndex,
894 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
896 self.visit_span(& $($mutability)? ty.span);
897 self.visit_ty(& $($mutability)? ty.inferred_ty, TyContext::UserTy(ty.span));
900 fn super_ty(&mut self, _ty: $(& $mutability)? Ty<'tcx>) {
903 fn super_region(&mut self, _region: & $($mutability)? ty::Region<'tcx>) {
906 fn super_const(&mut self, _const: & $($mutability)? &'tcx ty::Const<'tcx>) {
909 fn super_substs(&mut self, _substs: & $($mutability)? SubstsRef<'tcx>) {
912 // Convenience methods
916 body: &$($mutability)? Body<'tcx>,
919 macro_rules! basic_blocks {
920 (mut) => (body.basic_blocks_mut());
921 () => (body.basic_blocks());
923 let basic_block = & $($mutability)? basic_blocks!($($mutability)?)[location.block];
924 if basic_block.statements.len() == location.statement_index {
925 if let Some(ref $($mutability)? terminator) = basic_block.terminator {
926 self.visit_terminator(terminator, location)
929 let statement = & $($mutability)?
930 basic_block.statements[location.statement_index];
931 self.visit_statement(statement, location)
938 macro_rules! visit_place_fns {
940 fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
944 place: &mut Place<'tcx>,
945 context: PlaceContext,
948 self.visit_local(&mut place.local, context, location);
950 if let Some(new_projection) = self.process_projection(&place.projection, location) {
951 place.projection = self.tcx().intern_place_elems(&new_projection);
955 fn process_projection<'a>(
957 projection: &'a [PlaceElem<'tcx>],
959 ) -> Option<Vec<PlaceElem<'tcx>>> {
960 let mut projection = Cow::Borrowed(projection);
962 for i in 0..projection.len() {
963 if let Some(&elem) = projection.get(i) {
964 if let Some(elem) = self.process_projection_elem(elem, location) {
965 // This converts the borrowed projection into `Cow::Owned(_)` and returns a
966 // clone of the projection so we can mutate and reintern later.
967 let vec = projection.to_mut();
974 Cow::Borrowed(_) => None,
975 Cow::Owned(vec) => Some(vec),
979 fn process_projection_elem(
981 elem: PlaceElem<'tcx>,
983 ) -> Option<PlaceElem<'tcx>> {
985 PlaceElem::Index(local) => {
986 let mut new_local = local;
989 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
993 if new_local == local { None } else { Some(PlaceElem::Index(new_local)) }
995 PlaceElem::Field(field, ty) => {
997 self.visit_ty(&mut new_ty, TyContext::Location(location));
998 if ty != new_ty { Some(PlaceElem::Field(field, new_ty)) } else { None }
1001 | PlaceElem::ConstantIndex { .. }
1002 | PlaceElem::Subslice { .. }
1003 | PlaceElem::Downcast(..) => None,
1009 fn visit_projection(
1011 place_ref: PlaceRef<'tcx>,
1012 context: PlaceContext,
1015 self.super_projection(place_ref, context, location);
1018 fn visit_projection_elem(
1021 proj_base: &[PlaceElem<'tcx>],
1022 elem: PlaceElem<'tcx>,
1023 context: PlaceContext,
1026 self.super_projection_elem(local, proj_base, elem, context, location);
1029 fn super_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
1030 let mut context = context;
1032 if !place.projection.is_empty() {
1033 if context.is_use() {
1034 // ^ Only change the context if it is a real use, not a "use" in debuginfo.
1035 context = if context.is_mutating_use() {
1036 PlaceContext::MutatingUse(MutatingUseContext::Projection)
1038 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
1043 self.visit_local(&place.local, context, location);
1045 self.visit_projection(place.as_ref(), context, location);
1048 fn super_projection(
1050 place_ref: PlaceRef<'tcx>,
1051 context: PlaceContext,
1054 // FIXME: Use PlaceRef::iter_projections, once that exists.
1055 let mut cursor = place_ref.projection;
1056 while let &[ref proj_base @ .., elem] = cursor {
1058 self.visit_projection_elem(place_ref.local, cursor, elem, context, location);
1062 fn super_projection_elem(
1065 _proj_base: &[PlaceElem<'tcx>],
1066 elem: PlaceElem<'tcx>,
1067 _context: PlaceContext,
1071 ProjectionElem::Field(_field, ty) => {
1072 self.visit_ty(ty, TyContext::Location(location));
1074 ProjectionElem::Index(local) => {
1077 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
1081 ProjectionElem::Deref
1082 | ProjectionElem::Subslice { from: _, to: _, from_end: _ }
1083 | ProjectionElem::ConstantIndex { offset: _, min_length: _, from_end: _ }
1084 | ProjectionElem::Downcast(_, _) => {}
1090 make_mir_visitor!(Visitor,);
1091 make_mir_visitor!(MutVisitor, mut);
1093 pub trait MirVisitable<'tcx> {
1094 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>);
1097 impl<'tcx> MirVisitable<'tcx> for Statement<'tcx> {
1098 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1099 visitor.visit_statement(self, location)
1103 impl<'tcx> MirVisitable<'tcx> for Terminator<'tcx> {
1104 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1105 visitor.visit_terminator(self, location)
1109 impl<'tcx> MirVisitable<'tcx> for Option<Terminator<'tcx>> {
1110 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1111 visitor.visit_terminator(self.as_ref().unwrap(), location)
1115 /// Extra information passed to `visit_ty` and friends to give context
1116 /// about where the type etc appears.
1118 pub enum TyContext {
1120 /// The index of the local variable we are visiting.
1123 /// The source location where this local variable was declared.
1124 source_info: SourceInfo,
1127 /// The inferred type of a user type annotation.
1130 /// The return type of the function.
1131 ReturnTy(SourceInfo),
1133 YieldTy(SourceInfo),
1135 /// A type found at some location.
1139 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1140 pub enum NonMutatingUseContext {
1141 /// Being inspected in some way, like loading a len.
1143 /// Consumed as part of an operand.
1145 /// Consumed as part of an operand.
1153 /// AddressOf for *const pointer.
1155 /// Used as base for another place, e.g., `x` in `x.y`. Will not mutate the place.
1156 /// For example, the projection `x.y` is not marked as a mutation in these cases:
1164 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1165 pub enum MutatingUseContext {
1166 /// Appears as LHS of an assignment.
1168 /// Output operand of an inline assembly block.
1170 /// Destination of a call.
1172 /// Destination of a yield.
1178 /// AddressOf for *mut pointer.
1180 /// Used as base for another place, e.g., `x` in `x.y`. Could potentially mutate the place.
1181 /// For example, the projection `x.y` is marked as a mutation in these cases:
1187 /// Retagging, a "Stacked Borrows" shadow state operation
1191 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1192 pub enum NonUseContext {
1193 /// Starting a storage live range.
1195 /// Ending a storage live range.
1197 /// User type annotation assertions for NLL.
1199 /// The data of a user variable, for debug info.
1203 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1204 pub enum PlaceContext {
1205 NonMutatingUse(NonMutatingUseContext),
1206 MutatingUse(MutatingUseContext),
1207 NonUse(NonUseContext),
1211 /// Returns `true` if this place context represents a drop.
1213 pub fn is_drop(&self) -> bool {
1214 matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
1217 /// Returns `true` if this place context represents a borrow.
1218 pub fn is_borrow(&self) -> bool {
1221 PlaceContext::NonMutatingUse(
1222 NonMutatingUseContext::SharedBorrow
1223 | NonMutatingUseContext::ShallowBorrow
1224 | NonMutatingUseContext::UniqueBorrow
1225 ) | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
1229 /// Returns `true` if this place context represents a storage live or storage dead marker.
1231 pub fn is_storage_marker(&self) -> bool {
1234 PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead)
1238 /// Returns `true` if this place context represents a use that potentially changes the value.
1240 pub fn is_mutating_use(&self) -> bool {
1241 matches!(self, PlaceContext::MutatingUse(..))
1244 /// Returns `true` if this place context represents a use.
1246 pub fn is_use(&self) -> bool {
1247 !matches!(self, PlaceContext::NonUse(..))
1250 /// Returns `true` if this place context represents an assignment statement.
1251 pub fn is_place_assignment(&self) -> bool {
1254 PlaceContext::MutatingUse(
1255 MutatingUseContext::Store
1256 | MutatingUseContext::Call
1257 | MutatingUseContext::AsmOutput,