5 //! There are two visitors, one for immutable and one for mutable references,
6 //! but both are generated by the following macro. The code is written according
7 //! to the following conventions:
9 //! - introduce a `visit_foo` and a `super_foo` method for every MIR type
10 //! - `visit_foo`, by default, calls `super_foo`
11 //! - `super_foo`, by default, destructures the `foo` and calls `visit_foo`
13 //! This allows you as a user to override `visit_foo` for types are
14 //! interested in, and invoke (within that method) call
15 //! `self.super_foo` to get the default behavior. Just as in an OO
16 //! language, you should never call `super` methods ordinarily except
17 //! in that circumstance.
19 //! For the most part, we do not destructure things external to the
20 //! MIR, e.g., types, spans, etc, but simply visit them and stop. This
21 //! avoids duplication with other visitors like `TypeFoldable`.
25 //! The code is written in a very deliberate style intended to minimize
26 //! the chance of things being overlooked. You'll notice that we always
27 //! use pattern matching to reference fields and we ensure that all
28 //! matches are exhaustive.
30 //! For example, the `super_basic_block_data` method begins like this:
33 //! fn super_basic_block_data(&mut self,
34 //! block: BasicBlock,
35 //! data: & $($mutability)? BasicBlockData<'tcx>) {
36 //! let BasicBlockData {
42 //! for statement in statements {
43 //! self.visit_statement(block, statement);
50 //! Here we used `let BasicBlockData { <fields> } = *data` deliberately,
51 //! rather than writing `data.statements` in the body. This is because if one
52 //! adds a new field to `BasicBlockData`, one will be forced to revise this code,
53 //! and hence one will (hopefully) invoke the correct visit methods (if any).
55 //! For this to work, ALL MATCHES MUST BE EXHAUSTIVE IN FIELDS AND VARIANTS.
56 //! That means you never write `..` to skip over fields, nor do you write `_`
57 //! to skip over variants in a `match`.
59 //! The only place that `_` is acceptable is to match a field (or
60 //! variant argument) that does not require visiting, as in
61 //! `is_cleanup` above.
64 use crate::ty::subst::SubstsRef;
65 use crate::ty::{CanonicalUserTypeAnnotation, Ty};
68 macro_rules! make_mir_visitor {
69 ($visitor_trait_name:ident, $($mutability:ident)?) => {
70 pub trait $visitor_trait_name<'tcx> {
71 // Override these, and call `self.super_xxx` to revert back to the
76 body: &$($mutability)? Body<'tcx>,
78 self.super_body(body);
81 fn visit_basic_block_data(&mut self,
83 data: & $($mutability)? BasicBlockData<'tcx>) {
84 self.super_basic_block_data(block, data);
87 fn visit_source_scope_data(&mut self,
88 scope_data: & $($mutability)? SourceScopeData<'tcx>) {
89 self.super_source_scope_data(scope_data);
92 fn visit_statement(&mut self,
93 statement: & $($mutability)? Statement<'tcx>,
95 self.super_statement(statement, location);
98 fn visit_assign(&mut self,
99 place: & $($mutability)? Place<'tcx>,
100 rvalue: & $($mutability)? Rvalue<'tcx>,
101 location: Location) {
102 self.super_assign(place, rvalue, location);
105 fn visit_terminator(&mut self,
106 terminator: & $($mutability)? Terminator<'tcx>,
107 location: Location) {
108 self.super_terminator(terminator, location);
111 fn visit_assert_message(&mut self,
112 msg: & $($mutability)? AssertMessage<'tcx>,
113 location: Location) {
114 self.super_assert_message(msg, location);
117 fn visit_rvalue(&mut self,
118 rvalue: & $($mutability)? Rvalue<'tcx>,
119 location: Location) {
120 self.super_rvalue(rvalue, location);
123 fn visit_operand(&mut self,
124 operand: & $($mutability)? Operand<'tcx>,
125 location: Location) {
126 self.super_operand(operand, location);
129 fn visit_ascribe_user_ty(&mut self,
130 place: & $($mutability)? Place<'tcx>,
131 variance: & $($mutability)? ty::Variance,
132 user_ty: & $($mutability)? UserTypeProjection,
133 location: Location) {
134 self.super_ascribe_user_ty(place, variance, user_ty, location);
137 fn visit_coverage(&mut self,
138 coverage: & $($mutability)? Coverage,
139 location: Location) {
140 self.super_coverage(coverage, location);
143 fn visit_retag(&mut self,
144 kind: & $($mutability)? RetagKind,
145 place: & $($mutability)? Place<'tcx>,
146 location: Location) {
147 self.super_retag(kind, place, location);
150 fn visit_place(&mut self,
151 place: & $($mutability)? Place<'tcx>,
152 context: PlaceContext,
153 location: Location) {
154 self.super_place(place, context, location);
157 visit_place_fns!($($mutability)?);
159 fn visit_constant(&mut self,
160 constant: & $($mutability)? Constant<'tcx>,
161 location: Location) {
162 self.super_constant(constant, location);
165 fn visit_span(&mut self,
166 span: & $($mutability)? Span) {
167 self.super_span(span);
170 fn visit_source_info(&mut self,
171 source_info: & $($mutability)? SourceInfo) {
172 self.super_source_info(source_info);
175 fn visit_ty(&mut self,
176 ty: $(& $mutability)? Ty<'tcx>,
181 fn visit_user_type_projection(
183 ty: & $($mutability)? UserTypeProjection,
185 self.super_user_type_projection(ty);
188 fn visit_user_type_annotation(
190 index: UserTypeAnnotationIndex,
191 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
193 self.super_user_type_annotation(index, ty);
196 fn visit_region(&mut self,
197 region: & $($mutability)? ty::Region<'tcx>,
199 self.super_region(region);
202 fn visit_const(&mut self,
203 constant: & $($mutability)? &'tcx ty::Const<'tcx>,
205 self.super_const(constant);
208 fn visit_substs(&mut self,
209 substs: & $($mutability)? SubstsRef<'tcx>,
211 self.super_substs(substs);
214 fn visit_local_decl(&mut self,
216 local_decl: & $($mutability)? LocalDecl<'tcx>) {
217 self.super_local_decl(local, local_decl);
220 fn visit_var_debug_info(&mut self,
221 var_debug_info: & $($mutability)* VarDebugInfo<'tcx>) {
222 self.super_var_debug_info(var_debug_info);
225 fn visit_local(&mut self,
226 _local: & $($mutability)? Local,
227 _context: PlaceContext,
228 _location: Location) {
231 fn visit_source_scope(&mut self,
232 scope: & $($mutability)? SourceScope) {
233 self.super_source_scope(scope);
236 // The `super_xxx` methods comprise the default behavior and are
237 // not meant to be overridden.
241 body: &$($mutability)? Body<'tcx>,
243 let span = body.span;
244 if let Some(yield_ty) = &$($mutability)? body.yield_ty {
247 TyContext::YieldTy(SourceInfo::outermost(span))
251 // for best performance, we want to use an iterator rather
252 // than a for-loop, to avoid calling `body::Body::invalidate` for
254 macro_rules! basic_blocks {
255 (mut) => (body.basic_blocks_mut().iter_enumerated_mut());
256 () => (body.basic_blocks().iter_enumerated());
258 for (bb, data) in basic_blocks!($($mutability)?) {
259 self.visit_basic_block_data(bb, data);
262 for scope in &$($mutability)? body.source_scopes {
263 self.visit_source_scope_data(scope);
267 &$($mutability)? body.return_ty(),
268 TyContext::ReturnTy(SourceInfo::outermost(body.span))
271 for local in body.local_decls.indices() {
272 self.visit_local_decl(local, & $($mutability)? body.local_decls[local]);
275 macro_rules! type_annotations {
276 (mut) => (body.user_type_annotations.iter_enumerated_mut());
277 () => (body.user_type_annotations.iter_enumerated());
280 for (index, annotation) in type_annotations!($($mutability)?) {
281 self.visit_user_type_annotation(
286 for var_debug_info in &$($mutability)? body.var_debug_info {
287 self.visit_var_debug_info(var_debug_info);
290 self.visit_span(&$($mutability)? body.span);
292 for const_ in &$($mutability)? body.required_consts {
293 let location = START_BLOCK.start_location();
294 self.visit_constant(const_, location);
298 fn super_basic_block_data(&mut self,
300 data: & $($mutability)? BasicBlockData<'tcx>) {
308 for statement in statements {
309 let location = Location { block, statement_index: index };
310 self.visit_statement(statement, location);
314 if let Some(terminator) = terminator {
315 let location = Location { block, statement_index: index };
316 self.visit_terminator(terminator, location);
320 fn super_source_scope_data(
322 scope_data: & $($mutability)? SourceScopeData<'tcx>,
324 let SourceScopeData {
328 inlined_parent_scope,
332 self.visit_span(span);
333 if let Some(parent_scope) = parent_scope {
334 self.visit_source_scope(parent_scope);
336 if let Some((callee, callsite_span)) = inlined {
337 let location = START_BLOCK.start_location();
339 self.visit_span(callsite_span);
341 let ty::Instance { def: callee_def, substs: callee_substs } = callee;
343 ty::InstanceDef::Item(_def_id) => {}
345 ty::InstanceDef::Intrinsic(_def_id) |
346 ty::InstanceDef::VtableShim(_def_id) |
347 ty::InstanceDef::ReifyShim(_def_id) |
348 ty::InstanceDef::Virtual(_def_id, _) |
349 ty::InstanceDef::ClosureOnceShim { call_once: _def_id } |
350 ty::InstanceDef::DropGlue(_def_id, None) => {}
352 ty::InstanceDef::FnPtrShim(_def_id, ty) |
353 ty::InstanceDef::DropGlue(_def_id, Some(ty)) |
354 ty::InstanceDef::CloneShim(_def_id, ty) => {
355 // FIXME(eddyb) use a better `TyContext` here.
356 self.visit_ty(ty, TyContext::Location(location));
359 self.visit_substs(callee_substs, location);
361 if let Some(inlined_parent_scope) = inlined_parent_scope {
362 self.visit_source_scope(inlined_parent_scope);
366 fn super_statement(&mut self,
367 statement: & $($mutability)? Statement<'tcx>,
368 location: Location) {
374 self.visit_source_info(source_info);
376 StatementKind::Assign(
377 box(ref $($mutability)? place, ref $($mutability)? rvalue)
379 self.visit_assign(place, rvalue, location);
381 StatementKind::FakeRead(_, place) => {
384 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
388 StatementKind::SetDiscriminant { place, .. } => {
391 PlaceContext::MutatingUse(MutatingUseContext::Store),
395 StatementKind::StorageLive(local) => {
398 PlaceContext::NonUse(NonUseContext::StorageLive),
402 StatementKind::StorageDead(local) => {
405 PlaceContext::NonUse(NonUseContext::StorageDead),
409 StatementKind::LlvmInlineAsm(asm) => {
410 for output in & $($mutability)? asm.outputs[..] {
413 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
417 for (span, input) in & $($mutability)? asm.inputs[..] {
418 self.visit_span(span);
419 self.visit_operand(input, location);
422 StatementKind::Retag(kind, place) => {
423 self.visit_retag(kind, place, location);
425 StatementKind::AscribeUserType(
426 box(ref $($mutability)? place, ref $($mutability)? user_ty),
429 self.visit_ascribe_user_ty(place, variance, user_ty, location);
431 StatementKind::Coverage(coverage) => {
437 StatementKind::Nop => {}
441 fn super_assign(&mut self,
442 place: &$($mutability)? Place<'tcx>,
443 rvalue: &$($mutability)? Rvalue<'tcx>,
444 location: Location) {
447 PlaceContext::MutatingUse(MutatingUseContext::Store),
450 self.visit_rvalue(rvalue, location);
453 fn super_terminator(&mut self,
454 terminator: &$($mutability)? Terminator<'tcx>,
455 location: Location) {
456 let Terminator { source_info, kind } = terminator;
458 self.visit_source_info(source_info);
460 TerminatorKind::Goto { .. } |
461 TerminatorKind::Resume |
462 TerminatorKind::Abort |
463 TerminatorKind::GeneratorDrop |
464 TerminatorKind::Unreachable |
465 TerminatorKind::FalseEdge { .. } |
466 TerminatorKind::FalseUnwind { .. } => {
469 TerminatorKind::Return => {
470 // `return` logically moves from the return place `_0`. Note that the place
471 // cannot be changed by any visitor, though.
472 let $($mutability)? local = RETURN_PLACE;
474 & $($mutability)? local,
475 PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
482 "`MutVisitor` tried to mutate return place of `return` terminator"
486 TerminatorKind::SwitchInt {
491 self.visit_operand(discr, location);
492 self.visit_ty(switch_ty, TyContext::Location(location));
495 TerminatorKind::Drop {
502 PlaceContext::MutatingUse(MutatingUseContext::Drop),
507 TerminatorKind::DropAndReplace {
515 PlaceContext::MutatingUse(MutatingUseContext::Drop),
518 self.visit_operand(value, location);
521 TerminatorKind::Call {
529 self.visit_operand(func, location);
531 self.visit_operand(arg, location);
533 if let Some((destination, _)) = destination {
536 PlaceContext::MutatingUse(MutatingUseContext::Call),
542 TerminatorKind::Assert {
549 self.visit_operand(cond, location);
550 self.visit_assert_message(msg, location);
553 TerminatorKind::Yield {
559 self.visit_operand(value, location);
562 PlaceContext::MutatingUse(MutatingUseContext::Yield),
567 TerminatorKind::InlineAsm {
576 InlineAsmOperand::In { value, .. }
577 | InlineAsmOperand::Const { value } => {
578 self.visit_operand(value, location);
580 InlineAsmOperand::Out { place, .. } => {
581 if let Some(place) = place {
584 PlaceContext::MutatingUse(MutatingUseContext::Store),
589 InlineAsmOperand::InOut { in_value, out_place, .. } => {
590 self.visit_operand(in_value, location);
591 if let Some(out_place) = out_place {
594 PlaceContext::MutatingUse(MutatingUseContext::Store),
599 InlineAsmOperand::SymFn { value } => {
600 self.visit_constant(value, location);
602 InlineAsmOperand::SymStatic { def_id: _ } => {}
609 fn super_assert_message(&mut self,
610 msg: & $($mutability)? AssertMessage<'tcx>,
611 location: Location) {
612 use crate::mir::AssertKind::*;
614 BoundsCheck { len, index } => {
615 self.visit_operand(len, location);
616 self.visit_operand(index, location);
618 Overflow(_, l, r) => {
619 self.visit_operand(l, location);
620 self.visit_operand(r, location);
622 OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
623 self.visit_operand(op, location);
625 ResumedAfterReturn(_) | ResumedAfterPanic(_) => {
631 fn super_rvalue(&mut self,
632 rvalue: & $($mutability)? Rvalue<'tcx>,
633 location: Location) {
635 Rvalue::Use(operand) => {
636 self.visit_operand(operand, location);
639 Rvalue::Repeat(value, _) => {
640 self.visit_operand(value, location);
643 Rvalue::ThreadLocalRef(_) => {}
645 Rvalue::Ref(r, bk, path) => {
646 self.visit_region(r, location);
648 BorrowKind::Shared => PlaceContext::NonMutatingUse(
649 NonMutatingUseContext::SharedBorrow
651 BorrowKind::Shallow => PlaceContext::NonMutatingUse(
652 NonMutatingUseContext::ShallowBorrow
654 BorrowKind::Unique => PlaceContext::NonMutatingUse(
655 NonMutatingUseContext::UniqueBorrow
657 BorrowKind::Mut { .. } =>
658 PlaceContext::MutatingUse(MutatingUseContext::Borrow),
660 self.visit_place(path, ctx, location);
663 Rvalue::AddressOf(m, path) => {
665 Mutability::Mut => PlaceContext::MutatingUse(
666 MutatingUseContext::AddressOf
668 Mutability::Not => PlaceContext::NonMutatingUse(
669 NonMutatingUseContext::AddressOf
672 self.visit_place(path, ctx, location);
675 Rvalue::Len(path) => {
678 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
683 Rvalue::Cast(_cast_kind, operand, ty) => {
684 self.visit_operand(operand, location);
685 self.visit_ty(ty, TyContext::Location(location));
688 Rvalue::BinaryOp(_bin_op, lhs, rhs)
689 | Rvalue::CheckedBinaryOp(_bin_op, lhs, rhs) => {
690 self.visit_operand(lhs, location);
691 self.visit_operand(rhs, location);
694 Rvalue::UnaryOp(_un_op, op) => {
695 self.visit_operand(op, location);
698 Rvalue::Discriminant(place) => {
701 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
706 Rvalue::NullaryOp(_op, ty) => {
707 self.visit_ty(ty, TyContext::Location(location));
710 Rvalue::Aggregate(kind, operands) => {
711 let kind = &$($mutability)? **kind;
713 AggregateKind::Array(ty) => {
714 self.visit_ty(ty, TyContext::Location(location));
716 AggregateKind::Tuple => {
725 self.visit_substs(substs, location);
727 AggregateKind::Closure(
731 self.visit_substs(closure_substs, location);
733 AggregateKind::Generator(
738 self.visit_substs(generator_substs, location);
742 for operand in operands {
743 self.visit_operand(operand, location);
749 fn super_operand(&mut self,
750 operand: & $($mutability)? Operand<'tcx>,
751 location: Location) {
753 Operand::Copy(place) => {
756 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
760 Operand::Move(place) => {
763 PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
767 Operand::Constant(constant) => {
768 self.visit_constant(constant, location);
773 fn super_ascribe_user_ty(&mut self,
774 place: & $($mutability)? Place<'tcx>,
775 _variance: & $($mutability)? ty::Variance,
776 user_ty: & $($mutability)? UserTypeProjection,
777 location: Location) {
780 PlaceContext::NonUse(NonUseContext::AscribeUserTy),
783 self.visit_user_type_projection(user_ty);
786 fn super_coverage(&mut self,
787 _coverage: & $($mutability)? Coverage,
788 _location: Location) {
791 fn super_retag(&mut self,
792 _kind: & $($mutability)? RetagKind,
793 place: & $($mutability)? Place<'tcx>,
794 location: Location) {
797 PlaceContext::MutatingUse(MutatingUseContext::Retag),
802 fn super_local_decl(&mut self,
804 local_decl: & $($mutability)? LocalDecl<'tcx>) {
815 self.visit_ty(ty, TyContext::LocalDecl {
817 source_info: *source_info,
819 if let Some(user_ty) = user_ty {
820 for (user_ty, _) in & $($mutability)? user_ty.contents {
821 self.visit_user_type_projection(user_ty);
824 self.visit_source_info(source_info);
827 fn super_var_debug_info(&mut self,
828 var_debug_info: & $($mutability)? VarDebugInfo<'tcx>) {
835 self.visit_source_info(source_info);
836 let location = START_BLOCK.start_location();
838 VarDebugInfoContents::Const(c) => self.visit_constant(c, location),
839 VarDebugInfoContents::Place(place) =>
842 PlaceContext::NonUse(NonUseContext::VarDebugInfo),
848 fn super_source_scope(&mut self,
849 _scope: & $($mutability)? SourceScope) {
852 fn super_constant(&mut self,
853 constant: & $($mutability)? Constant<'tcx>,
854 location: Location) {
861 self.visit_span(span);
862 drop(user_ty); // no visit method for this
863 self.visit_const(literal, location);
866 fn super_span(&mut self, _span: & $($mutability)? Span) {
869 fn super_source_info(&mut self, source_info: & $($mutability)? SourceInfo) {
875 self.visit_span(span);
876 self.visit_source_scope(scope);
879 fn super_user_type_projection(
881 _ty: & $($mutability)? UserTypeProjection,
885 fn super_user_type_annotation(
887 _index: UserTypeAnnotationIndex,
888 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
890 self.visit_span(& $($mutability)? ty.span);
891 self.visit_ty(& $($mutability)? ty.inferred_ty, TyContext::UserTy(ty.span));
894 fn super_ty(&mut self, _ty: $(& $mutability)? Ty<'tcx>) {
897 fn super_region(&mut self, _region: & $($mutability)? ty::Region<'tcx>) {
900 fn super_const(&mut self, _const: & $($mutability)? &'tcx ty::Const<'tcx>) {
903 fn super_substs(&mut self, _substs: & $($mutability)? SubstsRef<'tcx>) {
906 // Convenience methods
910 body: &$($mutability)? Body<'tcx>,
913 macro_rules! basic_blocks {
914 (mut) => (body.basic_blocks_mut());
915 () => (body.basic_blocks());
917 let basic_block = & $($mutability)? basic_blocks!($($mutability)?)[location.block];
918 if basic_block.statements.len() == location.statement_index {
919 if let Some(ref $($mutability)? terminator) = basic_block.terminator {
920 self.visit_terminator(terminator, location)
923 let statement = & $($mutability)?
924 basic_block.statements[location.statement_index];
925 self.visit_statement(statement, location)
932 macro_rules! visit_place_fns {
934 fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
938 place: &mut Place<'tcx>,
939 context: PlaceContext,
942 self.visit_local(&mut place.local, context, location);
944 if let Some(new_projection) = self.process_projection(&place.projection, location) {
945 place.projection = self.tcx().intern_place_elems(&new_projection);
949 fn process_projection(
951 projection: &'a [PlaceElem<'tcx>],
953 ) -> Option<Vec<PlaceElem<'tcx>>> {
954 let mut projection = Cow::Borrowed(projection);
956 for i in 0..projection.len() {
957 if let Some(&elem) = projection.get(i) {
958 if let Some(elem) = self.process_projection_elem(elem, location) {
959 // This converts the borrowed projection into `Cow::Owned(_)` and returns a
960 // clone of the projection so we can mutate and reintern later.
961 let vec = projection.to_mut();
968 Cow::Borrowed(_) => None,
969 Cow::Owned(vec) => Some(vec),
973 fn process_projection_elem(
975 elem: PlaceElem<'tcx>,
977 ) -> Option<PlaceElem<'tcx>> {
979 PlaceElem::Index(local) => {
980 let mut new_local = local;
983 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
987 if new_local == local { None } else { Some(PlaceElem::Index(new_local)) }
990 | PlaceElem::Field(..)
991 | PlaceElem::ConstantIndex { .. }
992 | PlaceElem::Subslice { .. }
993 | PlaceElem::Downcast(..) => None,
1001 place_ref: PlaceRef<'tcx>,
1002 context: PlaceContext,
1005 self.super_projection(place_ref, context, location);
1008 fn visit_projection_elem(
1011 proj_base: &[PlaceElem<'tcx>],
1012 elem: PlaceElem<'tcx>,
1013 context: PlaceContext,
1016 self.super_projection_elem(local, proj_base, elem, context, location);
1019 fn super_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
1020 let mut context = context;
1022 if !place.projection.is_empty() {
1023 if context.is_use() {
1024 // ^ Only change the context if it is a real use, not a "use" in debuginfo.
1025 context = if context.is_mutating_use() {
1026 PlaceContext::MutatingUse(MutatingUseContext::Projection)
1028 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
1033 self.visit_local(&place.local, context, location);
1035 self.visit_projection(place.as_ref(), context, location);
1038 fn super_projection(
1040 place_ref: PlaceRef<'tcx>,
1041 context: PlaceContext,
1044 // FIXME: Use PlaceRef::iter_projections, once that exists.
1045 let mut cursor = place_ref.projection;
1046 while let &[ref proj_base @ .., elem] = cursor {
1048 self.visit_projection_elem(place_ref.local, cursor, elem, context, location);
1052 fn super_projection_elem(
1055 _proj_base: &[PlaceElem<'tcx>],
1056 elem: PlaceElem<'tcx>,
1057 _context: PlaceContext,
1061 ProjectionElem::Field(_field, ty) => {
1062 self.visit_ty(ty, TyContext::Location(location));
1064 ProjectionElem::Index(local) => {
1067 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
1071 ProjectionElem::Deref
1072 | ProjectionElem::Subslice { from: _, to: _, from_end: _ }
1073 | ProjectionElem::ConstantIndex { offset: _, min_length: _, from_end: _ }
1074 | ProjectionElem::Downcast(_, _) => {}
1080 make_mir_visitor!(Visitor,);
1081 make_mir_visitor!(MutVisitor, mut);
1083 pub trait MirVisitable<'tcx> {
1084 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>);
1087 impl<'tcx> MirVisitable<'tcx> for Statement<'tcx> {
1088 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1089 visitor.visit_statement(self, location)
1093 impl<'tcx> MirVisitable<'tcx> for Terminator<'tcx> {
1094 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1095 visitor.visit_terminator(self, location)
1099 impl<'tcx> MirVisitable<'tcx> for Option<Terminator<'tcx>> {
1100 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1101 visitor.visit_terminator(self.as_ref().unwrap(), location)
1105 /// Extra information passed to `visit_ty` and friends to give context
1106 /// about where the type etc appears.
1108 pub enum TyContext {
1110 /// The index of the local variable we are visiting.
1113 /// The source location where this local variable was declared.
1114 source_info: SourceInfo,
1117 /// The inferred type of a user type annotation.
1120 /// The return type of the function.
1121 ReturnTy(SourceInfo),
1123 YieldTy(SourceInfo),
1125 /// A type found at some location.
1129 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1130 pub enum NonMutatingUseContext {
1131 /// Being inspected in some way, like loading a len.
1133 /// Consumed as part of an operand.
1135 /// Consumed as part of an operand.
1143 /// AddressOf for *const pointer.
1145 /// Used as base for another place, e.g., `x` in `x.y`. Will not mutate the place.
1146 /// For example, the projection `x.y` is not marked as a mutation in these cases:
1154 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1155 pub enum MutatingUseContext {
1156 /// Appears as LHS of an assignment.
1158 /// Can often be treated as a `Store`, but needs to be separate because
1159 /// ASM is allowed to read outputs as well, so a `Store`-`AsmOutput` sequence
1160 /// cannot be simplified the way a `Store`-`Store` can be.
1162 /// Destination of a call.
1164 /// Destination of a yield.
1170 /// AddressOf for *mut pointer.
1172 /// Used as base for another place, e.g., `x` in `x.y`. Could potentially mutate the place.
1173 /// For example, the projection `x.y` is marked as a mutation in these cases:
1179 /// Retagging, a "Stacked Borrows" shadow state operation
1183 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1184 pub enum NonUseContext {
1185 /// Starting a storage live range.
1187 /// Ending a storage live range.
1189 /// User type annotation assertions for NLL.
1191 /// The data of an user variable, for debug info.
1195 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1196 pub enum PlaceContext {
1197 NonMutatingUse(NonMutatingUseContext),
1198 MutatingUse(MutatingUseContext),
1199 NonUse(NonUseContext),
1203 /// Returns `true` if this place context represents a drop.
1204 pub fn is_drop(&self) -> bool {
1205 matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
1208 /// Returns `true` if this place context represents a borrow.
1209 pub fn is_borrow(&self) -> bool {
1212 PlaceContext::NonMutatingUse(
1213 NonMutatingUseContext::SharedBorrow
1214 | NonMutatingUseContext::ShallowBorrow
1215 | NonMutatingUseContext::UniqueBorrow
1216 ) | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
1220 /// Returns `true` if this place context represents a storage live or storage dead marker.
1221 pub fn is_storage_marker(&self) -> bool {
1224 PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead)
1228 /// Returns `true` if this place context represents a use that potentially changes the value.
1229 pub fn is_mutating_use(&self) -> bool {
1230 matches!(self, PlaceContext::MutatingUse(..))
1233 /// Returns `true` if this place context represents a use that does not change the value.
1234 pub fn is_nonmutating_use(&self) -> bool {
1235 matches!(self, PlaceContext::NonMutatingUse(..))
1238 /// Returns `true` if this place context represents a use.
1239 pub fn is_use(&self) -> bool {
1240 !matches!(self, PlaceContext::NonUse(..))
1243 /// Returns `true` if this place context represents an assignment statement.
1244 pub fn is_place_assignment(&self) -> bool {
1247 PlaceContext::MutatingUse(
1248 MutatingUseContext::Store
1249 | MutatingUseContext::Call
1250 | MutatingUseContext::AsmOutput,