1 //! This query borrow-checks the MIR to (further) ensure it is not broken.
3 #![feature(bool_to_option)]
4 #![feature(box_patterns)]
5 #![feature(crate_visibility_modifier)]
7 #![feature(min_specialization)]
8 #![feature(stmt_expr_attributes)]
9 #![feature(trusted_step)]
10 #![feature(try_blocks)]
11 #![recursion_limit = "256"]
14 extern crate rustc_middle;
18 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
19 use rustc_data_structures::graph::dominators::Dominators;
20 use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorReported};
22 use rustc_hir::def_id::LocalDefId;
24 use rustc_index::bit_set::BitSet;
25 use rustc_index::vec::IndexVec;
26 use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
27 use rustc_middle::mir::{
28 traversal, Body, ClearCrossCrate, Local, Location, Mutability, Operand, Place, PlaceElem,
29 PlaceRef, VarDebugInfoContents,
31 use rustc_middle::mir::{AggregateKind, BasicBlock, BorrowCheckResult, BorrowKind};
32 use rustc_middle::mir::{Field, ProjectionElem, Promoted, Rvalue, Statement, StatementKind};
33 use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
34 use rustc_middle::ty::query::Providers;
35 use rustc_middle::ty::{self, CapturedPlace, ParamEnv, RegionVid, TyCtxt};
36 use rustc_session::lint::builtin::{MUTABLE_BORROW_RESERVATION_CONFLICT, UNUSED_MUT};
37 use rustc_span::{Span, Symbol, DUMMY_SP};
40 use smallvec::SmallVec;
41 use std::cell::RefCell;
42 use std::collections::BTreeMap;
47 use rustc_mir_dataflow::impls::{
48 EverInitializedPlaces, MaybeInitializedPlaces, MaybeUninitializedPlaces,
50 use rustc_mir_dataflow::move_paths::{InitIndex, MoveOutIndex, MovePathIndex};
51 use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult, MoveData, MoveError};
52 use rustc_mir_dataflow::Analysis;
53 use rustc_mir_dataflow::MoveDataParamEnv;
55 use self::diagnostics::{AccessKind, RegionName};
56 use self::location::LocationTable;
57 use self::prefixes::PrefixSet;
58 use self::MutateMode::{JustWrite, WriteAndRead};
61 use self::path_utils::*;
65 mod constraint_generation;
73 mod member_constraints;
82 mod universal_regions;
85 // A public API provided for the Rust compiler consumers.
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;
95 // FIXME(eddyb) perhaps move this somewhere more centrally.
98 place: CapturedPlace<'tcx>,
100 /// If true, the capture is behind a reference.
104 const DEREF_PROJECTION: &[PlaceElem<'_>; 1] = &[ProjectionElem::Deref];
106 pub fn provide(providers: &mut Providers) {
107 *providers = Providers {
108 mir_borrowck: |tcx, did| {
109 if let Some(def) = ty::WithOptConstParam::try_lookup(did, tcx) {
110 tcx.mir_borrowck_const_arg(def)
112 mir_borrowck(tcx, ty::WithOptConstParam::unknown(did))
115 mir_borrowck_const_arg: |tcx, (did, param_did)| {
116 mir_borrowck(tcx, ty::WithOptConstParam { did, const_param_did: Some(param_did) })
122 fn mir_borrowck<'tcx>(
124 def: ty::WithOptConstParam<LocalDefId>,
125 ) -> &'tcx BorrowCheckResult<'tcx> {
126 let (input_body, promoted) = tcx.mir_promoted(def);
127 debug!("run query mir_borrowck: {}", tcx.def_path_str(def.did.to_def_id()));
129 let opt_closure_req = tcx.infer_ctxt().with_opaque_type_inference(def.did).enter(|infcx| {
130 let input_body: &Body<'_> = &input_body.borrow();
131 let promoted: &IndexVec<_, _> = &promoted.borrow();
132 do_mir_borrowck(&infcx, input_body, promoted, false).0
134 debug!("mir_borrowck done");
136 tcx.arena.alloc(opt_closure_req)
139 /// Perform the actual borrow checking.
141 /// If `return_body_with_facts` is true, then return the body with non-erased
142 /// region ids on which the borrow checking was performed together with Polonius
144 #[instrument(skip(infcx, input_body, input_promoted), level = "debug")]
145 fn do_mir_borrowck<'a, 'tcx>(
146 infcx: &InferCtxt<'a, 'tcx>,
147 input_body: &Body<'tcx>,
148 input_promoted: &IndexVec<Promoted, Body<'tcx>>,
149 return_body_with_facts: bool,
150 ) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
151 let def = input_body.source.with_opt_param().as_local().unwrap();
156 let param_env = tcx.param_env(def.did);
157 let id = tcx.hir().local_def_id_to_hir_id(def.did);
159 let mut local_names = IndexVec::from_elem(None, &input_body.local_decls);
160 for var_debug_info in &input_body.var_debug_info {
161 if let VarDebugInfoContents::Place(place) = var_debug_info.value {
162 if let Some(local) = place.as_local() {
163 if let Some(prev_name) = local_names[local] {
164 if var_debug_info.name != prev_name {
166 var_debug_info.source_info.span,
167 "local {:?} has many names (`{}` vs `{}`)",
174 local_names[local] = Some(var_debug_info.name);
179 // Gather the upvars of a closure, if any.
180 let tables = tcx.typeck_opt_const_arg(def);
181 if let Some(ErrorReported) = tables.tainted_by_errors {
182 infcx.set_tainted_by_errors();
184 let upvars: Vec<_> = tables
185 .closure_min_captures_flattened(def.did.to_def_id())
186 .map(|captured_place| {
187 let capture = captured_place.info.capture_kind;
188 let by_ref = match capture {
189 ty::UpvarCapture::ByValue(_) => false,
190 ty::UpvarCapture::ByRef(..) => true,
192 Upvar { place: captured_place.clone(), by_ref }
196 // Replace all regions with fresh inference variables. This
197 // requires first making our own copy of the MIR. This copy will
198 // be modified (in place) to contain non-lexical lifetimes. It
199 // will have a lifetime tied to the inference context.
200 let mut body_owned = input_body.clone();
201 let mut promoted = input_promoted.clone();
203 nll::replace_regions_in_mir(infcx, param_env, &mut body_owned, &mut promoted);
204 let body = &body_owned; // no further changes
206 let location_table_owned = LocationTable::new(body);
207 let location_table = &location_table_owned;
209 let mut errors_buffer = Vec::new();
210 let (move_data, move_errors): (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>) =
211 match MoveData::gather_moves(&body, tcx, param_env) {
212 Ok(move_data) => (move_data, Vec::new()),
213 Err((move_data, move_errors)) => (move_data, move_errors),
215 let promoted_errors = promoted
217 .map(|(idx, body)| (idx, MoveData::gather_moves(&body, tcx, param_env)));
219 let mdpe = MoveDataParamEnv { move_data, param_env };
221 let mut flow_inits = MaybeInitializedPlaces::new(tcx, &body, &mdpe)
222 .into_engine(tcx, &body)
223 .pass_name("borrowck")
224 .iterate_to_fixpoint()
225 .into_results_cursor(&body);
227 let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(id).is_fn_or_closure();
229 Rc::new(BorrowSet::build(tcx, body, locals_are_invalidated_at_exit, &mdpe.move_data));
231 let use_polonius = return_body_with_facts || infcx.tcx.sess.opts.debugging_opts.polonius;
233 // Compute non-lexical lifetimes.
241 } = nll::compute_regions(
255 // Dump MIR results into a file, if that is enabled. This let us
256 // write unit-tests, as well as helping with debugging.
257 nll::dump_mir_results(infcx, &body, ®ioncx, &opt_closure_req);
259 // We also have a `#[rustc_regions]` annotation that causes us to dump
261 nll::dump_annotation(
270 // The various `flow_*` structures can be large. We drop `flow_inits` here
271 // so it doesn't overlap with the others below. This reduces peak memory
272 // usage significantly on some benchmarks.
275 let regioncx = Rc::new(regioncx);
277 let flow_borrows = Borrows::new(tcx, body, ®ioncx, &borrow_set)
278 .into_engine(tcx, body)
279 .pass_name("borrowck")
280 .iterate_to_fixpoint();
281 let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &mdpe)
282 .into_engine(tcx, body)
283 .pass_name("borrowck")
284 .iterate_to_fixpoint();
285 let flow_ever_inits = EverInitializedPlaces::new(tcx, body, &mdpe)
286 .into_engine(tcx, body)
287 .pass_name("borrowck")
288 .iterate_to_fixpoint();
290 let movable_generator = !matches!(
292 Node::Expr(&hir::Expr {
293 kind: hir::ExprKind::Closure(.., Some(hir::Movability::Static)),
298 for (idx, move_data_results) in promoted_errors {
299 let promoted_body = &promoted[idx];
301 if let Err((move_data, move_errors)) = move_data_results {
302 let mut promoted_mbcx = MirBorrowckCtxt {
306 move_data: &move_data,
307 location_table, // no need to create a real one for the promoted, it is not used
309 fn_self_span_reported: Default::default(),
310 locals_are_invalidated_at_exit,
311 access_place_error_reported: Default::default(),
312 reservation_error_reported: Default::default(),
313 reservation_warnings: Default::default(),
314 move_error_reported: BTreeMap::new(),
315 uninitialized_error_reported: Default::default(),
317 regioncx: regioncx.clone(),
318 used_mut: Default::default(),
319 used_mut_upvars: SmallVec::new(),
320 borrow_set: Rc::clone(&borrow_set),
321 dominators: Dominators::dummy(), // not used
323 local_names: IndexVec::from_elem(None, &promoted_body.local_decls),
324 region_names: RefCell::default(),
325 next_region_name: RefCell::new(1),
326 polonius_output: None,
328 promoted_mbcx.report_move_errors(move_errors);
329 errors_buffer = promoted_mbcx.errors_buffer;
333 let dominators = body.dominators();
335 let mut mbcx = MirBorrowckCtxt {
339 move_data: &mdpe.move_data,
342 locals_are_invalidated_at_exit,
343 fn_self_span_reported: Default::default(),
344 access_place_error_reported: Default::default(),
345 reservation_error_reported: Default::default(),
346 reservation_warnings: Default::default(),
347 move_error_reported: BTreeMap::new(),
348 uninitialized_error_reported: Default::default(),
350 regioncx: Rc::clone(®ioncx),
351 used_mut: Default::default(),
352 used_mut_upvars: SmallVec::new(),
353 borrow_set: Rc::clone(&borrow_set),
357 region_names: RefCell::default(),
358 next_region_name: RefCell::new(1),
362 // Compute and report region errors, if any.
363 mbcx.report_region_errors(nll_errors);
365 let results = BorrowckResults {
366 ever_inits: flow_ever_inits,
367 uninits: flow_uninits,
368 borrows: flow_borrows,
371 mbcx.report_move_errors(move_errors);
373 rustc_mir_dataflow::visit_results(
375 traversal::reverse_postorder(body).map(|(bb, _)| bb),
380 // Convert any reservation warnings into lints.
381 let reservation_warnings = mem::take(&mut mbcx.reservation_warnings);
382 for (_, (place, span, location, bk, borrow)) in reservation_warnings {
383 let mut initial_diag = mbcx.report_conflicting_borrow(location, (place, span), bk, &borrow);
385 let scope = mbcx.body.source_info(location).scope;
386 let lint_root = match &mbcx.body.source_scopes[scope].local_data {
387 ClearCrossCrate::Set(data) => data.lint_root,
391 // Span and message don't matter; we overwrite them below anyway
392 mbcx.infcx.tcx.struct_span_lint_hir(
393 MUTABLE_BORROW_RESERVATION_CONFLICT,
397 let mut diag = lint.build("");
399 diag.message = initial_diag.styled_message().clone();
400 diag.span = initial_diag.span.clone();
402 diag.buffer(&mut mbcx.errors_buffer);
405 initial_diag.cancel();
408 // For each non-user used mutable variable, check if it's been assigned from
409 // a user-declared local. If so, then put that local into the used_mut set.
410 // Note that this set is expected to be small - only upvars from closures
411 // would have a chance of erroneously adding non-user-defined mutable vars
413 let temporary_used_locals: FxHashSet<Local> = mbcx
416 .filter(|&local| !mbcx.body.local_decls[*local].is_user_variable())
419 // For the remaining unused locals that are marked as mutable, we avoid linting any that
420 // were never initialized. These locals may have been removed as unreachable code; or will be
421 // linted as unused variables.
422 let unused_mut_locals =
423 mbcx.body.mut_vars_iter().filter(|local| !mbcx.used_mut.contains(local)).collect();
424 mbcx.gather_used_muts(temporary_used_locals, unused_mut_locals);
426 debug!("mbcx.used_mut: {:?}", mbcx.used_mut);
427 let used_mut = mbcx.used_mut;
428 for local in mbcx.body.mut_vars_and_args_iter().filter(|local| !used_mut.contains(local)) {
429 let local_decl = &mbcx.body.local_decls[local];
430 let lint_root = match &mbcx.body.source_scopes[local_decl.source_info.scope].local_data {
431 ClearCrossCrate::Set(data) => data.lint_root,
435 // Skip over locals that begin with an underscore or have no name
436 match mbcx.local_names[local] {
438 if name.as_str().starts_with('_') {
445 let span = local_decl.source_info.span;
446 if span.desugaring_kind().is_some() {
447 // If the `mut` arises as part of a desugaring, we should ignore it.
451 tcx.struct_span_lint_hir(UNUSED_MUT, lint_root, span, |lint| {
452 let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
453 lint.build("variable does not need to be mutable")
454 .span_suggestion_short(
458 Applicability::MachineApplicable,
464 // Buffer any move errors that we collected and de-duplicated.
465 for (_, (_, diag)) in mbcx.move_error_reported {
466 diag.buffer(&mut mbcx.errors_buffer);
469 if !mbcx.errors_buffer.is_empty() {
470 mbcx.errors_buffer.sort_by_key(|diag| diag.sort_span);
472 for diag in mbcx.errors_buffer.drain(..) {
473 mbcx.infcx.tcx.sess.diagnostic().emit_diagnostic(&diag);
477 let result = BorrowCheckResult {
478 concrete_opaque_types: opaque_type_values,
479 closure_requirements: opt_closure_req,
480 used_mut_upvars: mbcx.used_mut_upvars,
483 let body_with_facts = if return_body_with_facts {
484 let output_facts = mbcx.polonius_output.expect("Polonius output was not computed");
485 Some(Box::new(BodyWithBorrowckFacts {
487 input_facts: *polonius_input.expect("Polonius input facts were not generated"),
489 location_table: location_table_owned,
495 debug!("do_mir_borrowck: result = {:#?}", result);
497 (result, body_with_facts)
500 /// A `Body` with information computed by the borrow checker. This struct is
501 /// intended to be consumed by compiler consumers.
503 /// We need to include the MIR body here because the region identifiers must
504 /// match the ones in the Polonius facts.
505 pub struct BodyWithBorrowckFacts<'tcx> {
506 /// A mir body that contains region identifiers.
507 pub body: Body<'tcx>,
508 /// Polonius input facts.
509 pub input_facts: AllFacts,
510 /// Polonius output facts.
511 pub output_facts: Rc<self::nll::PoloniusOutput>,
512 /// The table that maps Polonius points to locations in the table.
513 pub location_table: LocationTable,
516 struct MirBorrowckCtxt<'cx, 'tcx> {
517 infcx: &'cx InferCtxt<'cx, 'tcx>,
518 param_env: ParamEnv<'tcx>,
519 body: &'cx Body<'tcx>,
520 move_data: &'cx MoveData<'tcx>,
522 /// Map from MIR `Location` to `LocationIndex`; created
523 /// when MIR borrowck begins.
524 location_table: &'cx LocationTable,
526 movable_generator: bool,
527 /// This keeps track of whether local variables are free-ed when the function
528 /// exits even without a `StorageDead`, which appears to be the case for
531 /// I'm not sure this is the right approach - @eddyb could you try and
533 locals_are_invalidated_at_exit: bool,
534 /// This field keeps track of when borrow errors are reported in the access_place function
535 /// so that there is no duplicate reporting. This field cannot also be used for the conflicting
536 /// borrow errors that is handled by the `reservation_error_reported` field as the inclusion
537 /// of the `Span` type (while required to mute some errors) stops the muting of the reservation
539 access_place_error_reported: FxHashSet<(Place<'tcx>, Span)>,
540 /// This field keeps track of when borrow conflict errors are reported
541 /// for reservations, so that we don't report seemingly duplicate
542 /// errors for corresponding activations.
544 // FIXME: ideally this would be a set of `BorrowIndex`, not `Place`s,
545 // but it is currently inconvenient to track down the `BorrowIndex`
546 // at the time we detect and report a reservation error.
547 reservation_error_reported: FxHashSet<Place<'tcx>>,
548 /// This fields keeps track of the `Span`s that we have
549 /// used to report extra information for `FnSelfUse`, to avoid
550 /// unnecessarily verbose errors.
551 fn_self_span_reported: FxHashSet<Span>,
552 /// Migration warnings to be reported for #56254. We delay reporting these
553 /// so that we can suppress the warning if there's a corresponding error
554 /// for the activation of the borrow.
555 reservation_warnings:
556 FxHashMap<BorrowIndex, (Place<'tcx>, Span, Location, BorrowKind, BorrowData<'tcx>)>,
557 /// This field keeps track of move errors that are to be reported for given move indices.
559 /// There are situations where many errors can be reported for a single move out (see #53807)
560 /// and we want only the best of those errors.
562 /// The `report_use_of_moved_or_uninitialized` function checks this map and replaces the
563 /// diagnostic (if there is one) if the `Place` of the error being reported is a prefix of the
564 /// `Place` of the previous most diagnostic. This happens instead of buffering the error. Once
565 /// all move errors have been reported, any diagnostics in this map are added to the buffer
568 /// `BTreeMap` is used to preserve the order of insertions when iterating. This is necessary
569 /// when errors in the map are being re-added to the error buffer so that errors with the
570 /// same primary span come out in a consistent order.
571 move_error_reported: BTreeMap<Vec<MoveOutIndex>, (PlaceRef<'tcx>, DiagnosticBuilder<'cx>)>,
572 /// This field keeps track of errors reported in the checking of uninitialized variables,
573 /// so that we don't report seemingly duplicate errors.
574 uninitialized_error_reported: FxHashSet<PlaceRef<'tcx>>,
575 /// Errors to be reported buffer
576 errors_buffer: Vec<Diagnostic>,
577 /// This field keeps track of all the local variables that are declared mut and are mutated.
578 /// Used for the warning issued by an unused mutable local variable.
579 used_mut: FxHashSet<Local>,
580 /// If the function we're checking is a closure, then we'll need to report back the list of
581 /// mutable upvars that have been used. This field keeps track of them.
582 used_mut_upvars: SmallVec<[Field; 8]>,
583 /// Region inference context. This contains the results from region inference and lets us e.g.
584 /// find out which CFG points are contained in each borrow region.
585 regioncx: Rc<RegionInferenceContext<'tcx>>,
587 /// The set of borrows extracted from the MIR
588 borrow_set: Rc<BorrowSet<'tcx>>,
590 /// Dominators for MIR
591 dominators: Dominators<BasicBlock>,
593 /// Information about upvars not necessarily preserved in types or MIR
594 upvars: Vec<Upvar<'tcx>>,
596 /// Names of local (user) variables (extracted from `var_debug_info`).
597 local_names: IndexVec<Local, Option<Symbol>>,
599 /// Record the region names generated for each region in the given
600 /// MIR def so that we can reuse them later in help/error messages.
601 region_names: RefCell<FxHashMap<RegionVid, RegionName>>,
603 /// The counter for generating new region names.
604 next_region_name: RefCell<usize>,
606 /// Results of Polonius analysis.
607 polonius_output: Option<Rc<PoloniusOutput>>,
611 // 1. assignments are always made to mutable locations (FIXME: does that still really go here?)
612 // 2. loans made in overlapping scopes do not conflict
613 // 3. assignments do not affect things loaned out as immutable
614 // 4. moves do not affect things loaned out in any way
615 impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tcx> {
616 type FlowState = Flows<'cx, 'tcx>;
618 fn visit_statement_before_primary_effect(
620 flow_state: &Flows<'cx, 'tcx>,
621 stmt: &'cx Statement<'tcx>,
624 debug!("MirBorrowckCtxt::process_statement({:?}, {:?}): {:?}", location, stmt, flow_state);
625 let span = stmt.source_info.span;
627 self.check_activations(location, span, flow_state);
630 StatementKind::Assign(box (lhs, ref rhs)) => {
631 self.consume_rvalue(location, (rhs, span), flow_state);
633 self.mutate_place(location, (*lhs, span), Shallow(None), JustWrite, flow_state);
635 StatementKind::FakeRead(box (_, ref place)) => {
636 // Read for match doesn't access any memory and is used to
637 // assert that a place is safe and live. So we don't have to
638 // do any checks here.
640 // FIXME: Remove check that the place is initialized. This is
641 // needed for now because matches don't have never patterns yet.
642 // So this is the only place we prevent
646 self.check_if_path_or_subpath_is_moved(
648 InitializationRequiringAction::Use,
649 (place.as_ref(), span),
653 StatementKind::SetDiscriminant { place, variant_index: _ } => {
654 self.mutate_place(location, (**place, span), Shallow(None), JustWrite, flow_state);
656 StatementKind::LlvmInlineAsm(ref asm) => {
657 for (o, output) in iter::zip(&asm.asm.outputs, &*asm.outputs) {
659 // FIXME(eddyb) indirect inline asm outputs should
660 // be encoded through MIR place derefs instead.
664 (Deep, Read(ReadKind::Copy)),
665 LocalMutationIsAllowed::No,
668 self.check_if_path_or_subpath_is_moved(
670 InitializationRequiringAction::Use,
671 (output.as_ref(), o.span),
678 if o.is_rw { Deep } else { Shallow(None) },
679 if o.is_rw { WriteAndRead } else { JustWrite },
684 for (_, input) in asm.inputs.iter() {
685 self.consume_operand(location, (input, span), flow_state);
689 StatementKind::CopyNonOverlapping(box rustc_middle::mir::CopyNonOverlapping {
694 "Unexpected CopyNonOverlapping, should only appear after lower_intrinsics",
698 | StatementKind::Coverage(..)
699 | StatementKind::AscribeUserType(..)
700 | StatementKind::Retag { .. }
701 | StatementKind::StorageLive(..) => {
702 // `Nop`, `AscribeUserType`, `Retag`, and `StorageLive` are irrelevant
705 StatementKind::StorageDead(local) => {
708 (Place::from(*local), span),
709 (Shallow(None), Write(WriteKind::StorageDeadOrDrop)),
710 LocalMutationIsAllowed::Yes,
717 fn visit_terminator_before_primary_effect(
719 flow_state: &Flows<'cx, 'tcx>,
720 term: &'cx Terminator<'tcx>,
723 debug!("MirBorrowckCtxt::process_terminator({:?}, {:?}): {:?}", loc, term, flow_state);
724 let span = term.source_info.span;
726 self.check_activations(loc, span, flow_state);
729 TerminatorKind::SwitchInt { ref discr, switch_ty: _, targets: _ } => {
730 self.consume_operand(loc, (discr, span), flow_state);
732 TerminatorKind::Drop { place, target: _, unwind: _ } => {
734 "visit_terminator_drop \
735 loc: {:?} term: {:?} place: {:?} span: {:?}",
736 loc, term, place, span
742 (AccessDepth::Drop, Write(WriteKind::StorageDeadOrDrop)),
743 LocalMutationIsAllowed::Yes,
747 TerminatorKind::DropAndReplace {
749 value: ref new_value,
753 self.mutate_place(loc, (drop_place, span), Deep, JustWrite, flow_state);
754 self.consume_operand(loc, (new_value, span), flow_state);
756 TerminatorKind::Call {
764 self.consume_operand(loc, (func, span), flow_state);
766 self.consume_operand(loc, (arg, span), flow_state);
768 if let Some((dest, _ /*bb*/)) = *destination {
769 self.mutate_place(loc, (dest, span), Deep, JustWrite, flow_state);
772 TerminatorKind::Assert { ref cond, expected: _, ref msg, target: _, cleanup: _ } => {
773 self.consume_operand(loc, (cond, span), flow_state);
774 use rustc_middle::mir::AssertKind;
775 if let AssertKind::BoundsCheck { ref len, ref index } = *msg {
776 self.consume_operand(loc, (len, span), flow_state);
777 self.consume_operand(loc, (index, span), flow_state);
781 TerminatorKind::Yield { ref value, resume: _, resume_arg, drop: _ } => {
782 self.consume_operand(loc, (value, span), flow_state);
783 self.mutate_place(loc, (resume_arg, span), Deep, JustWrite, flow_state);
786 TerminatorKind::InlineAsm {
796 InlineAsmOperand::In { reg: _, ref value } => {
797 self.consume_operand(loc, (value, span), flow_state);
799 InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
800 if let Some(place) = place {
810 InlineAsmOperand::InOut { reg: _, late: _, ref in_value, out_place } => {
811 self.consume_operand(loc, (in_value, span), flow_state);
812 if let Some(out_place) = out_place {
822 InlineAsmOperand::Const { value: _ }
823 | InlineAsmOperand::SymFn { value: _ }
824 | InlineAsmOperand::SymStatic { def_id: _ } => {}
829 TerminatorKind::Goto { target: _ }
830 | TerminatorKind::Abort
831 | TerminatorKind::Unreachable
832 | TerminatorKind::Resume
833 | TerminatorKind::Return
834 | TerminatorKind::GeneratorDrop
835 | TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }
836 | TerminatorKind::FalseUnwind { real_target: _, unwind: _ } => {
837 // no data used, thus irrelevant to borrowck
842 fn visit_terminator_after_primary_effect(
844 flow_state: &Flows<'cx, 'tcx>,
845 term: &'cx Terminator<'tcx>,
848 let span = term.source_info.span;
851 TerminatorKind::Yield { value: _, resume: _, resume_arg: _, drop: _ } => {
852 if self.movable_generator {
853 // Look for any active borrows to locals
854 let borrow_set = self.borrow_set.clone();
855 for i in flow_state.borrows.iter() {
856 let borrow = &borrow_set[i];
857 self.check_for_local_borrow(borrow, span);
862 TerminatorKind::Resume | TerminatorKind::Return | TerminatorKind::GeneratorDrop => {
863 // Returning from the function implicitly kills storage for all locals and statics.
864 // Often, the storage will already have been killed by an explicit
865 // StorageDead, but we don't always emit those (notably on unwind paths),
866 // so this "extra check" serves as a kind of backup.
867 let borrow_set = self.borrow_set.clone();
868 for i in flow_state.borrows.iter() {
869 let borrow = &borrow_set[i];
870 self.check_for_invalidation_at_exit(loc, borrow, span);
874 TerminatorKind::Abort
875 | TerminatorKind::Assert { .. }
876 | TerminatorKind::Call { .. }
877 | TerminatorKind::Drop { .. }
878 | TerminatorKind::DropAndReplace { .. }
879 | TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }
880 | TerminatorKind::FalseUnwind { real_target: _, unwind: _ }
881 | TerminatorKind::Goto { .. }
882 | TerminatorKind::SwitchInt { .. }
883 | TerminatorKind::Unreachable
884 | TerminatorKind::InlineAsm { .. } => {}
889 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
895 use self::AccessDepth::{Deep, Shallow};
896 use self::ReadOrWrite::{Activation, Read, Reservation, Write};
898 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
899 enum ArtificialField {
904 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
906 /// From the RFC: "A *shallow* access means that the immediate
907 /// fields reached at P are accessed, but references or pointers
908 /// found within are not dereferenced. Right now, the only access
909 /// that is shallow is an assignment like `x = ...;`, which would
910 /// be a *shallow write* of `x`."
911 Shallow(Option<ArtificialField>),
913 /// From the RFC: "A *deep* access means that all data reachable
914 /// through the given place may be invalidated or accesses by
918 /// Access is Deep only when there is a Drop implementation that
919 /// can reach the data behind the reference.
923 /// Kind of access to a value: read or write
924 /// (For informational purposes only)
925 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
927 /// From the RFC: "A *read* means that the existing data may be
928 /// read, but will not be changed."
931 /// From the RFC: "A *write* means that the data may be mutated to
932 /// new values or otherwise invalidated (for example, it could be
933 /// de-initialized, as in a move operation).
936 /// For two-phase borrows, we distinguish a reservation (which is treated
937 /// like a Read) from an activation (which is treated like a write), and
938 /// each of those is furthermore distinguished from Reads/Writes above.
939 Reservation(WriteKind),
940 Activation(WriteKind, BorrowIndex),
943 /// Kind of read access to a value
944 /// (For informational purposes only)
945 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
951 /// Kind of write access to a value
952 /// (For informational purposes only)
953 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
956 MutableBorrow(BorrowKind),
961 /// When checking permissions for a place access, this flag is used to indicate that an immutable
962 /// local place can be mutated.
964 // FIXME: @nikomatsakis suggested that this flag could be removed with the following modifications:
965 // - Merge `check_access_permissions()` and `check_if_reassignment_to_immutable_state()`.
966 // - Split `is_mutable()` into `is_assignable()` (can be directly assigned) and
967 // `is_declared_mutable()`.
968 // - Take flow state into consideration in `is_assignable()` for local variables.
969 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
970 enum LocalMutationIsAllowed {
972 /// We want use of immutable upvars to cause a "write to immutable upvar"
973 /// error, not an "reassignment" error.
978 #[derive(Copy, Clone, Debug)]
979 enum InitializationRequiringAction {
988 struct RootPlace<'tcx> {
990 place_projection: &'tcx [PlaceElem<'tcx>],
991 is_local_mutation_allowed: LocalMutationIsAllowed,
994 impl InitializationRequiringAction {
995 fn as_noun(self) -> &'static str {
997 InitializationRequiringAction::Update => "update",
998 InitializationRequiringAction::Borrow => "borrow",
999 InitializationRequiringAction::MatchOn => "use", // no good noun
1000 InitializationRequiringAction::Use => "use",
1001 InitializationRequiringAction::Assignment => "assign",
1002 InitializationRequiringAction::PartialAssignment => "assign to part",
1006 fn as_verb_in_past_tense(self) -> &'static str {
1008 InitializationRequiringAction::Update => "updated",
1009 InitializationRequiringAction::Borrow => "borrowed",
1010 InitializationRequiringAction::MatchOn => "matched on",
1011 InitializationRequiringAction::Use => "used",
1012 InitializationRequiringAction::Assignment => "assigned",
1013 InitializationRequiringAction::PartialAssignment => "partially assigned",
1018 impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
1019 fn body(&self) -> &'cx Body<'tcx> {
1023 /// Checks an access to the given place to see if it is allowed. Examines the set of borrows
1024 /// that are in scope, as well as which paths have been initialized, to ensure that (a) the
1025 /// place is initialized and (b) it is not borrowed in some way that would prevent this
1028 /// Returns `true` if an error is reported.
1032 place_span: (Place<'tcx>, Span),
1033 kind: (AccessDepth, ReadOrWrite),
1034 is_local_mutation_allowed: LocalMutationIsAllowed,
1035 flow_state: &Flows<'cx, 'tcx>,
1037 let (sd, rw) = kind;
1039 if let Activation(_, borrow_index) = rw {
1040 if self.reservation_error_reported.contains(&place_span.0) {
1042 "skipping access_place for activation of invalid reservation \
1043 place: {:?} borrow_index: {:?}",
1044 place_span.0, borrow_index
1050 // Check is_empty() first because it's the common case, and doing that
1051 // way we avoid the clone() call.
1052 if !self.access_place_error_reported.is_empty()
1053 && self.access_place_error_reported.contains(&(place_span.0, place_span.1))
1056 "access_place: suppressing error place_span=`{:?}` kind=`{:?}`",
1062 let mutability_error = self.check_access_permissions(
1065 is_local_mutation_allowed,
1069 let conflict_error =
1070 self.check_access_for_conflict(location, place_span, sd, rw, flow_state);
1072 if let (Activation(_, borrow_idx), true) = (kind.1, conflict_error) {
1073 // Suppress this warning when there's an error being emitted for the
1074 // same borrow: fixing the error is likely to fix the warning.
1075 self.reservation_warnings.remove(&borrow_idx);
1078 if conflict_error || mutability_error {
1079 debug!("access_place: logging error place_span=`{:?}` kind=`{:?}`", place_span, kind);
1081 self.access_place_error_reported.insert((place_span.0, place_span.1));
1085 fn check_access_for_conflict(
1088 place_span: (Place<'tcx>, Span),
1091 flow_state: &Flows<'cx, 'tcx>,
1094 "check_access_for_conflict(location={:?}, place_span={:?}, sd={:?}, rw={:?})",
1095 location, place_span, sd, rw,
1098 let mut error_reported = false;
1099 let tcx = self.infcx.tcx;
1100 let body = self.body;
1101 let borrow_set = self.borrow_set.clone();
1103 // Use polonius output if it has been enabled.
1104 let polonius_output = self.polonius_output.clone();
1105 let borrows_in_scope = if let Some(polonius) = &polonius_output {
1106 let location = self.location_table.start_index(location);
1107 Either::Left(polonius.errors_at(location).iter().copied())
1109 Either::Right(flow_state.borrows.iter())
1112 each_borrow_involving_path(
1120 |this, borrow_index, borrow| match (rw, borrow.kind) {
1121 // Obviously an activation is compatible with its own
1122 // reservation (or even prior activating uses of same
1123 // borrow); so don't check if they interfere.
1125 // NOTE: *reservations* do conflict with themselves;
1126 // thus aren't injecting unsoundenss w/ this check.)
1127 (Activation(_, activating), _) if activating == borrow_index => {
1129 "check_access_for_conflict place_span: {:?} sd: {:?} rw: {:?} \
1130 skipping {:?} b/c activation of same borrow_index",
1134 (borrow_index, borrow),
1139 (Read(_), BorrowKind::Shared | BorrowKind::Shallow)
1141 Read(ReadKind::Borrow(BorrowKind::Shallow)),
1142 BorrowKind::Unique | BorrowKind::Mut { .. },
1143 ) => Control::Continue,
1145 (Write(WriteKind::Move), BorrowKind::Shallow) => {
1146 // Handled by initialization checks.
1150 (Read(kind), BorrowKind::Unique | BorrowKind::Mut { .. }) => {
1151 // Reading from mere reservations of mutable-borrows is OK.
1152 if !is_active(&this.dominators, borrow, location) {
1153 assert!(allow_two_phase_borrow(borrow.kind));
1154 return Control::Continue;
1157 error_reported = true;
1160 this.report_use_while_mutably_borrowed(location, place_span, borrow)
1161 .buffer(&mut this.errors_buffer);
1163 ReadKind::Borrow(bk) => {
1164 this.report_conflicting_borrow(location, place_span, bk, borrow)
1165 .buffer(&mut this.errors_buffer);
1172 Reservation(WriteKind::MutableBorrow(bk)),
1173 BorrowKind::Shallow | BorrowKind::Shared,
1174 ) if { tcx.migrate_borrowck() && this.borrow_set.contains(&location) } => {
1175 let bi = this.borrow_set.get_index_of(&location).unwrap();
1177 "recording invalid reservation of place: {:?} with \
1178 borrow index {:?} as warning",
1181 // rust-lang/rust#56254 - This was previously permitted on
1182 // the 2018 edition so we emit it as a warning. We buffer
1183 // these sepately so that we only emit a warning if borrow
1184 // checking was otherwise successful.
1185 this.reservation_warnings
1186 .insert(bi, (place_span.0, place_span.1, location, bk, borrow.clone()));
1188 // Don't suppress actual errors.
1192 (Reservation(kind) | Activation(kind, _) | Write(kind), _) => {
1194 Reservation(..) => {
1196 "recording invalid reservation of \
1200 this.reservation_error_reported.insert(place_span.0);
1202 Activation(_, activating) => {
1204 "observing check_place for activation of \
1205 borrow_index: {:?}",
1209 Read(..) | Write(..) => {}
1212 error_reported = true;
1214 WriteKind::MutableBorrow(bk) => {
1215 this.report_conflicting_borrow(location, place_span, bk, borrow)
1216 .buffer(&mut this.errors_buffer);
1218 WriteKind::StorageDeadOrDrop => this
1219 .report_borrowed_value_does_not_live_long_enough(
1225 WriteKind::Mutate => {
1226 this.report_illegal_mutation_of_borrowed(location, place_span, borrow)
1228 WriteKind::Move => {
1229 this.report_move_out_while_borrowed(location, place_span, borrow)
1243 place_span: (Place<'tcx>, Span),
1246 flow_state: &Flows<'cx, 'tcx>,
1248 // Write of P[i] or *P, or WriteAndRead of any P, requires P init'd.
1250 MutateMode::WriteAndRead => {
1251 self.check_if_path_or_subpath_is_moved(
1253 InitializationRequiringAction::Update,
1254 (place_span.0.as_ref(), place_span.1),
1258 MutateMode::JustWrite => {
1259 self.check_if_assigned_path_is_moved(location, place_span, flow_state);
1263 // Special case: you can assign an immutable local variable
1264 // (e.g., `x = ...`) so long as it has never been initialized
1265 // before (at this point in the flow).
1266 if let Some(local) = place_span.0.as_local() {
1267 if let Mutability::Not = self.body.local_decls[local].mutability {
1268 // check for reassignments to immutable local variables
1269 self.check_if_reassignment_to_immutable_state(
1270 location, local, place_span, flow_state,
1276 // Otherwise, use the normal access permission rules.
1280 (kind, Write(WriteKind::Mutate)),
1281 LocalMutationIsAllowed::No,
1289 (rvalue, span): (&'cx Rvalue<'tcx>, Span),
1290 flow_state: &Flows<'cx, 'tcx>,
1293 Rvalue::Ref(_ /*rgn*/, bk, place) => {
1294 let access_kind = match bk {
1295 BorrowKind::Shallow => {
1296 (Shallow(Some(ArtificialField::ShallowBorrow)), Read(ReadKind::Borrow(bk)))
1298 BorrowKind::Shared => (Deep, Read(ReadKind::Borrow(bk))),
1299 BorrowKind::Unique | BorrowKind::Mut { .. } => {
1300 let wk = WriteKind::MutableBorrow(bk);
1301 if allow_two_phase_borrow(bk) {
1302 (Deep, Reservation(wk))
1313 LocalMutationIsAllowed::No,
1317 let action = if bk == BorrowKind::Shallow {
1318 InitializationRequiringAction::MatchOn
1320 InitializationRequiringAction::Borrow
1323 self.check_if_path_or_subpath_is_moved(
1326 (place.as_ref(), span),
1331 Rvalue::AddressOf(mutability, place) => {
1332 let access_kind = match mutability {
1333 Mutability::Mut => (
1335 Write(WriteKind::MutableBorrow(BorrowKind::Mut {
1336 allow_two_phase_borrow: false,
1339 Mutability::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
1346 LocalMutationIsAllowed::No,
1350 self.check_if_path_or_subpath_is_moved(
1352 InitializationRequiringAction::Borrow,
1353 (place.as_ref(), span),
1358 Rvalue::ThreadLocalRef(_) => {}
1360 Rvalue::Use(ref operand)
1361 | Rvalue::Repeat(ref operand, _)
1362 | Rvalue::UnaryOp(_ /*un_op*/, ref operand)
1363 | Rvalue::Cast(_ /*cast_kind*/, ref operand, _ /*ty*/)
1364 | Rvalue::ShallowInitBox(ref operand, _ /*ty*/) => {
1365 self.consume_operand(location, (operand, span), flow_state)
1368 Rvalue::Len(place) | Rvalue::Discriminant(place) => {
1369 let af = match *rvalue {
1370 Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
1371 Rvalue::Discriminant(..) => None,
1372 _ => unreachable!(),
1377 (Shallow(af), Read(ReadKind::Copy)),
1378 LocalMutationIsAllowed::No,
1381 self.check_if_path_or_subpath_is_moved(
1383 InitializationRequiringAction::Use,
1384 (place.as_ref(), span),
1389 Rvalue::BinaryOp(_bin_op, box (ref operand1, ref operand2))
1390 | Rvalue::CheckedBinaryOp(_bin_op, box (ref operand1, ref operand2)) => {
1391 self.consume_operand(location, (operand1, span), flow_state);
1392 self.consume_operand(location, (operand2, span), flow_state);
1395 Rvalue::NullaryOp(_op, _ty) => {
1396 // nullary ops take no dynamic input; no borrowck effect.
1399 Rvalue::Aggregate(ref aggregate_kind, ref operands) => {
1400 // We need to report back the list of mutable upvars that were
1401 // moved into the closure and subsequently used by the closure,
1402 // in order to populate our used_mut set.
1403 match **aggregate_kind {
1404 AggregateKind::Closure(def_id, _) | AggregateKind::Generator(def_id, _, _) => {
1405 let BorrowCheckResult { used_mut_upvars, .. } =
1406 self.infcx.tcx.mir_borrowck(def_id.expect_local());
1407 debug!("{:?} used_mut_upvars={:?}", def_id, used_mut_upvars);
1408 for field in used_mut_upvars {
1409 self.propagate_closure_used_mut_upvar(&operands[field.index()]);
1412 AggregateKind::Adt(..)
1413 | AggregateKind::Array(..)
1414 | AggregateKind::Tuple { .. } => (),
1417 for operand in operands {
1418 self.consume_operand(location, (operand, span), flow_state);
1424 fn propagate_closure_used_mut_upvar(&mut self, operand: &Operand<'tcx>) {
1425 let propagate_closure_used_mut_place = |this: &mut Self, place: Place<'tcx>| {
1426 // We have three possibilities here:
1427 // a. We are modifying something through a mut-ref
1428 // b. We are modifying something that is local to our parent
1429 // c. Current body is a nested closure, and we are modifying path starting from
1430 // a Place captured by our parent closure.
1432 // Handle (c), the path being modified is exactly the path captured by our parent
1433 if let Some(field) = this.is_upvar_field_projection(place.as_ref()) {
1434 this.used_mut_upvars.push(field);
1438 for (place_ref, proj) in place.iter_projections().rev() {
1440 if proj == ProjectionElem::Deref {
1441 match place_ref.ty(this.body(), this.infcx.tcx).ty.kind() {
1442 // We aren't modifying a variable directly
1443 ty::Ref(_, _, hir::Mutability::Mut) => return,
1450 if let Some(field) = this.is_upvar_field_projection(place_ref) {
1451 this.used_mut_upvars.push(field);
1457 this.used_mut.insert(place.local);
1460 // This relies on the current way that by-value
1461 // captures of a closure are copied/moved directly
1462 // when generating MIR.
1464 Operand::Move(place) | Operand::Copy(place) => {
1465 match place.as_local() {
1466 Some(local) if !self.body.local_decls[local].is_user_variable() => {
1467 if self.body.local_decls[local].ty.is_mutable_ptr() {
1468 // The variable will be marked as mutable by the borrow.
1471 // This is an edge case where we have a `move` closure
1472 // inside a non-move closure, and the inner closure
1473 // contains a mutation:
1476 // || { move || { i += 1; }; };
1478 // In this case our usual strategy of assuming that the
1479 // variable will be captured by mutable reference is
1480 // wrong, since `i` can be copied into the inner
1481 // closure from a shared reference.
1483 // As such we have to search for the local that this
1484 // capture comes from and mark it as being used as mut.
1486 let temp_mpi = self.move_data.rev_lookup.find_local(local);
1487 let init = if let [init_index] = *self.move_data.init_path_map[temp_mpi] {
1488 &self.move_data.inits[init_index]
1490 bug!("temporary should be initialized exactly once")
1493 let loc = match init.location {
1494 InitLocation::Statement(stmt) => stmt,
1495 _ => bug!("temporary initialized in arguments"),
1498 let body = self.body;
1499 let bbd = &body[loc.block];
1500 let stmt = &bbd.statements[loc.statement_index];
1501 debug!("temporary assigned in: stmt={:?}", stmt);
1503 if let StatementKind::Assign(box (_, Rvalue::Ref(_, _, source))) = stmt.kind
1505 propagate_closure_used_mut_place(self, source);
1508 "closures should only capture user variables \
1509 or references to user variables"
1513 _ => propagate_closure_used_mut_place(self, place),
1516 Operand::Constant(..) => {}
1523 (operand, span): (&'cx Operand<'tcx>, Span),
1524 flow_state: &Flows<'cx, 'tcx>,
1527 Operand::Copy(place) => {
1528 // copy of place: check if this is "copy of frozen path"
1529 // (FIXME: see check_loans.rs)
1533 (Deep, Read(ReadKind::Copy)),
1534 LocalMutationIsAllowed::No,
1538 // Finally, check if path was already moved.
1539 self.check_if_path_or_subpath_is_moved(
1541 InitializationRequiringAction::Use,
1542 (place.as_ref(), span),
1546 Operand::Move(place) => {
1547 // move of place: check if this is move of already borrowed path
1551 (Deep, Write(WriteKind::Move)),
1552 LocalMutationIsAllowed::Yes,
1556 // Finally, check if path was already moved.
1557 self.check_if_path_or_subpath_is_moved(
1559 InitializationRequiringAction::Use,
1560 (place.as_ref(), span),
1564 Operand::Constant(_) => {}
1568 /// Checks whether a borrow of this place is invalidated when the function
1570 fn check_for_invalidation_at_exit(
1573 borrow: &BorrowData<'tcx>,
1576 debug!("check_for_invalidation_at_exit({:?})", borrow);
1577 let place = borrow.borrowed_place;
1578 let mut root_place = PlaceRef { local: place.local, projection: &[] };
1580 // FIXME(nll-rfc#40): do more precise destructor tracking here. For now
1581 // we just know that all locals are dropped at function exit (otherwise
1582 // we'll have a memory leak) and assume that all statics have a destructor.
1584 // FIXME: allow thread-locals to borrow other thread locals?
1586 let (might_be_alive, will_be_dropped) =
1587 if self.body.local_decls[root_place.local].is_ref_to_thread_local() {
1588 // Thread-locals might be dropped after the function exits
1589 // We have to dereference the outer reference because
1590 // borrows don't conflict behind shared references.
1591 root_place.projection = DEREF_PROJECTION;
1594 (false, self.locals_are_invalidated_at_exit)
1597 if !will_be_dropped {
1598 debug!("place_is_invalidated_at_exit({:?}) - won't be dropped", place);
1602 let sd = if might_be_alive { Deep } else { Shallow(None) };
1604 if places_conflict::borrow_conflicts_with_place(
1611 places_conflict::PlaceConflictBias::Overlap,
1613 debug!("check_for_invalidation_at_exit({:?}): INVALID", place);
1614 // FIXME: should be talking about the region lifetime instead
1615 // of just a span here.
1616 let span = self.infcx.tcx.sess.source_map().end_point(span);
1617 self.report_borrowed_value_does_not_live_long_enough(
1626 /// Reports an error if this is a borrow of local data.
1627 /// This is called for all Yield expressions on movable generators
1628 fn check_for_local_borrow(&mut self, borrow: &BorrowData<'tcx>, yield_span: Span) {
1629 debug!("check_for_local_borrow({:?})", borrow);
1631 if borrow_of_local_data(borrow.borrowed_place) {
1632 let err = self.cannot_borrow_across_generator_yield(
1633 self.retrieve_borrow_spans(borrow).var_or_use(),
1637 err.buffer(&mut self.errors_buffer);
1641 fn check_activations(&mut self, location: Location, span: Span, flow_state: &Flows<'cx, 'tcx>) {
1642 // Two-phase borrow support: For each activation that is newly
1643 // generated at this statement, check if it interferes with
1645 let borrow_set = self.borrow_set.clone();
1646 for &borrow_index in borrow_set.activations_at_location(location) {
1647 let borrow = &borrow_set[borrow_index];
1649 // only mutable borrows should be 2-phase
1650 assert!(match borrow.kind {
1651 BorrowKind::Shared | BorrowKind::Shallow => false,
1652 BorrowKind::Unique | BorrowKind::Mut { .. } => true,
1657 (borrow.borrowed_place, span),
1658 (Deep, Activation(WriteKind::MutableBorrow(borrow.kind), borrow_index)),
1659 LocalMutationIsAllowed::No,
1662 // We do not need to call `check_if_path_or_subpath_is_moved`
1663 // again, as we already called it when we made the
1664 // initial reservation.
1668 fn check_if_reassignment_to_immutable_state(
1672 place_span: (Place<'tcx>, Span),
1673 flow_state: &Flows<'cx, 'tcx>,
1675 debug!("check_if_reassignment_to_immutable_state({:?})", local);
1677 // Check if any of the initializiations of `local` have happened yet:
1678 if let Some(init_index) = self.is_local_ever_initialized(local, flow_state) {
1679 // And, if so, report an error.
1680 let init = &self.move_data.inits[init_index];
1681 let span = init.span(&self.body);
1682 self.report_illegal_reassignment(location, place_span, span, place_span.0);
1686 fn check_if_full_path_is_moved(
1689 desired_action: InitializationRequiringAction,
1690 place_span: (PlaceRef<'tcx>, Span),
1691 flow_state: &Flows<'cx, 'tcx>,
1693 let maybe_uninits = &flow_state.uninits;
1697 // 1. Move of `a.b.c`, use of `a.b.c`
1698 // 2. Move of `a.b.c`, use of `a.b.c.d` (without first reinitializing `a.b.c.d`)
1699 // 3. Uninitialized `(a.b.c: &_)`, use of `*a.b.c`; note that with
1700 // partial initialization support, one might have `a.x`
1701 // initialized but not `a.b`.
1705 // 4. Move of `a.b.c`, use of `a.b.d`
1706 // 5. Uninitialized `a.x`, initialized `a.b`, use of `a.b`
1707 // 6. Copied `(a.b: &_)`, use of `*(a.b).c`; note that `a.b`
1708 // must have been initialized for the use to be sound.
1709 // 7. Move of `a.b.c` then reinit of `a.b.c.d`, use of `a.b.c.d`
1711 // The dataflow tracks shallow prefixes distinctly (that is,
1712 // field-accesses on P distinctly from P itself), in order to
1713 // track substructure initialization separately from the whole
1716 // E.g., when looking at (*a.b.c).d, if the closest prefix for
1717 // which we have a MovePath is `a.b`, then that means that the
1718 // initialization state of `a.b` is all we need to inspect to
1719 // know if `a.b.c` is valid (and from that we infer that the
1720 // dereference and `.d` access is also valid, since we assume
1721 // `a.b.c` is assigned a reference to an initialized and
1722 // well-formed record structure.)
1724 // Therefore, if we seek out the *closest* prefix for which we
1725 // have a MovePath, that should capture the initialization
1726 // state for the place scenario.
1728 // This code covers scenarios 1, 2, and 3.
1730 debug!("check_if_full_path_is_moved place: {:?}", place_span.0);
1731 let (prefix, mpi) = self.move_path_closest_to(place_span.0);
1732 if maybe_uninits.contains(mpi) {
1733 self.report_use_of_moved_or_uninitialized(
1736 (prefix, place_span.0, place_span.1),
1739 } // Only query longest prefix with a MovePath, not further
1740 // ancestors; dataflow recurs on children when parents
1741 // move (to support partial (re)inits).
1743 // (I.e., querying parents breaks scenario 7; but may want
1744 // to do such a query based on partial-init feature-gate.)
1747 /// Subslices correspond to multiple move paths, so we iterate through the
1748 /// elements of the base array. For each element we check
1750 /// * Does this element overlap with our slice.
1751 /// * Is any part of it uninitialized.
1752 fn check_if_subslice_element_is_moved(
1755 desired_action: InitializationRequiringAction,
1756 place_span: (PlaceRef<'tcx>, Span),
1757 maybe_uninits: &BitSet<MovePathIndex>,
1761 if let Some(mpi) = self.move_path_for_place(place_span.0) {
1762 let move_paths = &self.move_data.move_paths;
1764 let root_path = &move_paths[mpi];
1765 for (child_mpi, child_move_path) in root_path.children(move_paths) {
1766 let last_proj = child_move_path.place.projection.last().unwrap();
1767 if let ProjectionElem::ConstantIndex { offset, from_end, .. } = last_proj {
1768 debug_assert!(!from_end, "Array constant indexing shouldn't be `from_end`.");
1770 if (from..to).contains(offset) {
1772 self.move_data.find_in_move_path_or_its_descendants(child_mpi, |mpi| {
1773 maybe_uninits.contains(mpi)
1776 if let Some(uninit_child) = uninit_child {
1777 self.report_use_of_moved_or_uninitialized(
1780 (place_span.0, place_span.0, place_span.1),
1783 return; // don't bother finding other problems.
1791 fn check_if_path_or_subpath_is_moved(
1794 desired_action: InitializationRequiringAction,
1795 place_span: (PlaceRef<'tcx>, Span),
1796 flow_state: &Flows<'cx, 'tcx>,
1798 let maybe_uninits = &flow_state.uninits;
1802 // 1. Move of `a.b.c`, use of `a` or `a.b`
1803 // partial initialization support, one might have `a.x`
1804 // initialized but not `a.b`.
1805 // 2. All bad scenarios from `check_if_full_path_is_moved`
1809 // 3. Move of `a.b.c`, use of `a.b.d`
1810 // 4. Uninitialized `a.x`, initialized `a.b`, use of `a.b`
1811 // 5. Copied `(a.b: &_)`, use of `*(a.b).c`; note that `a.b`
1812 // must have been initialized for the use to be sound.
1813 // 6. Move of `a.b.c` then reinit of `a.b.c.d`, use of `a.b.c.d`
1815 self.check_if_full_path_is_moved(location, desired_action, place_span, flow_state);
1817 if let Some((place_base, ProjectionElem::Subslice { from, to, from_end: false })) =
1818 place_span.0.last_projection()
1820 let place_ty = place_base.ty(self.body(), self.infcx.tcx);
1821 if let ty::Array(..) = place_ty.ty.kind() {
1822 self.check_if_subslice_element_is_moved(
1825 (place_base, place_span.1),
1834 // A move of any shallow suffix of `place` also interferes
1835 // with an attempt to use `place`. This is scenario 3 above.
1837 // (Distinct from handling of scenarios 1+2+4 above because
1838 // `place` does not interfere with suffixes of its prefixes,
1839 // e.g., `a.b.c` does not interfere with `a.b.d`)
1841 // This code covers scenario 1.
1843 debug!("check_if_path_or_subpath_is_moved place: {:?}", place_span.0);
1844 if let Some(mpi) = self.move_path_for_place(place_span.0) {
1845 let uninit_mpi = self
1847 .find_in_move_path_or_its_descendants(mpi, |mpi| maybe_uninits.contains(mpi));
1849 if let Some(uninit_mpi) = uninit_mpi {
1850 self.report_use_of_moved_or_uninitialized(
1853 (place_span.0, place_span.0, place_span.1),
1856 return; // don't bother finding other problems.
1861 /// Currently MoveData does not store entries for all places in
1862 /// the input MIR. For example it will currently filter out
1863 /// places that are Copy; thus we do not track places of shared
1864 /// reference type. This routine will walk up a place along its
1865 /// prefixes, searching for a foundational place that *is*
1866 /// tracked in the MoveData.
1868 /// An Err result includes a tag indicated why the search failed.
1869 /// Currently this can only occur if the place is built off of a
1870 /// static variable, as we do not track those in the MoveData.
1871 fn move_path_closest_to(&mut self, place: PlaceRef<'tcx>) -> (PlaceRef<'tcx>, MovePathIndex) {
1872 match self.move_data.rev_lookup.find(place) {
1873 LookupResult::Parent(Some(mpi)) | LookupResult::Exact(mpi) => {
1874 (self.move_data.move_paths[mpi].place.as_ref(), mpi)
1876 LookupResult::Parent(None) => panic!("should have move path for every Local"),
1880 fn move_path_for_place(&mut self, place: PlaceRef<'tcx>) -> Option<MovePathIndex> {
1881 // If returns None, then there is no move path corresponding
1882 // to a direct owner of `place` (which means there is nothing
1883 // that borrowck tracks for its analysis).
1885 match self.move_data.rev_lookup.find(place) {
1886 LookupResult::Parent(_) => None,
1887 LookupResult::Exact(mpi) => Some(mpi),
1891 fn check_if_assigned_path_is_moved(
1894 (place, span): (Place<'tcx>, Span),
1895 flow_state: &Flows<'cx, 'tcx>,
1897 debug!("check_if_assigned_path_is_moved place: {:?}", place);
1899 // None case => assigning to `x` does not require `x` be initialized.
1900 for (place_base, elem) in place.iter_projections().rev() {
1902 ProjectionElem::Index(_/*operand*/) |
1903 ProjectionElem::ConstantIndex { .. } |
1904 // assigning to P[i] requires P to be valid.
1905 ProjectionElem::Downcast(_/*adt_def*/, _/*variant_idx*/) =>
1906 // assigning to (P->variant) is okay if assigning to `P` is okay
1908 // FIXME: is this true even if P is an adt with a dtor?
1911 // assigning to (*P) requires P to be initialized
1912 ProjectionElem::Deref => {
1913 self.check_if_full_path_is_moved(
1914 location, InitializationRequiringAction::Use,
1915 (place_base, span), flow_state);
1916 // (base initialized; no need to
1921 ProjectionElem::Subslice { .. } => {
1922 panic!("we don't allow assignments to subslices, location: {:?}",
1926 ProjectionElem::Field(..) => {
1927 // if type of `P` has a dtor, then
1928 // assigning to `P.f` requires `P` itself
1929 // be already initialized
1930 let tcx = self.infcx.tcx;
1931 let base_ty = place_base.ty(self.body(), tcx).ty;
1932 match base_ty.kind() {
1933 ty::Adt(def, _) if def.has_dtor(tcx) => {
1934 self.check_if_path_or_subpath_is_moved(
1935 location, InitializationRequiringAction::Assignment,
1936 (place_base, span), flow_state);
1938 // (base initialized; no need to
1943 // Once `let s; s.x = V; read(s.x);`,
1944 // is allowed, remove this match arm.
1945 ty::Adt(..) | ty::Tuple(..) => {
1946 check_parent_of_field(self, location, place_base, span, flow_state);
1948 // rust-lang/rust#21232, #54499, #54986: during period where we reject
1949 // partial initialization, do not complain about unnecessary `mut` on
1950 // an attempt to do a partial initialization.
1951 self.used_mut.insert(place.local);
1960 fn check_parent_of_field<'cx, 'tcx>(
1961 this: &mut MirBorrowckCtxt<'cx, 'tcx>,
1963 base: PlaceRef<'tcx>,
1965 flow_state: &Flows<'cx, 'tcx>,
1967 // rust-lang/rust#21232: Until Rust allows reads from the
1968 // initialized parts of partially initialized structs, we
1969 // will, starting with the 2018 edition, reject attempts
1970 // to write to structs that are not fully initialized.
1972 // In other words, *until* we allow this:
1974 // 1. `let mut s; s.x = Val; read(s.x);`
1976 // we will for now disallow this:
1978 // 2. `let mut s; s.x = Val;`
1982 // 3. `let mut s = ...; drop(s); s.x=Val;`
1984 // This does not use check_if_path_or_subpath_is_moved,
1985 // because we want to *allow* reinitializations of fields:
1986 // e.g., want to allow
1988 // `let mut s = ...; drop(s.x); s.x=Val;`
1990 // This does not use check_if_full_path_is_moved on
1991 // `base`, because that would report an error about the
1992 // `base` as a whole, but in this scenario we *really*
1993 // want to report an error about the actual thing that was
1994 // moved, which may be some prefix of `base`.
1996 // Shallow so that we'll stop at any dereference; we'll
1997 // report errors about issues with such bases elsewhere.
1998 let maybe_uninits = &flow_state.uninits;
2000 // Find the shortest uninitialized prefix you can reach
2001 // without going over a Deref.
2002 let mut shortest_uninit_seen = None;
2003 for prefix in this.prefixes(base, PrefixSet::Shallow) {
2004 let mpi = match this.move_path_for_place(prefix) {
2009 if maybe_uninits.contains(mpi) {
2011 "check_parent_of_field updating shortest_uninit_seen from {:?} to {:?}",
2012 shortest_uninit_seen,
2015 shortest_uninit_seen = Some((prefix, mpi));
2017 debug!("check_parent_of_field {:?} is definitely initialized", (prefix, mpi));
2021 if let Some((prefix, mpi)) = shortest_uninit_seen {
2022 // Check for a reassignment into an uninitialized field of a union (for example,
2023 // after a move out). In this case, do not report an error here. There is an
2024 // exception, if this is the first assignment into the union (that is, there is
2025 // no move out from an earlier location) then this is an attempt at initialization
2026 // of the union - we should error in that case.
2027 let tcx = this.infcx.tcx;
2028 if base.ty(this.body(), tcx).ty.is_union() {
2029 if this.move_data.path_map[mpi].iter().any(|moi| {
2030 this.move_data.moves[*moi].source.is_predecessor_of(location, this.body)
2036 this.report_use_of_moved_or_uninitialized(
2038 InitializationRequiringAction::PartialAssignment,
2039 (prefix, base, span),
2046 /// Checks the permissions for the given place and read or write kind
2048 /// Returns `true` if an error is reported.
2049 fn check_access_permissions(
2051 (place, span): (Place<'tcx>, Span),
2053 is_local_mutation_allowed: LocalMutationIsAllowed,
2054 flow_state: &Flows<'cx, 'tcx>,
2058 "check_access_permissions({:?}, {:?}, is_local_mutation_allowed: {:?})",
2059 place, kind, is_local_mutation_allowed
2066 Reservation(WriteKind::MutableBorrow(
2067 borrow_kind @ (BorrowKind::Unique | BorrowKind::Mut { .. }),
2069 | Write(WriteKind::MutableBorrow(
2070 borrow_kind @ (BorrowKind::Unique | BorrowKind::Mut { .. }),
2072 let is_local_mutation_allowed = match borrow_kind {
2073 BorrowKind::Unique => LocalMutationIsAllowed::Yes,
2074 BorrowKind::Mut { .. } => is_local_mutation_allowed,
2075 BorrowKind::Shared | BorrowKind::Shallow => unreachable!(),
2077 match self.is_mutable(place.as_ref(), is_local_mutation_allowed) {
2079 self.add_used_mut(root_place, flow_state);
2083 error_access = AccessKind::MutableBorrow;
2084 the_place_err = place_err;
2088 Reservation(WriteKind::Mutate) | Write(WriteKind::Mutate) => {
2089 match self.is_mutable(place.as_ref(), is_local_mutation_allowed) {
2091 self.add_used_mut(root_place, flow_state);
2095 error_access = AccessKind::Mutate;
2096 the_place_err = place_err;
2103 | WriteKind::StorageDeadOrDrop
2104 | WriteKind::MutableBorrow(BorrowKind::Shared)
2105 | WriteKind::MutableBorrow(BorrowKind::Shallow),
2109 | WriteKind::StorageDeadOrDrop
2110 | WriteKind::MutableBorrow(BorrowKind::Shared)
2111 | WriteKind::MutableBorrow(BorrowKind::Shallow),
2113 if let (Err(_), true) = (
2114 self.is_mutable(place.as_ref(), is_local_mutation_allowed),
2115 self.errors_buffer.is_empty(),
2117 // rust-lang/rust#46908: In pure NLL mode this code path should be
2118 // unreachable, but we use `delay_span_bug` because we can hit this when
2119 // dereferencing a non-Copy raw pointer *and* have `-Ztreat-err-as-bug`
2120 // enabled. We don't want to ICE for that case, as other errors will have
2121 // been emitted (#52262).
2122 self.infcx.tcx.sess.delay_span_bug(
2125 "Accessing `{:?}` with the kind `{:?}` shouldn't be possible",
2133 // permission checks are done at Reservation point.
2139 | BorrowKind::Mut { .. }
2140 | BorrowKind::Shared
2141 | BorrowKind::Shallow,
2145 // Access authorized
2150 // rust-lang/rust#21232, #54986: during period where we reject
2151 // partial initialization, do not complain about mutability
2152 // errors except for actual mutation (as opposed to an attempt
2153 // to do a partial initialization).
2154 let previously_initialized =
2155 self.is_local_ever_initialized(place.local, flow_state).is_some();
2157 // at this point, we have set up the error reporting state.
2158 if previously_initialized {
2159 self.report_mutability_error(place, span, the_place_err, error_access, location);
2166 fn is_local_ever_initialized(
2169 flow_state: &Flows<'cx, 'tcx>,
2170 ) -> Option<InitIndex> {
2171 let mpi = self.move_data.rev_lookup.find_local(local);
2172 let ii = &self.move_data.init_path_map[mpi];
2174 if flow_state.ever_inits.contains(index) {
2181 /// Adds the place into the used mutable variables set
2182 fn add_used_mut(&mut self, root_place: RootPlace<'tcx>, flow_state: &Flows<'cx, 'tcx>) {
2184 RootPlace { place_local: local, place_projection: [], is_local_mutation_allowed } => {
2185 // If the local may have been initialized, and it is now currently being
2186 // mutated, then it is justified to be annotated with the `mut`
2187 // keyword, since the mutation may be a possible reassignment.
2188 if is_local_mutation_allowed != LocalMutationIsAllowed::Yes
2189 && self.is_local_ever_initialized(local, flow_state).is_some()
2191 self.used_mut.insert(local);
2196 place_projection: _,
2197 is_local_mutation_allowed: LocalMutationIsAllowed::Yes,
2201 place_projection: place_projection @ [.., _],
2202 is_local_mutation_allowed: _,
2204 if let Some(field) = self.is_upvar_field_projection(PlaceRef {
2206 projection: place_projection,
2208 self.used_mut_upvars.push(field);
2214 /// Whether this value can be written or borrowed mutably.
2215 /// Returns the root place if the place passed in is a projection.
2218 place: PlaceRef<'tcx>,
2219 is_local_mutation_allowed: LocalMutationIsAllowed,
2220 ) -> Result<RootPlace<'tcx>, PlaceRef<'tcx>> {
2221 debug!("is_mutable: place={:?}, is_local...={:?}", place, is_local_mutation_allowed);
2222 match place.last_projection() {
2224 let local = &self.body.local_decls[place.local];
2225 match local.mutability {
2226 Mutability::Not => match is_local_mutation_allowed {
2227 LocalMutationIsAllowed::Yes => Ok(RootPlace {
2228 place_local: place.local,
2229 place_projection: place.projection,
2230 is_local_mutation_allowed: LocalMutationIsAllowed::Yes,
2232 LocalMutationIsAllowed::ExceptUpvars => Ok(RootPlace {
2233 place_local: place.local,
2234 place_projection: place.projection,
2235 is_local_mutation_allowed: LocalMutationIsAllowed::ExceptUpvars,
2237 LocalMutationIsAllowed::No => Err(place),
2239 Mutability::Mut => Ok(RootPlace {
2240 place_local: place.local,
2241 place_projection: place.projection,
2242 is_local_mutation_allowed,
2246 Some((place_base, elem)) => {
2248 ProjectionElem::Deref => {
2249 let base_ty = place_base.ty(self.body(), self.infcx.tcx).ty;
2251 // Check the kind of deref to decide
2252 match base_ty.kind() {
2253 ty::Ref(_, _, mutbl) => {
2255 // Shared borrowed data is never mutable
2256 hir::Mutability::Not => Err(place),
2257 // Mutably borrowed data is mutable, but only if we have a
2258 // unique path to the `&mut`
2259 hir::Mutability::Mut => {
2260 let mode = match self.is_upvar_field_projection(place) {
2261 Some(field) if self.upvars[field.index()].by_ref => {
2262 is_local_mutation_allowed
2264 _ => LocalMutationIsAllowed::Yes,
2267 self.is_mutable(place_base, mode)
2271 ty::RawPtr(tnm) => {
2273 // `*const` raw pointers are not mutable
2274 hir::Mutability::Not => Err(place),
2275 // `*mut` raw pointers are always mutable, regardless of
2276 // context. The users have to check by themselves.
2277 hir::Mutability::Mut => Ok(RootPlace {
2278 place_local: place.local,
2279 place_projection: place.projection,
2280 is_local_mutation_allowed,
2284 // `Box<T>` owns its content, so mutable if its location is mutable
2285 _ if base_ty.is_box() => {
2286 self.is_mutable(place_base, is_local_mutation_allowed)
2288 // Deref should only be for reference, pointers or boxes
2289 _ => bug!("Deref of unexpected type: {:?}", base_ty),
2292 // All other projections are owned by their base path, so mutable if
2293 // base path is mutable
2294 ProjectionElem::Field(..)
2295 | ProjectionElem::Index(..)
2296 | ProjectionElem::ConstantIndex { .. }
2297 | ProjectionElem::Subslice { .. }
2298 | ProjectionElem::Downcast(..) => {
2299 let upvar_field_projection = self.is_upvar_field_projection(place);
2300 if let Some(field) = upvar_field_projection {
2301 let upvar = &self.upvars[field.index()];
2303 "is_mutable: upvar.mutability={:?} local_mutation_is_allowed={:?} \
2304 place={:?}, place_base={:?}",
2305 upvar, is_local_mutation_allowed, place, place_base
2307 match (upvar.place.mutability, is_local_mutation_allowed) {
2310 LocalMutationIsAllowed::No
2311 | LocalMutationIsAllowed::ExceptUpvars,
2313 (Mutability::Not, LocalMutationIsAllowed::Yes)
2314 | (Mutability::Mut, _) => {
2315 // Subtle: this is an upvar
2316 // reference, so it looks like
2317 // `self.foo` -- we want to double
2318 // check that the location `*self`
2319 // is mutable (i.e., this is not a
2320 // `Fn` closure). But if that
2321 // check succeeds, we want to
2322 // *blame* the mutability on
2323 // `place` (that is,
2324 // `self.foo`). This is used to
2325 // propagate the info about
2326 // whether mutability declarations
2327 // are used outwards, so that we register
2328 // the outer variable as mutable. Otherwise a
2329 // test like this fails to record the `mut`
2333 // fn foo<F: FnOnce()>(_f: F) { }
2335 // let var = Vec::new();
2342 self.is_mutable(place_base, is_local_mutation_allowed)?;
2344 place_local: place.local,
2345 place_projection: place.projection,
2346 is_local_mutation_allowed,
2351 self.is_mutable(place_base, is_local_mutation_allowed)
2359 /// If `place` is a field projection, and the field is being projected from a closure type,
2360 /// then returns the index of the field being projected. Note that this closure will always
2361 /// be `self` in the current MIR, because that is the only time we directly access the fields
2362 /// of a closure type.
2363 fn is_upvar_field_projection(&self, place_ref: PlaceRef<'tcx>) -> Option<Field> {
2364 path_utils::is_upvar_field_projection(self.infcx.tcx, &self.upvars, place_ref, self.body())
2368 /// The degree of overlap between 2 places for borrow-checking.
2370 /// The places might partially overlap - in this case, we give
2371 /// up and say that they might conflict. This occurs when
2372 /// different fields of a union are borrowed. For example,
2373 /// if `u` is a union, we have no way of telling how disjoint
2374 /// `u.a.x` and `a.b.y` are.
2376 /// The places have the same type, and are either completely disjoint
2377 /// or equal - i.e., they can't "partially" overlap as can occur with
2378 /// unions. This is the "base case" on which we recur for extensions
2381 /// The places are disjoint, so we know all extensions of them
2382 /// will also be disjoint.