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: block, statement_index: index };
310 self.visit_statement(statement, location);
314 if let Some(terminator) = terminator {
315 let location = Location { block: 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();
839 PlaceContext::NonUse(NonUseContext::VarDebugInfo),
844 fn super_source_scope(&mut self,
845 _scope: & $($mutability)? SourceScope) {
848 fn super_constant(&mut self,
849 constant: & $($mutability)? Constant<'tcx>,
850 location: Location) {
857 self.visit_span(span);
858 drop(user_ty); // no visit method for this
859 self.visit_const(literal, location);
862 fn super_span(&mut self, _span: & $($mutability)? Span) {
865 fn super_source_info(&mut self, source_info: & $($mutability)? SourceInfo) {
871 self.visit_span(span);
872 self.visit_source_scope(scope);
875 fn super_user_type_projection(
877 _ty: & $($mutability)? UserTypeProjection,
881 fn super_user_type_annotation(
883 _index: UserTypeAnnotationIndex,
884 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
886 self.visit_span(& $($mutability)? ty.span);
887 self.visit_ty(& $($mutability)? ty.inferred_ty, TyContext::UserTy(ty.span));
890 fn super_ty(&mut self, _ty: $(& $mutability)? Ty<'tcx>) {
893 fn super_region(&mut self, _region: & $($mutability)? ty::Region<'tcx>) {
896 fn super_const(&mut self, _const: & $($mutability)? &'tcx ty::Const<'tcx>) {
899 fn super_substs(&mut self, _substs: & $($mutability)? SubstsRef<'tcx>) {
902 // Convenience methods
906 body: &$($mutability)? Body<'tcx>,
909 macro_rules! basic_blocks {
910 (mut) => (body.basic_blocks_mut());
911 () => (body.basic_blocks());
913 let basic_block = & $($mutability)? basic_blocks!($($mutability)?)[location.block];
914 if basic_block.statements.len() == location.statement_index {
915 if let Some(ref $($mutability)? terminator) = basic_block.terminator {
916 self.visit_terminator(terminator, location)
919 let statement = & $($mutability)?
920 basic_block.statements[location.statement_index];
921 self.visit_statement(statement, location)
928 macro_rules! visit_place_fns {
930 fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
934 place: &mut Place<'tcx>,
935 context: PlaceContext,
938 self.visit_local(&mut place.local, context, location);
940 if let Some(new_projection) = self.process_projection(&place.projection, location) {
941 place.projection = self.tcx().intern_place_elems(&new_projection);
945 fn process_projection(
947 projection: &'a [PlaceElem<'tcx>],
949 ) -> Option<Vec<PlaceElem<'tcx>>> {
950 let mut projection = Cow::Borrowed(projection);
952 for i in 0..projection.len() {
953 if let Some(&elem) = projection.get(i) {
954 if let Some(elem) = self.process_projection_elem(elem, location) {
955 // This converts the borrowed projection into `Cow::Owned(_)` and returns a
956 // clone of the projection so we can mutate and reintern later.
957 let vec = projection.to_mut();
964 Cow::Borrowed(_) => None,
965 Cow::Owned(vec) => Some(vec),
969 fn process_projection_elem(
971 elem: PlaceElem<'tcx>,
973 ) -> Option<PlaceElem<'tcx>> {
975 PlaceElem::Index(local) => {
976 let mut new_local = local;
979 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
983 if new_local == local { None } else { Some(PlaceElem::Index(new_local)) }
986 | PlaceElem::Field(..)
987 | PlaceElem::ConstantIndex { .. }
988 | PlaceElem::Subslice { .. }
989 | PlaceElem::Downcast(..) => None,
998 projection: &[PlaceElem<'tcx>],
999 context: PlaceContext,
1002 self.super_projection(local, projection, context, location);
1005 fn visit_projection_elem(
1008 proj_base: &[PlaceElem<'tcx>],
1009 elem: PlaceElem<'tcx>,
1010 context: PlaceContext,
1013 self.super_projection_elem(local, proj_base, elem, context, location);
1016 fn super_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
1017 let mut context = context;
1019 if !place.projection.is_empty() {
1020 context = if context.is_mutating_use() {
1021 PlaceContext::MutatingUse(MutatingUseContext::Projection)
1023 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
1027 self.visit_local(&place.local, context, location);
1029 self.visit_projection(place.local, &place.projection, context, location);
1032 fn super_projection(
1035 projection: &[PlaceElem<'tcx>],
1036 context: PlaceContext,
1039 let mut cursor = projection;
1040 while let &[ref proj_base @ .., elem] = cursor {
1042 self.visit_projection_elem(local, cursor, elem, context, location);
1046 fn super_projection_elem(
1049 _proj_base: &[PlaceElem<'tcx>],
1050 elem: PlaceElem<'tcx>,
1051 _context: PlaceContext,
1055 ProjectionElem::Field(_field, ty) => {
1056 self.visit_ty(ty, TyContext::Location(location));
1058 ProjectionElem::Index(local) => {
1061 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
1065 ProjectionElem::Deref
1066 | ProjectionElem::Subslice { from: _, to: _, from_end: _ }
1067 | ProjectionElem::ConstantIndex { offset: _, min_length: _, from_end: _ }
1068 | ProjectionElem::Downcast(_, _) => {}
1074 make_mir_visitor!(Visitor,);
1075 make_mir_visitor!(MutVisitor, mut);
1077 pub trait MirVisitable<'tcx> {
1078 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>);
1081 impl<'tcx> MirVisitable<'tcx> for Statement<'tcx> {
1082 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1083 visitor.visit_statement(self, location)
1087 impl<'tcx> MirVisitable<'tcx> for Terminator<'tcx> {
1088 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1089 visitor.visit_terminator(self, location)
1093 impl<'tcx> MirVisitable<'tcx> for Option<Terminator<'tcx>> {
1094 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1095 visitor.visit_terminator(self.as_ref().unwrap(), location)
1099 /// Extra information passed to `visit_ty` and friends to give context
1100 /// about where the type etc appears.
1102 pub enum TyContext {
1104 /// The index of the local variable we are visiting.
1107 /// The source location where this local variable was declared.
1108 source_info: SourceInfo,
1111 /// The inferred type of a user type annotation.
1114 /// The return type of the function.
1115 ReturnTy(SourceInfo),
1117 YieldTy(SourceInfo),
1119 /// A type found at some location.
1123 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1124 pub enum NonMutatingUseContext {
1125 /// Being inspected in some way, like loading a len.
1127 /// Consumed as part of an operand.
1129 /// Consumed as part of an operand.
1137 /// AddressOf for *const pointer.
1139 /// Used as base for another place, e.g., `x` in `x.y`. Will not mutate the place.
1140 /// For example, the projection `x.y` is not marked as a mutation in these cases:
1148 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1149 pub enum MutatingUseContext {
1150 /// Appears as LHS of an assignment.
1152 /// Can often be treated as a `Store`, but needs to be separate because
1153 /// ASM is allowed to read outputs as well, so a `Store`-`AsmOutput` sequence
1154 /// cannot be simplified the way a `Store`-`Store` can be.
1156 /// Destination of a call.
1158 /// Destination of a yield.
1164 /// AddressOf for *mut pointer.
1166 /// Used as base for another place, e.g., `x` in `x.y`. Could potentially mutate the place.
1167 /// For example, the projection `x.y` is marked as a mutation in these cases:
1173 /// Retagging, a "Stacked Borrows" shadow state operation
1177 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1178 pub enum NonUseContext {
1179 /// Starting a storage live range.
1181 /// Ending a storage live range.
1183 /// User type annotation assertions for NLL.
1185 /// The data of an user variable, for debug info.
1189 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1190 pub enum PlaceContext {
1191 NonMutatingUse(NonMutatingUseContext),
1192 MutatingUse(MutatingUseContext),
1193 NonUse(NonUseContext),
1197 /// Returns `true` if this place context represents a drop.
1198 pub fn is_drop(&self) -> bool {
1199 matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
1202 /// Returns `true` if this place context represents a borrow.
1203 pub fn is_borrow(&self) -> bool {
1206 PlaceContext::NonMutatingUse(
1207 NonMutatingUseContext::SharedBorrow
1208 | NonMutatingUseContext::ShallowBorrow
1209 | NonMutatingUseContext::UniqueBorrow
1210 ) | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
1214 /// Returns `true` if this place context represents a storage live or storage dead marker.
1215 pub fn is_storage_marker(&self) -> bool {
1218 PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead)
1222 /// Returns `true` if this place context represents a use that potentially changes the value.
1223 pub fn is_mutating_use(&self) -> bool {
1224 matches!(self, PlaceContext::MutatingUse(..))
1227 /// Returns `true` if this place context represents a use that does not change the value.
1228 pub fn is_nonmutating_use(&self) -> bool {
1229 matches!(self, PlaceContext::NonMutatingUse(..))
1232 /// Returns `true` if this place context represents a use.
1233 pub fn is_use(&self) -> bool {
1234 !matches!(self, PlaceContext::NonUse(..))
1237 /// Returns `true` if this place context represents an assignment statement.
1238 pub fn is_place_assignment(&self) -> bool {
1241 PlaceContext::MutatingUse(
1242 MutatingUseContext::Store
1243 | MutatingUseContext::Call
1244 | MutatingUseContext::AsmOutput,