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