1 //! This query borrow-checks the MIR to (further) ensure it is not broken.
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"]
15 extern crate rustc_middle;
19 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
20 use rustc_data_structures::graph::dominators::Dominators;
21 use rustc_errors::{Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
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,
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};
40 use smallvec::SmallVec;
41 use std::cell::RefCell;
42 use std::collections::BTreeMap;
45 use rustc_mir_dataflow::impls::{
46 EverInitializedPlaces, MaybeInitializedPlaces, MaybeUninitializedPlaces,
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;
53 use crate::session_diagnostics::VarNeedNotMut;
55 use self::diagnostics::{AccessKind, RegionName};
56 use self::location::LocationTable;
57 use self::prefixes::PrefixSet;
60 use self::path_utils::*;
64 mod constraint_generation;
72 mod member_constraints;
80 mod session_diagnostics;
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 /// 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];
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)
116 mir_borrowck(tcx, ty::WithOptConstParam::unknown(did))
119 mir_borrowck_const_arg: |tcx, (did, param_did)| {
120 mir_borrowck(tcx, ty::WithOptConstParam { did, const_param_did: Some(param_did) })
126 fn mir_borrowck<'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;
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");
141 tcx.arena.alloc(opt_closure_req)
144 /// Perform the actual borrow checking.
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
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();
161 let param_env = tcx.param_env(def.did);
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 {
169 var_debug_info.source_info.span,
170 "local {:?} has many names (`{}` vs `{}`)",
176 local_names[local] = Some(var_debug_info.name);
181 let mut errors = error::BorrowckErrors::new();
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();
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,
197 Upvar { place: captured_place.clone(), by_ref }
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();
208 nll::replace_regions_in_mir(infcx, param_env, &mut body_owned, &mut promoted);
209 let body = &body_owned; // no further changes
211 let location_table_owned = LocationTable::new(body);
212 let location_table = &location_table_owned;
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),
219 let promoted_errors = promoted
221 .map(|(idx, body)| (idx, MoveData::gather_moves(&body, tcx, param_env)));
223 let mdpe = MoveDataParamEnv { move_data, param_env };
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);
231 let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(def.did).is_fn_or_closure();
233 Rc::new(BorrowSet::build(tcx, body, locals_are_invalidated_at_exit, &mdpe.move_data));
235 let use_polonius = return_body_with_facts || infcx.tcx.sess.opts.unstable_opts.polonius;
237 // Compute non-lexical lifetimes.
245 } = nll::compute_regions(
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, ®ioncx, &opt_closure_req);
263 // We also have a `#[rustc_regions]` annotation that causes us to dump
265 nll::dump_annotation(
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.
279 let regioncx = Rc::new(regioncx);
281 let flow_borrows = Borrows::new(tcx, body, ®ioncx, &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();
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()
305 for (idx, move_data_results) in promoted_errors {
306 let promoted_body = &promoted[idx];
308 if let Err((move_data, move_errors)) = move_data_results {
309 let mut promoted_mbcx = MirBorrowckCtxt {
313 move_data: &move_data,
314 location_table, // no need to create a real one for the promoted, it is not used
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
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,
333 promoted_mbcx.report_move_errors(move_errors);
334 errors = promoted_mbcx.errors;
338 let dominators = body.basic_blocks.dominators();
340 let mut mbcx = MirBorrowckCtxt {
344 move_data: &mdpe.move_data,
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(®ioncx),
353 used_mut: Default::default(),
354 used_mut_upvars: SmallVec::new(),
355 borrow_set: Rc::clone(&borrow_set),
359 region_names: RefCell::default(),
360 next_region_name: RefCell::new(1),
365 // Compute and report region errors, if any.
366 mbcx.report_region_errors(nll_errors);
368 let results = BorrowckResults {
369 ever_inits: flow_ever_inits,
370 uninits: flow_uninits,
371 borrows: flow_borrows,
374 mbcx.report_move_errors(move_errors);
376 rustc_mir_dataflow::visit_results(
378 traversal::reverse_postorder(body).map(|(bb, _)| bb),
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
388 let temporary_used_locals: FxHashSet<Local> = mbcx
391 .filter(|&local| !mbcx.body.local_decls[*local].is_user_variable())
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);
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,
410 // Skip over locals that begin with an underscore or have no name
411 match mbcx.local_names[local] {
413 if name.as_str().starts_with('_') {
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.
426 let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
428 tcx.emit_spanned_lint(UNUSED_MUT, lint_root, span, VarNeedNotMut { span: mut_span })
431 let tainted_by_errors = mbcx.emit_errors();
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,
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 {
444 input_facts: *polonius_input.expect("Polonius input facts were not generated"),
446 location_table: location_table_owned,
452 debug!("do_mir_borrowck: result = {:#?}", result);
454 (result, body_with_facts)
457 /// A `Body` with information computed by the borrow checker. This struct is
458 /// intended to be consumed by compiler consumers.
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,
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>,
479 /// Map from MIR `Location` to `LocationIndex`; created
480 /// when MIR borrowck begins.
481 location_table: &'cx LocationTable,
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
488 /// I'm not sure this is the right approach - @eddyb could you try and
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
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.
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>>,
522 /// The set of borrows extracted from the MIR
523 borrow_set: Rc<BorrowSet<'tcx>>,
525 /// Dominators for MIR
526 dominators: Dominators<BasicBlock>,
528 /// Information about upvars not necessarily preserved in types or MIR
529 upvars: Vec<Upvar<'tcx>>,
531 /// Names of local (user) variables (extracted from `var_debug_info`).
532 local_names: IndexVec<Local, Option<Symbol>>,
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>>,
538 /// The counter for generating new region names.
539 next_region_name: RefCell<usize>,
541 /// Results of Polonius analysis.
542 polonius_output: Option<Rc<PoloniusOutput>>,
544 errors: error::BorrowckErrors<'tcx>,
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>;
555 fn visit_statement_before_primary_effect(
557 flow_state: &Flows<'cx, 'tcx>,
558 stmt: &'cx Statement<'tcx>,
561 debug!("MirBorrowckCtxt::process_statement({:?}, {:?}): {:?}", location, stmt, flow_state);
562 let span = stmt.source_info.span;
564 self.check_activations(location, span, flow_state);
567 StatementKind::Assign(box (lhs, ref rhs)) => {
568 self.consume_rvalue(location, (rhs, span), flow_state);
570 self.mutate_place(location, (*lhs, span), Shallow(None), flow_state);
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.
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
583 self.check_if_path_or_subpath_is_moved(
585 InitializationRequiringAction::Use,
586 (place.as_ref(), span),
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!(
594 "Unexpected CopyNonOverlapping, should only appear after lower_intrinsics",
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) => {
606 (Place::from(*local), span),
607 (Shallow(None), Write(WriteKind::StorageDeadOrDrop)),
608 LocalMutationIsAllowed::Yes,
613 | StatementKind::Retag { .. }
614 | StatementKind::Deinit(..)
615 | StatementKind::SetDiscriminant { .. } => {
616 bug!("Statement not allowed in this MIR phase")
621 fn visit_terminator_before_primary_effect(
623 flow_state: &Flows<'cx, 'tcx>,
624 term: &'cx Terminator<'tcx>,
627 debug!("MirBorrowckCtxt::process_terminator({:?}, {:?}): {:?}", loc, term, flow_state);
628 let span = term.source_info.span;
630 self.check_activations(loc, span, flow_state);
633 TerminatorKind::SwitchInt { ref discr, switch_ty: _, targets: _ } => {
634 self.consume_operand(loc, (discr, span), flow_state);
636 TerminatorKind::Drop { place, target: _, unwind: _ } => {
638 "visit_terminator_drop \
639 loc: {:?} term: {:?} place: {:?} span: {:?}",
640 loc, term, place, span
646 (AccessDepth::Drop, Write(WriteKind::StorageDeadOrDrop)),
647 LocalMutationIsAllowed::Yes,
651 TerminatorKind::DropAndReplace {
653 value: ref new_value,
657 self.mutate_place(loc, (drop_place, span), Deep, flow_state);
658 self.consume_operand(loc, (new_value, span), flow_state);
660 TerminatorKind::Call {
669 self.consume_operand(loc, (func, span), flow_state);
671 self.consume_operand(loc, (arg, span), flow_state);
673 self.mutate_place(loc, (destination, span), Deep, flow_state);
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);
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);
689 TerminatorKind::InlineAsm {
699 InlineAsmOperand::In { reg: _, ref value } => {
700 self.consume_operand(loc, (value, span), flow_state);
702 InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
703 if let Some(place) = place {
704 self.mutate_place(loc, (place, span), Shallow(None), flow_state);
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 {
718 InlineAsmOperand::Const { value: _ }
719 | InlineAsmOperand::SymFn { value: _ }
720 | InlineAsmOperand::SymStatic { def_id: _ } => {}
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
738 fn visit_terminator_after_primary_effect(
740 flow_state: &Flows<'cx, 'tcx>,
741 term: &'cx Terminator<'tcx>,
744 let span = term.source_info.span;
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);
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);
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 { .. } => {}
785 use self::AccessDepth::{Deep, Shallow};
786 use self::ReadOrWrite::{Activation, Read, Reservation, Write};
788 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
789 enum ArtificialField {
794 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
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>),
803 /// From the RFC: "A *deep* access means that all data reachable
804 /// through the given place may be invalidated or accesses by
808 /// Access is Deep only when there is a Drop implementation that
809 /// can reach the data behind the reference.
813 /// Kind of access to a value: read or write
814 /// (For informational purposes only)
815 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
817 /// From the RFC: "A *read* means that the existing data may be
818 /// read, but will not be changed."
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).
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),
833 /// Kind of read access to a value
834 /// (For informational purposes only)
835 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
841 /// Kind of write access to a value
842 /// (For informational purposes only)
843 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
846 MutableBorrow(BorrowKind),
851 /// When checking permissions for a place access, this flag is used to indicate that an immutable
852 /// local place can be mutated.
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 {
862 /// We want use of immutable upvars to cause a "write to immutable upvar"
863 /// error, not an "reassignment" error.
868 #[derive(Copy, Clone, Debug)]
869 enum InitializationRequiringAction {
877 struct RootPlace<'tcx> {
879 place_projection: &'tcx [PlaceElem<'tcx>],
880 is_local_mutation_allowed: LocalMutationIsAllowed,
883 impl InitializationRequiringAction {
884 fn as_noun(self) -> &'static str {
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",
894 fn as_verb_in_past_tense(self) -> &'static str {
896 InitializationRequiringAction::Borrow => "borrowed",
897 InitializationRequiringAction::MatchOn => "matched on",
898 InitializationRequiringAction::Use => "used",
899 InitializationRequiringAction::Assignment => "assigned",
900 InitializationRequiringAction::PartialAssignment => "partially assigned",
904 fn as_general_verb_in_past_tense(self) -> &'static str {
906 InitializationRequiringAction::Borrow
907 | InitializationRequiringAction::MatchOn
908 | InitializationRequiringAction::Use => "used",
909 InitializationRequiringAction::Assignment => "assigned",
910 InitializationRequiringAction::PartialAssignment => "partially assigned",
915 impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
916 fn body(&self) -> &'cx Body<'tcx> {
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
925 /// Returns `true` if an error is reported.
929 place_span: (Place<'tcx>, Span),
930 kind: (AccessDepth, ReadOrWrite),
931 is_local_mutation_allowed: LocalMutationIsAllowed,
932 flow_state: &Flows<'cx, 'tcx>,
936 if let Activation(_, borrow_index) = rw {
937 if self.reservation_error_reported.contains(&place_span.0) {
939 "skipping access_place for activation of invalid reservation \
940 place: {:?} borrow_index: {:?}",
941 place_span.0, borrow_index
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))
953 "access_place: suppressing error place_span=`{:?}` kind=`{:?}`",
959 let mutability_error = self.check_access_permissions(
962 is_local_mutation_allowed,
967 self.check_access_for_conflict(location, place_span, sd, rw, flow_state);
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));
975 #[instrument(level = "debug", skip(self, flow_state))]
976 fn check_access_for_conflict(
979 place_span: (Place<'tcx>, Span),
982 flow_state: &Flows<'cx, 'tcx>,
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();
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())
995 Either::Right(flow_state.borrows.iter())
998 each_borrow_involving_path(
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.
1011 // NOTE: *reservations* do conflict with themselves;
1012 // thus aren't injecting unsoundness w/ this check.)
1013 (Activation(_, activating), _) if activating == borrow_index => {
1015 "check_access_for_conflict place_span: {:?} sd: {:?} rw: {:?} \
1016 skipping {:?} b/c activation of same borrow_index",
1020 (borrow_index, borrow),
1025 (Read(_), BorrowKind::Shared | BorrowKind::Shallow)
1027 Read(ReadKind::Borrow(BorrowKind::Shallow)),
1028 BorrowKind::Unique | BorrowKind::Mut { .. },
1029 ) => Control::Continue,
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
1037 (Write(WriteKind::Move), BorrowKind::Shallow) => {
1038 // Handled by initialization checks.
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;
1049 error_reported = true;
1053 .report_use_while_mutably_borrowed(location, place_span, borrow);
1054 this.buffer_error(err);
1056 ReadKind::Borrow(bk) => {
1058 this.report_conflicting_borrow(location, place_span, bk, borrow);
1059 this.buffer_error(err);
1065 (Reservation(kind) | Activation(kind, _) | Write(kind), _) => {
1067 Reservation(..) => {
1069 "recording invalid reservation of \
1073 this.reservation_error_reported.insert(place_span.0);
1075 Activation(_, activating) => {
1077 "observing check_place for activation of \
1078 borrow_index: {:?}",
1082 Read(..) | Write(..) => {}
1085 error_reported = true;
1087 WriteKind::MutableBorrow(bk) => {
1089 this.report_conflicting_borrow(location, place_span, bk, borrow);
1090 this.buffer_error(err);
1092 WriteKind::StorageDeadOrDrop => this
1093 .report_borrowed_value_does_not_live_long_enough(
1099 WriteKind::Mutate => {
1100 this.report_illegal_mutation_of_borrowed(location, place_span, borrow)
1102 WriteKind::Move => {
1103 this.report_move_out_while_borrowed(location, place_span, borrow)
1117 place_span: (Place<'tcx>, Span),
1119 flow_state: &Flows<'cx, 'tcx>,
1121 // Write of P[i] or *P requires P init'd.
1122 self.check_if_assigned_path_is_moved(location, place_span, flow_state);
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,
1137 // Otherwise, use the normal access permission rules.
1141 (kind, Write(WriteKind::Mutate)),
1142 LocalMutationIsAllowed::No,
1150 (rvalue, span): (&'cx Rvalue<'tcx>, Span),
1151 flow_state: &Flows<'cx, 'tcx>,
1154 Rvalue::Ref(_ /*rgn*/, bk, place) => {
1155 let access_kind = match bk {
1156 BorrowKind::Shallow => {
1157 (Shallow(Some(ArtificialField::ShallowBorrow)), Read(ReadKind::Borrow(bk)))
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))
1174 LocalMutationIsAllowed::No,
1178 let action = if bk == BorrowKind::Shallow {
1179 InitializationRequiringAction::MatchOn
1181 InitializationRequiringAction::Borrow
1184 self.check_if_path_or_subpath_is_moved(
1187 (place.as_ref(), span),
1192 Rvalue::AddressOf(mutability, place) => {
1193 let access_kind = match mutability {
1194 Mutability::Mut => (
1196 Write(WriteKind::MutableBorrow(BorrowKind::Mut {
1197 allow_two_phase_borrow: false,
1200 Mutability::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
1207 LocalMutationIsAllowed::No,
1211 self.check_if_path_or_subpath_is_moved(
1213 InitializationRequiringAction::Borrow,
1214 (place.as_ref(), span),
1219 Rvalue::ThreadLocalRef(_) => {}
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)
1228 Rvalue::CopyForDeref(place) => {
1232 (Deep, Read(ReadKind::Copy)),
1233 LocalMutationIsAllowed::No,
1237 // Finally, check if path was already moved.
1238 self.check_if_path_or_subpath_is_moved(
1240 InitializationRequiringAction::Use,
1241 (place.as_ref(), span),
1246 Rvalue::Len(place) | Rvalue::Discriminant(place) => {
1247 let af = match *rvalue {
1248 Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
1249 Rvalue::Discriminant(..) => None,
1250 _ => unreachable!(),
1255 (Shallow(af), Read(ReadKind::Copy)),
1256 LocalMutationIsAllowed::No,
1259 self.check_if_path_or_subpath_is_moved(
1261 InitializationRequiringAction::Use,
1262 (place.as_ref(), span),
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);
1273 Rvalue::NullaryOp(_op, _ty) => {
1274 // nullary ops take no dynamic input; no borrowck effect.
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()]);
1290 AggregateKind::Adt(..)
1291 | AggregateKind::Array(..)
1292 | AggregateKind::Tuple { .. } => (),
1295 for operand in operands {
1296 self.consume_operand(location, (operand, span), flow_state);
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.
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);
1316 for (place_ref, proj) in place.iter_projections().rev() {
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,
1328 if let Some(field) = this.is_upvar_field_projection(place_ref) {
1329 this.used_mut_upvars.push(field);
1335 this.used_mut.insert(place.local);
1338 // This relies on the current way that by-value
1339 // captures of a closure are copied/moved directly
1340 // when generating MIR.
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.
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:
1354 // || { move || { i += 1; }; };
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.
1361 // As such we have to search for the local that this
1362 // capture comes from and mark it as being used as mut.
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]
1368 bug!("temporary should be initialized exactly once")
1371 let InitLocation::Statement(loc) = init.location else {
1372 bug!("temporary initialized in arguments")
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);
1380 if let StatementKind::Assign(box (_, Rvalue::Ref(_, _, source))) = stmt.kind
1382 propagate_closure_used_mut_place(self, source);
1385 "closures should only capture user variables \
1386 or references to user variables"
1390 _ => propagate_closure_used_mut_place(self, place),
1393 Operand::Constant(..) => {}
1400 (operand, span): (&'cx Operand<'tcx>, Span),
1401 flow_state: &Flows<'cx, 'tcx>,
1404 Operand::Copy(place) => {
1405 // copy of place: check if this is "copy of frozen path"
1406 // (FIXME: see check_loans.rs)
1410 (Deep, Read(ReadKind::Copy)),
1411 LocalMutationIsAllowed::No,
1415 // Finally, check if path was already moved.
1416 self.check_if_path_or_subpath_is_moved(
1418 InitializationRequiringAction::Use,
1419 (place.as_ref(), span),
1423 Operand::Move(place) => {
1424 // move of place: check if this is move of already borrowed path
1428 (Deep, Write(WriteKind::Move)),
1429 LocalMutationIsAllowed::Yes,
1433 // Finally, check if path was already moved.
1434 self.check_if_path_or_subpath_is_moved(
1436 InitializationRequiringAction::Use,
1437 (place.as_ref(), span),
1441 Operand::Constant(_) => {}
1445 /// Checks whether a borrow of this place is invalidated when the function
1447 #[instrument(level = "debug", skip(self))]
1448 fn check_for_invalidation_at_exit(
1451 borrow: &BorrowData<'tcx>,
1454 let place = borrow.borrowed_place;
1455 let mut root_place = PlaceRef { local: place.local, projection: &[] };
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.
1461 // FIXME: allow thread-locals to borrow other thread locals?
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;
1471 (false, self.locals_are_invalidated_at_exit)
1474 if !will_be_dropped {
1475 debug!("place_is_invalidated_at_exit({:?}) - won't be dropped", place);
1479 let sd = if might_be_alive { Deep } else { Shallow(None) };
1481 if places_conflict::borrow_conflicts_with_place(
1488 places_conflict::PlaceConflictBias::Overlap,
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(
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);
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(),
1514 self.buffer_error(err);
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
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];
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,
1534 (borrow.borrowed_place, span),
1535 (Deep, Activation(WriteKind::MutableBorrow(borrow.kind), borrow_index)),
1536 LocalMutationIsAllowed::No,
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.
1545 fn check_if_reassignment_to_immutable_state(
1549 place_span: (Place<'tcx>, Span),
1550 flow_state: &Flows<'cx, 'tcx>,
1552 debug!("check_if_reassignment_to_immutable_state({:?})", local);
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);
1563 fn check_if_full_path_is_moved(
1566 desired_action: InitializationRequiringAction,
1567 place_span: (PlaceRef<'tcx>, Span),
1568 flow_state: &Flows<'cx, 'tcx>,
1570 let maybe_uninits = &flow_state.uninits;
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`.
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`
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
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.)
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.
1605 // This code covers scenarios 1, 2, and 3.
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(
1613 (prefix, place_span.0, place_span.1),
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).
1620 // (I.e., querying parents breaks scenario 7; but may want
1621 // to do such a query based on partial-init feature-gate.)
1624 /// Subslices correspond to multiple move paths, so we iterate through the
1625 /// elements of the base array. For each element we check
1627 /// * Does this element overlap with our slice.
1628 /// * Is any part of it uninitialized.
1629 fn check_if_subslice_element_is_moved(
1632 desired_action: InitializationRequiringAction,
1633 place_span: (PlaceRef<'tcx>, Span),
1634 maybe_uninits: &ChunkedBitSet<MovePathIndex>,
1638 if let Some(mpi) = self.move_path_for_place(place_span.0) {
1639 let move_paths = &self.move_data.move_paths;
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`.");
1647 if (from..to).contains(offset) {
1649 self.move_data.find_in_move_path_or_its_descendants(child_mpi, |mpi| {
1650 maybe_uninits.contains(mpi)
1653 if let Some(uninit_child) = uninit_child {
1654 self.report_use_of_moved_or_uninitialized(
1657 (place_span.0, place_span.0, place_span.1),
1660 return; // don't bother finding other problems.
1668 fn check_if_path_or_subpath_is_moved(
1671 desired_action: InitializationRequiringAction,
1672 place_span: (PlaceRef<'tcx>, Span),
1673 flow_state: &Flows<'cx, 'tcx>,
1675 let maybe_uninits = &flow_state.uninits;
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`
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`
1692 self.check_if_full_path_is_moved(location, desired_action, place_span, flow_state);
1694 if let Some((place_base, ProjectionElem::Subslice { from, to, from_end: false })) =
1695 place_span.0.last_projection()
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(
1702 (place_base, place_span.1),
1711 // A move of any shallow suffix of `place` also interferes
1712 // with an attempt to use `place`. This is scenario 3 above.
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`)
1718 // This code covers scenario 1.
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
1724 .find_in_move_path_or_its_descendants(mpi, |mpi| maybe_uninits.contains(mpi));
1726 if let Some(uninit_mpi) = uninit_mpi {
1727 self.report_use_of_moved_or_uninitialized(
1730 (place_span.0, place_span.0, place_span.1),
1733 return; // don't bother finding other problems.
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.
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)
1753 LookupResult::Parent(None) => panic!("should have move path for every Local"),
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).
1762 match self.move_data.rev_lookup.find(place) {
1763 LookupResult::Parent(_) => None,
1764 LookupResult::Exact(mpi) => Some(mpi),
1768 fn check_if_assigned_path_is_moved(
1771 (place, span): (Place<'tcx>, Span),
1772 flow_state: &Flows<'cx, 'tcx>,
1774 debug!("check_if_assigned_path_is_moved place: {:?}", place);
1776 // None case => assigning to `x` does not require `x` be initialized.
1777 for (place_base, elem) in place.iter_projections().rev() {
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
1786 // FIXME: is this true even if P is an adt with a dtor?
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
1799 ProjectionElem::Subslice { .. } => {
1800 panic!("we don't allow assignments to subslices, location: {:?}",
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);
1816 // (base initialized; no need to
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);
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);
1838 fn check_parent_of_field<'cx, 'tcx>(
1839 this: &mut MirBorrowckCtxt<'cx, 'tcx>,
1841 base: PlaceRef<'tcx>,
1843 flow_state: &Flows<'cx, 'tcx>,
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.
1850 // In other words, *until* we allow this:
1852 // 1. `let mut s; s.x = Val; read(s.x);`
1854 // we will for now disallow this:
1856 // 2. `let mut s; s.x = Val;`
1860 // 3. `let mut s = ...; drop(s); s.x=Val;`
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
1866 // `let mut s = ...; drop(s.x); s.x=Val;`
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`.
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;
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 };
1884 if maybe_uninits.contains(mpi) {
1886 "check_parent_of_field updating shortest_uninit_seen from {:?} to {:?}",
1887 shortest_uninit_seen,
1890 shortest_uninit_seen = Some((prefix, mpi));
1892 debug!("check_parent_of_field {:?} is definitely initialized", (prefix, mpi));
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)
1911 this.report_use_of_moved_or_uninitialized(
1913 InitializationRequiringAction::PartialAssignment,
1914 (prefix, base, span),
1921 /// Checks the permissions for the given place and read or write kind
1923 /// Returns `true` if an error is reported.
1924 fn check_access_permissions(
1926 (place, span): (Place<'tcx>, Span),
1928 is_local_mutation_allowed: LocalMutationIsAllowed,
1929 flow_state: &Flows<'cx, 'tcx>,
1933 "check_access_permissions({:?}, {:?}, is_local_mutation_allowed: {:?})",
1934 place, kind, is_local_mutation_allowed
1941 Reservation(WriteKind::MutableBorrow(
1942 borrow_kind @ (BorrowKind::Unique | BorrowKind::Mut { .. }),
1944 | Write(WriteKind::MutableBorrow(
1945 borrow_kind @ (BorrowKind::Unique | BorrowKind::Mut { .. }),
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!(),
1952 match self.is_mutable(place.as_ref(), is_local_mutation_allowed) {
1954 self.add_used_mut(root_place, flow_state);
1958 error_access = AccessKind::MutableBorrow;
1959 the_place_err = place_err;
1963 Reservation(WriteKind::Mutate) | Write(WriteKind::Mutate) => {
1964 match self.is_mutable(place.as_ref(), is_local_mutation_allowed) {
1966 self.add_used_mut(root_place, flow_state);
1970 error_access = AccessKind::Mutate;
1971 the_place_err = place_err;
1978 | WriteKind::StorageDeadOrDrop
1979 | WriteKind::MutableBorrow(BorrowKind::Shared)
1980 | WriteKind::MutableBorrow(BorrowKind::Shallow),
1984 | WriteKind::StorageDeadOrDrop
1985 | WriteKind::MutableBorrow(BorrowKind::Shared)
1986 | WriteKind::MutableBorrow(BorrowKind::Shallow),
1988 if self.is_mutable(place.as_ref(), is_local_mutation_allowed).is_err()
1989 && !self.has_buffered_errors()
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(
1999 "Accessing `{:?}` with the kind `{:?}` shouldn't be possible",
2007 // permission checks are done at Reservation point.
2013 | BorrowKind::Mut { .. }
2014 | BorrowKind::Shared
2015 | BorrowKind::Shallow,
2019 // Access authorized
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();
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);
2040 fn is_local_ever_initialized(
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];
2048 if flow_state.ever_inits.contains(index) {
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>) {
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()
2065 self.used_mut.insert(local);
2070 place_projection: _,
2071 is_local_mutation_allowed: LocalMutationIsAllowed::Yes,
2075 place_projection: place_projection @ [.., _],
2076 is_local_mutation_allowed: _,
2078 if let Some(field) = self.is_upvar_field_projection(PlaceRef {
2080 projection: place_projection,
2082 self.used_mut_upvars.push(field);
2088 /// Whether this value can be written or borrowed mutably.
2089 /// Returns the root place if the place passed in is a projection.
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() {
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,
2106 LocalMutationIsAllowed::ExceptUpvars => Ok(RootPlace {
2107 place_local: place.local,
2108 place_projection: place.projection,
2109 is_local_mutation_allowed: LocalMutationIsAllowed::ExceptUpvars,
2111 LocalMutationIsAllowed::No => Err(place),
2113 Mutability::Mut => Ok(RootPlace {
2114 place_local: place.local,
2115 place_projection: place.projection,
2116 is_local_mutation_allowed,
2120 Some((place_base, elem)) => {
2122 ProjectionElem::Deref => {
2123 let base_ty = place_base.ty(self.body(), self.infcx.tcx).ty;
2125 // Check the kind of deref to decide
2126 match base_ty.kind() {
2127 ty::Ref(_, _, 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
2138 _ => LocalMutationIsAllowed::Yes,
2141 self.is_mutable(place_base, mode)
2145 ty::RawPtr(tnm) => {
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,
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)
2162 // Deref should only be for reference, pointers or boxes
2163 _ => bug!("Deref of unexpected type: {:?}", base_ty),
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()];
2178 "is_mutable: upvar.mutability={:?} local_mutation_is_allowed={:?} \
2179 place={:?}, place_base={:?}",
2180 upvar, is_local_mutation_allowed, place, place_base
2182 match (upvar.place.mutability, is_local_mutation_allowed) {
2185 LocalMutationIsAllowed::No
2186 | LocalMutationIsAllowed::ExceptUpvars,
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`
2208 // fn foo<F: FnOnce()>(_f: F) { }
2210 // let var = Vec::new();
2217 self.is_mutable(place_base, is_local_mutation_allowed)?;
2219 place_local: place.local,
2220 place_projection: place.projection,
2221 is_local_mutation_allowed,
2226 self.is_mutable(place_base, is_local_mutation_allowed)
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())
2244 use rustc_errors::ErrorGuaranteed;
2248 pub struct BorrowckErrors<'tcx> {
2249 /// This field keeps track of move errors that are to be reported for given move indices.
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.
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
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>,
2271 impl BorrowckErrors<'_> {
2272 pub fn new() -> Self {
2274 buffered_move_errors: BTreeMap::new(),
2275 buffered: Default::default(),
2276 tainted_by_errors: None,
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);
2287 pub fn buffer_non_error_diag(&mut self, t: DiagnosticBuilder<'_, ()>) {
2288 t.buffer(&mut self.buffered);
2291 pub fn set_tainted_by_errors(&mut self) {
2292 self.tainted_by_errors = Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
2296 impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
2297 pub fn buffer_error(&mut self, t: DiagnosticBuilder<'_, ErrorGuaranteed>) {
2298 self.errors.buffer_error(t);
2301 pub fn buffer_non_error_diag(&mut self, t: DiagnosticBuilder<'_, ()>) {
2302 self.errors.buffer_non_error_diag(t);
2305 pub fn buffer_move_error(
2307 move_out_indices: Vec<MoveOutIndex>,
2308 place_and_err: (PlaceRef<'tcx>, DiagnosticBuilder<'tcx, ErrorGuaranteed>),
2310 if let Some((_, diag)) =
2311 self.errors.buffered_move_errors.insert(move_out_indices, place_and_err)
2313 // Cancel the old diagnostic so we don't ICE
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);
2328 if !self.errors.buffered.is_empty() {
2329 self.errors.buffered.sort_by_key(|diag| diag.sort_span);
2331 for mut diag in self.errors.buffered.drain(..) {
2332 self.infcx.tcx.sess.diagnostic().emit_diagnostic(&mut diag);
2336 self.errors.tainted_by_errors
2339 pub fn has_buffered_errors(&self) -> bool {
2340 self.errors.buffered.is_empty()
2343 pub fn has_move_error(
2345 move_out_indices: &[MoveOutIndex],
2346 ) -> Option<&(PlaceRef<'tcx>, DiagnosticBuilder<'cx, ErrorGuaranteed>)> {
2347 self.errors.buffered_move_errors.get(move_out_indices)
2352 /// The degree of overlap between 2 places for borrow-checking.
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.
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
2365 /// The places are disjoint, so we know all extensions of them
2366 /// will also be disjoint.