]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/lib.rs
Rollup merge of #92610 - GuillaumeGomez:css-class-instead-of-inline-style, r=jsha
[rust.git] / compiler / rustc_borrowck / src / lib.rs
1 //! This query borrow-checks the MIR to (further) ensure it is not broken.
2
3 #![feature(bool_to_option)]
4 #![feature(box_patterns)]
5 #![feature(crate_visibility_modifier)]
6 #![feature(let_else)]
7 #![feature(min_specialization)]
8 #![feature(stmt_expr_attributes)]
9 #![feature(trusted_step)]
10 #![feature(try_blocks)]
11 #![recursion_limit = "256"]
12
13 #[macro_use]
14 extern crate rustc_middle;
15 #[macro_use]
16 extern crate tracing;
17
18 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
19 use rustc_data_structures::graph::dominators::Dominators;
20 use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorReported};
21 use rustc_hir as hir;
22 use rustc_hir::def_id::LocalDefId;
23 use rustc_hir::Node;
24 use rustc_index::bit_set::BitSet;
25 use rustc_index::vec::IndexVec;
26 use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
27 use rustc_middle::mir::{
28     traversal, Body, ClearCrossCrate, Local, Location, Mutability, Operand, Place, PlaceElem,
29     PlaceRef, VarDebugInfoContents,
30 };
31 use rustc_middle::mir::{AggregateKind, BasicBlock, BorrowCheckResult, BorrowKind};
32 use rustc_middle::mir::{Field, ProjectionElem, Promoted, Rvalue, Statement, StatementKind};
33 use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
34 use rustc_middle::ty::query::Providers;
35 use rustc_middle::ty::{self, CapturedPlace, ParamEnv, RegionVid, TyCtxt};
36 use rustc_session::lint::builtin::{MUTABLE_BORROW_RESERVATION_CONFLICT, UNUSED_MUT};
37 use rustc_span::{Span, Symbol, DUMMY_SP};
38
39 use either::Either;
40 use smallvec::SmallVec;
41 use std::cell::RefCell;
42 use std::collections::BTreeMap;
43 use std::iter;
44 use std::mem;
45 use std::rc::Rc;
46
47 use rustc_mir_dataflow::impls::{
48     EverInitializedPlaces, MaybeInitializedPlaces, MaybeUninitializedPlaces,
49 };
50 use rustc_mir_dataflow::move_paths::{InitIndex, MoveOutIndex, MovePathIndex};
51 use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult, MoveData, MoveError};
52 use rustc_mir_dataflow::Analysis;
53 use rustc_mir_dataflow::MoveDataParamEnv;
54
55 use self::diagnostics::{AccessKind, RegionName};
56 use self::location::LocationTable;
57 use self::prefixes::PrefixSet;
58 use self::MutateMode::{JustWrite, WriteAndRead};
59 use facts::AllFacts;
60
61 use self::path_utils::*;
62
63 pub mod borrow_set;
64 mod borrowck_errors;
65 mod constraint_generation;
66 mod constraints;
67 mod dataflow;
68 mod def_use;
69 mod diagnostics;
70 mod facts;
71 mod invalidation;
72 mod location;
73 mod member_constraints;
74 mod nll;
75 mod path_utils;
76 mod place_ext;
77 mod places_conflict;
78 mod prefixes;
79 mod region_infer;
80 mod renumber;
81 mod type_check;
82 mod universal_regions;
83 mod used_muts;
84
85 // A public API provided for the Rust compiler consumers.
86 pub mod consumers;
87
88 use borrow_set::{BorrowData, BorrowSet};
89 use dataflow::{BorrowIndex, BorrowckFlowState as Flows, BorrowckResults, Borrows};
90 use nll::{PoloniusOutput, ToRegionVid};
91 use place_ext::PlaceExt;
92 use places_conflict::{places_conflict, PlaceConflictBias};
93 use region_infer::RegionInferenceContext;
94
95 // FIXME(eddyb) perhaps move this somewhere more centrally.
96 #[derive(Debug)]
97 struct Upvar<'tcx> {
98     place: CapturedPlace<'tcx>,
99
100     /// If true, the capture is behind a reference.
101     by_ref: bool,
102 }
103
104 const DEREF_PROJECTION: &[PlaceElem<'_>; 1] = &[ProjectionElem::Deref];
105
106 pub fn provide(providers: &mut Providers) {
107     *providers = Providers {
108         mir_borrowck: |tcx, did| {
109             if let Some(def) = ty::WithOptConstParam::try_lookup(did, tcx) {
110                 tcx.mir_borrowck_const_arg(def)
111             } else {
112                 mir_borrowck(tcx, ty::WithOptConstParam::unknown(did))
113             }
114         },
115         mir_borrowck_const_arg: |tcx, (did, param_did)| {
116             mir_borrowck(tcx, ty::WithOptConstParam { did, const_param_did: Some(param_did) })
117         },
118         ..*providers
119     };
120 }
121
122 fn mir_borrowck<'tcx>(
123     tcx: TyCtxt<'tcx>,
124     def: ty::WithOptConstParam<LocalDefId>,
125 ) -> &'tcx BorrowCheckResult<'tcx> {
126     let (input_body, promoted) = tcx.mir_promoted(def);
127     debug!("run query mir_borrowck: {}", tcx.def_path_str(def.did.to_def_id()));
128
129     let opt_closure_req = tcx.infer_ctxt().with_opaque_type_inference(def.did).enter(|infcx| {
130         let input_body: &Body<'_> = &input_body.borrow();
131         let promoted: &IndexVec<_, _> = &promoted.borrow();
132         do_mir_borrowck(&infcx, input_body, promoted, false).0
133     });
134     debug!("mir_borrowck done");
135
136     tcx.arena.alloc(opt_closure_req)
137 }
138
139 /// Perform the actual borrow checking.
140 ///
141 /// If `return_body_with_facts` is true, then return the body with non-erased
142 /// region ids on which the borrow checking was performed together with Polonius
143 /// facts.
144 #[instrument(skip(infcx, input_body, input_promoted), level = "debug")]
145 fn do_mir_borrowck<'a, 'tcx>(
146     infcx: &InferCtxt<'a, 'tcx>,
147     input_body: &Body<'tcx>,
148     input_promoted: &IndexVec<Promoted, Body<'tcx>>,
149     return_body_with_facts: bool,
150 ) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
151     let def = input_body.source.with_opt_param().as_local().unwrap();
152
153     debug!(?def);
154
155     let tcx = infcx.tcx;
156     let param_env = tcx.param_env(def.did);
157     let id = tcx.hir().local_def_id_to_hir_id(def.did);
158
159     let mut local_names = IndexVec::from_elem(None, &input_body.local_decls);
160     for var_debug_info in &input_body.var_debug_info {
161         if let VarDebugInfoContents::Place(place) = var_debug_info.value {
162             if let Some(local) = place.as_local() {
163                 if let Some(prev_name) = local_names[local] {
164                     if var_debug_info.name != prev_name {
165                         span_bug!(
166                             var_debug_info.source_info.span,
167                             "local {:?} has many names (`{}` vs `{}`)",
168                             local,
169                             prev_name,
170                             var_debug_info.name
171                         );
172                     }
173                 }
174                 local_names[local] = Some(var_debug_info.name);
175             }
176         }
177     }
178
179     // Gather the upvars of a closure, if any.
180     let tables = tcx.typeck_opt_const_arg(def);
181     if let Some(ErrorReported) = tables.tainted_by_errors {
182         infcx.set_tainted_by_errors();
183     }
184     let upvars: Vec<_> = tables
185         .closure_min_captures_flattened(def.did.to_def_id())
186         .map(|captured_place| {
187             let capture = captured_place.info.capture_kind;
188             let by_ref = match capture {
189                 ty::UpvarCapture::ByValue(_) => false,
190                 ty::UpvarCapture::ByRef(..) => true,
191             };
192             Upvar { place: captured_place.clone(), by_ref }
193         })
194         .collect();
195
196     // Replace all regions with fresh inference variables. This
197     // requires first making our own copy of the MIR. This copy will
198     // be modified (in place) to contain non-lexical lifetimes. It
199     // will have a lifetime tied to the inference context.
200     let mut body_owned = input_body.clone();
201     let mut promoted = input_promoted.clone();
202     let free_regions =
203         nll::replace_regions_in_mir(infcx, param_env, &mut body_owned, &mut promoted);
204     let body = &body_owned; // no further changes
205
206     let location_table_owned = LocationTable::new(body);
207     let location_table = &location_table_owned;
208
209     let mut errors_buffer = Vec::new();
210     let (move_data, move_errors): (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>) =
211         match MoveData::gather_moves(&body, tcx, param_env) {
212             Ok(move_data) => (move_data, Vec::new()),
213             Err((move_data, move_errors)) => (move_data, move_errors),
214         };
215     let promoted_errors = promoted
216         .iter_enumerated()
217         .map(|(idx, body)| (idx, MoveData::gather_moves(&body, tcx, param_env)));
218
219     let mdpe = MoveDataParamEnv { move_data, param_env };
220
221     let mut flow_inits = MaybeInitializedPlaces::new(tcx, &body, &mdpe)
222         .into_engine(tcx, &body)
223         .pass_name("borrowck")
224         .iterate_to_fixpoint()
225         .into_results_cursor(&body);
226
227     let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(id).is_fn_or_closure();
228     let borrow_set =
229         Rc::new(BorrowSet::build(tcx, body, locals_are_invalidated_at_exit, &mdpe.move_data));
230
231     let use_polonius = return_body_with_facts || infcx.tcx.sess.opts.debugging_opts.polonius;
232
233     // Compute non-lexical lifetimes.
234     let nll::NllOutput {
235         regioncx,
236         opaque_type_values,
237         polonius_input,
238         polonius_output,
239         opt_closure_req,
240         nll_errors,
241     } = nll::compute_regions(
242         infcx,
243         free_regions,
244         body,
245         &promoted,
246         location_table,
247         param_env,
248         &mut flow_inits,
249         &mdpe.move_data,
250         &borrow_set,
251         &upvars,
252         use_polonius,
253     );
254
255     // Dump MIR results into a file, if that is enabled. This let us
256     // write unit-tests, as well as helping with debugging.
257     nll::dump_mir_results(infcx, &body, &regioncx, &opt_closure_req);
258
259     // We also have a `#[rustc_regions]` annotation that causes us to dump
260     // information.
261     nll::dump_annotation(
262         infcx,
263         &body,
264         &regioncx,
265         &opt_closure_req,
266         &opaque_type_values,
267         &mut errors_buffer,
268     );
269
270     // The various `flow_*` structures can be large. We drop `flow_inits` here
271     // so it doesn't overlap with the others below. This reduces peak memory
272     // usage significantly on some benchmarks.
273     drop(flow_inits);
274
275     let regioncx = Rc::new(regioncx);
276
277     let flow_borrows = Borrows::new(tcx, body, &regioncx, &borrow_set)
278         .into_engine(tcx, body)
279         .pass_name("borrowck")
280         .iterate_to_fixpoint();
281     let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &mdpe)
282         .into_engine(tcx, body)
283         .pass_name("borrowck")
284         .iterate_to_fixpoint();
285     let flow_ever_inits = EverInitializedPlaces::new(tcx, body, &mdpe)
286         .into_engine(tcx, body)
287         .pass_name("borrowck")
288         .iterate_to_fixpoint();
289
290     let movable_generator = !matches!(
291         tcx.hir().get(id),
292         Node::Expr(&hir::Expr {
293             kind: hir::ExprKind::Closure(.., Some(hir::Movability::Static)),
294             ..
295         })
296     );
297
298     for (idx, move_data_results) in promoted_errors {
299         let promoted_body = &promoted[idx];
300
301         if let Err((move_data, move_errors)) = move_data_results {
302             let mut promoted_mbcx = MirBorrowckCtxt {
303                 infcx,
304                 param_env,
305                 body: promoted_body,
306                 move_data: &move_data,
307                 location_table, // no need to create a real one for the promoted, it is not used
308                 movable_generator,
309                 fn_self_span_reported: Default::default(),
310                 locals_are_invalidated_at_exit,
311                 access_place_error_reported: Default::default(),
312                 reservation_error_reported: Default::default(),
313                 reservation_warnings: Default::default(),
314                 move_error_reported: BTreeMap::new(),
315                 uninitialized_error_reported: Default::default(),
316                 errors_buffer,
317                 regioncx: regioncx.clone(),
318                 used_mut: Default::default(),
319                 used_mut_upvars: SmallVec::new(),
320                 borrow_set: Rc::clone(&borrow_set),
321                 dominators: Dominators::dummy(), // not used
322                 upvars: Vec::new(),
323                 local_names: IndexVec::from_elem(None, &promoted_body.local_decls),
324                 region_names: RefCell::default(),
325                 next_region_name: RefCell::new(1),
326                 polonius_output: None,
327             };
328             promoted_mbcx.report_move_errors(move_errors);
329             errors_buffer = promoted_mbcx.errors_buffer;
330         };
331     }
332
333     let dominators = body.dominators();
334
335     let mut mbcx = MirBorrowckCtxt {
336         infcx,
337         param_env,
338         body,
339         move_data: &mdpe.move_data,
340         location_table,
341         movable_generator,
342         locals_are_invalidated_at_exit,
343         fn_self_span_reported: Default::default(),
344         access_place_error_reported: Default::default(),
345         reservation_error_reported: Default::default(),
346         reservation_warnings: Default::default(),
347         move_error_reported: BTreeMap::new(),
348         uninitialized_error_reported: Default::default(),
349         errors_buffer,
350         regioncx: Rc::clone(&regioncx),
351         used_mut: Default::default(),
352         used_mut_upvars: SmallVec::new(),
353         borrow_set: Rc::clone(&borrow_set),
354         dominators,
355         upvars,
356         local_names,
357         region_names: RefCell::default(),
358         next_region_name: RefCell::new(1),
359         polonius_output,
360     };
361
362     // Compute and report region errors, if any.
363     mbcx.report_region_errors(nll_errors);
364
365     let results = BorrowckResults {
366         ever_inits: flow_ever_inits,
367         uninits: flow_uninits,
368         borrows: flow_borrows,
369     };
370
371     mbcx.report_move_errors(move_errors);
372
373     rustc_mir_dataflow::visit_results(
374         body,
375         traversal::reverse_postorder(body).map(|(bb, _)| bb),
376         &results,
377         &mut mbcx,
378     );
379
380     // Convert any reservation warnings into lints.
381     let reservation_warnings = mem::take(&mut mbcx.reservation_warnings);
382     for (_, (place, span, location, bk, borrow)) in reservation_warnings {
383         let mut initial_diag = mbcx.report_conflicting_borrow(location, (place, span), bk, &borrow);
384
385         let scope = mbcx.body.source_info(location).scope;
386         let lint_root = match &mbcx.body.source_scopes[scope].local_data {
387             ClearCrossCrate::Set(data) => data.lint_root,
388             _ => id,
389         };
390
391         // Span and message don't matter; we overwrite them below anyway
392         mbcx.infcx.tcx.struct_span_lint_hir(
393             MUTABLE_BORROW_RESERVATION_CONFLICT,
394             lint_root,
395             DUMMY_SP,
396             |lint| {
397                 let mut diag = lint.build("");
398
399                 diag.message = initial_diag.styled_message().clone();
400                 diag.span = initial_diag.span.clone();
401
402                 diag.buffer(&mut mbcx.errors_buffer);
403             },
404         );
405         initial_diag.cancel();
406     }
407
408     // For each non-user used mutable variable, check if it's been assigned from
409     // a user-declared local. If so, then put that local into the used_mut set.
410     // Note that this set is expected to be small - only upvars from closures
411     // would have a chance of erroneously adding non-user-defined mutable vars
412     // to the set.
413     let temporary_used_locals: FxHashSet<Local> = mbcx
414         .used_mut
415         .iter()
416         .filter(|&local| !mbcx.body.local_decls[*local].is_user_variable())
417         .cloned()
418         .collect();
419     // For the remaining unused locals that are marked as mutable, we avoid linting any that
420     // were never initialized. These locals may have been removed as unreachable code; or will be
421     // linted as unused variables.
422     let unused_mut_locals =
423         mbcx.body.mut_vars_iter().filter(|local| !mbcx.used_mut.contains(local)).collect();
424     mbcx.gather_used_muts(temporary_used_locals, unused_mut_locals);
425
426     debug!("mbcx.used_mut: {:?}", mbcx.used_mut);
427     let used_mut = mbcx.used_mut;
428     for local in mbcx.body.mut_vars_and_args_iter().filter(|local| !used_mut.contains(local)) {
429         let local_decl = &mbcx.body.local_decls[local];
430         let lint_root = match &mbcx.body.source_scopes[local_decl.source_info.scope].local_data {
431             ClearCrossCrate::Set(data) => data.lint_root,
432             _ => continue,
433         };
434
435         // Skip over locals that begin with an underscore or have no name
436         match mbcx.local_names[local] {
437             Some(name) => {
438                 if name.as_str().starts_with('_') {
439                     continue;
440                 }
441             }
442             None => continue,
443         }
444
445         let span = local_decl.source_info.span;
446         if span.desugaring_kind().is_some() {
447             // If the `mut` arises as part of a desugaring, we should ignore it.
448             continue;
449         }
450
451         tcx.struct_span_lint_hir(UNUSED_MUT, lint_root, span, |lint| {
452             let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
453             lint.build("variable does not need to be mutable")
454                 .span_suggestion_short(
455                     mut_span,
456                     "remove this `mut`",
457                     String::new(),
458                     Applicability::MachineApplicable,
459                 )
460                 .emit();
461         })
462     }
463
464     // Buffer any move errors that we collected and de-duplicated.
465     for (_, (_, diag)) in mbcx.move_error_reported {
466         diag.buffer(&mut mbcx.errors_buffer);
467     }
468
469     if !mbcx.errors_buffer.is_empty() {
470         mbcx.errors_buffer.sort_by_key(|diag| diag.sort_span);
471
472         for diag in mbcx.errors_buffer.drain(..) {
473             mbcx.infcx.tcx.sess.diagnostic().emit_diagnostic(&diag);
474         }
475     }
476
477     let result = BorrowCheckResult {
478         concrete_opaque_types: opaque_type_values,
479         closure_requirements: opt_closure_req,
480         used_mut_upvars: mbcx.used_mut_upvars,
481     };
482
483     let body_with_facts = if return_body_with_facts {
484         let output_facts = mbcx.polonius_output.expect("Polonius output was not computed");
485         Some(Box::new(BodyWithBorrowckFacts {
486             body: body_owned,
487             input_facts: *polonius_input.expect("Polonius input facts were not generated"),
488             output_facts,
489             location_table: location_table_owned,
490         }))
491     } else {
492         None
493     };
494
495     debug!("do_mir_borrowck: result = {:#?}", result);
496
497     (result, body_with_facts)
498 }
499
500 /// A `Body` with information computed by the borrow checker. This struct is
501 /// intended to be consumed by compiler consumers.
502 ///
503 /// We need to include the MIR body here because the region identifiers must
504 /// match the ones in the Polonius facts.
505 pub struct BodyWithBorrowckFacts<'tcx> {
506     /// A mir body that contains region identifiers.
507     pub body: Body<'tcx>,
508     /// Polonius input facts.
509     pub input_facts: AllFacts,
510     /// Polonius output facts.
511     pub output_facts: Rc<self::nll::PoloniusOutput>,
512     /// The table that maps Polonius points to locations in the table.
513     pub location_table: LocationTable,
514 }
515
516 struct MirBorrowckCtxt<'cx, 'tcx> {
517     infcx: &'cx InferCtxt<'cx, 'tcx>,
518     param_env: ParamEnv<'tcx>,
519     body: &'cx Body<'tcx>,
520     move_data: &'cx MoveData<'tcx>,
521
522     /// Map from MIR `Location` to `LocationIndex`; created
523     /// when MIR borrowck begins.
524     location_table: &'cx LocationTable,
525
526     movable_generator: bool,
527     /// This keeps track of whether local variables are free-ed when the function
528     /// exits even without a `StorageDead`, which appears to be the case for
529     /// constants.
530     ///
531     /// I'm not sure this is the right approach - @eddyb could you try and
532     /// figure this out?
533     locals_are_invalidated_at_exit: bool,
534     /// This field keeps track of when borrow errors are reported in the access_place function
535     /// so that there is no duplicate reporting. This field cannot also be used for the conflicting
536     /// borrow errors that is handled by the `reservation_error_reported` field as the inclusion
537     /// of the `Span` type (while required to mute some errors) stops the muting of the reservation
538     /// errors.
539     access_place_error_reported: FxHashSet<(Place<'tcx>, Span)>,
540     /// This field keeps track of when borrow conflict errors are reported
541     /// for reservations, so that we don't report seemingly duplicate
542     /// errors for corresponding activations.
543     //
544     // FIXME: ideally this would be a set of `BorrowIndex`, not `Place`s,
545     // but it is currently inconvenient to track down the `BorrowIndex`
546     // at the time we detect and report a reservation error.
547     reservation_error_reported: FxHashSet<Place<'tcx>>,
548     /// This fields keeps track of the `Span`s that we have
549     /// used to report extra information for `FnSelfUse`, to avoid
550     /// unnecessarily verbose errors.
551     fn_self_span_reported: FxHashSet<Span>,
552     /// Migration warnings to be reported for #56254. We delay reporting these
553     /// so that we can suppress the warning if there's a corresponding error
554     /// for the activation of the borrow.
555     reservation_warnings:
556         FxHashMap<BorrowIndex, (Place<'tcx>, Span, Location, BorrowKind, BorrowData<'tcx>)>,
557     /// This field keeps track of move errors that are to be reported for given move indices.
558     ///
559     /// There are situations where many errors can be reported for a single move out (see #53807)
560     /// and we want only the best of those errors.
561     ///
562     /// The `report_use_of_moved_or_uninitialized` function checks this map and replaces the
563     /// diagnostic (if there is one) if the `Place` of the error being reported is a prefix of the
564     /// `Place` of the previous most diagnostic. This happens instead of buffering the error. Once
565     /// all move errors have been reported, any diagnostics in this map are added to the buffer
566     /// to be emitted.
567     ///
568     /// `BTreeMap` is used to preserve the order of insertions when iterating. This is necessary
569     /// when errors in the map are being re-added to the error buffer so that errors with the
570     /// same primary span come out in a consistent order.
571     move_error_reported: BTreeMap<Vec<MoveOutIndex>, (PlaceRef<'tcx>, DiagnosticBuilder<'cx>)>,
572     /// This field keeps track of errors reported in the checking of uninitialized variables,
573     /// so that we don't report seemingly duplicate errors.
574     uninitialized_error_reported: FxHashSet<PlaceRef<'tcx>>,
575     /// Errors to be reported buffer
576     errors_buffer: Vec<Diagnostic>,
577     /// This field keeps track of all the local variables that are declared mut and are mutated.
578     /// Used for the warning issued by an unused mutable local variable.
579     used_mut: FxHashSet<Local>,
580     /// If the function we're checking is a closure, then we'll need to report back the list of
581     /// mutable upvars that have been used. This field keeps track of them.
582     used_mut_upvars: SmallVec<[Field; 8]>,
583     /// Region inference context. This contains the results from region inference and lets us e.g.
584     /// find out which CFG points are contained in each borrow region.
585     regioncx: Rc<RegionInferenceContext<'tcx>>,
586
587     /// The set of borrows extracted from the MIR
588     borrow_set: Rc<BorrowSet<'tcx>>,
589
590     /// Dominators for MIR
591     dominators: Dominators<BasicBlock>,
592
593     /// Information about upvars not necessarily preserved in types or MIR
594     upvars: Vec<Upvar<'tcx>>,
595
596     /// Names of local (user) variables (extracted from `var_debug_info`).
597     local_names: IndexVec<Local, Option<Symbol>>,
598
599     /// Record the region names generated for each region in the given
600     /// MIR def so that we can reuse them later in help/error messages.
601     region_names: RefCell<FxHashMap<RegionVid, RegionName>>,
602
603     /// The counter for generating new region names.
604     next_region_name: RefCell<usize>,
605
606     /// Results of Polonius analysis.
607     polonius_output: Option<Rc<PoloniusOutput>>,
608 }
609
610 // Check that:
611 // 1. assignments are always made to mutable locations (FIXME: does that still really go here?)
612 // 2. loans made in overlapping scopes do not conflict
613 // 3. assignments do not affect things loaned out as immutable
614 // 4. moves do not affect things loaned out in any way
615 impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tcx> {
616     type FlowState = Flows<'cx, 'tcx>;
617
618     fn visit_statement_before_primary_effect(
619         &mut self,
620         flow_state: &Flows<'cx, 'tcx>,
621         stmt: &'cx Statement<'tcx>,
622         location: Location,
623     ) {
624         debug!("MirBorrowckCtxt::process_statement({:?}, {:?}): {:?}", location, stmt, flow_state);
625         let span = stmt.source_info.span;
626
627         self.check_activations(location, span, flow_state);
628
629         match &stmt.kind {
630             StatementKind::Assign(box (lhs, ref rhs)) => {
631                 self.consume_rvalue(location, (rhs, span), flow_state);
632
633                 self.mutate_place(location, (*lhs, span), Shallow(None), JustWrite, flow_state);
634             }
635             StatementKind::FakeRead(box (_, ref place)) => {
636                 // Read for match doesn't access any memory and is used to
637                 // assert that a place is safe and live. So we don't have to
638                 // do any checks here.
639                 //
640                 // FIXME: Remove check that the place is initialized. This is
641                 // needed for now because matches don't have never patterns yet.
642                 // So this is the only place we prevent
643                 //      let x: !;
644                 //      match x {};
645                 // from compiling.
646                 self.check_if_path_or_subpath_is_moved(
647                     location,
648                     InitializationRequiringAction::Use,
649                     (place.as_ref(), span),
650                     flow_state,
651                 );
652             }
653             StatementKind::SetDiscriminant { place, variant_index: _ } => {
654                 self.mutate_place(location, (**place, span), Shallow(None), JustWrite, flow_state);
655             }
656             StatementKind::LlvmInlineAsm(ref asm) => {
657                 for (o, output) in iter::zip(&asm.asm.outputs, &*asm.outputs) {
658                     if o.is_indirect {
659                         // FIXME(eddyb) indirect inline asm outputs should
660                         // be encoded through MIR place derefs instead.
661                         self.access_place(
662                             location,
663                             (*output, o.span),
664                             (Deep, Read(ReadKind::Copy)),
665                             LocalMutationIsAllowed::No,
666                             flow_state,
667                         );
668                         self.check_if_path_or_subpath_is_moved(
669                             location,
670                             InitializationRequiringAction::Use,
671                             (output.as_ref(), o.span),
672                             flow_state,
673                         );
674                     } else {
675                         self.mutate_place(
676                             location,
677                             (*output, o.span),
678                             if o.is_rw { Deep } else { Shallow(None) },
679                             if o.is_rw { WriteAndRead } else { JustWrite },
680                             flow_state,
681                         );
682                     }
683                 }
684                 for (_, input) in asm.inputs.iter() {
685                     self.consume_operand(location, (input, span), flow_state);
686                 }
687             }
688
689             StatementKind::CopyNonOverlapping(box rustc_middle::mir::CopyNonOverlapping {
690                 ..
691             }) => {
692                 span_bug!(
693                     span,
694                     "Unexpected CopyNonOverlapping, should only appear after lower_intrinsics",
695                 )
696             }
697             StatementKind::Nop
698             | StatementKind::Coverage(..)
699             | StatementKind::AscribeUserType(..)
700             | StatementKind::Retag { .. }
701             | StatementKind::StorageLive(..) => {
702                 // `Nop`, `AscribeUserType`, `Retag`, and `StorageLive` are irrelevant
703                 // to borrow check.
704             }
705             StatementKind::StorageDead(local) => {
706                 self.access_place(
707                     location,
708                     (Place::from(*local), span),
709                     (Shallow(None), Write(WriteKind::StorageDeadOrDrop)),
710                     LocalMutationIsAllowed::Yes,
711                     flow_state,
712                 );
713             }
714         }
715     }
716
717     fn visit_terminator_before_primary_effect(
718         &mut self,
719         flow_state: &Flows<'cx, 'tcx>,
720         term: &'cx Terminator<'tcx>,
721         loc: Location,
722     ) {
723         debug!("MirBorrowckCtxt::process_terminator({:?}, {:?}): {:?}", loc, term, flow_state);
724         let span = term.source_info.span;
725
726         self.check_activations(loc, span, flow_state);
727
728         match term.kind {
729             TerminatorKind::SwitchInt { ref discr, switch_ty: _, targets: _ } => {
730                 self.consume_operand(loc, (discr, span), flow_state);
731             }
732             TerminatorKind::Drop { place, target: _, unwind: _ } => {
733                 debug!(
734                     "visit_terminator_drop \
735                      loc: {:?} term: {:?} place: {:?} span: {:?}",
736                     loc, term, place, span
737                 );
738
739                 self.access_place(
740                     loc,
741                     (place, span),
742                     (AccessDepth::Drop, Write(WriteKind::StorageDeadOrDrop)),
743                     LocalMutationIsAllowed::Yes,
744                     flow_state,
745                 );
746             }
747             TerminatorKind::DropAndReplace {
748                 place: drop_place,
749                 value: ref new_value,
750                 target: _,
751                 unwind: _,
752             } => {
753                 self.mutate_place(loc, (drop_place, span), Deep, JustWrite, flow_state);
754                 self.consume_operand(loc, (new_value, span), flow_state);
755             }
756             TerminatorKind::Call {
757                 ref func,
758                 ref args,
759                 ref destination,
760                 cleanup: _,
761                 from_hir_call: _,
762                 fn_span: _,
763             } => {
764                 self.consume_operand(loc, (func, span), flow_state);
765                 for arg in args {
766                     self.consume_operand(loc, (arg, span), flow_state);
767                 }
768                 if let Some((dest, _ /*bb*/)) = *destination {
769                     self.mutate_place(loc, (dest, span), Deep, JustWrite, flow_state);
770                 }
771             }
772             TerminatorKind::Assert { ref cond, expected: _, ref msg, target: _, cleanup: _ } => {
773                 self.consume_operand(loc, (cond, span), flow_state);
774                 use rustc_middle::mir::AssertKind;
775                 if let AssertKind::BoundsCheck { ref len, ref index } = *msg {
776                     self.consume_operand(loc, (len, span), flow_state);
777                     self.consume_operand(loc, (index, span), flow_state);
778                 }
779             }
780
781             TerminatorKind::Yield { ref value, resume: _, resume_arg, drop: _ } => {
782                 self.consume_operand(loc, (value, span), flow_state);
783                 self.mutate_place(loc, (resume_arg, span), Deep, JustWrite, flow_state);
784             }
785
786             TerminatorKind::InlineAsm {
787                 template: _,
788                 ref operands,
789                 options: _,
790                 line_spans: _,
791                 destination: _,
792                 cleanup: _,
793             } => {
794                 for op in operands {
795                     match *op {
796                         InlineAsmOperand::In { reg: _, ref value } => {
797                             self.consume_operand(loc, (value, span), flow_state);
798                         }
799                         InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
800                             if let Some(place) = place {
801                                 self.mutate_place(
802                                     loc,
803                                     (place, span),
804                                     Shallow(None),
805                                     JustWrite,
806                                     flow_state,
807                                 );
808                             }
809                         }
810                         InlineAsmOperand::InOut { reg: _, late: _, ref in_value, out_place } => {
811                             self.consume_operand(loc, (in_value, span), flow_state);
812                             if let Some(out_place) = out_place {
813                                 self.mutate_place(
814                                     loc,
815                                     (out_place, span),
816                                     Shallow(None),
817                                     JustWrite,
818                                     flow_state,
819                                 );
820                             }
821                         }
822                         InlineAsmOperand::Const { value: _ }
823                         | InlineAsmOperand::SymFn { value: _ }
824                         | InlineAsmOperand::SymStatic { def_id: _ } => {}
825                     }
826                 }
827             }
828
829             TerminatorKind::Goto { target: _ }
830             | TerminatorKind::Abort
831             | TerminatorKind::Unreachable
832             | TerminatorKind::Resume
833             | TerminatorKind::Return
834             | TerminatorKind::GeneratorDrop
835             | TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }
836             | TerminatorKind::FalseUnwind { real_target: _, unwind: _ } => {
837                 // no data used, thus irrelevant to borrowck
838             }
839         }
840     }
841
842     fn visit_terminator_after_primary_effect(
843         &mut self,
844         flow_state: &Flows<'cx, 'tcx>,
845         term: &'cx Terminator<'tcx>,
846         loc: Location,
847     ) {
848         let span = term.source_info.span;
849
850         match term.kind {
851             TerminatorKind::Yield { value: _, resume: _, resume_arg: _, drop: _ } => {
852                 if self.movable_generator {
853                     // Look for any active borrows to locals
854                     let borrow_set = self.borrow_set.clone();
855                     for i in flow_state.borrows.iter() {
856                         let borrow = &borrow_set[i];
857                         self.check_for_local_borrow(borrow, span);
858                     }
859                 }
860             }
861
862             TerminatorKind::Resume | TerminatorKind::Return | TerminatorKind::GeneratorDrop => {
863                 // Returning from the function implicitly kills storage for all locals and statics.
864                 // Often, the storage will already have been killed by an explicit
865                 // StorageDead, but we don't always emit those (notably on unwind paths),
866                 // so this "extra check" serves as a kind of backup.
867                 let borrow_set = self.borrow_set.clone();
868                 for i in flow_state.borrows.iter() {
869                     let borrow = &borrow_set[i];
870                     self.check_for_invalidation_at_exit(loc, borrow, span);
871                 }
872             }
873
874             TerminatorKind::Abort
875             | TerminatorKind::Assert { .. }
876             | TerminatorKind::Call { .. }
877             | TerminatorKind::Drop { .. }
878             | TerminatorKind::DropAndReplace { .. }
879             | TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }
880             | TerminatorKind::FalseUnwind { real_target: _, unwind: _ }
881             | TerminatorKind::Goto { .. }
882             | TerminatorKind::SwitchInt { .. }
883             | TerminatorKind::Unreachable
884             | TerminatorKind::InlineAsm { .. } => {}
885         }
886     }
887 }
888
889 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
890 enum MutateMode {
891     JustWrite,
892     WriteAndRead,
893 }
894
895 use self::AccessDepth::{Deep, Shallow};
896 use self::ReadOrWrite::{Activation, Read, Reservation, Write};
897
898 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
899 enum ArtificialField {
900     ArrayLength,
901     ShallowBorrow,
902 }
903
904 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
905 enum AccessDepth {
906     /// From the RFC: "A *shallow* access means that the immediate
907     /// fields reached at P are accessed, but references or pointers
908     /// found within are not dereferenced. Right now, the only access
909     /// that is shallow is an assignment like `x = ...;`, which would
910     /// be a *shallow write* of `x`."
911     Shallow(Option<ArtificialField>),
912
913     /// From the RFC: "A *deep* access means that all data reachable
914     /// through the given place may be invalidated or accesses by
915     /// this action."
916     Deep,
917
918     /// Access is Deep only when there is a Drop implementation that
919     /// can reach the data behind the reference.
920     Drop,
921 }
922
923 /// Kind of access to a value: read or write
924 /// (For informational purposes only)
925 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
926 enum ReadOrWrite {
927     /// From the RFC: "A *read* means that the existing data may be
928     /// read, but will not be changed."
929     Read(ReadKind),
930
931     /// From the RFC: "A *write* means that the data may be mutated to
932     /// new values or otherwise invalidated (for example, it could be
933     /// de-initialized, as in a move operation).
934     Write(WriteKind),
935
936     /// For two-phase borrows, we distinguish a reservation (which is treated
937     /// like a Read) from an activation (which is treated like a write), and
938     /// each of those is furthermore distinguished from Reads/Writes above.
939     Reservation(WriteKind),
940     Activation(WriteKind, BorrowIndex),
941 }
942
943 /// Kind of read access to a value
944 /// (For informational purposes only)
945 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
946 enum ReadKind {
947     Borrow(BorrowKind),
948     Copy,
949 }
950
951 /// Kind of write access to a value
952 /// (For informational purposes only)
953 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
954 enum WriteKind {
955     StorageDeadOrDrop,
956     MutableBorrow(BorrowKind),
957     Mutate,
958     Move,
959 }
960
961 /// When checking permissions for a place access, this flag is used to indicate that an immutable
962 /// local place can be mutated.
963 //
964 // FIXME: @nikomatsakis suggested that this flag could be removed with the following modifications:
965 // - Merge `check_access_permissions()` and `check_if_reassignment_to_immutable_state()`.
966 // - Split `is_mutable()` into `is_assignable()` (can be directly assigned) and
967 //   `is_declared_mutable()`.
968 // - Take flow state into consideration in `is_assignable()` for local variables.
969 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
970 enum LocalMutationIsAllowed {
971     Yes,
972     /// We want use of immutable upvars to cause a "write to immutable upvar"
973     /// error, not an "reassignment" error.
974     ExceptUpvars,
975     No,
976 }
977
978 #[derive(Copy, Clone, Debug)]
979 enum InitializationRequiringAction {
980     Update,
981     Borrow,
982     MatchOn,
983     Use,
984     Assignment,
985     PartialAssignment,
986 }
987
988 struct RootPlace<'tcx> {
989     place_local: Local,
990     place_projection: &'tcx [PlaceElem<'tcx>],
991     is_local_mutation_allowed: LocalMutationIsAllowed,
992 }
993
994 impl InitializationRequiringAction {
995     fn as_noun(self) -> &'static str {
996         match self {
997             InitializationRequiringAction::Update => "update",
998             InitializationRequiringAction::Borrow => "borrow",
999             InitializationRequiringAction::MatchOn => "use", // no good noun
1000             InitializationRequiringAction::Use => "use",
1001             InitializationRequiringAction::Assignment => "assign",
1002             InitializationRequiringAction::PartialAssignment => "assign to part",
1003         }
1004     }
1005
1006     fn as_verb_in_past_tense(self) -> &'static str {
1007         match self {
1008             InitializationRequiringAction::Update => "updated",
1009             InitializationRequiringAction::Borrow => "borrowed",
1010             InitializationRequiringAction::MatchOn => "matched on",
1011             InitializationRequiringAction::Use => "used",
1012             InitializationRequiringAction::Assignment => "assigned",
1013             InitializationRequiringAction::PartialAssignment => "partially assigned",
1014         }
1015     }
1016 }
1017
1018 impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
1019     fn body(&self) -> &'cx Body<'tcx> {
1020         self.body
1021     }
1022
1023     /// Checks an access to the given place to see if it is allowed. Examines the set of borrows
1024     /// that are in scope, as well as which paths have been initialized, to ensure that (a) the
1025     /// place is initialized and (b) it is not borrowed in some way that would prevent this
1026     /// access.
1027     ///
1028     /// Returns `true` if an error is reported.
1029     fn access_place(
1030         &mut self,
1031         location: Location,
1032         place_span: (Place<'tcx>, Span),
1033         kind: (AccessDepth, ReadOrWrite),
1034         is_local_mutation_allowed: LocalMutationIsAllowed,
1035         flow_state: &Flows<'cx, 'tcx>,
1036     ) {
1037         let (sd, rw) = kind;
1038
1039         if let Activation(_, borrow_index) = rw {
1040             if self.reservation_error_reported.contains(&place_span.0) {
1041                 debug!(
1042                     "skipping access_place for activation of invalid reservation \
1043                      place: {:?} borrow_index: {:?}",
1044                     place_span.0, borrow_index
1045                 );
1046                 return;
1047             }
1048         }
1049
1050         // Check is_empty() first because it's the common case, and doing that
1051         // way we avoid the clone() call.
1052         if !self.access_place_error_reported.is_empty()
1053             && self.access_place_error_reported.contains(&(place_span.0, place_span.1))
1054         {
1055             debug!(
1056                 "access_place: suppressing error place_span=`{:?}` kind=`{:?}`",
1057                 place_span, kind
1058             );
1059             return;
1060         }
1061
1062         let mutability_error = self.check_access_permissions(
1063             place_span,
1064             rw,
1065             is_local_mutation_allowed,
1066             flow_state,
1067             location,
1068         );
1069         let conflict_error =
1070             self.check_access_for_conflict(location, place_span, sd, rw, flow_state);
1071
1072         if let (Activation(_, borrow_idx), true) = (kind.1, conflict_error) {
1073             // Suppress this warning when there's an error being emitted for the
1074             // same borrow: fixing the error is likely to fix the warning.
1075             self.reservation_warnings.remove(&borrow_idx);
1076         }
1077
1078         if conflict_error || mutability_error {
1079             debug!("access_place: logging error place_span=`{:?}` kind=`{:?}`", place_span, kind);
1080
1081             self.access_place_error_reported.insert((place_span.0, place_span.1));
1082         }
1083     }
1084
1085     fn check_access_for_conflict(
1086         &mut self,
1087         location: Location,
1088         place_span: (Place<'tcx>, Span),
1089         sd: AccessDepth,
1090         rw: ReadOrWrite,
1091         flow_state: &Flows<'cx, 'tcx>,
1092     ) -> bool {
1093         debug!(
1094             "check_access_for_conflict(location={:?}, place_span={:?}, sd={:?}, rw={:?})",
1095             location, place_span, sd, rw,
1096         );
1097
1098         let mut error_reported = false;
1099         let tcx = self.infcx.tcx;
1100         let body = self.body;
1101         let borrow_set = self.borrow_set.clone();
1102
1103         // Use polonius output if it has been enabled.
1104         let polonius_output = self.polonius_output.clone();
1105         let borrows_in_scope = if let Some(polonius) = &polonius_output {
1106             let location = self.location_table.start_index(location);
1107             Either::Left(polonius.errors_at(location).iter().copied())
1108         } else {
1109             Either::Right(flow_state.borrows.iter())
1110         };
1111
1112         each_borrow_involving_path(
1113             self,
1114             tcx,
1115             body,
1116             location,
1117             (sd, place_span.0),
1118             &borrow_set,
1119             borrows_in_scope,
1120             |this, borrow_index, borrow| match (rw, borrow.kind) {
1121                 // Obviously an activation is compatible with its own
1122                 // reservation (or even prior activating uses of same
1123                 // borrow); so don't check if they interfere.
1124                 //
1125                 // NOTE: *reservations* do conflict with themselves;
1126                 // thus aren't injecting unsoundenss w/ this check.)
1127                 (Activation(_, activating), _) if activating == borrow_index => {
1128                     debug!(
1129                         "check_access_for_conflict place_span: {:?} sd: {:?} rw: {:?} \
1130                          skipping {:?} b/c activation of same borrow_index",
1131                         place_span,
1132                         sd,
1133                         rw,
1134                         (borrow_index, borrow),
1135                     );
1136                     Control::Continue
1137                 }
1138
1139                 (Read(_), BorrowKind::Shared | BorrowKind::Shallow)
1140                 | (
1141                     Read(ReadKind::Borrow(BorrowKind::Shallow)),
1142                     BorrowKind::Unique | BorrowKind::Mut { .. },
1143                 ) => Control::Continue,
1144
1145                 (Write(WriteKind::Move), BorrowKind::Shallow) => {
1146                     // Handled by initialization checks.
1147                     Control::Continue
1148                 }
1149
1150                 (Read(kind), BorrowKind::Unique | BorrowKind::Mut { .. }) => {
1151                     // Reading from mere reservations of mutable-borrows is OK.
1152                     if !is_active(&this.dominators, borrow, location) {
1153                         assert!(allow_two_phase_borrow(borrow.kind));
1154                         return Control::Continue;
1155                     }
1156
1157                     error_reported = true;
1158                     match kind {
1159                         ReadKind::Copy => {
1160                             this.report_use_while_mutably_borrowed(location, place_span, borrow)
1161                                 .buffer(&mut this.errors_buffer);
1162                         }
1163                         ReadKind::Borrow(bk) => {
1164                             this.report_conflicting_borrow(location, place_span, bk, borrow)
1165                                 .buffer(&mut this.errors_buffer);
1166                         }
1167                     }
1168                     Control::Break
1169                 }
1170
1171                 (
1172                     Reservation(WriteKind::MutableBorrow(bk)),
1173                     BorrowKind::Shallow | BorrowKind::Shared,
1174                 ) if { tcx.migrate_borrowck() && this.borrow_set.contains(&location) } => {
1175                     let bi = this.borrow_set.get_index_of(&location).unwrap();
1176                     debug!(
1177                         "recording invalid reservation of place: {:?} with \
1178                          borrow index {:?} as warning",
1179                         place_span.0, bi,
1180                     );
1181                     // rust-lang/rust#56254 - This was previously permitted on
1182                     // the 2018 edition so we emit it as a warning. We buffer
1183                     // these sepately so that we only emit a warning if borrow
1184                     // checking was otherwise successful.
1185                     this.reservation_warnings
1186                         .insert(bi, (place_span.0, place_span.1, location, bk, borrow.clone()));
1187
1188                     // Don't suppress actual errors.
1189                     Control::Continue
1190                 }
1191
1192                 (Reservation(kind) | Activation(kind, _) | Write(kind), _) => {
1193                     match rw {
1194                         Reservation(..) => {
1195                             debug!(
1196                                 "recording invalid reservation of \
1197                                  place: {:?}",
1198                                 place_span.0
1199                             );
1200                             this.reservation_error_reported.insert(place_span.0);
1201                         }
1202                         Activation(_, activating) => {
1203                             debug!(
1204                                 "observing check_place for activation of \
1205                                  borrow_index: {:?}",
1206                                 activating
1207                             );
1208                         }
1209                         Read(..) | Write(..) => {}
1210                     }
1211
1212                     error_reported = true;
1213                     match kind {
1214                         WriteKind::MutableBorrow(bk) => {
1215                             this.report_conflicting_borrow(location, place_span, bk, borrow)
1216                                 .buffer(&mut this.errors_buffer);
1217                         }
1218                         WriteKind::StorageDeadOrDrop => this
1219                             .report_borrowed_value_does_not_live_long_enough(
1220                                 location,
1221                                 borrow,
1222                                 place_span,
1223                                 Some(kind),
1224                             ),
1225                         WriteKind::Mutate => {
1226                             this.report_illegal_mutation_of_borrowed(location, place_span, borrow)
1227                         }
1228                         WriteKind::Move => {
1229                             this.report_move_out_while_borrowed(location, place_span, borrow)
1230                         }
1231                     }
1232                     Control::Break
1233                 }
1234             },
1235         );
1236
1237         error_reported
1238     }
1239
1240     fn mutate_place(
1241         &mut self,
1242         location: Location,
1243         place_span: (Place<'tcx>, Span),
1244         kind: AccessDepth,
1245         mode: MutateMode,
1246         flow_state: &Flows<'cx, 'tcx>,
1247     ) {
1248         // Write of P[i] or *P, or WriteAndRead of any P, requires P init'd.
1249         match mode {
1250             MutateMode::WriteAndRead => {
1251                 self.check_if_path_or_subpath_is_moved(
1252                     location,
1253                     InitializationRequiringAction::Update,
1254                     (place_span.0.as_ref(), place_span.1),
1255                     flow_state,
1256                 );
1257             }
1258             MutateMode::JustWrite => {
1259                 self.check_if_assigned_path_is_moved(location, place_span, flow_state);
1260             }
1261         }
1262
1263         // Special case: you can assign an immutable local variable
1264         // (e.g., `x = ...`) so long as it has never been initialized
1265         // before (at this point in the flow).
1266         if let Some(local) = place_span.0.as_local() {
1267             if let Mutability::Not = self.body.local_decls[local].mutability {
1268                 // check for reassignments to immutable local variables
1269                 self.check_if_reassignment_to_immutable_state(
1270                     location, local, place_span, flow_state,
1271                 );
1272                 return;
1273             }
1274         }
1275
1276         // Otherwise, use the normal access permission rules.
1277         self.access_place(
1278             location,
1279             place_span,
1280             (kind, Write(WriteKind::Mutate)),
1281             LocalMutationIsAllowed::No,
1282             flow_state,
1283         );
1284     }
1285
1286     fn consume_rvalue(
1287         &mut self,
1288         location: Location,
1289         (rvalue, span): (&'cx Rvalue<'tcx>, Span),
1290         flow_state: &Flows<'cx, 'tcx>,
1291     ) {
1292         match *rvalue {
1293             Rvalue::Ref(_ /*rgn*/, bk, place) => {
1294                 let access_kind = match bk {
1295                     BorrowKind::Shallow => {
1296                         (Shallow(Some(ArtificialField::ShallowBorrow)), Read(ReadKind::Borrow(bk)))
1297                     }
1298                     BorrowKind::Shared => (Deep, Read(ReadKind::Borrow(bk))),
1299                     BorrowKind::Unique | BorrowKind::Mut { .. } => {
1300                         let wk = WriteKind::MutableBorrow(bk);
1301                         if allow_two_phase_borrow(bk) {
1302                             (Deep, Reservation(wk))
1303                         } else {
1304                             (Deep, Write(wk))
1305                         }
1306                     }
1307                 };
1308
1309                 self.access_place(
1310                     location,
1311                     (place, span),
1312                     access_kind,
1313                     LocalMutationIsAllowed::No,
1314                     flow_state,
1315                 );
1316
1317                 let action = if bk == BorrowKind::Shallow {
1318                     InitializationRequiringAction::MatchOn
1319                 } else {
1320                     InitializationRequiringAction::Borrow
1321                 };
1322
1323                 self.check_if_path_or_subpath_is_moved(
1324                     location,
1325                     action,
1326                     (place.as_ref(), span),
1327                     flow_state,
1328                 );
1329             }
1330
1331             Rvalue::AddressOf(mutability, place) => {
1332                 let access_kind = match mutability {
1333                     Mutability::Mut => (
1334                         Deep,
1335                         Write(WriteKind::MutableBorrow(BorrowKind::Mut {
1336                             allow_two_phase_borrow: false,
1337                         })),
1338                     ),
1339                     Mutability::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
1340                 };
1341
1342                 self.access_place(
1343                     location,
1344                     (place, span),
1345                     access_kind,
1346                     LocalMutationIsAllowed::No,
1347                     flow_state,
1348                 );
1349
1350                 self.check_if_path_or_subpath_is_moved(
1351                     location,
1352                     InitializationRequiringAction::Borrow,
1353                     (place.as_ref(), span),
1354                     flow_state,
1355                 );
1356             }
1357
1358             Rvalue::ThreadLocalRef(_) => {}
1359
1360             Rvalue::Use(ref operand)
1361             | Rvalue::Repeat(ref operand, _)
1362             | Rvalue::UnaryOp(_ /*un_op*/, ref operand)
1363             | Rvalue::Cast(_ /*cast_kind*/, ref operand, _ /*ty*/)
1364             | Rvalue::ShallowInitBox(ref operand, _ /*ty*/) => {
1365                 self.consume_operand(location, (operand, span), flow_state)
1366             }
1367
1368             Rvalue::Len(place) | Rvalue::Discriminant(place) => {
1369                 let af = match *rvalue {
1370                     Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
1371                     Rvalue::Discriminant(..) => None,
1372                     _ => unreachable!(),
1373                 };
1374                 self.access_place(
1375                     location,
1376                     (place, span),
1377                     (Shallow(af), Read(ReadKind::Copy)),
1378                     LocalMutationIsAllowed::No,
1379                     flow_state,
1380                 );
1381                 self.check_if_path_or_subpath_is_moved(
1382                     location,
1383                     InitializationRequiringAction::Use,
1384                     (place.as_ref(), span),
1385                     flow_state,
1386                 );
1387             }
1388
1389             Rvalue::BinaryOp(_bin_op, box (ref operand1, ref operand2))
1390             | Rvalue::CheckedBinaryOp(_bin_op, box (ref operand1, ref operand2)) => {
1391                 self.consume_operand(location, (operand1, span), flow_state);
1392                 self.consume_operand(location, (operand2, span), flow_state);
1393             }
1394
1395             Rvalue::NullaryOp(_op, _ty) => {
1396                 // nullary ops take no dynamic input; no borrowck effect.
1397             }
1398
1399             Rvalue::Aggregate(ref aggregate_kind, ref operands) => {
1400                 // We need to report back the list of mutable upvars that were
1401                 // moved into the closure and subsequently used by the closure,
1402                 // in order to populate our used_mut set.
1403                 match **aggregate_kind {
1404                     AggregateKind::Closure(def_id, _) | AggregateKind::Generator(def_id, _, _) => {
1405                         let BorrowCheckResult { used_mut_upvars, .. } =
1406                             self.infcx.tcx.mir_borrowck(def_id.expect_local());
1407                         debug!("{:?} used_mut_upvars={:?}", def_id, used_mut_upvars);
1408                         for field in used_mut_upvars {
1409                             self.propagate_closure_used_mut_upvar(&operands[field.index()]);
1410                         }
1411                     }
1412                     AggregateKind::Adt(..)
1413                     | AggregateKind::Array(..)
1414                     | AggregateKind::Tuple { .. } => (),
1415                 }
1416
1417                 for operand in operands {
1418                     self.consume_operand(location, (operand, span), flow_state);
1419                 }
1420             }
1421         }
1422     }
1423
1424     fn propagate_closure_used_mut_upvar(&mut self, operand: &Operand<'tcx>) {
1425         let propagate_closure_used_mut_place = |this: &mut Self, place: Place<'tcx>| {
1426             // We have three possibilities here:
1427             // a. We are modifying something through a mut-ref
1428             // b. We are modifying something that is local to our parent
1429             // c. Current body is a nested closure, and we are modifying path starting from
1430             //    a Place captured by our parent closure.
1431
1432             // Handle (c), the path being modified is exactly the path captured by our parent
1433             if let Some(field) = this.is_upvar_field_projection(place.as_ref()) {
1434                 this.used_mut_upvars.push(field);
1435                 return;
1436             }
1437
1438             for (place_ref, proj) in place.iter_projections().rev() {
1439                 // Handle (a)
1440                 if proj == ProjectionElem::Deref {
1441                     match place_ref.ty(this.body(), this.infcx.tcx).ty.kind() {
1442                         // We aren't modifying a variable directly
1443                         ty::Ref(_, _, hir::Mutability::Mut) => return,
1444
1445                         _ => {}
1446                     }
1447                 }
1448
1449                 // Handle (c)
1450                 if let Some(field) = this.is_upvar_field_projection(place_ref) {
1451                     this.used_mut_upvars.push(field);
1452                     return;
1453                 }
1454             }
1455
1456             // Handle(b)
1457             this.used_mut.insert(place.local);
1458         };
1459
1460         // This relies on the current way that by-value
1461         // captures of a closure are copied/moved directly
1462         // when generating MIR.
1463         match *operand {
1464             Operand::Move(place) | Operand::Copy(place) => {
1465                 match place.as_local() {
1466                     Some(local) if !self.body.local_decls[local].is_user_variable() => {
1467                         if self.body.local_decls[local].ty.is_mutable_ptr() {
1468                             // The variable will be marked as mutable by the borrow.
1469                             return;
1470                         }
1471                         // This is an edge case where we have a `move` closure
1472                         // inside a non-move closure, and the inner closure
1473                         // contains a mutation:
1474                         //
1475                         // let mut i = 0;
1476                         // || { move || { i += 1; }; };
1477                         //
1478                         // In this case our usual strategy of assuming that the
1479                         // variable will be captured by mutable reference is
1480                         // wrong, since `i` can be copied into the inner
1481                         // closure from a shared reference.
1482                         //
1483                         // As such we have to search for the local that this
1484                         // capture comes from and mark it as being used as mut.
1485
1486                         let temp_mpi = self.move_data.rev_lookup.find_local(local);
1487                         let init = if let [init_index] = *self.move_data.init_path_map[temp_mpi] {
1488                             &self.move_data.inits[init_index]
1489                         } else {
1490                             bug!("temporary should be initialized exactly once")
1491                         };
1492
1493                         let loc = match init.location {
1494                             InitLocation::Statement(stmt) => stmt,
1495                             _ => bug!("temporary initialized in arguments"),
1496                         };
1497
1498                         let body = self.body;
1499                         let bbd = &body[loc.block];
1500                         let stmt = &bbd.statements[loc.statement_index];
1501                         debug!("temporary assigned in: stmt={:?}", stmt);
1502
1503                         if let StatementKind::Assign(box (_, Rvalue::Ref(_, _, source))) = stmt.kind
1504                         {
1505                             propagate_closure_used_mut_place(self, source);
1506                         } else {
1507                             bug!(
1508                                 "closures should only capture user variables \
1509                                  or references to user variables"
1510                             );
1511                         }
1512                     }
1513                     _ => propagate_closure_used_mut_place(self, place),
1514                 }
1515             }
1516             Operand::Constant(..) => {}
1517         }
1518     }
1519
1520     fn consume_operand(
1521         &mut self,
1522         location: Location,
1523         (operand, span): (&'cx Operand<'tcx>, Span),
1524         flow_state: &Flows<'cx, 'tcx>,
1525     ) {
1526         match *operand {
1527             Operand::Copy(place) => {
1528                 // copy of place: check if this is "copy of frozen path"
1529                 // (FIXME: see check_loans.rs)
1530                 self.access_place(
1531                     location,
1532                     (place, span),
1533                     (Deep, Read(ReadKind::Copy)),
1534                     LocalMutationIsAllowed::No,
1535                     flow_state,
1536                 );
1537
1538                 // Finally, check if path was already moved.
1539                 self.check_if_path_or_subpath_is_moved(
1540                     location,
1541                     InitializationRequiringAction::Use,
1542                     (place.as_ref(), span),
1543                     flow_state,
1544                 );
1545             }
1546             Operand::Move(place) => {
1547                 // move of place: check if this is move of already borrowed path
1548                 self.access_place(
1549                     location,
1550                     (place, span),
1551                     (Deep, Write(WriteKind::Move)),
1552                     LocalMutationIsAllowed::Yes,
1553                     flow_state,
1554                 );
1555
1556                 // Finally, check if path was already moved.
1557                 self.check_if_path_or_subpath_is_moved(
1558                     location,
1559                     InitializationRequiringAction::Use,
1560                     (place.as_ref(), span),
1561                     flow_state,
1562                 );
1563             }
1564             Operand::Constant(_) => {}
1565         }
1566     }
1567
1568     /// Checks whether a borrow of this place is invalidated when the function
1569     /// exits
1570     fn check_for_invalidation_at_exit(
1571         &mut self,
1572         location: Location,
1573         borrow: &BorrowData<'tcx>,
1574         span: Span,
1575     ) {
1576         debug!("check_for_invalidation_at_exit({:?})", borrow);
1577         let place = borrow.borrowed_place;
1578         let mut root_place = PlaceRef { local: place.local, projection: &[] };
1579
1580         // FIXME(nll-rfc#40): do more precise destructor tracking here. For now
1581         // we just know that all locals are dropped at function exit (otherwise
1582         // we'll have a memory leak) and assume that all statics have a destructor.
1583         //
1584         // FIXME: allow thread-locals to borrow other thread locals?
1585
1586         let (might_be_alive, will_be_dropped) =
1587             if self.body.local_decls[root_place.local].is_ref_to_thread_local() {
1588                 // Thread-locals might be dropped after the function exits
1589                 // We have to dereference the outer reference because
1590                 // borrows don't conflict behind shared references.
1591                 root_place.projection = DEREF_PROJECTION;
1592                 (true, true)
1593             } else {
1594                 (false, self.locals_are_invalidated_at_exit)
1595             };
1596
1597         if !will_be_dropped {
1598             debug!("place_is_invalidated_at_exit({:?}) - won't be dropped", place);
1599             return;
1600         }
1601
1602         let sd = if might_be_alive { Deep } else { Shallow(None) };
1603
1604         if places_conflict::borrow_conflicts_with_place(
1605             self.infcx.tcx,
1606             &self.body,
1607             place,
1608             borrow.kind,
1609             root_place,
1610             sd,
1611             places_conflict::PlaceConflictBias::Overlap,
1612         ) {
1613             debug!("check_for_invalidation_at_exit({:?}): INVALID", place);
1614             // FIXME: should be talking about the region lifetime instead
1615             // of just a span here.
1616             let span = self.infcx.tcx.sess.source_map().end_point(span);
1617             self.report_borrowed_value_does_not_live_long_enough(
1618                 location,
1619                 borrow,
1620                 (place, span),
1621                 None,
1622             )
1623         }
1624     }
1625
1626     /// Reports an error if this is a borrow of local data.
1627     /// This is called for all Yield expressions on movable generators
1628     fn check_for_local_borrow(&mut self, borrow: &BorrowData<'tcx>, yield_span: Span) {
1629         debug!("check_for_local_borrow({:?})", borrow);
1630
1631         if borrow_of_local_data(borrow.borrowed_place) {
1632             let err = self.cannot_borrow_across_generator_yield(
1633                 self.retrieve_borrow_spans(borrow).var_or_use(),
1634                 yield_span,
1635             );
1636
1637             err.buffer(&mut self.errors_buffer);
1638         }
1639     }
1640
1641     fn check_activations(&mut self, location: Location, span: Span, flow_state: &Flows<'cx, 'tcx>) {
1642         // Two-phase borrow support: For each activation that is newly
1643         // generated at this statement, check if it interferes with
1644         // another borrow.
1645         let borrow_set = self.borrow_set.clone();
1646         for &borrow_index in borrow_set.activations_at_location(location) {
1647             let borrow = &borrow_set[borrow_index];
1648
1649             // only mutable borrows should be 2-phase
1650             assert!(match borrow.kind {
1651                 BorrowKind::Shared | BorrowKind::Shallow => false,
1652                 BorrowKind::Unique | BorrowKind::Mut { .. } => true,
1653             });
1654
1655             self.access_place(
1656                 location,
1657                 (borrow.borrowed_place, span),
1658                 (Deep, Activation(WriteKind::MutableBorrow(borrow.kind), borrow_index)),
1659                 LocalMutationIsAllowed::No,
1660                 flow_state,
1661             );
1662             // We do not need to call `check_if_path_or_subpath_is_moved`
1663             // again, as we already called it when we made the
1664             // initial reservation.
1665         }
1666     }
1667
1668     fn check_if_reassignment_to_immutable_state(
1669         &mut self,
1670         location: Location,
1671         local: Local,
1672         place_span: (Place<'tcx>, Span),
1673         flow_state: &Flows<'cx, 'tcx>,
1674     ) {
1675         debug!("check_if_reassignment_to_immutable_state({:?})", local);
1676
1677         // Check if any of the initializiations of `local` have happened yet:
1678         if let Some(init_index) = self.is_local_ever_initialized(local, flow_state) {
1679             // And, if so, report an error.
1680             let init = &self.move_data.inits[init_index];
1681             let span = init.span(&self.body);
1682             self.report_illegal_reassignment(location, place_span, span, place_span.0);
1683         }
1684     }
1685
1686     fn check_if_full_path_is_moved(
1687         &mut self,
1688         location: Location,
1689         desired_action: InitializationRequiringAction,
1690         place_span: (PlaceRef<'tcx>, Span),
1691         flow_state: &Flows<'cx, 'tcx>,
1692     ) {
1693         let maybe_uninits = &flow_state.uninits;
1694
1695         // Bad scenarios:
1696         //
1697         // 1. Move of `a.b.c`, use of `a.b.c`
1698         // 2. Move of `a.b.c`, use of `a.b.c.d` (without first reinitializing `a.b.c.d`)
1699         // 3. Uninitialized `(a.b.c: &_)`, use of `*a.b.c`; note that with
1700         //    partial initialization support, one might have `a.x`
1701         //    initialized but not `a.b`.
1702         //
1703         // OK scenarios:
1704         //
1705         // 4. Move of `a.b.c`, use of `a.b.d`
1706         // 5. Uninitialized `a.x`, initialized `a.b`, use of `a.b`
1707         // 6. Copied `(a.b: &_)`, use of `*(a.b).c`; note that `a.b`
1708         //    must have been initialized for the use to be sound.
1709         // 7. Move of `a.b.c` then reinit of `a.b.c.d`, use of `a.b.c.d`
1710
1711         // The dataflow tracks shallow prefixes distinctly (that is,
1712         // field-accesses on P distinctly from P itself), in order to
1713         // track substructure initialization separately from the whole
1714         // structure.
1715         //
1716         // E.g., when looking at (*a.b.c).d, if the closest prefix for
1717         // which we have a MovePath is `a.b`, then that means that the
1718         // initialization state of `a.b` is all we need to inspect to
1719         // know if `a.b.c` is valid (and from that we infer that the
1720         // dereference and `.d` access is also valid, since we assume
1721         // `a.b.c` is assigned a reference to an initialized and
1722         // well-formed record structure.)
1723
1724         // Therefore, if we seek out the *closest* prefix for which we
1725         // have a MovePath, that should capture the initialization
1726         // state for the place scenario.
1727         //
1728         // This code covers scenarios 1, 2, and 3.
1729
1730         debug!("check_if_full_path_is_moved place: {:?}", place_span.0);
1731         let (prefix, mpi) = self.move_path_closest_to(place_span.0);
1732         if maybe_uninits.contains(mpi) {
1733             self.report_use_of_moved_or_uninitialized(
1734                 location,
1735                 desired_action,
1736                 (prefix, place_span.0, place_span.1),
1737                 mpi,
1738             );
1739         } // Only query longest prefix with a MovePath, not further
1740         // ancestors; dataflow recurs on children when parents
1741         // move (to support partial (re)inits).
1742         //
1743         // (I.e., querying parents breaks scenario 7; but may want
1744         // to do such a query based on partial-init feature-gate.)
1745     }
1746
1747     /// Subslices correspond to multiple move paths, so we iterate through the
1748     /// elements of the base array. For each element we check
1749     ///
1750     /// * Does this element overlap with our slice.
1751     /// * Is any part of it uninitialized.
1752     fn check_if_subslice_element_is_moved(
1753         &mut self,
1754         location: Location,
1755         desired_action: InitializationRequiringAction,
1756         place_span: (PlaceRef<'tcx>, Span),
1757         maybe_uninits: &BitSet<MovePathIndex>,
1758         from: u64,
1759         to: u64,
1760     ) {
1761         if let Some(mpi) = self.move_path_for_place(place_span.0) {
1762             let move_paths = &self.move_data.move_paths;
1763
1764             let root_path = &move_paths[mpi];
1765             for (child_mpi, child_move_path) in root_path.children(move_paths) {
1766                 let last_proj = child_move_path.place.projection.last().unwrap();
1767                 if let ProjectionElem::ConstantIndex { offset, from_end, .. } = last_proj {
1768                     debug_assert!(!from_end, "Array constant indexing shouldn't be `from_end`.");
1769
1770                     if (from..to).contains(offset) {
1771                         let uninit_child =
1772                             self.move_data.find_in_move_path_or_its_descendants(child_mpi, |mpi| {
1773                                 maybe_uninits.contains(mpi)
1774                             });
1775
1776                         if let Some(uninit_child) = uninit_child {
1777                             self.report_use_of_moved_or_uninitialized(
1778                                 location,
1779                                 desired_action,
1780                                 (place_span.0, place_span.0, place_span.1),
1781                                 uninit_child,
1782                             );
1783                             return; // don't bother finding other problems.
1784                         }
1785                     }
1786                 }
1787             }
1788         }
1789     }
1790
1791     fn check_if_path_or_subpath_is_moved(
1792         &mut self,
1793         location: Location,
1794         desired_action: InitializationRequiringAction,
1795         place_span: (PlaceRef<'tcx>, Span),
1796         flow_state: &Flows<'cx, 'tcx>,
1797     ) {
1798         let maybe_uninits = &flow_state.uninits;
1799
1800         // Bad scenarios:
1801         //
1802         // 1. Move of `a.b.c`, use of `a` or `a.b`
1803         //    partial initialization support, one might have `a.x`
1804         //    initialized but not `a.b`.
1805         // 2. All bad scenarios from `check_if_full_path_is_moved`
1806         //
1807         // OK scenarios:
1808         //
1809         // 3. Move of `a.b.c`, use of `a.b.d`
1810         // 4. Uninitialized `a.x`, initialized `a.b`, use of `a.b`
1811         // 5. Copied `(a.b: &_)`, use of `*(a.b).c`; note that `a.b`
1812         //    must have been initialized for the use to be sound.
1813         // 6. Move of `a.b.c` then reinit of `a.b.c.d`, use of `a.b.c.d`
1814
1815         self.check_if_full_path_is_moved(location, desired_action, place_span, flow_state);
1816
1817         if let Some((place_base, ProjectionElem::Subslice { from, to, from_end: false })) =
1818             place_span.0.last_projection()
1819         {
1820             let place_ty = place_base.ty(self.body(), self.infcx.tcx);
1821             if let ty::Array(..) = place_ty.ty.kind() {
1822                 self.check_if_subslice_element_is_moved(
1823                     location,
1824                     desired_action,
1825                     (place_base, place_span.1),
1826                     maybe_uninits,
1827                     from,
1828                     to,
1829                 );
1830                 return;
1831             }
1832         }
1833
1834         // A move of any shallow suffix of `place` also interferes
1835         // with an attempt to use `place`. This is scenario 3 above.
1836         //
1837         // (Distinct from handling of scenarios 1+2+4 above because
1838         // `place` does not interfere with suffixes of its prefixes,
1839         // e.g., `a.b.c` does not interfere with `a.b.d`)
1840         //
1841         // This code covers scenario 1.
1842
1843         debug!("check_if_path_or_subpath_is_moved place: {:?}", place_span.0);
1844         if let Some(mpi) = self.move_path_for_place(place_span.0) {
1845             let uninit_mpi = self
1846                 .move_data
1847                 .find_in_move_path_or_its_descendants(mpi, |mpi| maybe_uninits.contains(mpi));
1848
1849             if let Some(uninit_mpi) = uninit_mpi {
1850                 self.report_use_of_moved_or_uninitialized(
1851                     location,
1852                     desired_action,
1853                     (place_span.0, place_span.0, place_span.1),
1854                     uninit_mpi,
1855                 );
1856                 return; // don't bother finding other problems.
1857             }
1858         }
1859     }
1860
1861     /// Currently MoveData does not store entries for all places in
1862     /// the input MIR. For example it will currently filter out
1863     /// places that are Copy; thus we do not track places of shared
1864     /// reference type. This routine will walk up a place along its
1865     /// prefixes, searching for a foundational place that *is*
1866     /// tracked in the MoveData.
1867     ///
1868     /// An Err result includes a tag indicated why the search failed.
1869     /// Currently this can only occur if the place is built off of a
1870     /// static variable, as we do not track those in the MoveData.
1871     fn move_path_closest_to(&mut self, place: PlaceRef<'tcx>) -> (PlaceRef<'tcx>, MovePathIndex) {
1872         match self.move_data.rev_lookup.find(place) {
1873             LookupResult::Parent(Some(mpi)) | LookupResult::Exact(mpi) => {
1874                 (self.move_data.move_paths[mpi].place.as_ref(), mpi)
1875             }
1876             LookupResult::Parent(None) => panic!("should have move path for every Local"),
1877         }
1878     }
1879
1880     fn move_path_for_place(&mut self, place: PlaceRef<'tcx>) -> Option<MovePathIndex> {
1881         // If returns None, then there is no move path corresponding
1882         // to a direct owner of `place` (which means there is nothing
1883         // that borrowck tracks for its analysis).
1884
1885         match self.move_data.rev_lookup.find(place) {
1886             LookupResult::Parent(_) => None,
1887             LookupResult::Exact(mpi) => Some(mpi),
1888         }
1889     }
1890
1891     fn check_if_assigned_path_is_moved(
1892         &mut self,
1893         location: Location,
1894         (place, span): (Place<'tcx>, Span),
1895         flow_state: &Flows<'cx, 'tcx>,
1896     ) {
1897         debug!("check_if_assigned_path_is_moved place: {:?}", place);
1898
1899         // None case => assigning to `x` does not require `x` be initialized.
1900         for (place_base, elem) in place.iter_projections().rev() {
1901             match elem {
1902                 ProjectionElem::Index(_/*operand*/) |
1903                 ProjectionElem::ConstantIndex { .. } |
1904                 // assigning to P[i] requires P to be valid.
1905                 ProjectionElem::Downcast(_/*adt_def*/, _/*variant_idx*/) =>
1906                 // assigning to (P->variant) is okay if assigning to `P` is okay
1907                 //
1908                 // FIXME: is this true even if P is an adt with a dtor?
1909                 { }
1910
1911                 // assigning to (*P) requires P to be initialized
1912                 ProjectionElem::Deref => {
1913                     self.check_if_full_path_is_moved(
1914                         location, InitializationRequiringAction::Use,
1915                         (place_base, span), flow_state);
1916                     // (base initialized; no need to
1917                     // recur further)
1918                     break;
1919                 }
1920
1921                 ProjectionElem::Subslice { .. } => {
1922                     panic!("we don't allow assignments to subslices, location: {:?}",
1923                            location);
1924                 }
1925
1926                 ProjectionElem::Field(..) => {
1927                     // if type of `P` has a dtor, then
1928                     // assigning to `P.f` requires `P` itself
1929                     // be already initialized
1930                     let tcx = self.infcx.tcx;
1931                     let base_ty = place_base.ty(self.body(), tcx).ty;
1932                     match base_ty.kind() {
1933                         ty::Adt(def, _) if def.has_dtor(tcx) => {
1934                             self.check_if_path_or_subpath_is_moved(
1935                                 location, InitializationRequiringAction::Assignment,
1936                                 (place_base, span), flow_state);
1937
1938                             // (base initialized; no need to
1939                             // recur further)
1940                             break;
1941                         }
1942
1943                         // Once `let s; s.x = V; read(s.x);`,
1944                         // is allowed, remove this match arm.
1945                         ty::Adt(..) | ty::Tuple(..) => {
1946                             check_parent_of_field(self, location, place_base, span, flow_state);
1947
1948                             // rust-lang/rust#21232, #54499, #54986: during period where we reject
1949                             // partial initialization, do not complain about unnecessary `mut` on
1950                             // an attempt to do a partial initialization.
1951                             self.used_mut.insert(place.local);
1952                         }
1953
1954                         _ => {}
1955                     }
1956                 }
1957             }
1958         }
1959
1960         fn check_parent_of_field<'cx, 'tcx>(
1961             this: &mut MirBorrowckCtxt<'cx, 'tcx>,
1962             location: Location,
1963             base: PlaceRef<'tcx>,
1964             span: Span,
1965             flow_state: &Flows<'cx, 'tcx>,
1966         ) {
1967             // rust-lang/rust#21232: Until Rust allows reads from the
1968             // initialized parts of partially initialized structs, we
1969             // will, starting with the 2018 edition, reject attempts
1970             // to write to structs that are not fully initialized.
1971             //
1972             // In other words, *until* we allow this:
1973             //
1974             // 1. `let mut s; s.x = Val; read(s.x);`
1975             //
1976             // we will for now disallow this:
1977             //
1978             // 2. `let mut s; s.x = Val;`
1979             //
1980             // and also this:
1981             //
1982             // 3. `let mut s = ...; drop(s); s.x=Val;`
1983             //
1984             // This does not use check_if_path_or_subpath_is_moved,
1985             // because we want to *allow* reinitializations of fields:
1986             // e.g., want to allow
1987             //
1988             // `let mut s = ...; drop(s.x); s.x=Val;`
1989             //
1990             // This does not use check_if_full_path_is_moved on
1991             // `base`, because that would report an error about the
1992             // `base` as a whole, but in this scenario we *really*
1993             // want to report an error about the actual thing that was
1994             // moved, which may be some prefix of `base`.
1995
1996             // Shallow so that we'll stop at any dereference; we'll
1997             // report errors about issues with such bases elsewhere.
1998             let maybe_uninits = &flow_state.uninits;
1999
2000             // Find the shortest uninitialized prefix you can reach
2001             // without going over a Deref.
2002             let mut shortest_uninit_seen = None;
2003             for prefix in this.prefixes(base, PrefixSet::Shallow) {
2004                 let mpi = match this.move_path_for_place(prefix) {
2005                     Some(mpi) => mpi,
2006                     None => continue,
2007                 };
2008
2009                 if maybe_uninits.contains(mpi) {
2010                     debug!(
2011                         "check_parent_of_field updating shortest_uninit_seen from {:?} to {:?}",
2012                         shortest_uninit_seen,
2013                         Some((prefix, mpi))
2014                     );
2015                     shortest_uninit_seen = Some((prefix, mpi));
2016                 } else {
2017                     debug!("check_parent_of_field {:?} is definitely initialized", (prefix, mpi));
2018                 }
2019             }
2020
2021             if let Some((prefix, mpi)) = shortest_uninit_seen {
2022                 // Check for a reassignment into an uninitialized field of a union (for example,
2023                 // after a move out). In this case, do not report an error here. There is an
2024                 // exception, if this is the first assignment into the union (that is, there is
2025                 // no move out from an earlier location) then this is an attempt at initialization
2026                 // of the union - we should error in that case.
2027                 let tcx = this.infcx.tcx;
2028                 if base.ty(this.body(), tcx).ty.is_union() {
2029                     if this.move_data.path_map[mpi].iter().any(|moi| {
2030                         this.move_data.moves[*moi].source.is_predecessor_of(location, this.body)
2031                     }) {
2032                         return;
2033                     }
2034                 }
2035
2036                 this.report_use_of_moved_or_uninitialized(
2037                     location,
2038                     InitializationRequiringAction::PartialAssignment,
2039                     (prefix, base, span),
2040                     mpi,
2041                 );
2042             }
2043         }
2044     }
2045
2046     /// Checks the permissions for the given place and read or write kind
2047     ///
2048     /// Returns `true` if an error is reported.
2049     fn check_access_permissions(
2050         &mut self,
2051         (place, span): (Place<'tcx>, Span),
2052         kind: ReadOrWrite,
2053         is_local_mutation_allowed: LocalMutationIsAllowed,
2054         flow_state: &Flows<'cx, 'tcx>,
2055         location: Location,
2056     ) -> bool {
2057         debug!(
2058             "check_access_permissions({:?}, {:?}, is_local_mutation_allowed: {:?})",
2059             place, kind, is_local_mutation_allowed
2060         );
2061
2062         let error_access;
2063         let the_place_err;
2064
2065         match kind {
2066             Reservation(WriteKind::MutableBorrow(
2067                 borrow_kind @ (BorrowKind::Unique | BorrowKind::Mut { .. }),
2068             ))
2069             | Write(WriteKind::MutableBorrow(
2070                 borrow_kind @ (BorrowKind::Unique | BorrowKind::Mut { .. }),
2071             )) => {
2072                 let is_local_mutation_allowed = match borrow_kind {
2073                     BorrowKind::Unique => LocalMutationIsAllowed::Yes,
2074                     BorrowKind::Mut { .. } => is_local_mutation_allowed,
2075                     BorrowKind::Shared | BorrowKind::Shallow => unreachable!(),
2076                 };
2077                 match self.is_mutable(place.as_ref(), is_local_mutation_allowed) {
2078                     Ok(root_place) => {
2079                         self.add_used_mut(root_place, flow_state);
2080                         return false;
2081                     }
2082                     Err(place_err) => {
2083                         error_access = AccessKind::MutableBorrow;
2084                         the_place_err = place_err;
2085                     }
2086                 }
2087             }
2088             Reservation(WriteKind::Mutate) | Write(WriteKind::Mutate) => {
2089                 match self.is_mutable(place.as_ref(), is_local_mutation_allowed) {
2090                     Ok(root_place) => {
2091                         self.add_used_mut(root_place, flow_state);
2092                         return false;
2093                     }
2094                     Err(place_err) => {
2095                         error_access = AccessKind::Mutate;
2096                         the_place_err = place_err;
2097                     }
2098                 }
2099             }
2100
2101             Reservation(
2102                 WriteKind::Move
2103                 | WriteKind::StorageDeadOrDrop
2104                 | WriteKind::MutableBorrow(BorrowKind::Shared)
2105                 | WriteKind::MutableBorrow(BorrowKind::Shallow),
2106             )
2107             | Write(
2108                 WriteKind::Move
2109                 | WriteKind::StorageDeadOrDrop
2110                 | WriteKind::MutableBorrow(BorrowKind::Shared)
2111                 | WriteKind::MutableBorrow(BorrowKind::Shallow),
2112             ) => {
2113                 if let (Err(_), true) = (
2114                     self.is_mutable(place.as_ref(), is_local_mutation_allowed),
2115                     self.errors_buffer.is_empty(),
2116                 ) {
2117                     // rust-lang/rust#46908: In pure NLL mode this code path should be
2118                     // unreachable, but we use `delay_span_bug` because we can hit this when
2119                     // dereferencing a non-Copy raw pointer *and* have `-Ztreat-err-as-bug`
2120                     // enabled. We don't want to ICE for that case, as other errors will have
2121                     // been emitted (#52262).
2122                     self.infcx.tcx.sess.delay_span_bug(
2123                         span,
2124                         &format!(
2125                             "Accessing `{:?}` with the kind `{:?}` shouldn't be possible",
2126                             place, kind,
2127                         ),
2128                     );
2129                 }
2130                 return false;
2131             }
2132             Activation(..) => {
2133                 // permission checks are done at Reservation point.
2134                 return false;
2135             }
2136             Read(
2137                 ReadKind::Borrow(
2138                     BorrowKind::Unique
2139                     | BorrowKind::Mut { .. }
2140                     | BorrowKind::Shared
2141                     | BorrowKind::Shallow,
2142                 )
2143                 | ReadKind::Copy,
2144             ) => {
2145                 // Access authorized
2146                 return false;
2147             }
2148         }
2149
2150         // rust-lang/rust#21232, #54986: during period where we reject
2151         // partial initialization, do not complain about mutability
2152         // errors except for actual mutation (as opposed to an attempt
2153         // to do a partial initialization).
2154         let previously_initialized =
2155             self.is_local_ever_initialized(place.local, flow_state).is_some();
2156
2157         // at this point, we have set up the error reporting state.
2158         if previously_initialized {
2159             self.report_mutability_error(place, span, the_place_err, error_access, location);
2160             true
2161         } else {
2162             false
2163         }
2164     }
2165
2166     fn is_local_ever_initialized(
2167         &self,
2168         local: Local,
2169         flow_state: &Flows<'cx, 'tcx>,
2170     ) -> Option<InitIndex> {
2171         let mpi = self.move_data.rev_lookup.find_local(local);
2172         let ii = &self.move_data.init_path_map[mpi];
2173         for &index in ii {
2174             if flow_state.ever_inits.contains(index) {
2175                 return Some(index);
2176             }
2177         }
2178         None
2179     }
2180
2181     /// Adds the place into the used mutable variables set
2182     fn add_used_mut(&mut self, root_place: RootPlace<'tcx>, flow_state: &Flows<'cx, 'tcx>) {
2183         match root_place {
2184             RootPlace { place_local: local, place_projection: [], is_local_mutation_allowed } => {
2185                 // If the local may have been initialized, and it is now currently being
2186                 // mutated, then it is justified to be annotated with the `mut`
2187                 // keyword, since the mutation may be a possible reassignment.
2188                 if is_local_mutation_allowed != LocalMutationIsAllowed::Yes
2189                     && self.is_local_ever_initialized(local, flow_state).is_some()
2190                 {
2191                     self.used_mut.insert(local);
2192                 }
2193             }
2194             RootPlace {
2195                 place_local: _,
2196                 place_projection: _,
2197                 is_local_mutation_allowed: LocalMutationIsAllowed::Yes,
2198             } => {}
2199             RootPlace {
2200                 place_local,
2201                 place_projection: place_projection @ [.., _],
2202                 is_local_mutation_allowed: _,
2203             } => {
2204                 if let Some(field) = self.is_upvar_field_projection(PlaceRef {
2205                     local: place_local,
2206                     projection: place_projection,
2207                 }) {
2208                     self.used_mut_upvars.push(field);
2209                 }
2210             }
2211         }
2212     }
2213
2214     /// Whether this value can be written or borrowed mutably.
2215     /// Returns the root place if the place passed in is a projection.
2216     fn is_mutable(
2217         &self,
2218         place: PlaceRef<'tcx>,
2219         is_local_mutation_allowed: LocalMutationIsAllowed,
2220     ) -> Result<RootPlace<'tcx>, PlaceRef<'tcx>> {
2221         debug!("is_mutable: place={:?}, is_local...={:?}", place, is_local_mutation_allowed);
2222         match place.last_projection() {
2223             None => {
2224                 let local = &self.body.local_decls[place.local];
2225                 match local.mutability {
2226                     Mutability::Not => match is_local_mutation_allowed {
2227                         LocalMutationIsAllowed::Yes => Ok(RootPlace {
2228                             place_local: place.local,
2229                             place_projection: place.projection,
2230                             is_local_mutation_allowed: LocalMutationIsAllowed::Yes,
2231                         }),
2232                         LocalMutationIsAllowed::ExceptUpvars => Ok(RootPlace {
2233                             place_local: place.local,
2234                             place_projection: place.projection,
2235                             is_local_mutation_allowed: LocalMutationIsAllowed::ExceptUpvars,
2236                         }),
2237                         LocalMutationIsAllowed::No => Err(place),
2238                     },
2239                     Mutability::Mut => Ok(RootPlace {
2240                         place_local: place.local,
2241                         place_projection: place.projection,
2242                         is_local_mutation_allowed,
2243                     }),
2244                 }
2245             }
2246             Some((place_base, elem)) => {
2247                 match elem {
2248                     ProjectionElem::Deref => {
2249                         let base_ty = place_base.ty(self.body(), self.infcx.tcx).ty;
2250
2251                         // Check the kind of deref to decide
2252                         match base_ty.kind() {
2253                             ty::Ref(_, _, mutbl) => {
2254                                 match mutbl {
2255                                     // Shared borrowed data is never mutable
2256                                     hir::Mutability::Not => Err(place),
2257                                     // Mutably borrowed data is mutable, but only if we have a
2258                                     // unique path to the `&mut`
2259                                     hir::Mutability::Mut => {
2260                                         let mode = match self.is_upvar_field_projection(place) {
2261                                             Some(field) if self.upvars[field.index()].by_ref => {
2262                                                 is_local_mutation_allowed
2263                                             }
2264                                             _ => LocalMutationIsAllowed::Yes,
2265                                         };
2266
2267                                         self.is_mutable(place_base, mode)
2268                                     }
2269                                 }
2270                             }
2271                             ty::RawPtr(tnm) => {
2272                                 match tnm.mutbl {
2273                                     // `*const` raw pointers are not mutable
2274                                     hir::Mutability::Not => Err(place),
2275                                     // `*mut` raw pointers are always mutable, regardless of
2276                                     // context. The users have to check by themselves.
2277                                     hir::Mutability::Mut => Ok(RootPlace {
2278                                         place_local: place.local,
2279                                         place_projection: place.projection,
2280                                         is_local_mutation_allowed,
2281                                     }),
2282                                 }
2283                             }
2284                             // `Box<T>` owns its content, so mutable if its location is mutable
2285                             _ if base_ty.is_box() => {
2286                                 self.is_mutable(place_base, is_local_mutation_allowed)
2287                             }
2288                             // Deref should only be for reference, pointers or boxes
2289                             _ => bug!("Deref of unexpected type: {:?}", base_ty),
2290                         }
2291                     }
2292                     // All other projections are owned by their base path, so mutable if
2293                     // base path is mutable
2294                     ProjectionElem::Field(..)
2295                     | ProjectionElem::Index(..)
2296                     | ProjectionElem::ConstantIndex { .. }
2297                     | ProjectionElem::Subslice { .. }
2298                     | ProjectionElem::Downcast(..) => {
2299                         let upvar_field_projection = self.is_upvar_field_projection(place);
2300                         if let Some(field) = upvar_field_projection {
2301                             let upvar = &self.upvars[field.index()];
2302                             debug!(
2303                                 "is_mutable: upvar.mutability={:?} local_mutation_is_allowed={:?} \
2304                                  place={:?}, place_base={:?}",
2305                                 upvar, is_local_mutation_allowed, place, place_base
2306                             );
2307                             match (upvar.place.mutability, is_local_mutation_allowed) {
2308                                 (
2309                                     Mutability::Not,
2310                                     LocalMutationIsAllowed::No
2311                                     | LocalMutationIsAllowed::ExceptUpvars,
2312                                 ) => Err(place),
2313                                 (Mutability::Not, LocalMutationIsAllowed::Yes)
2314                                 | (Mutability::Mut, _) => {
2315                                     // Subtle: this is an upvar
2316                                     // reference, so it looks like
2317                                     // `self.foo` -- we want to double
2318                                     // check that the location `*self`
2319                                     // is mutable (i.e., this is not a
2320                                     // `Fn` closure).  But if that
2321                                     // check succeeds, we want to
2322                                     // *blame* the mutability on
2323                                     // `place` (that is,
2324                                     // `self.foo`). This is used to
2325                                     // propagate the info about
2326                                     // whether mutability declarations
2327                                     // are used outwards, so that we register
2328                                     // the outer variable as mutable. Otherwise a
2329                                     // test like this fails to record the `mut`
2330                                     // as needed:
2331                                     //
2332                                     // ```
2333                                     // fn foo<F: FnOnce()>(_f: F) { }
2334                                     // fn main() {
2335                                     //     let var = Vec::new();
2336                                     //     foo(move || {
2337                                     //         var.push(1);
2338                                     //     });
2339                                     // }
2340                                     // ```
2341                                     let _ =
2342                                         self.is_mutable(place_base, is_local_mutation_allowed)?;
2343                                     Ok(RootPlace {
2344                                         place_local: place.local,
2345                                         place_projection: place.projection,
2346                                         is_local_mutation_allowed,
2347                                     })
2348                                 }
2349                             }
2350                         } else {
2351                             self.is_mutable(place_base, is_local_mutation_allowed)
2352                         }
2353                     }
2354                 }
2355             }
2356         }
2357     }
2358
2359     /// If `place` is a field projection, and the field is being projected from a closure type,
2360     /// then returns the index of the field being projected. Note that this closure will always
2361     /// be `self` in the current MIR, because that is the only time we directly access the fields
2362     /// of a closure type.
2363     fn is_upvar_field_projection(&self, place_ref: PlaceRef<'tcx>) -> Option<Field> {
2364         path_utils::is_upvar_field_projection(self.infcx.tcx, &self.upvars, place_ref, self.body())
2365     }
2366 }
2367
2368 /// The degree of overlap between 2 places for borrow-checking.
2369 enum Overlap {
2370     /// The places might partially overlap - in this case, we give
2371     /// up and say that they might conflict. This occurs when
2372     /// different fields of a union are borrowed. For example,
2373     /// if `u` is a union, we have no way of telling how disjoint
2374     /// `u.a.x` and `a.b.y` are.
2375     Arbitrary,
2376     /// The places have the same type, and are either completely disjoint
2377     /// or equal - i.e., they can't "partially" overlap as can occur with
2378     /// unions. This is the "base case" on which we recur for extensions
2379     /// of the place.
2380     EqualOrDisjoint,
2381     /// The places are disjoint, so we know all extensions of them
2382     /// will also be disjoint.
2383     Disjoint,
2384 }