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 fn visit_basic_block_data(
86 data: & $($mutability)? BasicBlockData<'tcx>,
88 self.super_basic_block_data(block, data);
91 fn visit_source_scope_data(
93 scope_data: & $($mutability)? SourceScopeData<'tcx>,
95 self.super_source_scope_data(scope_data);
100 statement: & $($mutability)? Statement<'tcx>,
103 self.super_statement(statement, location);
108 place: & $($mutability)? Place<'tcx>,
109 rvalue: & $($mutability)? Rvalue<'tcx>,
112 self.super_assign(place, rvalue, location);
117 terminator: & $($mutability)? Terminator<'tcx>,
120 self.super_terminator(terminator, location);
123 fn visit_assert_message(
125 msg: & $($mutability)? AssertMessage<'tcx>,
128 self.super_assert_message(msg, location);
133 rvalue: & $($mutability)? Rvalue<'tcx>,
136 self.super_rvalue(rvalue, location);
141 operand: & $($mutability)? Operand<'tcx>,
144 self.super_operand(operand, location);
147 fn visit_ascribe_user_ty(
149 place: & $($mutability)? Place<'tcx>,
150 variance: $(& $mutability)? ty::Variance,
151 user_ty: & $($mutability)? UserTypeProjection,
154 self.super_ascribe_user_ty(place, variance, user_ty, location);
159 coverage: & $($mutability)? Coverage,
162 self.super_coverage(coverage, location);
167 kind: $(& $mutability)? RetagKind,
168 place: & $($mutability)? Place<'tcx>,
171 self.super_retag(kind, place, location);
176 place: & $($mutability)? Place<'tcx>,
177 context: PlaceContext,
180 self.super_place(place, context, location);
183 visit_place_fns!($($mutability)?);
187 constant: & $($mutability)? Constant<'tcx>,
190 self.super_constant(constant, location);
195 span: $(& $mutability)? Span,
197 self.super_span(span);
200 fn visit_source_info(
202 source_info: & $($mutability)? SourceInfo,
204 self.super_source_info(source_info);
209 ty: $(& $mutability)? Ty<'tcx>,
215 fn visit_user_type_projection(
217 ty: & $($mutability)? UserTypeProjection,
219 self.super_user_type_projection(ty);
222 fn visit_user_type_annotation(
224 index: UserTypeAnnotationIndex,
225 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
227 self.super_user_type_annotation(index, ty);
232 region: $(& $mutability)? ty::Region<'tcx>,
235 self.super_region(region);
240 constant: $(& $mutability)? ty::Const<'tcx>,
243 self.super_const(constant);
248 substs: & $($mutability)? SubstsRef<'tcx>,
251 self.super_substs(substs);
257 local_decl: & $($mutability)? LocalDecl<'tcx>,
259 self.super_local_decl(local, local_decl);
262 fn visit_var_debug_info(
264 var_debug_info: & $($mutability)* VarDebugInfo<'tcx>,
266 self.super_var_debug_info(var_debug_info);
271 _local: $(& $mutability)? Local,
272 _context: PlaceContext,
276 fn visit_source_scope(
278 scope: $(& $mutability)? SourceScope,
280 self.super_source_scope(scope);
283 // The `super_xxx` methods comprise the default behavior and are
284 // not meant to be overridden.
288 body: &$($mutability)? Body<'tcx>,
290 let span = body.span;
291 if let Some(gen) = &$($mutability)? body.generator {
292 if let Some(yield_ty) = $(& $mutability)? gen.yield_ty {
295 TyContext::YieldTy(SourceInfo::outermost(span))
300 // for best performance, we want to use an iterator rather
301 // than a for-loop, to avoid calling `body::Body::invalidate` for
303 #[allow(unused_macro_rules)]
304 macro_rules! basic_blocks {
305 (mut) => (body.basic_blocks_mut().iter_enumerated_mut());
306 () => (body.basic_blocks().iter_enumerated());
308 for (bb, data) in basic_blocks!($($mutability)?) {
309 self.visit_basic_block_data(bb, data);
312 for scope in &$($mutability)? body.source_scopes {
313 self.visit_source_scope_data(scope);
317 $(& $mutability)? body.return_ty(),
318 TyContext::ReturnTy(SourceInfo::outermost(body.span))
321 for local in body.local_decls.indices() {
322 self.visit_local_decl(local, & $($mutability)? body.local_decls[local]);
325 #[allow(unused_macro_rules)]
326 macro_rules! type_annotations {
327 (mut) => (body.user_type_annotations.iter_enumerated_mut());
328 () => (body.user_type_annotations.iter_enumerated());
331 for (index, annotation) in type_annotations!($($mutability)?) {
332 self.visit_user_type_annotation(
337 for var_debug_info in &$($mutability)? body.var_debug_info {
338 self.visit_var_debug_info(var_debug_info);
341 self.visit_span($(& $mutability)? body.span);
343 for const_ in &$($mutability)? body.required_consts {
344 let location = START_BLOCK.start_location();
345 self.visit_constant(const_, location);
349 fn super_basic_block_data(&mut self,
351 data: & $($mutability)? BasicBlockData<'tcx>) {
359 for statement in statements {
360 let location = Location { block, statement_index: index };
361 self.visit_statement(statement, location);
365 if let Some(terminator) = terminator {
366 let location = Location { block, statement_index: index };
367 self.visit_terminator(terminator, location);
371 fn super_source_scope_data(
373 scope_data: & $($mutability)? SourceScopeData<'tcx>,
375 let SourceScopeData {
379 inlined_parent_scope,
383 self.visit_span($(& $mutability)? *span);
384 if let Some(parent_scope) = parent_scope {
385 self.visit_source_scope($(& $mutability)? *parent_scope);
387 if let Some((callee, callsite_span)) = inlined {
388 let location = START_BLOCK.start_location();
390 self.visit_span($(& $mutability)? *callsite_span);
392 let ty::Instance { def: callee_def, substs: callee_substs } = callee;
394 ty::InstanceDef::Item(_def_id) => {}
396 ty::InstanceDef::Intrinsic(_def_id) |
397 ty::InstanceDef::VtableShim(_def_id) |
398 ty::InstanceDef::ReifyShim(_def_id) |
399 ty::InstanceDef::Virtual(_def_id, _) |
400 ty::InstanceDef::ClosureOnceShim { call_once: _def_id, track_caller: _ } |
401 ty::InstanceDef::DropGlue(_def_id, None) => {}
403 ty::InstanceDef::FnPtrShim(_def_id, ty) |
404 ty::InstanceDef::DropGlue(_def_id, Some(ty)) |
405 ty::InstanceDef::CloneShim(_def_id, ty) => {
406 // FIXME(eddyb) use a better `TyContext` here.
407 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
410 self.visit_substs(callee_substs, location);
412 if let Some(inlined_parent_scope) = inlined_parent_scope {
413 self.visit_source_scope($(& $mutability)? *inlined_parent_scope);
417 fn super_statement(&mut self,
418 statement: & $($mutability)? Statement<'tcx>,
419 location: Location) {
425 self.visit_source_info(source_info);
427 StatementKind::Assign(
430 self.visit_assign(place, rvalue, location);
432 StatementKind::FakeRead(box (_, place)) => {
435 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
439 StatementKind::SetDiscriminant { place, .. } => {
442 PlaceContext::MutatingUse(MutatingUseContext::SetDiscriminant),
446 StatementKind::Deinit(place) => {
449 PlaceContext::MutatingUse(MutatingUseContext::Deinit),
453 StatementKind::StorageLive(local) => {
455 $(& $mutability)? *local,
456 PlaceContext::NonUse(NonUseContext::StorageLive),
460 StatementKind::StorageDead(local) => {
462 $(& $mutability)? *local,
463 PlaceContext::NonUse(NonUseContext::StorageDead),
467 StatementKind::Retag(kind, place) => {
468 self.visit_retag($(& $mutability)? *kind, place, location);
470 StatementKind::AscribeUserType(
471 box (place, user_ty),
474 self.visit_ascribe_user_ty(place, $(& $mutability)? *variance, user_ty, location);
476 StatementKind::Coverage(coverage) => {
482 StatementKind::CopyNonOverlapping(box crate::mir::CopyNonOverlapping{
487 self.visit_operand(src, location);
488 self.visit_operand(dst, location);
489 self.visit_operand(count, location)
491 StatementKind::Nop => {}
495 fn super_assign(&mut self,
496 place: &$($mutability)? Place<'tcx>,
497 rvalue: &$($mutability)? Rvalue<'tcx>,
498 location: Location) {
501 PlaceContext::MutatingUse(MutatingUseContext::Store),
504 self.visit_rvalue(rvalue, location);
507 fn super_terminator(&mut self,
508 terminator: &$($mutability)? Terminator<'tcx>,
509 location: Location) {
510 let Terminator { source_info, kind } = terminator;
512 self.visit_source_info(source_info);
514 TerminatorKind::Goto { .. } |
515 TerminatorKind::Resume |
516 TerminatorKind::Abort |
517 TerminatorKind::GeneratorDrop |
518 TerminatorKind::Unreachable |
519 TerminatorKind::FalseEdge { .. } |
520 TerminatorKind::FalseUnwind { .. } => {}
522 TerminatorKind::Return => {
523 // `return` logically moves from the return place `_0`. Note that the place
524 // cannot be changed by any visitor, though.
525 let $($mutability)? local = RETURN_PLACE;
527 $(& $mutability)? local,
528 PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
535 "`MutVisitor` tried to mutate return place of `return` terminator"
539 TerminatorKind::SwitchInt {
544 self.visit_operand(discr, location);
545 self.visit_ty($(& $mutability)? *switch_ty, TyContext::Location(location));
548 TerminatorKind::Drop {
555 PlaceContext::MutatingUse(MutatingUseContext::Drop),
560 TerminatorKind::DropAndReplace {
568 PlaceContext::MutatingUse(MutatingUseContext::Drop),
571 self.visit_operand(value, location);
574 TerminatorKind::Call {
583 self.visit_operand(func, location);
585 self.visit_operand(arg, location);
589 PlaceContext::MutatingUse(MutatingUseContext::Call),
594 TerminatorKind::Assert {
601 self.visit_operand(cond, location);
602 self.visit_assert_message(msg, location);
605 TerminatorKind::Yield {
611 self.visit_operand(value, location);
614 PlaceContext::MutatingUse(MutatingUseContext::Yield),
619 TerminatorKind::InlineAsm {
629 InlineAsmOperand::In { value, .. } => {
630 self.visit_operand(value, location);
632 InlineAsmOperand::Out { place: Some(place), .. } => {
635 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
639 InlineAsmOperand::InOut { in_value, out_place, .. } => {
640 self.visit_operand(in_value, location);
641 if let Some(out_place) = out_place {
644 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
649 InlineAsmOperand::Const { value }
650 | InlineAsmOperand::SymFn { value } => {
651 self.visit_constant(value, location);
653 InlineAsmOperand::Out { place: None, .. }
654 | InlineAsmOperand::SymStatic { def_id: _ } => {}
661 fn super_assert_message(&mut self,
662 msg: & $($mutability)? AssertMessage<'tcx>,
663 location: Location) {
664 use crate::mir::AssertKind::*;
666 BoundsCheck { len, index } => {
667 self.visit_operand(len, location);
668 self.visit_operand(index, location);
670 Overflow(_, l, r) => {
671 self.visit_operand(l, location);
672 self.visit_operand(r, location);
674 OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
675 self.visit_operand(op, location);
677 ResumedAfterReturn(_) | ResumedAfterPanic(_) => {
683 fn super_rvalue(&mut self,
684 rvalue: & $($mutability)? Rvalue<'tcx>,
685 location: Location) {
687 Rvalue::Use(operand) => {
688 self.visit_operand(operand, location);
691 Rvalue::Repeat(value, _) => {
692 self.visit_operand(value, location);
695 Rvalue::ThreadLocalRef(_) => {}
697 Rvalue::Ref(r, bk, path) => {
698 self.visit_region($(& $mutability)? *r, location);
700 BorrowKind::Shared => PlaceContext::NonMutatingUse(
701 NonMutatingUseContext::SharedBorrow
703 BorrowKind::Shallow => PlaceContext::NonMutatingUse(
704 NonMutatingUseContext::ShallowBorrow
706 BorrowKind::Unique => PlaceContext::NonMutatingUse(
707 NonMutatingUseContext::UniqueBorrow
709 BorrowKind::Mut { .. } =>
710 PlaceContext::MutatingUse(MutatingUseContext::Borrow),
712 self.visit_place(path, ctx, location);
715 Rvalue::AddressOf(m, path) => {
717 Mutability::Mut => PlaceContext::MutatingUse(
718 MutatingUseContext::AddressOf
720 Mutability::Not => PlaceContext::NonMutatingUse(
721 NonMutatingUseContext::AddressOf
724 self.visit_place(path, ctx, location);
727 Rvalue::Len(path) => {
730 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
735 Rvalue::Cast(_cast_kind, operand, ty) => {
736 self.visit_operand(operand, location);
737 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
740 Rvalue::BinaryOp(_bin_op, box(lhs, rhs))
741 | Rvalue::CheckedBinaryOp(_bin_op, box(lhs, rhs)) => {
742 self.visit_operand(lhs, location);
743 self.visit_operand(rhs, location);
746 Rvalue::UnaryOp(_un_op, op) => {
747 self.visit_operand(op, location);
750 Rvalue::Discriminant(place) => {
753 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
758 Rvalue::NullaryOp(_op, ty) => {
759 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
762 Rvalue::Aggregate(kind, operands) => {
763 let kind = &$($mutability)? **kind;
765 AggregateKind::Array(ty) => {
766 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
768 AggregateKind::Tuple => {
777 self.visit_substs(substs, location);
779 AggregateKind::Closure(
783 self.visit_substs(closure_substs, location);
785 AggregateKind::Generator(
790 self.visit_substs(generator_substs, location);
794 for operand in operands {
795 self.visit_operand(operand, location);
799 Rvalue::ShallowInitBox(operand, ty) => {
800 self.visit_operand(operand, location);
801 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
806 fn super_operand(&mut self,
807 operand: & $($mutability)? Operand<'tcx>,
808 location: Location) {
810 Operand::Copy(place) => {
813 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
817 Operand::Move(place) => {
820 PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
824 Operand::Constant(constant) => {
825 self.visit_constant(constant, location);
830 fn super_ascribe_user_ty(&mut self,
831 place: & $($mutability)? Place<'tcx>,
832 _variance: $(& $mutability)? ty::Variance,
833 user_ty: & $($mutability)? UserTypeProjection,
834 location: Location) {
837 PlaceContext::NonUse(NonUseContext::AscribeUserTy),
840 self.visit_user_type_projection(user_ty);
843 fn super_coverage(&mut self,
844 _coverage: & $($mutability)? Coverage,
845 _location: Location) {
848 fn super_retag(&mut self,
849 _kind: $(& $mutability)? RetagKind,
850 place: & $($mutability)? Place<'tcx>,
851 location: Location) {
854 PlaceContext::MutatingUse(MutatingUseContext::Retag),
859 fn super_local_decl(&mut self,
861 local_decl: & $($mutability)? LocalDecl<'tcx>) {
872 self.visit_ty($(& $mutability)? *ty, TyContext::LocalDecl {
874 source_info: *source_info,
876 if let Some(user_ty) = user_ty {
877 for (user_ty, _) in & $($mutability)? user_ty.contents {
878 self.visit_user_type_projection(user_ty);
881 self.visit_source_info(source_info);
884 fn super_var_debug_info(
886 var_debug_info: & $($mutability)? VarDebugInfo<'tcx>
894 self.visit_source_info(source_info);
895 let location = START_BLOCK.start_location();
897 VarDebugInfoContents::Const(c) => self.visit_constant(c, location),
898 VarDebugInfoContents::Place(place) =>
901 PlaceContext::NonUse(NonUseContext::VarDebugInfo),
907 fn super_source_scope(
909 _scope: $(& $mutability)? SourceScope
914 constant: & $($mutability)? Constant<'tcx>,
923 self.visit_span($(& $mutability)? *span);
924 drop(user_ty); // no visit method for this
926 ConstantKind::Ty(ct) => self.visit_const($(& $mutability)? *ct, location),
927 ConstantKind::Val(_, ty) => self.visit_ty($(& $mutability)? *ty, TyContext::Location(location)),
931 fn super_span(&mut self, _span: $(& $mutability)? Span) {
934 fn super_source_info(&mut self, source_info: & $($mutability)? SourceInfo) {
940 self.visit_span($(& $mutability)? *span);
941 self.visit_source_scope($(& $mutability)? *scope);
944 fn super_user_type_projection(
946 _ty: & $($mutability)? UserTypeProjection,
950 fn super_user_type_annotation(
952 _index: UserTypeAnnotationIndex,
953 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
955 self.visit_span($(& $mutability)? ty.span);
956 self.visit_ty($(& $mutability)? ty.inferred_ty, TyContext::UserTy(ty.span));
959 fn super_ty(&mut self, _ty: $(& $mutability)? Ty<'tcx>) {
962 fn super_region(&mut self, _region: $(& $mutability)? ty::Region<'tcx>) {
965 fn super_const(&mut self, _const: $(& $mutability)? ty::Const<'tcx>) {
968 fn super_substs(&mut self, _substs: & $($mutability)? SubstsRef<'tcx>) {
971 // Convenience methods
975 body: &$($mutability)? Body<'tcx>,
978 #[allow(unused_macro_rules)]
979 macro_rules! basic_blocks {
980 (mut) => (body.basic_blocks_mut());
981 () => (body.basic_blocks());
983 let basic_block = & $($mutability)? basic_blocks!($($mutability)?)[location.block];
984 if basic_block.statements.len() == location.statement_index {
985 if let Some(ref $($mutability)? terminator) = basic_block.terminator {
986 self.visit_terminator(terminator, location)
989 let statement = & $($mutability)?
990 basic_block.statements[location.statement_index];
991 self.visit_statement(statement, location)
998 macro_rules! visit_place_fns {
1000 fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
1004 place: &mut Place<'tcx>,
1005 context: PlaceContext,
1008 self.visit_local(&mut place.local, context, location);
1010 if let Some(new_projection) = self.process_projection(&place.projection, location) {
1011 place.projection = self.tcx().intern_place_elems(&new_projection);
1015 fn process_projection<'a>(
1017 projection: &'a [PlaceElem<'tcx>],
1019 ) -> Option<Vec<PlaceElem<'tcx>>> {
1020 let mut projection = Cow::Borrowed(projection);
1022 for i in 0..projection.len() {
1023 if let Some(&elem) = projection.get(i) {
1024 if let Some(elem) = self.process_projection_elem(elem, location) {
1025 // This converts the borrowed projection into `Cow::Owned(_)` and returns a
1026 // clone of the projection so we can mutate and reintern later.
1027 let vec = projection.to_mut();
1034 Cow::Borrowed(_) => None,
1035 Cow::Owned(vec) => Some(vec),
1039 fn process_projection_elem(
1041 elem: PlaceElem<'tcx>,
1043 ) -> Option<PlaceElem<'tcx>> {
1045 PlaceElem::Index(local) => {
1046 let mut new_local = local;
1049 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
1053 if new_local == local { None } else { Some(PlaceElem::Index(new_local)) }
1055 PlaceElem::Field(field, ty) => {
1056 let mut new_ty = ty;
1057 self.visit_ty(&mut new_ty, TyContext::Location(location));
1058 if ty != new_ty { Some(PlaceElem::Field(field, new_ty)) } else { None }
1061 | PlaceElem::ConstantIndex { .. }
1062 | PlaceElem::Subslice { .. }
1063 | PlaceElem::Downcast(..) => None,
1069 fn visit_projection(
1071 place_ref: PlaceRef<'tcx>,
1072 context: PlaceContext,
1075 self.super_projection(place_ref, context, location);
1078 fn visit_projection_elem(
1081 proj_base: &[PlaceElem<'tcx>],
1082 elem: PlaceElem<'tcx>,
1083 context: PlaceContext,
1086 self.super_projection_elem(local, proj_base, elem, context, location);
1089 fn super_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
1090 let mut context = context;
1092 if !place.projection.is_empty() {
1093 if context.is_use() {
1094 // ^ Only change the context if it is a real use, not a "use" in debuginfo.
1095 context = if context.is_mutating_use() {
1096 PlaceContext::MutatingUse(MutatingUseContext::Projection)
1098 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
1103 self.visit_local(place.local, context, location);
1105 self.visit_projection(place.as_ref(), context, location);
1108 fn super_projection(
1110 place_ref: PlaceRef<'tcx>,
1111 context: PlaceContext,
1114 for (base, elem) in place_ref.iter_projections().rev() {
1115 let base_proj = base.projection;
1116 self.visit_projection_elem(place_ref.local, base_proj, elem, context, location);
1120 fn super_projection_elem(
1123 _proj_base: &[PlaceElem<'tcx>],
1124 elem: PlaceElem<'tcx>,
1125 _context: PlaceContext,
1129 ProjectionElem::Field(_field, ty) => {
1130 self.visit_ty(ty, TyContext::Location(location));
1132 ProjectionElem::Index(local) => {
1135 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
1139 ProjectionElem::Deref
1140 | ProjectionElem::Subslice { from: _, to: _, from_end: _ }
1141 | ProjectionElem::ConstantIndex { offset: _, min_length: _, from_end: _ }
1142 | ProjectionElem::Downcast(_, _) => {}
1148 make_mir_visitor!(Visitor,);
1149 make_mir_visitor!(MutVisitor, mut);
1151 pub trait MirVisitable<'tcx> {
1152 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>);
1155 impl<'tcx> MirVisitable<'tcx> for Statement<'tcx> {
1156 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1157 visitor.visit_statement(self, location)
1161 impl<'tcx> MirVisitable<'tcx> for Terminator<'tcx> {
1162 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1163 visitor.visit_terminator(self, location)
1167 impl<'tcx> MirVisitable<'tcx> for Option<Terminator<'tcx>> {
1168 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1169 visitor.visit_terminator(self.as_ref().unwrap(), location)
1173 /// Extra information passed to `visit_ty` and friends to give context
1174 /// about where the type etc appears.
1176 pub enum TyContext {
1178 /// The index of the local variable we are visiting.
1181 /// The source location where this local variable was declared.
1182 source_info: SourceInfo,
1185 /// The inferred type of a user type annotation.
1188 /// The return type of the function.
1189 ReturnTy(SourceInfo),
1191 YieldTy(SourceInfo),
1193 /// A type found at some location.
1197 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1198 pub enum NonMutatingUseContext {
1199 /// Being inspected in some way, like loading a len.
1201 /// Consumed as part of an operand.
1203 /// Consumed as part of an operand.
1211 /// AddressOf for *const pointer.
1213 /// Used as base for another place, e.g., `x` in `x.y`. Will not mutate the place.
1214 /// For example, the projection `x.y` is not marked as a mutation in these cases:
1215 /// ```ignore (illustrative)
1222 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1223 pub enum MutatingUseContext {
1224 /// Appears as LHS of an assignment.
1226 /// Appears on `SetDiscriminant`
1228 /// Appears on `Deinit`
1230 /// Output operand of an inline assembly block.
1232 /// Destination of a call.
1234 /// Destination of a yield.
1240 /// AddressOf for *mut pointer.
1242 /// Used as base for another place, e.g., `x` in `x.y`. Could potentially mutate the place.
1243 /// For example, the projection `x.y` is marked as a mutation in these cases:
1244 /// ```ignore (illustrative)
1249 /// Retagging, a "Stacked Borrows" shadow state operation
1253 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1254 pub enum NonUseContext {
1255 /// Starting a storage live range.
1257 /// Ending a storage live range.
1259 /// User type annotation assertions for NLL.
1261 /// The data of a user variable, for debug info.
1265 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1266 pub enum PlaceContext {
1267 NonMutatingUse(NonMutatingUseContext),
1268 MutatingUse(MutatingUseContext),
1269 NonUse(NonUseContext),
1273 /// Returns `true` if this place context represents a drop.
1275 pub fn is_drop(&self) -> bool {
1276 matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
1279 /// Returns `true` if this place context represents a borrow.
1280 pub fn is_borrow(&self) -> bool {
1283 PlaceContext::NonMutatingUse(
1284 NonMutatingUseContext::SharedBorrow
1285 | NonMutatingUseContext::ShallowBorrow
1286 | NonMutatingUseContext::UniqueBorrow
1287 ) | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
1291 /// Returns `true` if this place context represents a storage live or storage dead marker.
1293 pub fn is_storage_marker(&self) -> bool {
1296 PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead)
1300 /// Returns `true` if this place context represents a use that potentially changes the value.
1302 pub fn is_mutating_use(&self) -> bool {
1303 matches!(self, PlaceContext::MutatingUse(..))
1306 /// Returns `true` if this place context represents a use.
1308 pub fn is_use(&self) -> bool {
1309 !matches!(self, PlaceContext::NonUse(..))
1312 /// Returns `true` if this place context represents an assignment statement.
1313 pub fn is_place_assignment(&self) -> bool {
1316 PlaceContext::MutatingUse(
1317 MutatingUseContext::Store
1318 | MutatingUseContext::Call
1319 | MutatingUseContext::AsmOutput,