]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/mir/visit.rs
Rollup merge of #94115 - scottmcm:iter-process-by-ref, r=yaahc
[rust.git] / compiler / rustc_middle / src / mir / visit.rs
1 //! # The MIR Visitor
2 //!
3 //! ## Overview
4 //!
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:
8 //!
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`
12 //!
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.
18 //!
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`.
22 //!
23 //! ## Updating
24 //!
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.
29 //!
30 //! For example, the `super_basic_block_data` method begins like this:
31 //!
32 //! ```rust
33 //! fn super_basic_block_data(&mut self,
34 //!                           block: BasicBlock,
35 //!                           data: & $($mutability)? BasicBlockData<'tcx>) {
36 //!     let BasicBlockData {
37 //!         statements,
38 //!         terminator,
39 //!         is_cleanup: _
40 //!     } = *data;
41 //!
42 //!     for statement in statements {
43 //!         self.visit_statement(block, statement);
44 //!     }
45 //!
46 //!     ...
47 //! }
48 //! ```
49 //!
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).
54 //!
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`.
58 //!
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.
62
63 use crate::mir::*;
64 use crate::ty::subst::SubstsRef;
65 use crate::ty::{CanonicalUserTypeAnnotation, Ty};
66 use rustc_span::Span;
67
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
72             // default behavior.
73
74             fn visit_body(
75                 &mut self,
76                 body: &$($mutability)? Body<'tcx>,
77             ) {
78                 self.super_body(body);
79             }
80
81             fn visit_basic_block_data(&mut self,
82                                       block: BasicBlock,
83                                       data: & $($mutability)? BasicBlockData<'tcx>) {
84                 self.super_basic_block_data(block, data);
85             }
86
87             fn visit_source_scope_data(&mut self,
88                                            scope_data: & $($mutability)? SourceScopeData<'tcx>) {
89                 self.super_source_scope_data(scope_data);
90             }
91
92             fn visit_statement(&mut self,
93                                statement: & $($mutability)? Statement<'tcx>,
94                                location: Location) {
95                 self.super_statement(statement, location);
96             }
97
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);
103             }
104
105             fn visit_terminator(&mut self,
106                                 terminator: & $($mutability)? Terminator<'tcx>,
107                                 location: Location) {
108                 self.super_terminator(terminator, location);
109             }
110
111             fn visit_assert_message(&mut self,
112                                     msg: & $($mutability)? AssertMessage<'tcx>,
113                                     location: Location) {
114                 self.super_assert_message(msg, location);
115             }
116
117             fn visit_rvalue(&mut self,
118                             rvalue: & $($mutability)? Rvalue<'tcx>,
119                             location: Location) {
120                 self.super_rvalue(rvalue, location);
121             }
122
123             fn visit_operand(&mut self,
124                              operand: & $($mutability)? Operand<'tcx>,
125                              location: Location) {
126                 self.super_operand(operand, location);
127             }
128
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);
135             }
136
137             fn visit_coverage(&mut self,
138                               coverage: & $($mutability)? Coverage,
139                               location: Location) {
140                 self.super_coverage(coverage, location);
141             }
142
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);
148             }
149
150             fn visit_place(&mut self,
151                             place: & $($mutability)? Place<'tcx>,
152                             context: PlaceContext,
153                             location: Location) {
154                 self.super_place(place, context, location);
155             }
156
157             visit_place_fns!($($mutability)?);
158
159             fn visit_constant(&mut self,
160                               constant: & $($mutability)? Constant<'tcx>,
161                               location: Location) {
162                 self.super_constant(constant, location);
163             }
164
165             // The macro results in a false positive of sorts, where &mut Span
166             // is fine, but &Span is not; just allow the lint.
167             #[allow(rustc::pass_by_value)]
168             fn visit_span(&mut self,
169                           span: & $($mutability)? Span) {
170                 self.super_span(span);
171             }
172
173             fn visit_source_info(&mut self,
174                                  source_info: & $($mutability)? SourceInfo) {
175                 self.super_source_info(source_info);
176             }
177
178             fn visit_ty(&mut self,
179                         ty: $(& $mutability)? Ty<'tcx>,
180                         _: TyContext) {
181                 self.super_ty(ty);
182             }
183
184             fn visit_user_type_projection(
185                 &mut self,
186                 ty: & $($mutability)? UserTypeProjection,
187             ) {
188                 self.super_user_type_projection(ty);
189             }
190
191             fn visit_user_type_annotation(
192                 &mut self,
193                 index: UserTypeAnnotationIndex,
194                 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
195             ) {
196                 self.super_user_type_annotation(index, ty);
197             }
198
199             fn visit_region(&mut self,
200                             region: $(& $mutability)? ty::Region<'tcx>,
201                             _: Location) {
202                 self.super_region(region);
203             }
204
205             fn visit_const(&mut self,
206                            constant: $(& $mutability)? ty::Const<'tcx>,
207                            _: Location) {
208                 self.super_const(constant);
209             }
210
211             fn visit_substs(&mut self,
212                             substs: & $($mutability)? SubstsRef<'tcx>,
213                             _: Location) {
214                 self.super_substs(substs);
215             }
216
217             fn visit_local_decl(&mut self,
218                                 local: Local,
219                                 local_decl: & $($mutability)? LocalDecl<'tcx>) {
220                 self.super_local_decl(local, local_decl);
221             }
222
223             fn visit_var_debug_info(&mut self,
224                                     var_debug_info: & $($mutability)* VarDebugInfo<'tcx>) {
225                 self.super_var_debug_info(var_debug_info);
226             }
227
228             #[allow(rustc::pass_by_value)]
229             fn visit_local(&mut self,
230                             _local: & $($mutability)? Local,
231                             _context: PlaceContext,
232                             _location: Location) {
233             }
234
235             #[allow(rustc::pass_by_value)]
236             fn visit_source_scope(&mut self,
237                                       scope: & $($mutability)? SourceScope) {
238                 self.super_source_scope(scope);
239             }
240
241             // The `super_xxx` methods comprise the default behavior and are
242             // not meant to be overridden.
243
244             fn super_body(
245                 &mut self,
246                 body: &$($mutability)? Body<'tcx>,
247             ) {
248                 let span = body.span;
249                 if let Some(gen) = &$($mutability)? body.generator {
250                     if let Some(yield_ty) = $(& $mutability)? gen.yield_ty {
251                         self.visit_ty(
252                             yield_ty,
253                             TyContext::YieldTy(SourceInfo::outermost(span))
254                         );
255                     }
256                 }
257
258                 // for best performance, we want to use an iterator rather
259                 // than a for-loop, to avoid calling `body::Body::invalidate` for
260                 // each basic block.
261                 macro_rules! basic_blocks {
262                     (mut) => (body.basic_blocks_mut().iter_enumerated_mut());
263                     () => (body.basic_blocks().iter_enumerated());
264                 }
265                 for (bb, data) in basic_blocks!($($mutability)?) {
266                     self.visit_basic_block_data(bb, data);
267                 }
268
269                 for scope in &$($mutability)? body.source_scopes {
270                     self.visit_source_scope_data(scope);
271                 }
272
273                 self.visit_ty(
274                     $(& $mutability)? body.return_ty(),
275                     TyContext::ReturnTy(SourceInfo::outermost(body.span))
276                 );
277
278                 for local in body.local_decls.indices() {
279                     self.visit_local_decl(local, & $($mutability)? body.local_decls[local]);
280                 }
281
282                 macro_rules! type_annotations {
283                     (mut) => (body.user_type_annotations.iter_enumerated_mut());
284                     () => (body.user_type_annotations.iter_enumerated());
285                 }
286
287                 for (index, annotation) in type_annotations!($($mutability)?) {
288                     self.visit_user_type_annotation(
289                         index, annotation
290                     );
291                 }
292
293                 for var_debug_info in &$($mutability)? body.var_debug_info {
294                     self.visit_var_debug_info(var_debug_info);
295                 }
296
297                 self.visit_span(&$($mutability)? body.span);
298
299                 for const_ in &$($mutability)? body.required_consts {
300                     let location = START_BLOCK.start_location();
301                     self.visit_constant(const_, location);
302                 }
303             }
304
305             fn super_basic_block_data(&mut self,
306                                       block: BasicBlock,
307                                       data: & $($mutability)? BasicBlockData<'tcx>) {
308                 let BasicBlockData {
309                     statements,
310                     terminator,
311                     is_cleanup: _
312                 } = data;
313
314                 let mut index = 0;
315                 for statement in statements {
316                     let location = Location { block, statement_index: index };
317                     self.visit_statement(statement, location);
318                     index += 1;
319                 }
320
321                 if let Some(terminator) = terminator {
322                     let location = Location { block, statement_index: index };
323                     self.visit_terminator(terminator, location);
324                 }
325             }
326
327             fn super_source_scope_data(
328                 &mut self,
329                 scope_data: & $($mutability)? SourceScopeData<'tcx>,
330             ) {
331                 let SourceScopeData {
332                     span,
333                     parent_scope,
334                     inlined,
335                     inlined_parent_scope,
336                     local_data: _,
337                 } = scope_data;
338
339                 self.visit_span(span);
340                 if let Some(parent_scope) = parent_scope {
341                     self.visit_source_scope(parent_scope);
342                 }
343                 if let Some((callee, callsite_span)) = inlined {
344                     let location = START_BLOCK.start_location();
345
346                     self.visit_span(callsite_span);
347
348                     let ty::Instance { def: callee_def, substs: callee_substs } = callee;
349                     match callee_def {
350                         ty::InstanceDef::Item(_def_id) => {}
351
352                         ty::InstanceDef::Intrinsic(_def_id) |
353                         ty::InstanceDef::VtableShim(_def_id) |
354                         ty::InstanceDef::ReifyShim(_def_id) |
355                         ty::InstanceDef::Virtual(_def_id, _) |
356                         ty::InstanceDef::ClosureOnceShim { call_once: _def_id, track_caller: _ } |
357                         ty::InstanceDef::DropGlue(_def_id, None) => {}
358
359                         ty::InstanceDef::FnPtrShim(_def_id, ty) |
360                         ty::InstanceDef::DropGlue(_def_id, Some(ty)) |
361                         ty::InstanceDef::CloneShim(_def_id, ty) => {
362                             // FIXME(eddyb) use a better `TyContext` here.
363                             self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
364                         }
365                     }
366                     self.visit_substs(callee_substs, location);
367                 }
368                 if let Some(inlined_parent_scope) = inlined_parent_scope {
369                     self.visit_source_scope(inlined_parent_scope);
370                 }
371             }
372
373             fn super_statement(&mut self,
374                                statement: & $($mutability)? Statement<'tcx>,
375                                location: Location) {
376                 let Statement {
377                     source_info,
378                     kind,
379                 } = statement;
380
381                 self.visit_source_info(source_info);
382                 match kind {
383                     StatementKind::Assign(
384                         box(ref $($mutability)? place, ref $($mutability)? rvalue)
385                     ) => {
386                         self.visit_assign(place, rvalue, location);
387                     }
388                     StatementKind::FakeRead(box (_, place)) => {
389                         self.visit_place(
390                             place,
391                             PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
392                             location
393                         );
394                     }
395                     StatementKind::SetDiscriminant { place, .. } => {
396                         self.visit_place(
397                             place,
398                             PlaceContext::MutatingUse(MutatingUseContext::Store),
399                             location
400                         );
401                     }
402                     StatementKind::StorageLive(local) => {
403                         self.visit_local(
404                             local,
405                             PlaceContext::NonUse(NonUseContext::StorageLive),
406                             location
407                         );
408                     }
409                     StatementKind::StorageDead(local) => {
410                         self.visit_local(
411                             local,
412                             PlaceContext::NonUse(NonUseContext::StorageDead),
413                             location
414                         );
415                     }
416                     StatementKind::Retag(kind, place) => {
417                         self.visit_retag(kind, place, location);
418                     }
419                     StatementKind::AscribeUserType(
420                         box(ref $($mutability)? place, ref $($mutability)? user_ty),
421                         variance
422                     ) => {
423                         self.visit_ascribe_user_ty(place, variance, user_ty, location);
424                     }
425                     StatementKind::Coverage(coverage) => {
426                         self.visit_coverage(
427                             coverage,
428                             location
429                         )
430                     }
431                     StatementKind::CopyNonOverlapping(box crate::mir::CopyNonOverlapping{
432                       ref $($mutability)? src,
433                       ref $($mutability)? dst,
434                       ref $($mutability)? count,
435                     }) => {
436                       self.visit_operand(src, location);
437                       self.visit_operand(dst, location);
438                       self.visit_operand(count, location)
439                     }
440                     StatementKind::Nop => {}
441                 }
442             }
443
444             fn super_assign(&mut self,
445                             place: &$($mutability)? Place<'tcx>,
446                             rvalue: &$($mutability)? Rvalue<'tcx>,
447                             location: Location) {
448                 self.visit_place(
449                     place,
450                     PlaceContext::MutatingUse(MutatingUseContext::Store),
451                     location
452                 );
453                 self.visit_rvalue(rvalue, location);
454             }
455
456             fn super_terminator(&mut self,
457                                 terminator: &$($mutability)? Terminator<'tcx>,
458                                 location: Location) {
459                 let Terminator { source_info, kind } = terminator;
460
461                 self.visit_source_info(source_info);
462                 match kind {
463                     TerminatorKind::Goto { .. } |
464                     TerminatorKind::Resume |
465                     TerminatorKind::Abort |
466                     TerminatorKind::GeneratorDrop |
467                     TerminatorKind::Unreachable |
468                     TerminatorKind::FalseEdge { .. } |
469                     TerminatorKind::FalseUnwind { .. } => {
470                     }
471
472                     TerminatorKind::Return => {
473                         // `return` logically moves from the return place `_0`. Note that the place
474                         // cannot be changed by any visitor, though.
475                         let $($mutability)? local = RETURN_PLACE;
476                         self.visit_local(
477                             & $($mutability)? local,
478                             PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
479                             location,
480                         );
481
482                         assert_eq!(
483                             local,
484                             RETURN_PLACE,
485                             "`MutVisitor` tried to mutate return place of `return` terminator"
486                         );
487                     }
488
489                     TerminatorKind::SwitchInt {
490                         discr,
491                         switch_ty,
492                         targets: _
493                     } => {
494                         self.visit_operand(discr, location);
495                         self.visit_ty($(& $mutability)? *switch_ty, TyContext::Location(location));
496                     }
497
498                     TerminatorKind::Drop {
499                         place,
500                         target: _,
501                         unwind: _,
502                     } => {
503                         self.visit_place(
504                             place,
505                             PlaceContext::MutatingUse(MutatingUseContext::Drop),
506                             location
507                         );
508                     }
509
510                     TerminatorKind::DropAndReplace {
511                         place,
512                         value,
513                         target: _,
514                         unwind: _,
515                     } => {
516                         self.visit_place(
517                             place,
518                             PlaceContext::MutatingUse(MutatingUseContext::Drop),
519                             location
520                         );
521                         self.visit_operand(value, location);
522                     }
523
524                     TerminatorKind::Call {
525                         func,
526                         args,
527                         destination,
528                         cleanup: _,
529                         from_hir_call: _,
530                         fn_span: _
531                     } => {
532                         self.visit_operand(func, location);
533                         for arg in args {
534                             self.visit_operand(arg, location);
535                         }
536                         if let Some((destination, _)) = destination {
537                             self.visit_place(
538                                 destination,
539                                 PlaceContext::MutatingUse(MutatingUseContext::Call),
540                                 location
541                             );
542                         }
543                     }
544
545                     TerminatorKind::Assert {
546                         cond,
547                         expected: _,
548                         msg,
549                         target: _,
550                         cleanup: _,
551                     } => {
552                         self.visit_operand(cond, location);
553                         self.visit_assert_message(msg, location);
554                     }
555
556                     TerminatorKind::Yield {
557                         value,
558                         resume: _,
559                         resume_arg,
560                         drop: _,
561                     } => {
562                         self.visit_operand(value, location);
563                         self.visit_place(
564                             resume_arg,
565                             PlaceContext::MutatingUse(MutatingUseContext::Yield),
566                             location,
567                         );
568                     }
569
570                     TerminatorKind::InlineAsm {
571                         template: _,
572                         operands,
573                         options: _,
574                         line_spans: _,
575                         destination: _,
576                         cleanup: _,
577                     } => {
578                         for op in operands {
579                             match op {
580                                 InlineAsmOperand::In { value, .. } => {
581                                     self.visit_operand(value, location);
582                                 }
583                                 InlineAsmOperand::Out { place: Some(place), .. } => {
584                                     self.visit_place(
585                                         place,
586                                         PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
587                                         location,
588                                     );
589                                 }
590                                 InlineAsmOperand::InOut { in_value, out_place, .. } => {
591                                     self.visit_operand(in_value, location);
592                                     if let Some(out_place) = out_place {
593                                         self.visit_place(
594                                             out_place,
595                                             PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
596                                             location,
597                                         );
598                                     }
599                                 }
600                                 InlineAsmOperand::Const { value }
601                                 | InlineAsmOperand::SymFn { value } => {
602                                     self.visit_constant(value, location);
603                                 }
604                                 InlineAsmOperand::Out { place: None, .. }
605                                 | InlineAsmOperand::SymStatic { def_id: _ } => {}
606                             }
607                         }
608                     }
609                 }
610             }
611
612             fn super_assert_message(&mut self,
613                                     msg: & $($mutability)? AssertMessage<'tcx>,
614                                     location: Location) {
615                 use crate::mir::AssertKind::*;
616                 match msg {
617                     BoundsCheck { len, index } => {
618                         self.visit_operand(len, location);
619                         self.visit_operand(index, location);
620                     }
621                     Overflow(_, l, r) => {
622                         self.visit_operand(l, location);
623                         self.visit_operand(r, location);
624                     }
625                     OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
626                         self.visit_operand(op, location);
627                     }
628                     ResumedAfterReturn(_) | ResumedAfterPanic(_) => {
629                         // Nothing to visit
630                     }
631                 }
632             }
633
634             fn super_rvalue(&mut self,
635                             rvalue: & $($mutability)? Rvalue<'tcx>,
636                             location: Location) {
637                 match rvalue {
638                     Rvalue::Use(operand) => {
639                         self.visit_operand(operand, location);
640                     }
641
642                     Rvalue::Repeat(value, _) => {
643                         self.visit_operand(value, location);
644                     }
645
646                     Rvalue::ThreadLocalRef(_) => {}
647
648                     Rvalue::Ref(r, bk, path) => {
649                         self.visit_region($(& $mutability)? *r, location);
650                         let ctx = match bk {
651                             BorrowKind::Shared => PlaceContext::NonMutatingUse(
652                                 NonMutatingUseContext::SharedBorrow
653                             ),
654                             BorrowKind::Shallow => PlaceContext::NonMutatingUse(
655                                 NonMutatingUseContext::ShallowBorrow
656                             ),
657                             BorrowKind::Unique => PlaceContext::NonMutatingUse(
658                                 NonMutatingUseContext::UniqueBorrow
659                             ),
660                             BorrowKind::Mut { .. } =>
661                                 PlaceContext::MutatingUse(MutatingUseContext::Borrow),
662                         };
663                         self.visit_place(path, ctx, location);
664                     }
665
666                     Rvalue::AddressOf(m, path) => {
667                         let ctx = match m {
668                             Mutability::Mut => PlaceContext::MutatingUse(
669                                 MutatingUseContext::AddressOf
670                             ),
671                             Mutability::Not => PlaceContext::NonMutatingUse(
672                                 NonMutatingUseContext::AddressOf
673                             ),
674                         };
675                         self.visit_place(path, ctx, location);
676                     }
677
678                     Rvalue::Len(path) => {
679                         self.visit_place(
680                             path,
681                             PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
682                             location
683                         );
684                     }
685
686                     Rvalue::Cast(_cast_kind, operand, ty) => {
687                         self.visit_operand(operand, location);
688                         self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
689                     }
690
691                     Rvalue::BinaryOp(_bin_op, box(lhs, rhs))
692                     | Rvalue::CheckedBinaryOp(_bin_op, box(lhs, rhs)) => {
693                         self.visit_operand(lhs, location);
694                         self.visit_operand(rhs, location);
695                     }
696
697                     Rvalue::UnaryOp(_un_op, op) => {
698                         self.visit_operand(op, location);
699                     }
700
701                     Rvalue::Discriminant(place) => {
702                         self.visit_place(
703                             place,
704                             PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
705                             location
706                         );
707                     }
708
709                     Rvalue::NullaryOp(_op, ty) => {
710                         self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
711                     }
712
713                     Rvalue::Aggregate(kind, operands) => {
714                         let kind = &$($mutability)? **kind;
715                         match kind {
716                             AggregateKind::Array(ty) => {
717                                 self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
718                             }
719                             AggregateKind::Tuple => {
720                             }
721                             AggregateKind::Adt(
722                                 _adt_def,
723                                 _variant_index,
724                                 substs,
725                                 _user_substs,
726                                 _active_field_index
727                             ) => {
728                                 self.visit_substs(substs, location);
729                             }
730                             AggregateKind::Closure(
731                                 _,
732                                 closure_substs
733                             ) => {
734                                 self.visit_substs(closure_substs, location);
735                             }
736                             AggregateKind::Generator(
737                                 _,
738                                 generator_substs,
739                                 _movability,
740                             ) => {
741                                 self.visit_substs(generator_substs, location);
742                             }
743                         }
744
745                         for operand in operands {
746                             self.visit_operand(operand, location);
747                         }
748                     }
749
750                     Rvalue::ShallowInitBox(operand, ty) => {
751                         self.visit_operand(operand, location);
752                         self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
753                     }
754                 }
755             }
756
757             fn super_operand(&mut self,
758                              operand: & $($mutability)? Operand<'tcx>,
759                              location: Location) {
760                 match operand {
761                     Operand::Copy(place) => {
762                         self.visit_place(
763                             place,
764                             PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
765                             location
766                         );
767                     }
768                     Operand::Move(place) => {
769                         self.visit_place(
770                             place,
771                             PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
772                             location
773                         );
774                     }
775                     Operand::Constant(constant) => {
776                         self.visit_constant(constant, location);
777                     }
778                 }
779             }
780
781             fn super_ascribe_user_ty(&mut self,
782                                      place: & $($mutability)? Place<'tcx>,
783                                      _variance: & $($mutability)? ty::Variance,
784                                      user_ty: & $($mutability)? UserTypeProjection,
785                                      location: Location) {
786                 self.visit_place(
787                     place,
788                     PlaceContext::NonUse(NonUseContext::AscribeUserTy),
789                     location
790                 );
791                 self.visit_user_type_projection(user_ty);
792             }
793
794             fn super_coverage(&mut self,
795                               _coverage: & $($mutability)? Coverage,
796                               _location: Location) {
797             }
798
799             fn super_retag(&mut self,
800                            _kind: & $($mutability)? RetagKind,
801                            place: & $($mutability)? Place<'tcx>,
802                            location: Location) {
803                 self.visit_place(
804                     place,
805                     PlaceContext::MutatingUse(MutatingUseContext::Retag),
806                     location,
807                 );
808             }
809
810             fn super_local_decl(&mut self,
811                                 local: Local,
812                                 local_decl: & $($mutability)? LocalDecl<'tcx>) {
813                 let LocalDecl {
814                     mutability: _,
815                     ty,
816                     user_ty,
817                     source_info,
818                     internal: _,
819                     local_info: _,
820                     is_block_tail: _,
821                 } = local_decl;
822
823                 self.visit_ty($(& $mutability)? *ty, TyContext::LocalDecl {
824                     local,
825                     source_info: *source_info,
826                 });
827                 if let Some(user_ty) = user_ty {
828                     for (user_ty, _) in & $($mutability)? user_ty.contents {
829                         self.visit_user_type_projection(user_ty);
830                     }
831                 }
832                 self.visit_source_info(source_info);
833             }
834
835             fn super_var_debug_info(&mut self,
836                                     var_debug_info: & $($mutability)? VarDebugInfo<'tcx>) {
837                 let VarDebugInfo {
838                     name: _,
839                     source_info,
840                     value,
841                 } = var_debug_info;
842
843                 self.visit_source_info(source_info);
844                 let location = START_BLOCK.start_location();
845                 match value {
846                     VarDebugInfoContents::Const(c) => self.visit_constant(c, location),
847                     VarDebugInfoContents::Place(place) =>
848                         self.visit_place(
849                             place,
850                             PlaceContext::NonUse(NonUseContext::VarDebugInfo),
851                             location
852                         ),
853                 }
854             }
855
856             #[allow(rustc::pass_by_value)]
857             fn super_source_scope(&mut self,
858                                       _scope: & $($mutability)? SourceScope) {
859             }
860
861             fn super_constant(&mut self,
862                               constant: & $($mutability)? Constant<'tcx>,
863                               location: Location) {
864                 let Constant {
865                     span,
866                     user_ty,
867                     literal,
868                 } = constant;
869
870                 self.visit_span(span);
871                 drop(user_ty); // no visit method for this
872                 match literal {
873                     ConstantKind::Ty(ct) => self.visit_const($(& $mutability)? *ct, location),
874                     ConstantKind::Val(_, ty) => self.visit_ty($(& $mutability)? *ty, TyContext::Location(location)),
875                 }
876             }
877
878             // The macro results in a false positive of sorts, where &mut Span
879             // is fine, but &Span is not; just allow the lint.
880             #[allow(rustc::pass_by_value)]
881             fn super_span(&mut self, _span: & $($mutability)? Span) {
882             }
883
884             fn super_source_info(&mut self, source_info: & $($mutability)? SourceInfo) {
885                 let SourceInfo {
886                     span,
887                     scope,
888                 } = source_info;
889
890                 self.visit_span(span);
891                 self.visit_source_scope(scope);
892             }
893
894             fn super_user_type_projection(
895                 &mut self,
896                 _ty: & $($mutability)? UserTypeProjection,
897             ) {
898             }
899
900             fn super_user_type_annotation(
901                 &mut self,
902                 _index: UserTypeAnnotationIndex,
903                 ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
904             ) {
905                 self.visit_span(& $($mutability)? ty.span);
906                 self.visit_ty($(& $mutability)? ty.inferred_ty, TyContext::UserTy(ty.span));
907             }
908
909             fn super_ty(&mut self, _ty: $(& $mutability)? Ty<'tcx>) {
910             }
911
912             fn super_region(&mut self, _region: $(& $mutability)? ty::Region<'tcx>) {
913             }
914
915             fn super_const(&mut self, _const: $(& $mutability)? ty::Const<'tcx>) {
916             }
917
918             fn super_substs(&mut self, _substs: & $($mutability)? SubstsRef<'tcx>) {
919             }
920
921             // Convenience methods
922
923             fn visit_location(
924                 &mut self,
925                 body: &$($mutability)? Body<'tcx>,
926                 location: Location
927             ) {
928                 macro_rules! basic_blocks {
929                     (mut) => (body.basic_blocks_mut());
930                     () => (body.basic_blocks());
931                 }
932                 let basic_block = & $($mutability)? basic_blocks!($($mutability)?)[location.block];
933                 if basic_block.statements.len() == location.statement_index {
934                     if let Some(ref $($mutability)? terminator) = basic_block.terminator {
935                         self.visit_terminator(terminator, location)
936                     }
937                 } else {
938                     let statement = & $($mutability)?
939                         basic_block.statements[location.statement_index];
940                     self.visit_statement(statement, location)
941                 }
942             }
943         }
944     }
945 }
946
947 macro_rules! visit_place_fns {
948     (mut) => {
949         fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
950
951         fn super_place(
952             &mut self,
953             place: &mut Place<'tcx>,
954             context: PlaceContext,
955             location: Location,
956         ) {
957             self.visit_local(&mut place.local, context, location);
958
959             if let Some(new_projection) = self.process_projection(&place.projection, location) {
960                 place.projection = self.tcx().intern_place_elems(&new_projection);
961             }
962         }
963
964         fn process_projection<'a>(
965             &mut self,
966             projection: &'a [PlaceElem<'tcx>],
967             location: Location,
968         ) -> Option<Vec<PlaceElem<'tcx>>> {
969             let mut projection = Cow::Borrowed(projection);
970
971             for i in 0..projection.len() {
972                 if let Some(&elem) = projection.get(i) {
973                     if let Some(elem) = self.process_projection_elem(elem, location) {
974                         // This converts the borrowed projection into `Cow::Owned(_)` and returns a
975                         // clone of the projection so we can mutate and reintern later.
976                         let vec = projection.to_mut();
977                         vec[i] = elem;
978                     }
979                 }
980             }
981
982             match projection {
983                 Cow::Borrowed(_) => None,
984                 Cow::Owned(vec) => Some(vec),
985             }
986         }
987
988         fn process_projection_elem(
989             &mut self,
990             elem: PlaceElem<'tcx>,
991             location: Location,
992         ) -> Option<PlaceElem<'tcx>> {
993             match elem {
994                 PlaceElem::Index(local) => {
995                     let mut new_local = local;
996                     self.visit_local(
997                         &mut new_local,
998                         PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
999                         location,
1000                     );
1001
1002                     if new_local == local { None } else { Some(PlaceElem::Index(new_local)) }
1003                 }
1004                 PlaceElem::Field(field, ty) => {
1005                     let mut new_ty = ty;
1006                     self.visit_ty(&mut new_ty, TyContext::Location(location));
1007                     if ty != new_ty { Some(PlaceElem::Field(field, new_ty)) } else { None }
1008                 }
1009                 PlaceElem::Deref
1010                 | PlaceElem::ConstantIndex { .. }
1011                 | PlaceElem::Subslice { .. }
1012                 | PlaceElem::Downcast(..) => None,
1013             }
1014         }
1015     };
1016
1017     () => {
1018         fn visit_projection(
1019             &mut self,
1020             place_ref: PlaceRef<'tcx>,
1021             context: PlaceContext,
1022             location: Location,
1023         ) {
1024             self.super_projection(place_ref, context, location);
1025         }
1026
1027         fn visit_projection_elem(
1028             &mut self,
1029             local: Local,
1030             proj_base: &[PlaceElem<'tcx>],
1031             elem: PlaceElem<'tcx>,
1032             context: PlaceContext,
1033             location: Location,
1034         ) {
1035             self.super_projection_elem(local, proj_base, elem, context, location);
1036         }
1037
1038         fn super_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
1039             let mut context = context;
1040
1041             if !place.projection.is_empty() {
1042                 if context.is_use() {
1043                     // ^ Only change the context if it is a real use, not a "use" in debuginfo.
1044                     context = if context.is_mutating_use() {
1045                         PlaceContext::MutatingUse(MutatingUseContext::Projection)
1046                     } else {
1047                         PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
1048                     };
1049                 }
1050             }
1051
1052             self.visit_local(&place.local, context, location);
1053
1054             self.visit_projection(place.as_ref(), context, location);
1055         }
1056
1057         fn super_projection(
1058             &mut self,
1059             place_ref: PlaceRef<'tcx>,
1060             context: PlaceContext,
1061             location: Location,
1062         ) {
1063             // FIXME: Use PlaceRef::iter_projections, once that exists.
1064             let mut cursor = place_ref.projection;
1065             while let &[ref proj_base @ .., elem] = cursor {
1066                 cursor = proj_base;
1067                 self.visit_projection_elem(place_ref.local, cursor, elem, context, location);
1068             }
1069         }
1070
1071         fn super_projection_elem(
1072             &mut self,
1073             _local: Local,
1074             _proj_base: &[PlaceElem<'tcx>],
1075             elem: PlaceElem<'tcx>,
1076             _context: PlaceContext,
1077             location: Location,
1078         ) {
1079             match elem {
1080                 ProjectionElem::Field(_field, ty) => {
1081                     self.visit_ty(ty, TyContext::Location(location));
1082                 }
1083                 ProjectionElem::Index(local) => {
1084                     self.visit_local(
1085                         &local,
1086                         PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
1087                         location,
1088                     );
1089                 }
1090                 ProjectionElem::Deref
1091                 | ProjectionElem::Subslice { from: _, to: _, from_end: _ }
1092                 | ProjectionElem::ConstantIndex { offset: _, min_length: _, from_end: _ }
1093                 | ProjectionElem::Downcast(_, _) => {}
1094             }
1095         }
1096     };
1097 }
1098
1099 make_mir_visitor!(Visitor,);
1100 make_mir_visitor!(MutVisitor, mut);
1101
1102 pub trait MirVisitable<'tcx> {
1103     fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>);
1104 }
1105
1106 impl<'tcx> MirVisitable<'tcx> for Statement<'tcx> {
1107     fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1108         visitor.visit_statement(self, location)
1109     }
1110 }
1111
1112 impl<'tcx> MirVisitable<'tcx> for Terminator<'tcx> {
1113     fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1114         visitor.visit_terminator(self, location)
1115     }
1116 }
1117
1118 impl<'tcx> MirVisitable<'tcx> for Option<Terminator<'tcx>> {
1119     fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1120         visitor.visit_terminator(self.as_ref().unwrap(), location)
1121     }
1122 }
1123
1124 /// Extra information passed to `visit_ty` and friends to give context
1125 /// about where the type etc appears.
1126 #[derive(Debug)]
1127 pub enum TyContext {
1128     LocalDecl {
1129         /// The index of the local variable we are visiting.
1130         local: Local,
1131
1132         /// The source location where this local variable was declared.
1133         source_info: SourceInfo,
1134     },
1135
1136     /// The inferred type of a user type annotation.
1137     UserTy(Span),
1138
1139     /// The return type of the function.
1140     ReturnTy(SourceInfo),
1141
1142     YieldTy(SourceInfo),
1143
1144     /// A type found at some location.
1145     Location(Location),
1146 }
1147
1148 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1149 pub enum NonMutatingUseContext {
1150     /// Being inspected in some way, like loading a len.
1151     Inspect,
1152     /// Consumed as part of an operand.
1153     Copy,
1154     /// Consumed as part of an operand.
1155     Move,
1156     /// Shared borrow.
1157     SharedBorrow,
1158     /// Shallow borrow.
1159     ShallowBorrow,
1160     /// Unique borrow.
1161     UniqueBorrow,
1162     /// AddressOf for *const pointer.
1163     AddressOf,
1164     /// Used as base for another place, e.g., `x` in `x.y`. Will not mutate the place.
1165     /// For example, the projection `x.y` is not marked as a mutation in these cases:
1166     ///
1167     ///     z = x.y;
1168     ///     f(&x.y);
1169     ///
1170     Projection,
1171 }
1172
1173 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1174 pub enum MutatingUseContext {
1175     /// Appears as LHS of an assignment.
1176     Store,
1177     /// Output operand of an inline assembly block.
1178     AsmOutput,
1179     /// Destination of a call.
1180     Call,
1181     /// Destination of a yield.
1182     Yield,
1183     /// Being dropped.
1184     Drop,
1185     /// Mutable borrow.
1186     Borrow,
1187     /// AddressOf for *mut pointer.
1188     AddressOf,
1189     /// Used as base for another place, e.g., `x` in `x.y`. Could potentially mutate the place.
1190     /// For example, the projection `x.y` is marked as a mutation in these cases:
1191     ///
1192     ///     x.y = ...;
1193     ///     f(&mut x.y);
1194     ///
1195     Projection,
1196     /// Retagging, a "Stacked Borrows" shadow state operation
1197     Retag,
1198 }
1199
1200 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1201 pub enum NonUseContext {
1202     /// Starting a storage live range.
1203     StorageLive,
1204     /// Ending a storage live range.
1205     StorageDead,
1206     /// User type annotation assertions for NLL.
1207     AscribeUserTy,
1208     /// The data of a user variable, for debug info.
1209     VarDebugInfo,
1210 }
1211
1212 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1213 pub enum PlaceContext {
1214     NonMutatingUse(NonMutatingUseContext),
1215     MutatingUse(MutatingUseContext),
1216     NonUse(NonUseContext),
1217 }
1218
1219 impl PlaceContext {
1220     /// Returns `true` if this place context represents a drop.
1221     #[inline]
1222     pub fn is_drop(&self) -> bool {
1223         matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
1224     }
1225
1226     /// Returns `true` if this place context represents a borrow.
1227     pub fn is_borrow(&self) -> bool {
1228         matches!(
1229             self,
1230             PlaceContext::NonMutatingUse(
1231                 NonMutatingUseContext::SharedBorrow
1232                     | NonMutatingUseContext::ShallowBorrow
1233                     | NonMutatingUseContext::UniqueBorrow
1234             ) | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
1235         )
1236     }
1237
1238     /// Returns `true` if this place context represents a storage live or storage dead marker.
1239     #[inline]
1240     pub fn is_storage_marker(&self) -> bool {
1241         matches!(
1242             self,
1243             PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead)
1244         )
1245     }
1246
1247     /// Returns `true` if this place context represents a use that potentially changes the value.
1248     #[inline]
1249     pub fn is_mutating_use(&self) -> bool {
1250         matches!(self, PlaceContext::MutatingUse(..))
1251     }
1252
1253     /// Returns `true` if this place context represents a use.
1254     #[inline]
1255     pub fn is_use(&self) -> bool {
1256         !matches!(self, PlaceContext::NonUse(..))
1257     }
1258
1259     /// Returns `true` if this place context represents an assignment statement.
1260     pub fn is_place_assignment(&self) -> bool {
1261         matches!(
1262             self,
1263             PlaceContext::MutatingUse(
1264                 MutatingUseContext::Store
1265                     | MutatingUseContext::Call
1266                     | MutatingUseContext::AsmOutput,
1267             )
1268         )
1269     }
1270 }