1use crate::mir::*;
66use crate::ty::CanonicalUserTypeAnnotation;
67
68macro_rules! make_mir_visitor {
69 ($visitor_trait_name:ident, $($mutability:ident)?) => {
70 pub trait $visitor_trait_name<'tcx> {
71 fn visit_body(
75 &mut self,
76 body: &$($mutability)? Body<'tcx>,
77 ) {
78 self.super_body(body);
79 }
80
81 extra_body_methods!($($mutability)?);
82
83 fn visit_basic_block_data(
84 &mut self,
85 block: BasicBlock,
86 data: & $($mutability)? BasicBlockData<'tcx>,
87 ) {
88 self.super_basic_block_data(block, data);
89 }
90
91 fn visit_source_scope_data(
92 &mut self,
93 scope_data: & $($mutability)? SourceScopeData<'tcx>,
94 ) {
95 self.super_source_scope_data(scope_data);
96 }
97
98 fn visit_statement(
99 &mut self,
100 statement: & $($mutability)? Statement<'tcx>,
101 location: Location,
102 ) {
103 self.super_statement(statement, location);
104 }
105
106 fn visit_assign(
107 &mut self,
108 place: & $($mutability)? Place<'tcx>,
109 rvalue: & $($mutability)? Rvalue<'tcx>,
110 location: Location,
111 ) {
112 self.super_assign(place, rvalue, location);
113 }
114
115 fn visit_terminator(
116 &mut self,
117 terminator: & $($mutability)? Terminator<'tcx>,
118 location: Location,
119 ) {
120 self.super_terminator(terminator, location);
121 }
122
123 fn visit_assert_message(
124 &mut self,
125 msg: & $($mutability)? AssertMessage<'tcx>,
126 location: Location,
127 ) {
128 self.super_assert_message(msg, location);
129 }
130
131 fn visit_rvalue(
132 &mut self,
133 rvalue: & $($mutability)? Rvalue<'tcx>,
134 location: Location,
135 ) {
136 self.super_rvalue(rvalue, location);
137 }
138
139 fn visit_operand(
140 &mut self,
141 operand: & $($mutability)? Operand<'tcx>,
142 location: Location,
143 ) {
144 self.super_operand(operand, location);
145 }
146
147 fn visit_ascribe_user_ty(
148 &mut self,
149 place: & $($mutability)? Place<'tcx>,
150 variance: $(& $mutability)? ty::Variance,
151 user_ty: & $($mutability)? UserTypeProjection,
152 location: Location,
153 ) {
154 self.super_ascribe_user_ty(place, variance, user_ty, location);
155 }
156
157 fn visit_coverage(
158 &mut self,
159 kind: & $($mutability)? coverage::CoverageKind,
160 location: Location,
161 ) {
162 self.super_coverage(kind, location);
163 }
164
165 fn visit_retag(
166 &mut self,
167 kind: $(& $mutability)? RetagKind,
168 place: & $($mutability)? Place<'tcx>,
169 location: Location,
170 ) {
171 self.super_retag(kind, place, location);
172 }
173
174 fn visit_place(
175 &mut self,
176 place: & $($mutability)? Place<'tcx>,
177 context: PlaceContext,
178 location: Location,
179 ) {
180 self.super_place(place, context, location);
181 }
182
183 visit_place_fns!($($mutability)?);
184
185 fn visit_const_operand(
188 &mut self,
189 constant: & $($mutability)? ConstOperand<'tcx>,
190 location: Location,
191 ) {
192 self.super_const_operand(constant, location);
193 }
194
195 fn visit_ty_const(
196 &mut self,
197 ct: $( & $mutability)? ty::Const<'tcx>,
198 location: Location,
199 ) {
200 self.super_ty_const(ct, location);
201 }
202
203 fn visit_span(
204 &mut self,
205 span: $(& $mutability)? Span,
206 ) {
207 self.super_span(span);
208 }
209
210 fn visit_source_info(
211 &mut self,
212 source_info: & $($mutability)? SourceInfo,
213 ) {
214 self.super_source_info(source_info);
215 }
216
217 fn visit_ty(
218 &mut self,
219 ty: $(& $mutability)? Ty<'tcx>,
220 _: TyContext,
221 ) {
222 self.super_ty(ty);
223 }
224
225 fn visit_user_type_projection(
226 &mut self,
227 ty: & $($mutability)? UserTypeProjection,
228 ) {
229 self.super_user_type_projection(ty);
230 }
231
232 fn visit_user_type_annotation(
233 &mut self,
234 index: UserTypeAnnotationIndex,
235 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
236 ) {
237 self.super_user_type_annotation(index, ty);
238 }
239
240 fn visit_region(
241 &mut self,
242 region: $(& $mutability)? ty::Region<'tcx>,
243 _: Location,
244 ) {
245 self.super_region(region);
246 }
247
248 fn visit_args(
249 &mut self,
250 args: & $($mutability)? GenericArgsRef<'tcx>,
251 _: Location,
252 ) {
253 self.super_args(args);
254 }
255
256 fn visit_local_decl(
257 &mut self,
258 local: Local,
259 local_decl: & $($mutability)? LocalDecl<'tcx>,
260 ) {
261 self.super_local_decl(local, local_decl);
262 }
263
264 fn visit_var_debug_info(
265 &mut self,
266 var_debug_info: & $($mutability)* VarDebugInfo<'tcx>,
267 ) {
268 self.super_var_debug_info(var_debug_info);
269 }
270
271 fn visit_local(
272 &mut self,
273 _local: $(& $mutability)? Local,
274 _context: PlaceContext,
275 _location: Location,
276 ) {}
277
278 fn visit_source_scope(
279 &mut self,
280 scope: $(& $mutability)? SourceScope,
281 ) {
282 self.super_source_scope(scope);
283 }
284
285 fn super_body(
289 &mut self,
290 body: &$($mutability)? Body<'tcx>,
291 ) {
292 super_body!(self, body, $($mutability, true)?);
293 }
294
295 fn super_basic_block_data(&mut self,
296 block: BasicBlock,
297 data: & $($mutability)? BasicBlockData<'tcx>) {
298 let BasicBlockData {
299 statements,
300 terminator,
301 is_cleanup: _
302 } = data;
303
304 let mut index = 0;
305 for statement in statements {
306 let location = Location { block, statement_index: index };
307 self.visit_statement(statement, location);
308 index += 1;
309 }
310
311 if let Some(terminator) = terminator {
312 let location = Location { block, statement_index: index };
313 self.visit_terminator(terminator, location);
314 }
315 }
316
317 fn super_source_scope_data(
318 &mut self,
319 scope_data: & $($mutability)? SourceScopeData<'tcx>,
320 ) {
321 let SourceScopeData {
322 span,
323 parent_scope,
324 inlined,
325 inlined_parent_scope,
326 local_data: _,
327 } = scope_data;
328
329 self.visit_span($(& $mutability)? *span);
330 if let Some(parent_scope) = parent_scope {
331 self.visit_source_scope($(& $mutability)? *parent_scope);
332 }
333 if let Some((callee, callsite_span)) = inlined {
334 let location = Location::START;
335
336 self.visit_span($(& $mutability)? *callsite_span);
337
338 let ty::Instance { def: callee_def, args: callee_args } = callee;
339 match callee_def {
340 ty::InstanceKind::Item(_def_id) => {}
341
342 ty::InstanceKind::Intrinsic(_def_id) |
343 ty::InstanceKind::VTableShim(_def_id) |
344 ty::InstanceKind::ReifyShim(_def_id, _) |
345 ty::InstanceKind::Virtual(_def_id, _) |
346 ty::InstanceKind::ThreadLocalShim(_def_id) |
347 ty::InstanceKind::ClosureOnceShim { call_once: _def_id, track_caller: _ } |
348 ty::InstanceKind::ConstructCoroutineInClosureShim {
349 coroutine_closure_def_id: _def_id,
350 receiver_by_ref: _,
351 } |
352 ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, None) |
353 ty::InstanceKind::DropGlue(_def_id, None) => {}
354
355 ty::InstanceKind::FnPtrShim(_def_id, ty) |
356 ty::InstanceKind::DropGlue(_def_id, Some(ty)) |
357 ty::InstanceKind::CloneShim(_def_id, ty) |
358 ty::InstanceKind::FnPtrAddrShim(_def_id, ty) |
359 ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, Some(ty)) => {
360 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
362 }
363 }
364 self.visit_args(callee_args, location);
365 }
366 if let Some(inlined_parent_scope) = inlined_parent_scope {
367 self.visit_source_scope($(& $mutability)? *inlined_parent_scope);
368 }
369 }
370
371 fn super_statement(&mut self,
372 statement: & $($mutability)? Statement<'tcx>,
373 location: Location) {
374 let Statement {
375 source_info,
376 kind,
377 } = statement;
378
379 self.visit_source_info(source_info);
380 match kind {
381 StatementKind::Assign(
382 box (place, rvalue)
383 ) => {
384 self.visit_assign(place, rvalue, location);
385 }
386 StatementKind::FakeRead(box (_, place)) => {
387 self.visit_place(
388 place,
389 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
390 location
391 );
392 }
393 StatementKind::SetDiscriminant { place, .. } => {
394 self.visit_place(
395 place,
396 PlaceContext::MutatingUse(MutatingUseContext::SetDiscriminant),
397 location
398 );
399 }
400 StatementKind::Deinit(place) => {
401 self.visit_place(
402 place,
403 PlaceContext::MutatingUse(MutatingUseContext::Deinit),
404 location
405 )
406 }
407 StatementKind::StorageLive(local) => {
408 self.visit_local(
409 $(& $mutability)? *local,
410 PlaceContext::NonUse(NonUseContext::StorageLive),
411 location
412 );
413 }
414 StatementKind::StorageDead(local) => {
415 self.visit_local(
416 $(& $mutability)? *local,
417 PlaceContext::NonUse(NonUseContext::StorageDead),
418 location
419 );
420 }
421 StatementKind::Retag(kind, place) => {
422 self.visit_retag($(& $mutability)? *kind, place, location);
423 }
424 StatementKind::PlaceMention(place) => {
425 self.visit_place(
426 place,
427 PlaceContext::NonMutatingUse(NonMutatingUseContext::PlaceMention),
428 location
429 );
430 }
431 StatementKind::AscribeUserType(
432 box (place, user_ty),
433 variance
434 ) => {
435 self.visit_ascribe_user_ty(place, $(& $mutability)? *variance, user_ty, location);
436 }
437 StatementKind::Coverage(coverage) => {
438 self.visit_coverage(
439 coverage,
440 location
441 )
442 }
443 StatementKind::Intrinsic(box ref $($mutability)? intrinsic) => {
444 match intrinsic {
445 NonDivergingIntrinsic::Assume(op) => self.visit_operand(op, location),
446 NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping { src, dst, count }) => {
447 self.visit_operand(src, location);
448 self.visit_operand(dst, location);
449 self.visit_operand(count, location);
450 }
451 }
452 }
453 StatementKind::ConstEvalCounter => {}
454 StatementKind::Nop => {}
455 StatementKind::BackwardIncompatibleDropHint { .. } => {}
456 }
457 }
458
459 fn super_assign(&mut self,
460 place: &$($mutability)? Place<'tcx>,
461 rvalue: &$($mutability)? Rvalue<'tcx>,
462 location: Location) {
463 self.visit_place(
464 place,
465 PlaceContext::MutatingUse(MutatingUseContext::Store),
466 location
467 );
468 self.visit_rvalue(rvalue, location);
469 }
470
471 fn super_terminator(&mut self,
472 terminator: &$($mutability)? Terminator<'tcx>,
473 location: Location) {
474 let Terminator { source_info, kind } = terminator;
475
476 self.visit_source_info(source_info);
477 match kind {
478 TerminatorKind::Goto { .. } |
479 TerminatorKind::UnwindResume |
480 TerminatorKind::UnwindTerminate(_) |
481 TerminatorKind::CoroutineDrop |
482 TerminatorKind::Unreachable |
483 TerminatorKind::FalseEdge { .. } |
484 TerminatorKind::FalseUnwind { .. } => {}
485
486 TerminatorKind::Return => {
487 let $($mutability)? local = RETURN_PLACE;
490 self.visit_local(
491 $(& $mutability)? local,
492 PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
493 location,
494 );
495
496 assert_eq!(
497 local,
498 RETURN_PLACE,
499 "`MutVisitor` tried to mutate return place of `return` terminator"
500 );
501 }
502
503 TerminatorKind::SwitchInt {
504 discr,
505 targets: _
506 } => {
507 self.visit_operand(discr, location);
508 }
509
510 TerminatorKind::Drop {
511 place,
512 target: _,
513 unwind: _,
514 replace: _,
515 } => {
516 self.visit_place(
517 place,
518 PlaceContext::MutatingUse(MutatingUseContext::Drop),
519 location
520 );
521 }
522
523 TerminatorKind::Call {
524 func,
525 args,
526 destination,
527 target: _,
528 unwind: _,
529 call_source: _,
530 fn_span,
531 } => {
532 self.visit_span($(& $mutability)? *fn_span);
533 self.visit_operand(func, location);
534 for arg in args {
535 self.visit_operand(&$($mutability)? arg.node, location);
536 }
537 self.visit_place(
538 destination,
539 PlaceContext::MutatingUse(MutatingUseContext::Call),
540 location
541 );
542 }
543
544 TerminatorKind::TailCall {
545 func,
546 args,
547 fn_span,
548 } => {
549 self.visit_span($(& $mutability)? *fn_span);
550 self.visit_operand(func, location);
551 for arg in args {
552 self.visit_operand(&$($mutability)? arg.node, location);
553 }
554 },
555
556 TerminatorKind::Assert {
557 cond,
558 expected: _,
559 msg,
560 target: _,
561 unwind: _,
562 } => {
563 self.visit_operand(cond, location);
564 self.visit_assert_message(msg, location);
565 }
566
567 TerminatorKind::Yield {
568 value,
569 resume: _,
570 resume_arg,
571 drop: _,
572 } => {
573 self.visit_operand(value, location);
574 self.visit_place(
575 resume_arg,
576 PlaceContext::MutatingUse(MutatingUseContext::Yield),
577 location,
578 );
579 }
580
581 TerminatorKind::InlineAsm {
582 asm_macro: _,
583 template: _,
584 operands,
585 options: _,
586 line_spans: _,
587 targets: _,
588 unwind: _,
589 } => {
590 for op in operands {
591 match op {
592 InlineAsmOperand::In { value, .. } => {
593 self.visit_operand(value, location);
594 }
595 InlineAsmOperand::Out { place: Some(place), .. } => {
596 self.visit_place(
597 place,
598 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
599 location,
600 );
601 }
602 InlineAsmOperand::InOut { in_value, out_place, .. } => {
603 self.visit_operand(in_value, location);
604 if let Some(out_place) = out_place {
605 self.visit_place(
606 out_place,
607 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
608 location,
609 );
610 }
611 }
612 InlineAsmOperand::Const { value }
613 | InlineAsmOperand::SymFn { value } => {
614 self.visit_const_operand(value, location);
615 }
616 InlineAsmOperand::Out { place: None, .. }
617 | InlineAsmOperand::SymStatic { def_id: _ }
618 | InlineAsmOperand::Label { target_index: _ } => {}
619 }
620 }
621 }
622 }
623 }
624
625 fn super_assert_message(&mut self,
626 msg: & $($mutability)? AssertMessage<'tcx>,
627 location: Location) {
628 use crate::mir::AssertKind::*;
629 match msg {
630 BoundsCheck { len, index } => {
631 self.visit_operand(len, location);
632 self.visit_operand(index, location);
633 }
634 Overflow(_, l, r) => {
635 self.visit_operand(l, location);
636 self.visit_operand(r, location);
637 }
638 OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
639 self.visit_operand(op, location);
640 }
641 ResumedAfterReturn(_) | ResumedAfterPanic(_) | NullPointerDereference => {
642 }
644 MisalignedPointerDereference { required, found } => {
645 self.visit_operand(required, location);
646 self.visit_operand(found, location);
647 }
648 }
649 }
650
651 fn super_rvalue(&mut self,
652 rvalue: & $($mutability)? Rvalue<'tcx>,
653 location: Location) {
654 match rvalue {
655 Rvalue::Use(operand) => {
656 self.visit_operand(operand, location);
657 }
658
659 Rvalue::Repeat(value, ct) => {
660 self.visit_operand(value, location);
661 self.visit_ty_const($(&$mutability)? *ct, location);
662 }
663
664 Rvalue::ThreadLocalRef(_) => {}
665
666 Rvalue::Ref(r, bk, path) => {
667 self.visit_region($(& $mutability)? *r, location);
668 let ctx = match bk {
669 BorrowKind::Shared => PlaceContext::NonMutatingUse(
670 NonMutatingUseContext::SharedBorrow
671 ),
672 BorrowKind::Fake(_) => PlaceContext::NonMutatingUse(
673 NonMutatingUseContext::FakeBorrow
674 ),
675 BorrowKind::Mut { .. } =>
676 PlaceContext::MutatingUse(MutatingUseContext::Borrow),
677 };
678 self.visit_place(path, ctx, location);
679 }
680 Rvalue::CopyForDeref(place) => {
681 self.visit_place(
682 place,
683 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
684 location
685 );
686 }
687
688 Rvalue::RawPtr(m, path) => {
689 let ctx = match m {
690 RawPtrKind::Mut => PlaceContext::MutatingUse(
691 MutatingUseContext::RawBorrow
692 ),
693 RawPtrKind::Const => PlaceContext::NonMutatingUse(
694 NonMutatingUseContext::RawBorrow
695 ),
696 RawPtrKind::FakeForPtrMetadata => PlaceContext::NonMutatingUse(
697 NonMutatingUseContext::Inspect
698 ),
699 };
700 self.visit_place(path, ctx, location);
701 }
702
703 Rvalue::Len(path) => {
704 self.visit_place(
705 path,
706 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
707 location
708 );
709 }
710
711 Rvalue::Cast(_cast_kind, operand, ty) => {
712 self.visit_operand(operand, location);
713 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
714 }
715
716 Rvalue::BinaryOp(_bin_op, box(lhs, rhs)) => {
717 self.visit_operand(lhs, location);
718 self.visit_operand(rhs, location);
719 }
720
721 Rvalue::UnaryOp(_un_op, op) => {
722 self.visit_operand(op, location);
723 }
724
725 Rvalue::Discriminant(place) => {
726 self.visit_place(
727 place,
728 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
729 location
730 );
731 }
732
733 Rvalue::NullaryOp(_op, ty) => {
734 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
735 }
736
737 Rvalue::Aggregate(kind, operands) => {
738 let kind = &$($mutability)? **kind;
739 match kind {
740 AggregateKind::Array(ty) => {
741 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
742 }
743 AggregateKind::Tuple => {
744 }
745 AggregateKind::Adt(
746 _adt_def,
747 _variant_index,
748 args,
749 _user_args,
750 _active_field_index
751 ) => {
752 self.visit_args(args, location);
753 }
754 AggregateKind::Closure(
755 _,
756 closure_args
757 ) => {
758 self.visit_args(closure_args, location);
759 }
760 AggregateKind::Coroutine(
761 _,
762 coroutine_args,
763 ) => {
764 self.visit_args(coroutine_args, location);
765 }
766 AggregateKind::CoroutineClosure(
767 _,
768 coroutine_closure_args,
769 ) => {
770 self.visit_args(coroutine_closure_args, location);
771 }
772 AggregateKind::RawPtr(ty, _) => {
773 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
774 }
775 }
776
777 for operand in operands {
778 self.visit_operand(operand, location);
779 }
780 }
781
782 Rvalue::ShallowInitBox(operand, ty) => {
783 self.visit_operand(operand, location);
784 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
785 }
786
787 Rvalue::WrapUnsafeBinder(op, ty) => {
788 self.visit_operand(op, location);
789 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
790 }
791 }
792 }
793
794 fn super_operand(&mut self,
795 operand: & $($mutability)? Operand<'tcx>,
796 location: Location) {
797 match operand {
798 Operand::Copy(place) => {
799 self.visit_place(
800 place,
801 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
802 location
803 );
804 }
805 Operand::Move(place) => {
806 self.visit_place(
807 place,
808 PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
809 location
810 );
811 }
812 Operand::Constant(constant) => {
813 self.visit_const_operand(constant, location);
814 }
815 }
816 }
817
818 fn super_ascribe_user_ty(&mut self,
819 place: & $($mutability)? Place<'tcx>,
820 variance: $(& $mutability)? ty::Variance,
821 user_ty: & $($mutability)? UserTypeProjection,
822 location: Location) {
823 self.visit_place(
824 place,
825 PlaceContext::NonUse(NonUseContext::AscribeUserTy($(* &$mutability *)? variance)),
826 location
827 );
828 self.visit_user_type_projection(user_ty);
829 }
830
831 fn super_coverage(&mut self,
832 _kind: & $($mutability)? coverage::CoverageKind,
833 _location: Location) {
834 }
835
836 fn super_retag(&mut self,
837 _kind: $(& $mutability)? RetagKind,
838 place: & $($mutability)? Place<'tcx>,
839 location: Location) {
840 self.visit_place(
841 place,
842 PlaceContext::MutatingUse(MutatingUseContext::Retag),
843 location,
844 );
845 }
846
847 fn super_local_decl(&mut self,
848 local: Local,
849 local_decl: & $($mutability)? LocalDecl<'tcx>) {
850 let LocalDecl {
851 mutability: _,
852 ty,
853 user_ty,
854 source_info,
855 local_info: _,
856 } = local_decl;
857
858 self.visit_source_info(source_info);
859
860 self.visit_ty($(& $mutability)? *ty, TyContext::LocalDecl {
861 local,
862 source_info: *source_info,
863 });
864 if let Some(user_ty) = user_ty {
865 for (user_ty, _) in & $($mutability)? user_ty.contents {
866 self.visit_user_type_projection(user_ty);
867 }
868 }
869 }
870
871 fn super_var_debug_info(
872 &mut self,
873 var_debug_info: & $($mutability)? VarDebugInfo<'tcx>
874 ) {
875 let VarDebugInfo {
876 name: _,
877 source_info,
878 composite,
879 value,
880 argument_index: _,
881 } = var_debug_info;
882
883 self.visit_source_info(source_info);
884 let location = Location::START;
885 if let Some(box VarDebugInfoFragment { ref $($mutability)? ty, ref $($mutability)? projection }) = composite {
886 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
887 for elem in projection {
888 let ProjectionElem::Field(_, ty) = elem else { bug!() };
889 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
890 }
891 }
892 match value {
893 VarDebugInfoContents::Const(c) => self.visit_const_operand(c, location),
894 VarDebugInfoContents::Place(place) =>
895 self.visit_place(
896 place,
897 PlaceContext::NonUse(NonUseContext::VarDebugInfo),
898 location
899 ),
900 }
901 }
902
903 fn super_source_scope(
904 &mut self,
905 _scope: $(& $mutability)? SourceScope
906 ) {}
907
908 fn super_const_operand(
909 &mut self,
910 constant: & $($mutability)? ConstOperand<'tcx>,
911 location: Location
912 ) {
913 let ConstOperand {
914 span,
915 user_ty: _, const_,
917 } = constant;
918
919 self.visit_span($(& $mutability)? *span);
920 match const_ {
921 Const::Ty(_, ct) => self.visit_ty_const($(&$mutability)? *ct, location),
922 Const::Val(_, ty) => self.visit_ty($(& $mutability)? *ty, TyContext::Location(location)),
923 Const::Unevaluated(_, ty) => self.visit_ty($(& $mutability)? *ty, TyContext::Location(location)),
924 }
925 }
926
927 fn super_ty_const(
928 &mut self,
929 _ct: $(& $mutability)? ty::Const<'tcx>,
930 _location: Location,
931 ) {
932
933 }
934
935 fn super_span(&mut self, _span: $(& $mutability)? Span) {
936 }
937
938 fn super_source_info(&mut self, source_info: & $($mutability)? SourceInfo) {
939 let SourceInfo {
940 span,
941 scope,
942 } = source_info;
943
944 self.visit_span($(& $mutability)? *span);
945 self.visit_source_scope($(& $mutability)? *scope);
946 }
947
948 fn super_user_type_projection(
949 &mut self,
950 _ty: & $($mutability)? UserTypeProjection,
951 ) {
952 }
953
954 fn super_user_type_annotation(
955 &mut self,
956 _index: UserTypeAnnotationIndex,
957 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
958 ) {
959 self.visit_span($(& $mutability)? ty.span);
960 self.visit_ty($(& $mutability)? ty.inferred_ty, TyContext::UserTy(ty.span));
961 }
962
963 fn super_ty(&mut self, _ty: $(& $mutability)? Ty<'tcx>) {
964 }
965
966 fn super_region(&mut self, _region: $(& $mutability)? ty::Region<'tcx>) {
967 }
968
969 fn super_args(&mut self, _args: & $($mutability)? GenericArgsRef<'tcx>) {
970 }
971
972 fn visit_location(
975 &mut self,
976 body: &$($mutability)? Body<'tcx>,
977 location: Location
978 ) {
979 let basic_block = & $($mutability)? basic_blocks!(body, $($mutability, true)?)[location.block];
980 if basic_block.statements.len() == location.statement_index {
981 if let Some(ref $($mutability)? terminator) = basic_block.terminator {
982 self.visit_terminator(terminator, location)
983 }
984 } else {
985 let statement = & $($mutability)?
986 basic_block.statements[location.statement_index];
987 self.visit_statement(statement, location)
988 }
989 }
990 }
991 }
992}
993
994macro_rules! basic_blocks {
995 ($body:ident, mut, true) => {
996 $body.basic_blocks.as_mut()
997 };
998 ($body:ident, mut, false) => {
999 $body.basic_blocks.as_mut_preserves_cfg()
1000 };
1001 ($body:ident,) => {
1002 $body.basic_blocks
1003 };
1004}
1005
1006macro_rules! basic_blocks_iter {
1007 ($body:ident, mut, $invalidate:tt) => {
1008 basic_blocks!($body, mut, $invalidate).iter_enumerated_mut()
1009 };
1010 ($body:ident,) => {
1011 basic_blocks!($body,).iter_enumerated()
1012 };
1013}
1014
1015macro_rules! extra_body_methods {
1016 (mut) => {
1017 fn visit_body_preserves_cfg(&mut self, body: &mut Body<'tcx>) {
1018 self.super_body_preserves_cfg(body);
1019 }
1020
1021 fn super_body_preserves_cfg(&mut self, body: &mut Body<'tcx>) {
1022 super_body!(self, body, mut, false);
1023 }
1024 };
1025 () => {};
1026}
1027
1028macro_rules! super_body {
1029 ($self:ident, $body:ident, $($mutability:ident, $invalidate:tt)?) => {
1030 let span = $body.span;
1031 if let Some(coroutine) = &$($mutability)? $body.coroutine {
1032 if let Some(yield_ty) = $(& $mutability)? coroutine.yield_ty {
1033 $self.visit_ty(
1034 yield_ty,
1035 TyContext::YieldTy(SourceInfo::outermost(span))
1036 );
1037 }
1038 if let Some(resume_ty) = $(& $mutability)? coroutine.resume_ty {
1039 $self.visit_ty(
1040 resume_ty,
1041 TyContext::ResumeTy(SourceInfo::outermost(span))
1042 );
1043 }
1044 }
1045
1046 for (bb, data) in basic_blocks_iter!($body, $($mutability, $invalidate)?) {
1047 $self.visit_basic_block_data(bb, data);
1048 }
1049
1050 for scope in &$($mutability)? $body.source_scopes {
1051 $self.visit_source_scope_data(scope);
1052 }
1053
1054 $self.visit_ty(
1055 $(& $mutability)? $body.return_ty(),
1056 TyContext::ReturnTy(SourceInfo::outermost($body.span))
1057 );
1058
1059 for local in $body.local_decls.indices() {
1060 $self.visit_local_decl(local, & $($mutability)? $body.local_decls[local]);
1061 }
1062
1063 #[allow(unused_macro_rules)]
1064 macro_rules! type_annotations {
1065 (mut) => ($body.user_type_annotations.iter_enumerated_mut());
1066 () => ($body.user_type_annotations.iter_enumerated());
1067 }
1068
1069 for (index, annotation) in type_annotations!($($mutability)?) {
1070 $self.visit_user_type_annotation(
1071 index, annotation
1072 );
1073 }
1074
1075 for var_debug_info in &$($mutability)? $body.var_debug_info {
1076 $self.visit_var_debug_info(var_debug_info);
1077 }
1078
1079 $self.visit_span($(& $mutability)? $body.span);
1080
1081 if let Some(required_consts) = &$($mutability)? $body.required_consts {
1082 for const_ in required_consts {
1083 let location = Location::START;
1084 $self.visit_const_operand(const_, location);
1085 }
1086 }
1087 }
1088}
1089
1090macro_rules! visit_place_fns {
1091 (mut) => {
1092 fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
1093
1094 fn super_place(
1095 &mut self,
1096 place: &mut Place<'tcx>,
1097 context: PlaceContext,
1098 location: Location,
1099 ) {
1100 self.visit_local(&mut place.local, context, location);
1101
1102 if let Some(new_projection) = self.process_projection(&place.projection, location) {
1103 place.projection = self.tcx().mk_place_elems(&new_projection);
1104 }
1105 }
1106
1107 fn process_projection<'a>(
1108 &mut self,
1109 projection: &'a [PlaceElem<'tcx>],
1110 location: Location,
1111 ) -> Option<Vec<PlaceElem<'tcx>>> {
1112 let mut projection = Cow::Borrowed(projection);
1113
1114 for i in 0..projection.len() {
1115 if let Some(&elem) = projection.get(i) {
1116 if let Some(elem) = self.process_projection_elem(elem, location) {
1117 let vec = projection.to_mut();
1120 vec[i] = elem;
1121 }
1122 }
1123 }
1124
1125 match projection {
1126 Cow::Borrowed(_) => None,
1127 Cow::Owned(vec) => Some(vec),
1128 }
1129 }
1130
1131 fn process_projection_elem(
1132 &mut self,
1133 elem: PlaceElem<'tcx>,
1134 location: Location,
1135 ) -> Option<PlaceElem<'tcx>> {
1136 match elem {
1137 PlaceElem::Index(local) => {
1138 let mut new_local = local;
1139 self.visit_local(
1140 &mut new_local,
1141 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
1142 location,
1143 );
1144
1145 if new_local == local { None } else { Some(PlaceElem::Index(new_local)) }
1146 }
1147 PlaceElem::Field(field, ty) => {
1148 let mut new_ty = ty;
1149 self.visit_ty(&mut new_ty, TyContext::Location(location));
1150 if ty != new_ty { Some(PlaceElem::Field(field, new_ty)) } else { None }
1151 }
1152 PlaceElem::OpaqueCast(ty) => {
1153 let mut new_ty = ty;
1154 self.visit_ty(&mut new_ty, TyContext::Location(location));
1155 if ty != new_ty { Some(PlaceElem::OpaqueCast(new_ty)) } else { None }
1156 }
1157 PlaceElem::Subtype(ty) => {
1158 let mut new_ty = ty;
1159 self.visit_ty(&mut new_ty, TyContext::Location(location));
1160 if ty != new_ty { Some(PlaceElem::Subtype(new_ty)) } else { None }
1161 }
1162 PlaceElem::UnwrapUnsafeBinder(ty) => {
1163 let mut new_ty = ty;
1164 self.visit_ty(&mut new_ty, TyContext::Location(location));
1165 if ty != new_ty { Some(PlaceElem::UnwrapUnsafeBinder(new_ty)) } else { None }
1166 }
1167 PlaceElem::Deref
1168 | PlaceElem::ConstantIndex { .. }
1169 | PlaceElem::Subslice { .. }
1170 | PlaceElem::Downcast(..) => None,
1171 }
1172 }
1173 };
1174
1175 () => {
1176 fn visit_projection(
1177 &mut self,
1178 place_ref: PlaceRef<'tcx>,
1179 context: PlaceContext,
1180 location: Location,
1181 ) {
1182 self.super_projection(place_ref, context, location);
1183 }
1184
1185 fn visit_projection_elem(
1186 &mut self,
1187 place_ref: PlaceRef<'tcx>,
1188 elem: PlaceElem<'tcx>,
1189 context: PlaceContext,
1190 location: Location,
1191 ) {
1192 self.super_projection_elem(place_ref, elem, context, location);
1193 }
1194
1195 fn super_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
1196 let mut context = context;
1197
1198 if !place.projection.is_empty() {
1199 if context.is_use() {
1200 context = if context.is_mutating_use() {
1202 PlaceContext::MutatingUse(MutatingUseContext::Projection)
1203 } else {
1204 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
1205 };
1206 }
1207 }
1208
1209 self.visit_local(place.local, context, location);
1210
1211 self.visit_projection(place.as_ref(), context, location);
1212 }
1213
1214 fn super_projection(
1215 &mut self,
1216 place_ref: PlaceRef<'tcx>,
1217 context: PlaceContext,
1218 location: Location,
1219 ) {
1220 for (base, elem) in place_ref.iter_projections().rev() {
1221 self.visit_projection_elem(base, elem, context, location);
1222 }
1223 }
1224
1225 fn super_projection_elem(
1226 &mut self,
1227 _place_ref: PlaceRef<'tcx>,
1228 elem: PlaceElem<'tcx>,
1229 _context: PlaceContext,
1230 location: Location,
1231 ) {
1232 match elem {
1233 ProjectionElem::OpaqueCast(ty)
1234 | ProjectionElem::Subtype(ty)
1235 | ProjectionElem::Field(_, ty)
1236 | ProjectionElem::UnwrapUnsafeBinder(ty) => {
1237 self.visit_ty(ty, TyContext::Location(location));
1238 }
1239 ProjectionElem::Index(local) => {
1240 self.visit_local(
1241 local,
1242 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
1243 location,
1244 );
1245 }
1246 ProjectionElem::Deref
1247 | ProjectionElem::Subslice { from: _, to: _, from_end: _ }
1248 | ProjectionElem::ConstantIndex { offset: _, min_length: _, from_end: _ }
1249 | ProjectionElem::Downcast(_, _) => {}
1250 }
1251 }
1252 };
1253}
1254
1255make_mir_visitor!(Visitor,);
1256make_mir_visitor!(MutVisitor, mut);
1257
1258pub trait MirVisitable<'tcx> {
1259 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>);
1260}
1261
1262impl<'tcx> MirVisitable<'tcx> for Statement<'tcx> {
1263 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1264 visitor.visit_statement(self, location)
1265 }
1266}
1267
1268impl<'tcx> MirVisitable<'tcx> for Terminator<'tcx> {
1269 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1270 visitor.visit_terminator(self, location)
1271 }
1272}
1273
1274impl<'tcx> MirVisitable<'tcx> for Option<Terminator<'tcx>> {
1275 fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1276 visitor.visit_terminator(self.as_ref().unwrap(), location)
1277 }
1278}
1279
1280#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
1283pub enum TyContext {
1284 LocalDecl {
1285 local: Local,
1287
1288 source_info: SourceInfo,
1290 },
1291
1292 UserTy(Span),
1294
1295 ReturnTy(SourceInfo),
1297
1298 YieldTy(SourceInfo),
1299
1300 ResumeTy(SourceInfo),
1301
1302 Location(Location),
1304}
1305
1306#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1307pub enum NonMutatingUseContext {
1308 Inspect,
1310 Copy,
1312 Move,
1314 SharedBorrow,
1316 FakeBorrow,
1320 RawBorrow,
1322 PlaceMention,
1327 Projection,
1334}
1335
1336#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1337pub enum MutatingUseContext {
1338 Store,
1340 SetDiscriminant,
1342 Deinit,
1344 AsmOutput,
1346 Call,
1348 Yield,
1350 Drop,
1352 Borrow,
1354 RawBorrow,
1356 Projection,
1363 Retag,
1365}
1366
1367#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1368pub enum NonUseContext {
1369 StorageLive,
1371 StorageDead,
1373 AscribeUserTy(ty::Variance),
1375 VarDebugInfo,
1377}
1378
1379#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1380pub enum PlaceContext {
1381 NonMutatingUse(NonMutatingUseContext),
1382 MutatingUse(MutatingUseContext),
1383 NonUse(NonUseContext),
1384}
1385
1386impl PlaceContext {
1387 #[inline]
1389 pub fn is_drop(self) -> bool {
1390 matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
1391 }
1392
1393 pub fn is_borrow(self) -> bool {
1395 matches!(
1396 self,
1397 PlaceContext::NonMutatingUse(
1398 NonMutatingUseContext::SharedBorrow | NonMutatingUseContext::FakeBorrow
1399 ) | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
1400 )
1401 }
1402
1403 pub fn is_address_of(self) -> bool {
1405 matches!(
1406 self,
1407 PlaceContext::NonMutatingUse(NonMutatingUseContext::RawBorrow)
1408 | PlaceContext::MutatingUse(MutatingUseContext::RawBorrow)
1409 )
1410 }
1411
1412 #[inline]
1414 pub fn is_storage_marker(self) -> bool {
1415 matches!(
1416 self,
1417 PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead)
1418 )
1419 }
1420
1421 #[inline]
1423 pub fn is_mutating_use(self) -> bool {
1424 matches!(self, PlaceContext::MutatingUse(..))
1425 }
1426
1427 #[inline]
1429 pub fn is_use(self) -> bool {
1430 !matches!(self, PlaceContext::NonUse(..))
1431 }
1432
1433 pub fn is_place_assignment(self) -> bool {
1435 matches!(
1436 self,
1437 PlaceContext::MutatingUse(
1438 MutatingUseContext::Store
1439 | MutatingUseContext::Call
1440 | MutatingUseContext::AsmOutput,
1441 )
1442 )
1443 }
1444
1445 pub fn ambient_variance(self) -> ty::Variance {
1447 use NonMutatingUseContext::*;
1448 use NonUseContext::*;
1449 match self {
1450 PlaceContext::MutatingUse(_) => ty::Invariant,
1451 PlaceContext::NonUse(StorageDead | StorageLive | VarDebugInfo) => ty::Invariant,
1452 PlaceContext::NonMutatingUse(
1453 Inspect | Copy | Move | PlaceMention | SharedBorrow | FakeBorrow | RawBorrow
1454 | Projection,
1455 ) => ty::Covariant,
1456 PlaceContext::NonUse(AscribeUserTy(variance)) => variance,
1457 }
1458 }
1459}