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