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:
32 //! ```ignore (pseudo-rust)
33 //! fn super_basic_block_data(
35 //! block: BasicBlock,
36 //! data: & $($mutability)? BasicBlockData<'tcx>
38 //! let BasicBlockData {
44 //! for statement in statements {
45 //! self.visit_statement(block, statement);
52 //! Here we used `let BasicBlockData { <fields> } = *data` deliberately,
53 //! rather than writing `data.statements` in the body. This is because if one
54 //! adds a new field to `BasicBlockData`, one will be forced to revise this code,
55 //! and hence one will (hopefully) invoke the correct visit methods (if any).
57 //! For this to work, ALL MATCHES MUST BE EXHAUSTIVE IN FIELDS AND VARIANTS.
58 //! That means you never write `..` to skip over fields, nor do you write `_`
59 //! to skip over variants in a `match`.
61 //! The only place that `_` is acceptable is to match a field (or
62 //! variant argument) that does not require visiting, as in
63 //! `is_cleanup` above.
66 use crate::ty::subst::SubstsRef;
67 use crate::ty::{CanonicalUserTypeAnnotation, Ty};
70 macro_rules! make_mir_visitor {
71 ($visitor_trait_name:ident, $($mutability:ident)?) => {
72 pub trait $visitor_trait_name<'tcx> {
73 // Override these, and call `self.super_xxx` to revert back to the
78 body: &$($mutability)? Body<'tcx>,
80 self.super_body(body);
83 extra_body_methods!($($mutability)?);
85 fn visit_basic_block_data(
88 data: & $($mutability)? BasicBlockData<'tcx>,
90 self.super_basic_block_data(block, data);
93 fn visit_source_scope_data(
95 scope_data: & $($mutability)? SourceScopeData<'tcx>,
97 self.super_source_scope_data(scope_data);
102 statement: & $($mutability)? Statement<'tcx>,
105 self.super_statement(statement, location);
110 place: & $($mutability)? Place<'tcx>,
111 rvalue: & $($mutability)? Rvalue<'tcx>,
114 self.super_assign(place, rvalue, location);
119 terminator: & $($mutability)? Terminator<'tcx>,
122 self.super_terminator(terminator, location);
125 fn visit_assert_message(
127 msg: & $($mutability)? AssertMessage<'tcx>,
130 self.super_assert_message(msg, location);
135 rvalue: & $($mutability)? Rvalue<'tcx>,
138 self.super_rvalue(rvalue, location);
143 operand: & $($mutability)? Operand<'tcx>,
146 self.super_operand(operand, location);
149 fn visit_ascribe_user_ty(
151 place: & $($mutability)? Place<'tcx>,
152 variance: $(& $mutability)? ty::Variance,
153 user_ty: & $($mutability)? UserTypeProjection,
156 self.super_ascribe_user_ty(place, variance, user_ty, location);
161 coverage: & $($mutability)? Coverage,
164 self.super_coverage(coverage, location);
169 kind: $(& $mutability)? RetagKind,
170 place: & $($mutability)? Place<'tcx>,
173 self.super_retag(kind, place, location);
178 place: & $($mutability)? Place<'tcx>,
179 context: PlaceContext,
182 self.super_place(place, context, location);
185 visit_place_fns!($($mutability)?);
189 constant: & $($mutability)? Constant<'tcx>,
192 self.super_constant(constant, location);
197 span: $(& $mutability)? Span,
199 self.super_span(span);
202 fn visit_source_info(
204 source_info: & $($mutability)? SourceInfo,
206 self.super_source_info(source_info);
211 ty: $(& $mutability)? Ty<'tcx>,
217 fn visit_user_type_projection(
219 ty: & $($mutability)? UserTypeProjection,
221 self.super_user_type_projection(ty);
224 fn visit_user_type_annotation(
226 index: UserTypeAnnotationIndex,
227 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
229 self.super_user_type_annotation(index, ty);
234 region: $(& $mutability)? ty::Region<'tcx>,
237 self.super_region(region);
242 substs: & $($mutability)? SubstsRef<'tcx>,
245 self.super_substs(substs);
251 local_decl: & $($mutability)? LocalDecl<'tcx>,
253 self.super_local_decl(local, local_decl);
256 fn visit_var_debug_info(
258 var_debug_info: & $($mutability)* VarDebugInfo<'tcx>,
260 self.super_var_debug_info(var_debug_info);
265 _local: $(& $mutability)? Local,
266 _context: PlaceContext,
270 fn visit_source_scope(
272 scope: $(& $mutability)? SourceScope,
274 self.super_source_scope(scope);
277 // The `super_xxx` methods comprise the default behavior and are
278 // not meant to be overridden.
282 body: &$($mutability)? Body<'tcx>,
284 super_body!(self, body, $($mutability, true)?);
287 fn super_basic_block_data(&mut self,
289 data: & $($mutability)? BasicBlockData<'tcx>) {
297 for statement in statements {
298 let location = Location { block, statement_index: index };
299 self.visit_statement(statement, location);
303 if let Some(terminator) = terminator {
304 let location = Location { block, statement_index: index };
305 self.visit_terminator(terminator, location);
309 fn super_source_scope_data(
311 scope_data: & $($mutability)? SourceScopeData<'tcx>,
313 let SourceScopeData {
317 inlined_parent_scope,
321 self.visit_span($(& $mutability)? *span);
322 if let Some(parent_scope) = parent_scope {
323 self.visit_source_scope($(& $mutability)? *parent_scope);
325 if let Some((callee, callsite_span)) = inlined {
326 let location = START_BLOCK.start_location();
328 self.visit_span($(& $mutability)? *callsite_span);
330 let ty::Instance { def: callee_def, substs: callee_substs } = callee;
332 ty::InstanceDef::Item(_def_id) => {}
334 ty::InstanceDef::Intrinsic(_def_id) |
335 ty::InstanceDef::VTableShim(_def_id) |
336 ty::InstanceDef::ReifyShim(_def_id) |
337 ty::InstanceDef::Virtual(_def_id, _) |
338 ty::InstanceDef::ClosureOnceShim { call_once: _def_id, track_caller: _ } |
339 ty::InstanceDef::DropGlue(_def_id, None) => {}
341 ty::InstanceDef::FnPtrShim(_def_id, ty) |
342 ty::InstanceDef::DropGlue(_def_id, Some(ty)) |
343 ty::InstanceDef::CloneShim(_def_id, ty) => {
344 // FIXME(eddyb) use a better `TyContext` here.
345 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
348 self.visit_substs(callee_substs, location);
350 if let Some(inlined_parent_scope) = inlined_parent_scope {
351 self.visit_source_scope($(& $mutability)? *inlined_parent_scope);
355 fn super_statement(&mut self,
356 statement: & $($mutability)? Statement<'tcx>,
357 location: Location) {
363 self.visit_source_info(source_info);
365 StatementKind::Assign(
368 self.visit_assign(place, rvalue, location);
370 StatementKind::FakeRead(box (_, place)) => {
373 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
377 StatementKind::SetDiscriminant { place, .. } => {
380 PlaceContext::MutatingUse(MutatingUseContext::SetDiscriminant),
384 StatementKind::Deinit(place) => {
387 PlaceContext::MutatingUse(MutatingUseContext::Deinit),
391 StatementKind::StorageLive(local) => {
393 $(& $mutability)? *local,
394 PlaceContext::NonUse(NonUseContext::StorageLive),
398 StatementKind::StorageDead(local) => {
400 $(& $mutability)? *local,
401 PlaceContext::NonUse(NonUseContext::StorageDead),
405 StatementKind::Retag(kind, place) => {
406 self.visit_retag($(& $mutability)? *kind, place, location);
408 StatementKind::AscribeUserType(
409 box (place, user_ty),
412 self.visit_ascribe_user_ty(place, $(& $mutability)? *variance, user_ty, location);
414 StatementKind::Coverage(coverage) => {
420 StatementKind::Intrinsic(box ref $($mutability)? intrinsic) => {
422 NonDivergingIntrinsic::Assume(op) => self.visit_operand(op, location),
423 NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping { src, dst, count }) => {
424 self.visit_operand(src, location);
425 self.visit_operand(dst, location);
426 self.visit_operand(count, location);
430 StatementKind::Nop => {}
434 fn super_assign(&mut self,
435 place: &$($mutability)? Place<'tcx>,
436 rvalue: &$($mutability)? Rvalue<'tcx>,
437 location: Location) {
440 PlaceContext::MutatingUse(MutatingUseContext::Store),
443 self.visit_rvalue(rvalue, location);
446 fn super_terminator(&mut self,
447 terminator: &$($mutability)? Terminator<'tcx>,
448 location: Location) {
449 let Terminator { source_info, kind } = terminator;
451 self.visit_source_info(source_info);
453 TerminatorKind::Goto { .. } |
454 TerminatorKind::Resume |
455 TerminatorKind::Abort |
456 TerminatorKind::GeneratorDrop |
457 TerminatorKind::Unreachable |
458 TerminatorKind::FalseEdge { .. } |
459 TerminatorKind::FalseUnwind { .. } => {}
461 TerminatorKind::Return => {
462 // `return` logically moves from the return place `_0`. Note that the place
463 // cannot be changed by any visitor, though.
464 let $($mutability)? local = RETURN_PLACE;
466 $(& $mutability)? local,
467 PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
474 "`MutVisitor` tried to mutate return place of `return` terminator"
478 TerminatorKind::SwitchInt {
483 self.visit_operand(discr, location);
484 self.visit_ty($(& $mutability)? *switch_ty, TyContext::Location(location));
487 TerminatorKind::Drop {
494 PlaceContext::MutatingUse(MutatingUseContext::Drop),
499 TerminatorKind::DropAndReplace {
507 PlaceContext::MutatingUse(MutatingUseContext::Drop),
510 self.visit_operand(value, location);
513 TerminatorKind::Call {
522 self.visit_operand(func, location);
524 self.visit_operand(arg, location);
528 PlaceContext::MutatingUse(MutatingUseContext::Call),
533 TerminatorKind::Assert {
540 self.visit_operand(cond, location);
541 self.visit_assert_message(msg, location);
544 TerminatorKind::Yield {
550 self.visit_operand(value, location);
553 PlaceContext::MutatingUse(MutatingUseContext::Yield),
558 TerminatorKind::InlineAsm {
568 InlineAsmOperand::In { value, .. } => {
569 self.visit_operand(value, location);
571 InlineAsmOperand::Out { place: Some(place), .. } => {
574 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
578 InlineAsmOperand::InOut { in_value, out_place, .. } => {
579 self.visit_operand(in_value, location);
580 if let Some(out_place) = out_place {
583 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
588 InlineAsmOperand::Const { value }
589 | InlineAsmOperand::SymFn { value } => {
590 self.visit_constant(value, location);
592 InlineAsmOperand::Out { place: None, .. }
593 | InlineAsmOperand::SymStatic { def_id: _ } => {}
600 fn super_assert_message(&mut self,
601 msg: & $($mutability)? AssertMessage<'tcx>,
602 location: Location) {
603 use crate::mir::AssertKind::*;
605 BoundsCheck { len, index } => {
606 self.visit_operand(len, location);
607 self.visit_operand(index, location);
609 Overflow(_, l, r) => {
610 self.visit_operand(l, location);
611 self.visit_operand(r, location);
613 OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
614 self.visit_operand(op, location);
616 ResumedAfterReturn(_) | ResumedAfterPanic(_) => {
622 fn super_rvalue(&mut self,
623 rvalue: & $($mutability)? Rvalue<'tcx>,
624 location: Location) {
626 Rvalue::Use(operand) => {
627 self.visit_operand(operand, location);
630 Rvalue::Repeat(value, _) => {
631 self.visit_operand(value, location);
634 Rvalue::ThreadLocalRef(_) => {}
636 Rvalue::Ref(r, bk, path) => {
637 self.visit_region($(& $mutability)? *r, location);
639 BorrowKind::Shared => PlaceContext::NonMutatingUse(
640 NonMutatingUseContext::SharedBorrow
642 BorrowKind::Shallow => PlaceContext::NonMutatingUse(
643 NonMutatingUseContext::ShallowBorrow
645 BorrowKind::Unique => PlaceContext::NonMutatingUse(
646 NonMutatingUseContext::UniqueBorrow
648 BorrowKind::Mut { .. } =>
649 PlaceContext::MutatingUse(MutatingUseContext::Borrow),
651 self.visit_place(path, ctx, location);
653 Rvalue::CopyForDeref(place) => {
656 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
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($(& $mutability)? *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($(& $mutability)? *ty, TyContext::Location(location));
708 Rvalue::Aggregate(kind, operands) => {
709 let kind = &$($mutability)? **kind;
711 AggregateKind::Array(ty) => {
712 self.visit_ty($(& $mutability)? *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($(& $mutability)? *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($(& $mutability)? *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(
832 var_debug_info: & $($mutability)? VarDebugInfo<'tcx>
840 self.visit_source_info(source_info);
841 let location = START_BLOCK.start_location();
843 VarDebugInfoContents::Const(c) => self.visit_constant(c, location),
844 VarDebugInfoContents::Place(place) =>
847 PlaceContext::NonUse(NonUseContext::VarDebugInfo),
850 VarDebugInfoContents::Composite { ty, fragments } => {
851 // FIXME(eddyb) use a better `TyContext` here.
852 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
853 for VarDebugInfoFragment { projection: _, contents } in fragments {
856 PlaceContext::NonUse(NonUseContext::VarDebugInfo),
864 fn super_source_scope(
866 _scope: $(& $mutability)? SourceScope
871 constant: & $($mutability)? Constant<'tcx>,
880 self.visit_span($(& $mutability)? *span);
881 drop(user_ty); // no visit method for this
883 ConstantKind::Ty(_) => {}
884 ConstantKind::Val(_, ty) => self.visit_ty($(& $mutability)? *ty, TyContext::Location(location)),
885 ConstantKind::Unevaluated(_, ty) => self.visit_ty($(& $mutability)? *ty, TyContext::Location(location)),
889 fn super_span(&mut self, _span: $(& $mutability)? Span) {
892 fn super_source_info(&mut self, source_info: & $($mutability)? SourceInfo) {
898 self.visit_span($(& $mutability)? *span);
899 self.visit_source_scope($(& $mutability)? *scope);
902 fn super_user_type_projection(
904 _ty: & $($mutability)? UserTypeProjection,
908 fn super_user_type_annotation(
910 _index: UserTypeAnnotationIndex,
911 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
913 self.visit_span($(& $mutability)? ty.span);
914 self.visit_ty($(& $mutability)? ty.inferred_ty, TyContext::UserTy(ty.span));
917 fn super_ty(&mut self, _ty: $(& $mutability)? Ty<'tcx>) {
920 fn super_region(&mut self, _region: $(& $mutability)? ty::Region<'tcx>) {
923 fn super_substs(&mut self, _substs: & $($mutability)? SubstsRef<'tcx>) {
926 // Convenience methods
930 body: &$($mutability)? Body<'tcx>,
933 let basic_block = & $($mutability)? basic_blocks!(body, $($mutability, true)?)[location.block];
934 if basic_block.statements.len() == location.statement_index {
935 if let Some(ref $($mutability)? terminator) = basic_block.terminator {
936 self.visit_terminator(terminator, location)
939 let statement = & $($mutability)?
940 basic_block.statements[location.statement_index];
941 self.visit_statement(statement, location)
948 macro_rules! basic_blocks {
949 ($body:ident, mut, true) => {
950 $body.basic_blocks.as_mut()
952 ($body:ident, mut, false) => {
953 $body.basic_blocks.as_mut_preserves_cfg()
960 macro_rules! basic_blocks_iter {
961 ($body:ident, mut, $invalidate:tt) => {
962 basic_blocks!($body, mut, $invalidate).iter_enumerated_mut()
965 basic_blocks!($body,).iter_enumerated()
969 macro_rules! extra_body_methods {
971 fn visit_body_preserves_cfg(&mut self, body: &mut Body<'tcx>) {
972 self.super_body_preserves_cfg(body);
975 fn super_body_preserves_cfg(&mut self, body: &mut Body<'tcx>) {
976 super_body!(self, body, mut, false);
982 macro_rules! super_body {
983 ($self:ident, $body:ident, $($mutability:ident, $invalidate:tt)?) => {
984 let span = $body.span;
985 if let Some(gen) = &$($mutability)? $body.generator {
986 if let Some(yield_ty) = $(& $mutability)? gen.yield_ty {
989 TyContext::YieldTy(SourceInfo::outermost(span))
994 for (bb, data) in basic_blocks_iter!($body, $($mutability, $invalidate)?) {
995 $self.visit_basic_block_data(bb, data);
998 for scope in &$($mutability)? $body.source_scopes {
999 $self.visit_source_scope_data(scope);
1003 $(& $mutability)? $body.return_ty(),
1004 TyContext::ReturnTy(SourceInfo::outermost($body.span))
1007 for local in $body.local_decls.indices() {
1008 $self.visit_local_decl(local, & $($mutability)? $body.local_decls[local]);
1011 #[allow(unused_macro_rules)]
1012 macro_rules! type_annotations {
1013 (mut) => ($body.user_type_annotations.iter_enumerated_mut());
1014 () => ($body.user_type_annotations.iter_enumerated());
1017 for (index, annotation) in type_annotations!($($mutability)?) {
1018 $self.visit_user_type_annotation(
1023 for var_debug_info in &$($mutability)? $body.var_debug_info {
1024 $self.visit_var_debug_info(var_debug_info);
1027 $self.visit_span($(& $mutability)? $body.span);
1029 for const_ in &$($mutability)? $body.required_consts {
1030 let location = START_BLOCK.start_location();
1031 $self.visit_constant(const_, location);
1036 macro_rules! visit_place_fns {
1038 fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
1042 place: &mut Place<'tcx>,
1043 context: PlaceContext,
1046 self.visit_local(&mut place.local, context, location);
1048 if let Some(new_projection) = self.process_projection(&place.projection, location) {
1049 place.projection = self.tcx().intern_place_elems(&new_projection);
1053 fn process_projection<'a>(
1055 projection: &'a [PlaceElem<'tcx>],
1057 ) -> Option<Vec<PlaceElem<'tcx>>> {
1058 let mut projection = Cow::Borrowed(projection);
1060 for i in 0..projection.len() {
1061 if let Some(&elem) = projection.get(i) {
1062 if let Some(elem) = self.process_projection_elem(elem, location) {
1063 // This converts the borrowed projection into `Cow::Owned(_)` and returns a
1064 // clone of the projection so we can mutate and reintern later.
1065 let vec = projection.to_mut();
1072 Cow::Borrowed(_) => None,
1073 Cow::Owned(vec) => Some(vec),
1077 fn process_projection_elem(
1079 elem: PlaceElem<'tcx>,
1081 ) -> Option<PlaceElem<'tcx>> {
1083 PlaceElem::Index(local) => {
1084 let mut new_local = local;
1087 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
1091 if new_local == local { None } else { Some(PlaceElem::Index(new_local)) }
1093 PlaceElem::Field(field, ty) => {
1094 let mut new_ty = ty;
1095 self.visit_ty(&mut new_ty, TyContext::Location(location));
1096 if ty != new_ty { Some(PlaceElem::Field(field, new_ty)) } else { None }
1098 PlaceElem::OpaqueCast(ty) => {
1099 let mut new_ty = ty;
1100 self.visit_ty(&mut new_ty, TyContext::Location(location));
1101 if ty != new_ty { Some(PlaceElem::OpaqueCast(new_ty)) } else { None }
1104 | PlaceElem::ConstantIndex { .. }
1105 | PlaceElem::Subslice { .. }
1106 | PlaceElem::Downcast(..) => None,
1112 fn visit_projection(
1114 place_ref: PlaceRef<'tcx>,
1115 context: PlaceContext,
1118 self.super_projection(place_ref, context, location);
1121 fn visit_projection_elem(
1124 proj_base: &[PlaceElem<'tcx>],
1125 elem: PlaceElem<'tcx>,
1126 context: PlaceContext,
1129 self.super_projection_elem(local, proj_base, elem, context, location);
1132 fn super_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
1133 let mut context = context;
1135 if !place.projection.is_empty() {
1136 if context.is_use() {
1137 // ^ Only change the context if it is a real use, not a "use" in debuginfo.
1138 context = if context.is_mutating_use() {
1139 PlaceContext::MutatingUse(MutatingUseContext::Projection)
1141 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
1146 self.visit_local(place.local, context, location);
1148 self.visit_projection(place.as_ref(), context, location);
1151 fn super_projection(
1153 place_ref: PlaceRef<'tcx>,
1154 context: PlaceContext,
1157 for (base, elem) in place_ref.iter_projections().rev() {
1158 let base_proj = base.projection;
1159 self.visit_projection_elem(place_ref.local, base_proj, elem, context, location);
1163 fn super_projection_elem(
1166 _proj_base: &[PlaceElem<'tcx>],
1167 elem: PlaceElem<'tcx>,
1168 _context: PlaceContext,
1172 ProjectionElem::OpaqueCast(ty) | ProjectionElem::Field(_, ty) => {
1173 self.visit_ty(ty, TyContext::Location(location));
1175 ProjectionElem::Index(local) => {
1178 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
1182 ProjectionElem::Deref
1183 | ProjectionElem::Subslice { from: _, to: _, from_end: _ }
1184 | ProjectionElem::ConstantIndex { offset: _, min_length: _, from_end: _ }
1185 | ProjectionElem::Downcast(_, _) => {}
1191 make_mir_visitor!(Visitor,);
1192 make_mir_visitor!(MutVisitor, mut);
1194 pub trait MirVisitable<'tcx> {
1195 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>);
1198 impl<'tcx> MirVisitable<'tcx> for Statement<'tcx> {
1199 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1200 visitor.visit_statement(self, location)
1204 impl<'tcx> MirVisitable<'tcx> for Terminator<'tcx> {
1205 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1206 visitor.visit_terminator(self, location)
1210 impl<'tcx> MirVisitable<'tcx> for Option<Terminator<'tcx>> {
1211 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1212 visitor.visit_terminator(self.as_ref().unwrap(), location)
1216 /// Extra information passed to `visit_ty` and friends to give context
1217 /// about where the type etc appears.
1219 pub enum TyContext {
1221 /// The index of the local variable we are visiting.
1224 /// The source location where this local variable was declared.
1225 source_info: SourceInfo,
1228 /// The inferred type of a user type annotation.
1231 /// The return type of the function.
1232 ReturnTy(SourceInfo),
1234 YieldTy(SourceInfo),
1236 /// A type found at some location.
1240 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1241 pub enum NonMutatingUseContext {
1242 /// Being inspected in some way, like loading a len.
1244 /// Consumed as part of an operand.
1246 /// Consumed as part of an operand.
1254 /// AddressOf for *const pointer.
1256 /// Used as base for another place, e.g., `x` in `x.y`. Will not mutate the place.
1257 /// For example, the projection `x.y` is not marked as a mutation in these cases:
1258 /// ```ignore (illustrative)
1265 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1266 pub enum MutatingUseContext {
1267 /// Appears as LHS of an assignment.
1269 /// Appears on `SetDiscriminant`
1271 /// Appears on `Deinit`
1273 /// Output operand of an inline assembly block.
1275 /// Destination of a call.
1277 /// Destination of a yield.
1283 /// AddressOf for *mut pointer.
1285 /// Used as base for another place, e.g., `x` in `x.y`. Could potentially mutate the place.
1286 /// For example, the projection `x.y` is marked as a mutation in these cases:
1287 /// ```ignore (illustrative)
1292 /// Retagging, a "Stacked Borrows" shadow state operation
1296 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1297 pub enum NonUseContext {
1298 /// Starting a storage live range.
1300 /// Ending a storage live range.
1302 /// User type annotation assertions for NLL.
1304 /// The data of a user variable, for debug info.
1308 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1309 pub enum PlaceContext {
1310 NonMutatingUse(NonMutatingUseContext),
1311 MutatingUse(MutatingUseContext),
1312 NonUse(NonUseContext),
1316 /// Returns `true` if this place context represents a drop.
1318 pub fn is_drop(&self) -> bool {
1319 matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
1322 /// Returns `true` if this place context represents a borrow.
1323 pub fn is_borrow(&self) -> bool {
1326 PlaceContext::NonMutatingUse(
1327 NonMutatingUseContext::SharedBorrow
1328 | NonMutatingUseContext::ShallowBorrow
1329 | NonMutatingUseContext::UniqueBorrow
1330 ) | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
1334 /// Returns `true` if this place context represents an address-of.
1335 pub fn is_address_of(&self) -> bool {
1338 PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf)
1339 | PlaceContext::MutatingUse(MutatingUseContext::AddressOf)
1343 /// Returns `true` if this place context represents a storage live or storage dead marker.
1345 pub fn is_storage_marker(&self) -> bool {
1348 PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead)
1352 /// Returns `true` if this place context represents a use that potentially changes the value.
1354 pub fn is_mutating_use(&self) -> bool {
1355 matches!(self, PlaceContext::MutatingUse(..))
1358 /// Returns `true` if this place context represents a use.
1360 pub fn is_use(&self) -> bool {
1361 !matches!(self, PlaceContext::NonUse(..))
1364 /// Returns `true` if this place context represents an assignment statement.
1365 pub fn is_place_assignment(&self) -> bool {
1368 PlaceContext::MutatingUse(
1369 MutatingUseContext::Store
1370 | MutatingUseContext::Call
1371 | MutatingUseContext::AsmOutput,