5 //! There are two visitors, one for immutable and one for mutable references,
6 //! but both are generated by the `make_mir_visitor` macro.
7 //! The code is written according 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 to override `visit_foo` for types you 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 {
482 self.visit_operand(discr, location);
485 TerminatorKind::Drop {
492 PlaceContext::MutatingUse(MutatingUseContext::Drop),
497 TerminatorKind::DropAndReplace {
505 PlaceContext::MutatingUse(MutatingUseContext::Drop),
508 self.visit_operand(value, location);
511 TerminatorKind::Call {
520 self.visit_operand(func, location);
522 self.visit_operand(arg, location);
526 PlaceContext::MutatingUse(MutatingUseContext::Call),
531 TerminatorKind::Assert {
538 self.visit_operand(cond, location);
539 self.visit_assert_message(msg, location);
542 TerminatorKind::Yield {
548 self.visit_operand(value, location);
551 PlaceContext::MutatingUse(MutatingUseContext::Yield),
556 TerminatorKind::InlineAsm {
566 InlineAsmOperand::In { value, .. } => {
567 self.visit_operand(value, location);
569 InlineAsmOperand::Out { place: Some(place), .. } => {
572 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
576 InlineAsmOperand::InOut { in_value, out_place, .. } => {
577 self.visit_operand(in_value, location);
578 if let Some(out_place) = out_place {
581 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
586 InlineAsmOperand::Const { value }
587 | InlineAsmOperand::SymFn { value } => {
588 self.visit_constant(value, location);
590 InlineAsmOperand::Out { place: None, .. }
591 | InlineAsmOperand::SymStatic { def_id: _ } => {}
598 fn super_assert_message(&mut self,
599 msg: & $($mutability)? AssertMessage<'tcx>,
600 location: Location) {
601 use crate::mir::AssertKind::*;
603 BoundsCheck { len, index } => {
604 self.visit_operand(len, location);
605 self.visit_operand(index, location);
607 Overflow(_, l, r) => {
608 self.visit_operand(l, location);
609 self.visit_operand(r, location);
611 OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
612 self.visit_operand(op, location);
614 ResumedAfterReturn(_) | ResumedAfterPanic(_) => {
620 fn super_rvalue(&mut self,
621 rvalue: & $($mutability)? Rvalue<'tcx>,
622 location: Location) {
624 Rvalue::Use(operand) => {
625 self.visit_operand(operand, location);
628 Rvalue::Repeat(value, _) => {
629 self.visit_operand(value, location);
632 Rvalue::ThreadLocalRef(_) => {}
634 Rvalue::Ref(r, bk, path) => {
635 self.visit_region($(& $mutability)? *r, location);
637 BorrowKind::Shared => PlaceContext::NonMutatingUse(
638 NonMutatingUseContext::SharedBorrow
640 BorrowKind::Shallow => PlaceContext::NonMutatingUse(
641 NonMutatingUseContext::ShallowBorrow
643 BorrowKind::Unique => PlaceContext::NonMutatingUse(
644 NonMutatingUseContext::UniqueBorrow
646 BorrowKind::Mut { .. } =>
647 PlaceContext::MutatingUse(MutatingUseContext::Borrow),
649 self.visit_place(path, ctx, location);
651 Rvalue::CopyForDeref(place) => {
654 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
659 Rvalue::AddressOf(m, path) => {
661 Mutability::Mut => PlaceContext::MutatingUse(
662 MutatingUseContext::AddressOf
664 Mutability::Not => PlaceContext::NonMutatingUse(
665 NonMutatingUseContext::AddressOf
668 self.visit_place(path, ctx, location);
671 Rvalue::Len(path) => {
674 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
679 Rvalue::Cast(_cast_kind, operand, ty) => {
680 self.visit_operand(operand, location);
681 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
684 Rvalue::BinaryOp(_bin_op, box(lhs, rhs))
685 | Rvalue::CheckedBinaryOp(_bin_op, box(lhs, rhs)) => {
686 self.visit_operand(lhs, location);
687 self.visit_operand(rhs, location);
690 Rvalue::UnaryOp(_un_op, op) => {
691 self.visit_operand(op, location);
694 Rvalue::Discriminant(place) => {
697 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
702 Rvalue::NullaryOp(_op, ty) => {
703 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
706 Rvalue::Aggregate(kind, operands) => {
707 let kind = &$($mutability)? **kind;
709 AggregateKind::Array(ty) => {
710 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
712 AggregateKind::Tuple => {
721 self.visit_substs(substs, location);
723 AggregateKind::Closure(
727 self.visit_substs(closure_substs, location);
729 AggregateKind::Generator(
734 self.visit_substs(generator_substs, location);
738 for operand in operands {
739 self.visit_operand(operand, location);
743 Rvalue::ShallowInitBox(operand, ty) => {
744 self.visit_operand(operand, location);
745 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
750 fn super_operand(&mut self,
751 operand: & $($mutability)? Operand<'tcx>,
752 location: Location) {
754 Operand::Copy(place) => {
757 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
761 Operand::Move(place) => {
764 PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
768 Operand::Constant(constant) => {
769 self.visit_constant(constant, location);
774 fn super_ascribe_user_ty(&mut self,
775 place: & $($mutability)? Place<'tcx>,
776 _variance: $(& $mutability)? ty::Variance,
777 user_ty: & $($mutability)? UserTypeProjection,
778 location: Location) {
781 PlaceContext::NonUse(NonUseContext::AscribeUserTy),
784 self.visit_user_type_projection(user_ty);
787 fn super_coverage(&mut self,
788 _coverage: & $($mutability)? Coverage,
789 _location: Location) {
792 fn super_retag(&mut self,
793 _kind: $(& $mutability)? RetagKind,
794 place: & $($mutability)? Place<'tcx>,
795 location: Location) {
798 PlaceContext::MutatingUse(MutatingUseContext::Retag),
803 fn super_local_decl(&mut self,
805 local_decl: & $($mutability)? LocalDecl<'tcx>) {
816 self.visit_ty($(& $mutability)? *ty, TyContext::LocalDecl {
818 source_info: *source_info,
820 if let Some(user_ty) = user_ty {
821 for (user_ty, _) in & $($mutability)? user_ty.contents {
822 self.visit_user_type_projection(user_ty);
825 self.visit_source_info(source_info);
828 fn super_var_debug_info(
830 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),
848 VarDebugInfoContents::Composite { ty, fragments } => {
849 // FIXME(eddyb) use a better `TyContext` here.
850 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
851 for VarDebugInfoFragment { projection: _, contents } in fragments {
854 PlaceContext::NonUse(NonUseContext::VarDebugInfo),
862 fn super_source_scope(
864 _scope: $(& $mutability)? SourceScope
869 constant: & $($mutability)? Constant<'tcx>,
878 self.visit_span($(& $mutability)? *span);
879 drop(user_ty); // no visit method for this
881 ConstantKind::Ty(_) => {}
882 ConstantKind::Val(_, ty) => self.visit_ty($(& $mutability)? *ty, TyContext::Location(location)),
883 ConstantKind::Unevaluated(_, ty) => self.visit_ty($(& $mutability)? *ty, TyContext::Location(location)),
887 fn super_span(&mut self, _span: $(& $mutability)? Span) {
890 fn super_source_info(&mut self, source_info: & $($mutability)? SourceInfo) {
896 self.visit_span($(& $mutability)? *span);
897 self.visit_source_scope($(& $mutability)? *scope);
900 fn super_user_type_projection(
902 _ty: & $($mutability)? UserTypeProjection,
906 fn super_user_type_annotation(
908 _index: UserTypeAnnotationIndex,
909 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
911 self.visit_span($(& $mutability)? ty.span);
912 self.visit_ty($(& $mutability)? ty.inferred_ty, TyContext::UserTy(ty.span));
915 fn super_ty(&mut self, _ty: $(& $mutability)? Ty<'tcx>) {
918 fn super_region(&mut self, _region: $(& $mutability)? ty::Region<'tcx>) {
921 fn super_substs(&mut self, _substs: & $($mutability)? SubstsRef<'tcx>) {
924 // Convenience methods
928 body: &$($mutability)? Body<'tcx>,
931 let basic_block = & $($mutability)? basic_blocks!(body, $($mutability, true)?)[location.block];
932 if basic_block.statements.len() == location.statement_index {
933 if let Some(ref $($mutability)? terminator) = basic_block.terminator {
934 self.visit_terminator(terminator, location)
937 let statement = & $($mutability)?
938 basic_block.statements[location.statement_index];
939 self.visit_statement(statement, location)
946 macro_rules! basic_blocks {
947 ($body:ident, mut, true) => {
948 $body.basic_blocks.as_mut()
950 ($body:ident, mut, false) => {
951 $body.basic_blocks.as_mut_preserves_cfg()
958 macro_rules! basic_blocks_iter {
959 ($body:ident, mut, $invalidate:tt) => {
960 basic_blocks!($body, mut, $invalidate).iter_enumerated_mut()
963 basic_blocks!($body,).iter_enumerated()
967 macro_rules! extra_body_methods {
969 fn visit_body_preserves_cfg(&mut self, body: &mut Body<'tcx>) {
970 self.super_body_preserves_cfg(body);
973 fn super_body_preserves_cfg(&mut self, body: &mut Body<'tcx>) {
974 super_body!(self, body, mut, false);
980 macro_rules! super_body {
981 ($self:ident, $body:ident, $($mutability:ident, $invalidate:tt)?) => {
982 let span = $body.span;
983 if let Some(gen) = &$($mutability)? $body.generator {
984 if let Some(yield_ty) = $(& $mutability)? gen.yield_ty {
987 TyContext::YieldTy(SourceInfo::outermost(span))
992 for (bb, data) in basic_blocks_iter!($body, $($mutability, $invalidate)?) {
993 $self.visit_basic_block_data(bb, data);
996 for scope in &$($mutability)? $body.source_scopes {
997 $self.visit_source_scope_data(scope);
1001 $(& $mutability)? $body.return_ty(),
1002 TyContext::ReturnTy(SourceInfo::outermost($body.span))
1005 for local in $body.local_decls.indices() {
1006 $self.visit_local_decl(local, & $($mutability)? $body.local_decls[local]);
1009 #[allow(unused_macro_rules)]
1010 macro_rules! type_annotations {
1011 (mut) => ($body.user_type_annotations.iter_enumerated_mut());
1012 () => ($body.user_type_annotations.iter_enumerated());
1015 for (index, annotation) in type_annotations!($($mutability)?) {
1016 $self.visit_user_type_annotation(
1021 for var_debug_info in &$($mutability)? $body.var_debug_info {
1022 $self.visit_var_debug_info(var_debug_info);
1025 $self.visit_span($(& $mutability)? $body.span);
1027 for const_ in &$($mutability)? $body.required_consts {
1028 let location = START_BLOCK.start_location();
1029 $self.visit_constant(const_, location);
1034 macro_rules! visit_place_fns {
1036 fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
1040 place: &mut Place<'tcx>,
1041 context: PlaceContext,
1044 self.visit_local(&mut place.local, context, location);
1046 if let Some(new_projection) = self.process_projection(&place.projection, location) {
1047 place.projection = self.tcx().intern_place_elems(&new_projection);
1051 fn process_projection<'a>(
1053 projection: &'a [PlaceElem<'tcx>],
1055 ) -> Option<Vec<PlaceElem<'tcx>>> {
1056 let mut projection = Cow::Borrowed(projection);
1058 for i in 0..projection.len() {
1059 if let Some(&elem) = projection.get(i) {
1060 if let Some(elem) = self.process_projection_elem(elem, location) {
1061 // This converts the borrowed projection into `Cow::Owned(_)` and returns a
1062 // clone of the projection so we can mutate and reintern later.
1063 let vec = projection.to_mut();
1070 Cow::Borrowed(_) => None,
1071 Cow::Owned(vec) => Some(vec),
1075 fn process_projection_elem(
1077 elem: PlaceElem<'tcx>,
1079 ) -> Option<PlaceElem<'tcx>> {
1081 PlaceElem::Index(local) => {
1082 let mut new_local = local;
1085 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
1089 if new_local == local { None } else { Some(PlaceElem::Index(new_local)) }
1091 PlaceElem::Field(field, ty) => {
1092 let mut new_ty = ty;
1093 self.visit_ty(&mut new_ty, TyContext::Location(location));
1094 if ty != new_ty { Some(PlaceElem::Field(field, new_ty)) } else { None }
1096 PlaceElem::OpaqueCast(ty) => {
1097 let mut new_ty = ty;
1098 self.visit_ty(&mut new_ty, TyContext::Location(location));
1099 if ty != new_ty { Some(PlaceElem::OpaqueCast(new_ty)) } else { None }
1102 | PlaceElem::ConstantIndex { .. }
1103 | PlaceElem::Subslice { .. }
1104 | PlaceElem::Downcast(..) => None,
1110 fn visit_projection(
1112 place_ref: PlaceRef<'tcx>,
1113 context: PlaceContext,
1116 self.super_projection(place_ref, context, location);
1119 fn visit_projection_elem(
1122 proj_base: &[PlaceElem<'tcx>],
1123 elem: PlaceElem<'tcx>,
1124 context: PlaceContext,
1127 self.super_projection_elem(local, proj_base, elem, context, location);
1130 fn super_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
1131 let mut context = context;
1133 if !place.projection.is_empty() {
1134 if context.is_use() {
1135 // ^ Only change the context if it is a real use, not a "use" in debuginfo.
1136 context = if context.is_mutating_use() {
1137 PlaceContext::MutatingUse(MutatingUseContext::Projection)
1139 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
1144 self.visit_local(place.local, context, location);
1146 self.visit_projection(place.as_ref(), context, location);
1149 fn super_projection(
1151 place_ref: PlaceRef<'tcx>,
1152 context: PlaceContext,
1155 for (base, elem) in place_ref.iter_projections().rev() {
1156 let base_proj = base.projection;
1157 self.visit_projection_elem(place_ref.local, base_proj, elem, context, location);
1161 fn super_projection_elem(
1164 _proj_base: &[PlaceElem<'tcx>],
1165 elem: PlaceElem<'tcx>,
1166 _context: PlaceContext,
1170 ProjectionElem::OpaqueCast(ty) | ProjectionElem::Field(_, ty) => {
1171 self.visit_ty(ty, TyContext::Location(location));
1173 ProjectionElem::Index(local) => {
1176 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
1180 ProjectionElem::Deref
1181 | ProjectionElem::Subslice { from: _, to: _, from_end: _ }
1182 | ProjectionElem::ConstantIndex { offset: _, min_length: _, from_end: _ }
1183 | ProjectionElem::Downcast(_, _) => {}
1189 make_mir_visitor!(Visitor,);
1190 make_mir_visitor!(MutVisitor, mut);
1192 pub trait MirVisitable<'tcx> {
1193 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>);
1196 impl<'tcx> MirVisitable<'tcx> for Statement<'tcx> {
1197 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1198 visitor.visit_statement(self, location)
1202 impl<'tcx> MirVisitable<'tcx> for Terminator<'tcx> {
1203 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1204 visitor.visit_terminator(self, location)
1208 impl<'tcx> MirVisitable<'tcx> for Option<Terminator<'tcx>> {
1209 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1210 visitor.visit_terminator(self.as_ref().unwrap(), location)
1214 /// Extra information passed to `visit_ty` and friends to give context
1215 /// about where the type etc appears.
1217 pub enum TyContext {
1219 /// The index of the local variable we are visiting.
1222 /// The source location where this local variable was declared.
1223 source_info: SourceInfo,
1226 /// The inferred type of a user type annotation.
1229 /// The return type of the function.
1230 ReturnTy(SourceInfo),
1232 YieldTy(SourceInfo),
1234 /// A type found at some location.
1238 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1239 pub enum NonMutatingUseContext {
1240 /// Being inspected in some way, like loading a len.
1242 /// Consumed as part of an operand.
1244 /// Consumed as part of an operand.
1252 /// AddressOf for *const pointer.
1254 /// Used as base for another place, e.g., `x` in `x.y`. Will not mutate the place.
1255 /// For example, the projection `x.y` is not marked as a mutation in these cases:
1256 /// ```ignore (illustrative)
1263 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1264 pub enum MutatingUseContext {
1265 /// Appears as LHS of an assignment.
1267 /// Appears on `SetDiscriminant`
1269 /// Appears on `Deinit`
1271 /// Output operand of an inline assembly block.
1273 /// Destination of a call.
1275 /// Destination of a yield.
1281 /// AddressOf for *mut pointer.
1283 /// Used as base for another place, e.g., `x` in `x.y`. Could potentially mutate the place.
1284 /// For example, the projection `x.y` is marked as a mutation in these cases:
1285 /// ```ignore (illustrative)
1290 /// Retagging, a "Stacked Borrows" shadow state operation
1294 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1295 pub enum NonUseContext {
1296 /// Starting a storage live range.
1298 /// Ending a storage live range.
1300 /// User type annotation assertions for NLL.
1302 /// The data of a user variable, for debug info.
1306 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1307 pub enum PlaceContext {
1308 NonMutatingUse(NonMutatingUseContext),
1309 MutatingUse(MutatingUseContext),
1310 NonUse(NonUseContext),
1314 /// Returns `true` if this place context represents a drop.
1316 pub fn is_drop(&self) -> bool {
1317 matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
1320 /// Returns `true` if this place context represents a borrow.
1321 pub fn is_borrow(&self) -> bool {
1324 PlaceContext::NonMutatingUse(
1325 NonMutatingUseContext::SharedBorrow
1326 | NonMutatingUseContext::ShallowBorrow
1327 | NonMutatingUseContext::UniqueBorrow
1328 ) | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
1332 /// Returns `true` if this place context represents an address-of.
1333 pub fn is_address_of(&self) -> bool {
1336 PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf)
1337 | PlaceContext::MutatingUse(MutatingUseContext::AddressOf)
1341 /// Returns `true` if this place context represents a storage live or storage dead marker.
1343 pub fn is_storage_marker(&self) -> bool {
1346 PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead)
1350 /// Returns `true` if this place context represents a use that potentially changes the value.
1352 pub fn is_mutating_use(&self) -> bool {
1353 matches!(self, PlaceContext::MutatingUse(..))
1356 /// Returns `true` if this place context represents a use.
1358 pub fn is_use(&self) -> bool {
1359 !matches!(self, PlaceContext::NonUse(..))
1362 /// Returns `true` if this place context represents an assignment statement.
1363 pub fn is_place_assignment(&self) -> bool {
1366 PlaceContext::MutatingUse(
1367 MutatingUseContext::Store
1368 | MutatingUseContext::Call
1369 | MutatingUseContext::AsmOutput,