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 // The macro results in a false positive of sorts, where &mut Span
166 // is fine, but &Span is not; just allow the lint.
167 #[allow(rustc::pass_by_value)]
168 fn visit_span(&mut self,
169 span: & $($mutability)? Span) {
170 self.super_span(span);
173 fn visit_source_info(&mut self,
174 source_info: & $($mutability)? SourceInfo) {
175 self.super_source_info(source_info);
178 fn visit_ty(&mut self,
179 ty: $(& $mutability)? Ty<'tcx>,
184 fn visit_user_type_projection(
186 ty: & $($mutability)? UserTypeProjection,
188 self.super_user_type_projection(ty);
191 fn visit_user_type_annotation(
193 index: UserTypeAnnotationIndex,
194 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
196 self.super_user_type_annotation(index, ty);
199 fn visit_region(&mut self,
200 region: $(& $mutability)? ty::Region<'tcx>,
202 self.super_region(region);
205 fn visit_const(&mut self,
206 constant: $(& $mutability)? ty::Const<'tcx>,
208 self.super_const(constant);
211 fn visit_substs(&mut self,
212 substs: & $($mutability)? SubstsRef<'tcx>,
214 self.super_substs(substs);
217 fn visit_local_decl(&mut self,
219 local_decl: & $($mutability)? LocalDecl<'tcx>) {
220 self.super_local_decl(local, local_decl);
223 fn visit_var_debug_info(&mut self,
224 var_debug_info: & $($mutability)* VarDebugInfo<'tcx>) {
225 self.super_var_debug_info(var_debug_info);
228 #[allow(rustc::pass_by_value)]
229 fn visit_local(&mut self,
230 _local: & $($mutability)? Local,
231 _context: PlaceContext,
232 _location: Location) {
235 #[allow(rustc::pass_by_value)]
236 fn visit_source_scope(&mut self,
237 scope: & $($mutability)? SourceScope) {
238 self.super_source_scope(scope);
241 // The `super_xxx` methods comprise the default behavior and are
242 // not meant to be overridden.
246 body: &$($mutability)? Body<'tcx>,
248 let span = body.span;
249 if let Some(gen) = &$($mutability)? body.generator {
250 if let Some(yield_ty) = $(& $mutability)? gen.yield_ty {
253 TyContext::YieldTy(SourceInfo::outermost(span))
258 // for best performance, we want to use an iterator rather
259 // than a for-loop, to avoid calling `body::Body::invalidate` for
261 macro_rules! basic_blocks {
262 (mut) => (body.basic_blocks_mut().iter_enumerated_mut());
263 () => (body.basic_blocks().iter_enumerated());
265 for (bb, data) in basic_blocks!($($mutability)?) {
266 self.visit_basic_block_data(bb, data);
269 for scope in &$($mutability)? body.source_scopes {
270 self.visit_source_scope_data(scope);
274 $(& $mutability)? body.return_ty(),
275 TyContext::ReturnTy(SourceInfo::outermost(body.span))
278 for local in body.local_decls.indices() {
279 self.visit_local_decl(local, & $($mutability)? body.local_decls[local]);
282 macro_rules! type_annotations {
283 (mut) => (body.user_type_annotations.iter_enumerated_mut());
284 () => (body.user_type_annotations.iter_enumerated());
287 for (index, annotation) in type_annotations!($($mutability)?) {
288 self.visit_user_type_annotation(
293 for var_debug_info in &$($mutability)? body.var_debug_info {
294 self.visit_var_debug_info(var_debug_info);
297 self.visit_span(&$($mutability)? body.span);
299 for const_ in &$($mutability)? body.required_consts {
300 let location = START_BLOCK.start_location();
301 self.visit_constant(const_, location);
305 fn super_basic_block_data(&mut self,
307 data: & $($mutability)? BasicBlockData<'tcx>) {
315 for statement in statements {
316 let location = Location { block, statement_index: index };
317 self.visit_statement(statement, location);
321 if let Some(terminator) = terminator {
322 let location = Location { block, statement_index: index };
323 self.visit_terminator(terminator, location);
327 fn super_source_scope_data(
329 scope_data: & $($mutability)? SourceScopeData<'tcx>,
331 let SourceScopeData {
335 inlined_parent_scope,
339 self.visit_span(span);
340 if let Some(parent_scope) = parent_scope {
341 self.visit_source_scope(parent_scope);
343 if let Some((callee, callsite_span)) = inlined {
344 let location = START_BLOCK.start_location();
346 self.visit_span(callsite_span);
348 let ty::Instance { def: callee_def, substs: callee_substs } = callee;
350 ty::InstanceDef::Item(_def_id) => {}
352 ty::InstanceDef::Intrinsic(_def_id) |
353 ty::InstanceDef::VtableShim(_def_id) |
354 ty::InstanceDef::ReifyShim(_def_id) |
355 ty::InstanceDef::Virtual(_def_id, _) |
356 ty::InstanceDef::ClosureOnceShim { call_once: _def_id, track_caller: _ } |
357 ty::InstanceDef::DropGlue(_def_id, None) => {}
359 ty::InstanceDef::FnPtrShim(_def_id, ty) |
360 ty::InstanceDef::DropGlue(_def_id, Some(ty)) |
361 ty::InstanceDef::CloneShim(_def_id, ty) => {
362 // FIXME(eddyb) use a better `TyContext` here.
363 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
366 self.visit_substs(callee_substs, location);
368 if let Some(inlined_parent_scope) = inlined_parent_scope {
369 self.visit_source_scope(inlined_parent_scope);
373 fn super_statement(&mut self,
374 statement: & $($mutability)? Statement<'tcx>,
375 location: Location) {
381 self.visit_source_info(source_info);
383 StatementKind::Assign(
384 box(ref $($mutability)? place, ref $($mutability)? rvalue)
386 self.visit_assign(place, rvalue, location);
388 StatementKind::FakeRead(box (_, place)) => {
391 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
395 StatementKind::SetDiscriminant { place, .. } => {
398 PlaceContext::MutatingUse(MutatingUseContext::SetDiscriminant),
402 StatementKind::Deinit(place) => {
405 PlaceContext::MutatingUse(MutatingUseContext::Deinit),
409 StatementKind::StorageLive(local) => {
412 PlaceContext::NonUse(NonUseContext::StorageLive),
416 StatementKind::StorageDead(local) => {
419 PlaceContext::NonUse(NonUseContext::StorageDead),
423 StatementKind::Retag(kind, place) => {
424 self.visit_retag(kind, place, location);
426 StatementKind::AscribeUserType(
427 box(ref $($mutability)? place, ref $($mutability)? user_ty),
430 self.visit_ascribe_user_ty(place, variance, user_ty, location);
432 StatementKind::Coverage(coverage) => {
438 StatementKind::CopyNonOverlapping(box crate::mir::CopyNonOverlapping{
439 ref $($mutability)? src,
440 ref $($mutability)? dst,
441 ref $($mutability)? count,
443 self.visit_operand(src, location);
444 self.visit_operand(dst, location);
445 self.visit_operand(count, location)
447 StatementKind::Nop => {}
451 fn super_assign(&mut self,
452 place: &$($mutability)? Place<'tcx>,
453 rvalue: &$($mutability)? Rvalue<'tcx>,
454 location: Location) {
457 PlaceContext::MutatingUse(MutatingUseContext::Store),
460 self.visit_rvalue(rvalue, location);
463 fn super_terminator(&mut self,
464 terminator: &$($mutability)? Terminator<'tcx>,
465 location: Location) {
466 let Terminator { source_info, kind } = terminator;
468 self.visit_source_info(source_info);
470 TerminatorKind::Goto { .. } |
471 TerminatorKind::Resume |
472 TerminatorKind::Abort |
473 TerminatorKind::GeneratorDrop |
474 TerminatorKind::Unreachable |
475 TerminatorKind::FalseEdge { .. } |
476 TerminatorKind::FalseUnwind { .. } => {
479 TerminatorKind::Return => {
480 // `return` logically moves from the return place `_0`. Note that the place
481 // cannot be changed by any visitor, though.
482 let $($mutability)? local = RETURN_PLACE;
484 & $($mutability)? local,
485 PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
492 "`MutVisitor` tried to mutate return place of `return` terminator"
496 TerminatorKind::SwitchInt {
501 self.visit_operand(discr, location);
502 self.visit_ty($(& $mutability)? *switch_ty, TyContext::Location(location));
505 TerminatorKind::Drop {
512 PlaceContext::MutatingUse(MutatingUseContext::Drop),
517 TerminatorKind::DropAndReplace {
525 PlaceContext::MutatingUse(MutatingUseContext::Drop),
528 self.visit_operand(value, location);
531 TerminatorKind::Call {
539 self.visit_operand(func, location);
541 self.visit_operand(arg, location);
543 if let Some((destination, _)) = destination {
546 PlaceContext::MutatingUse(MutatingUseContext::Call),
552 TerminatorKind::Assert {
559 self.visit_operand(cond, location);
560 self.visit_assert_message(msg, location);
563 TerminatorKind::Yield {
569 self.visit_operand(value, location);
572 PlaceContext::MutatingUse(MutatingUseContext::Yield),
577 TerminatorKind::InlineAsm {
587 InlineAsmOperand::In { value, .. } => {
588 self.visit_operand(value, location);
590 InlineAsmOperand::Out { place: Some(place), .. } => {
593 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
597 InlineAsmOperand::InOut { in_value, out_place, .. } => {
598 self.visit_operand(in_value, location);
599 if let Some(out_place) = out_place {
602 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
607 InlineAsmOperand::Const { value }
608 | InlineAsmOperand::SymFn { value } => {
609 self.visit_constant(value, location);
611 InlineAsmOperand::Out { place: None, .. }
612 | InlineAsmOperand::SymStatic { def_id: _ } => {}
619 fn super_assert_message(&mut self,
620 msg: & $($mutability)? AssertMessage<'tcx>,
621 location: Location) {
622 use crate::mir::AssertKind::*;
624 BoundsCheck { len, index } => {
625 self.visit_operand(len, location);
626 self.visit_operand(index, location);
628 Overflow(_, l, r) => {
629 self.visit_operand(l, location);
630 self.visit_operand(r, location);
632 OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
633 self.visit_operand(op, location);
635 ResumedAfterReturn(_) | ResumedAfterPanic(_) => {
641 fn super_rvalue(&mut self,
642 rvalue: & $($mutability)? Rvalue<'tcx>,
643 location: Location) {
645 Rvalue::Use(operand) => {
646 self.visit_operand(operand, location);
649 Rvalue::Repeat(value, _) => {
650 self.visit_operand(value, location);
653 Rvalue::ThreadLocalRef(_) => {}
655 Rvalue::Ref(r, bk, path) => {
656 self.visit_region($(& $mutability)? *r, location);
658 BorrowKind::Shared => PlaceContext::NonMutatingUse(
659 NonMutatingUseContext::SharedBorrow
661 BorrowKind::Shallow => PlaceContext::NonMutatingUse(
662 NonMutatingUseContext::ShallowBorrow
664 BorrowKind::Unique => PlaceContext::NonMutatingUse(
665 NonMutatingUseContext::UniqueBorrow
667 BorrowKind::Mut { .. } =>
668 PlaceContext::MutatingUse(MutatingUseContext::Borrow),
670 self.visit_place(path, ctx, location);
673 Rvalue::AddressOf(m, path) => {
675 Mutability::Mut => PlaceContext::MutatingUse(
676 MutatingUseContext::AddressOf
678 Mutability::Not => PlaceContext::NonMutatingUse(
679 NonMutatingUseContext::AddressOf
682 self.visit_place(path, ctx, location);
685 Rvalue::Len(path) => {
688 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
693 Rvalue::Cast(_cast_kind, operand, ty) => {
694 self.visit_operand(operand, location);
695 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
698 Rvalue::BinaryOp(_bin_op, box(lhs, rhs))
699 | Rvalue::CheckedBinaryOp(_bin_op, box(lhs, rhs)) => {
700 self.visit_operand(lhs, location);
701 self.visit_operand(rhs, location);
704 Rvalue::UnaryOp(_un_op, op) => {
705 self.visit_operand(op, location);
708 Rvalue::Discriminant(place) => {
711 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
716 Rvalue::NullaryOp(_op, ty) => {
717 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
720 Rvalue::Aggregate(kind, operands) => {
721 let kind = &$($mutability)? **kind;
723 AggregateKind::Array(ty) => {
724 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
726 AggregateKind::Tuple => {
735 self.visit_substs(substs, location);
737 AggregateKind::Closure(
741 self.visit_substs(closure_substs, location);
743 AggregateKind::Generator(
748 self.visit_substs(generator_substs, location);
752 for operand in operands {
753 self.visit_operand(operand, location);
757 Rvalue::ShallowInitBox(operand, ty) => {
758 self.visit_operand(operand, location);
759 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
764 fn super_operand(&mut self,
765 operand: & $($mutability)? Operand<'tcx>,
766 location: Location) {
768 Operand::Copy(place) => {
771 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
775 Operand::Move(place) => {
778 PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
782 Operand::Constant(constant) => {
783 self.visit_constant(constant, location);
788 fn super_ascribe_user_ty(&mut self,
789 place: & $($mutability)? Place<'tcx>,
790 _variance: & $($mutability)? ty::Variance,
791 user_ty: & $($mutability)? UserTypeProjection,
792 location: Location) {
795 PlaceContext::NonUse(NonUseContext::AscribeUserTy),
798 self.visit_user_type_projection(user_ty);
801 fn super_coverage(&mut self,
802 _coverage: & $($mutability)? Coverage,
803 _location: Location) {
806 fn super_retag(&mut self,
807 _kind: & $($mutability)? RetagKind,
808 place: & $($mutability)? Place<'tcx>,
809 location: Location) {
812 PlaceContext::MutatingUse(MutatingUseContext::Retag),
817 fn super_local_decl(&mut self,
819 local_decl: & $($mutability)? LocalDecl<'tcx>) {
830 self.visit_ty($(& $mutability)? *ty, TyContext::LocalDecl {
832 source_info: *source_info,
834 if let Some(user_ty) = user_ty {
835 for (user_ty, _) in & $($mutability)? user_ty.contents {
836 self.visit_user_type_projection(user_ty);
839 self.visit_source_info(source_info);
842 fn super_var_debug_info(&mut self,
843 var_debug_info: & $($mutability)? VarDebugInfo<'tcx>) {
850 self.visit_source_info(source_info);
851 let location = START_BLOCK.start_location();
853 VarDebugInfoContents::Const(c) => self.visit_constant(c, location),
854 VarDebugInfoContents::Place(place) =>
857 PlaceContext::NonUse(NonUseContext::VarDebugInfo),
863 #[allow(rustc::pass_by_value)]
864 fn super_source_scope(&mut self,
865 _scope: & $($mutability)? SourceScope) {
868 fn super_constant(&mut self,
869 constant: & $($mutability)? Constant<'tcx>,
870 location: Location) {
877 self.visit_span(span);
878 drop(user_ty); // no visit method for this
880 ConstantKind::Ty(ct) => self.visit_const($(& $mutability)? *ct, location),
881 ConstantKind::Val(_, ty) => self.visit_ty($(& $mutability)? *ty, TyContext::Location(location)),
885 // The macro results in a false positive of sorts, where &mut Span
886 // is fine, but &Span is not; just allow the lint.
887 #[allow(rustc::pass_by_value)]
888 fn super_span(&mut self, _span: & $($mutability)? Span) {
891 fn super_source_info(&mut self, source_info: & $($mutability)? SourceInfo) {
897 self.visit_span(span);
898 self.visit_source_scope(scope);
901 fn super_user_type_projection(
903 _ty: & $($mutability)? UserTypeProjection,
907 fn super_user_type_annotation(
909 _index: UserTypeAnnotationIndex,
910 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
912 self.visit_span(& $($mutability)? ty.span);
913 self.visit_ty($(& $mutability)? ty.inferred_ty, TyContext::UserTy(ty.span));
916 fn super_ty(&mut self, _ty: $(& $mutability)? Ty<'tcx>) {
919 fn super_region(&mut self, _region: $(& $mutability)? ty::Region<'tcx>) {
922 fn super_const(&mut self, _const: $(& $mutability)? ty::Const<'tcx>) {
925 fn super_substs(&mut self, _substs: & $($mutability)? SubstsRef<'tcx>) {
928 // Convenience methods
932 body: &$($mutability)? Body<'tcx>,
935 macro_rules! basic_blocks {
936 (mut) => (body.basic_blocks_mut());
937 () => (body.basic_blocks());
939 let basic_block = & $($mutability)? basic_blocks!($($mutability)?)[location.block];
940 if basic_block.statements.len() == location.statement_index {
941 if let Some(ref $($mutability)? terminator) = basic_block.terminator {
942 self.visit_terminator(terminator, location)
945 let statement = & $($mutability)?
946 basic_block.statements[location.statement_index];
947 self.visit_statement(statement, location)
954 macro_rules! visit_place_fns {
956 fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
960 place: &mut Place<'tcx>,
961 context: PlaceContext,
964 self.visit_local(&mut place.local, context, location);
966 if let Some(new_projection) = self.process_projection(&place.projection, location) {
967 place.projection = self.tcx().intern_place_elems(&new_projection);
971 fn process_projection<'a>(
973 projection: &'a [PlaceElem<'tcx>],
975 ) -> Option<Vec<PlaceElem<'tcx>>> {
976 let mut projection = Cow::Borrowed(projection);
978 for i in 0..projection.len() {
979 if let Some(&elem) = projection.get(i) {
980 if let Some(elem) = self.process_projection_elem(elem, location) {
981 // This converts the borrowed projection into `Cow::Owned(_)` and returns a
982 // clone of the projection so we can mutate and reintern later.
983 let vec = projection.to_mut();
990 Cow::Borrowed(_) => None,
991 Cow::Owned(vec) => Some(vec),
995 fn process_projection_elem(
997 elem: PlaceElem<'tcx>,
999 ) -> Option<PlaceElem<'tcx>> {
1001 PlaceElem::Index(local) => {
1002 let mut new_local = local;
1005 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
1009 if new_local == local { None } else { Some(PlaceElem::Index(new_local)) }
1011 PlaceElem::Field(field, ty) => {
1012 let mut new_ty = ty;
1013 self.visit_ty(&mut new_ty, TyContext::Location(location));
1014 if ty != new_ty { Some(PlaceElem::Field(field, new_ty)) } else { None }
1017 | PlaceElem::ConstantIndex { .. }
1018 | PlaceElem::Subslice { .. }
1019 | PlaceElem::Downcast(..) => None,
1025 fn visit_projection(
1027 place_ref: PlaceRef<'tcx>,
1028 context: PlaceContext,
1031 self.super_projection(place_ref, context, location);
1034 fn visit_projection_elem(
1037 proj_base: &[PlaceElem<'tcx>],
1038 elem: PlaceElem<'tcx>,
1039 context: PlaceContext,
1042 self.super_projection_elem(local, proj_base, elem, context, location);
1045 fn super_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
1046 let mut context = context;
1048 if !place.projection.is_empty() {
1049 if context.is_use() {
1050 // ^ Only change the context if it is a real use, not a "use" in debuginfo.
1051 context = if context.is_mutating_use() {
1052 PlaceContext::MutatingUse(MutatingUseContext::Projection)
1054 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
1059 self.visit_local(&place.local, context, location);
1061 self.visit_projection(place.as_ref(), context, location);
1064 fn super_projection(
1066 place_ref: PlaceRef<'tcx>,
1067 context: PlaceContext,
1070 // FIXME: Use PlaceRef::iter_projections, once that exists.
1071 let mut cursor = place_ref.projection;
1072 while let &[ref proj_base @ .., elem] = cursor {
1074 self.visit_projection_elem(place_ref.local, cursor, elem, context, location);
1078 fn super_projection_elem(
1081 _proj_base: &[PlaceElem<'tcx>],
1082 elem: PlaceElem<'tcx>,
1083 _context: PlaceContext,
1087 ProjectionElem::Field(_field, ty) => {
1088 self.visit_ty(ty, TyContext::Location(location));
1090 ProjectionElem::Index(local) => {
1093 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
1097 ProjectionElem::Deref
1098 | ProjectionElem::Subslice { from: _, to: _, from_end: _ }
1099 | ProjectionElem::ConstantIndex { offset: _, min_length: _, from_end: _ }
1100 | ProjectionElem::Downcast(_, _) => {}
1106 make_mir_visitor!(Visitor,);
1107 make_mir_visitor!(MutVisitor, mut);
1109 pub trait MirVisitable<'tcx> {
1110 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>);
1113 impl<'tcx> MirVisitable<'tcx> for Statement<'tcx> {
1114 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1115 visitor.visit_statement(self, location)
1119 impl<'tcx> MirVisitable<'tcx> for Terminator<'tcx> {
1120 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1121 visitor.visit_terminator(self, location)
1125 impl<'tcx> MirVisitable<'tcx> for Option<Terminator<'tcx>> {
1126 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1127 visitor.visit_terminator(self.as_ref().unwrap(), location)
1131 /// Extra information passed to `visit_ty` and friends to give context
1132 /// about where the type etc appears.
1134 pub enum TyContext {
1136 /// The index of the local variable we are visiting.
1139 /// The source location where this local variable was declared.
1140 source_info: SourceInfo,
1143 /// The inferred type of a user type annotation.
1146 /// The return type of the function.
1147 ReturnTy(SourceInfo),
1149 YieldTy(SourceInfo),
1151 /// A type found at some location.
1155 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1156 pub enum NonMutatingUseContext {
1157 /// Being inspected in some way, like loading a len.
1159 /// Consumed as part of an operand.
1161 /// Consumed as part of an operand.
1169 /// AddressOf for *const pointer.
1171 /// Used as base for another place, e.g., `x` in `x.y`. Will not mutate the place.
1172 /// For example, the projection `x.y` is not marked as a mutation in these cases:
1180 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1181 pub enum MutatingUseContext {
1182 /// Appears as LHS of an assignment.
1184 /// Appears on `SetDiscriminant`
1186 /// Appears on `Deinit`
1188 /// Output operand of an inline assembly block.
1190 /// Destination of a call.
1192 /// Destination of a yield.
1198 /// AddressOf for *mut pointer.
1200 /// Used as base for another place, e.g., `x` in `x.y`. Could potentially mutate the place.
1201 /// For example, the projection `x.y` is marked as a mutation in these cases:
1207 /// Retagging, a "Stacked Borrows" shadow state operation
1211 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1212 pub enum NonUseContext {
1213 /// Starting a storage live range.
1215 /// Ending a storage live range.
1217 /// User type annotation assertions for NLL.
1219 /// The data of a user variable, for debug info.
1223 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1224 pub enum PlaceContext {
1225 NonMutatingUse(NonMutatingUseContext),
1226 MutatingUse(MutatingUseContext),
1227 NonUse(NonUseContext),
1231 /// Returns `true` if this place context represents a drop.
1233 pub fn is_drop(&self) -> bool {
1234 matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
1237 /// Returns `true` if this place context represents a borrow.
1238 pub fn is_borrow(&self) -> bool {
1241 PlaceContext::NonMutatingUse(
1242 NonMutatingUseContext::SharedBorrow
1243 | NonMutatingUseContext::ShallowBorrow
1244 | NonMutatingUseContext::UniqueBorrow
1245 ) | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
1249 /// Returns `true` if this place context represents a storage live or storage dead marker.
1251 pub fn is_storage_marker(&self) -> bool {
1254 PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead)
1258 /// Returns `true` if this place context represents a use that potentially changes the value.
1260 pub fn is_mutating_use(&self) -> bool {
1261 matches!(self, PlaceContext::MutatingUse(..))
1264 /// Returns `true` if this place context represents a use.
1266 pub fn is_use(&self) -> bool {
1267 !matches!(self, PlaceContext::NonUse(..))
1270 /// Returns `true` if this place context represents an assignment statement.
1271 pub fn is_place_assignment(&self) -> bool {
1274 PlaceContext::MutatingUse(
1275 MutatingUseContext::Store
1276 | MutatingUseContext::Call
1277 | MutatingUseContext::AsmOutput,