1//! This crate implemens MIR typeck and MIR borrowck.23// tidy-alphabetical-start4#![allow(internal_features)]5#![feature(box_patterns)]6#![feature(default_field_values)]7#![feature(file_buffered)]8#![feature(negative_impls)]9#![feature(never_type)]10#![feature(rustc_attrs)]11#![feature(stmt_expr_attributes)]12#![feature(try_blocks)]13// tidy-alphabetical-end1415use std::borrow::Cow;16use std::cell::{OnceCell, RefCell};17use std::marker::PhantomData;18use std::ops::{ControlFlow, Deref};19use std::rc::Rc;2021use borrow_set::LocalsStateAtExit;22use polonius_engine::AllFacts;23use root_cx::BorrowCheckRootCtxt;24use rustc_abi::FieldIdx;25use rustc_data_structures::frozen::Frozen;26use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};27use rustc_data_structures::graph::dominators::Dominators;28use rustc_hir as hir;29use rustc_hir::CRATE_HIR_ID;30use rustc_hir::def_id::LocalDefId;31use rustc_index::bit_set::MixedBitSet;32use rustc_index::{IndexSlice, IndexVec};33use rustc_infer::infer::outlives::env::RegionBoundPairs;34use rustc_infer::infer::{35 InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin, TyCtxtInferExt,36};37use rustc_middle::mir::*;38use rustc_middle::query::Providers;39use rustc_middle::ty::{40 self, ParamEnv, RegionVid, Ty, TyCtxt, TypeFoldable, TypeVisitable, TypingMode, fold_regions,41};42use rustc_middle::{bug, span_bug};43use rustc_mir_dataflow::impls::{EverInitializedPlaces, MaybeUninitializedPlaces};44use rustc_mir_dataflow::move_paths::{45 InitIndex, InitLocation, LookupResult, MoveData, MovePathIndex,46};47use rustc_mir_dataflow::points::DenseLocationMap;48use rustc_mir_dataflow::{Analysis, EntryStates, Results, ResultsVisitor, visit_results};49use rustc_session::lint::builtin::{TAIL_EXPR_DROP_ORDER, UNUSED_MUT};50use rustc_span::{ErrorGuaranteed, Span, Symbol};51use smallvec::SmallVec;52use tracing::{debug, instrument};5354use crate::borrow_set::{BorrowData, BorrowSet};55use crate::consumers::{BodyWithBorrowckFacts, RustcFacts};56use crate::dataflow::{BorrowIndex, Borrowck, BorrowckDomain, Borrows};57use crate::diagnostics::{58 AccessKind, BorrowckDiagnosticsBuffer, IllegalMoveOriginKind, MoveError, RegionName,59};60use crate::path_utils::*;61use crate::place_ext::PlaceExt;62use crate::places_conflict::{PlaceConflictBias, places_conflict};63use crate::polonius::PoloniusContext;64use crate::polonius::legacy::{65 PoloniusFacts, PoloniusFactsExt, PoloniusLocationTable, PoloniusOutput,66};67use crate::prefixes::PrefixSet;68use crate::region_infer::RegionInferenceContext;69use crate::region_infer::opaque_types::DeferredOpaqueTypeError;70use crate::renumber::RegionCtxt;71use crate::session_diagnostics::VarNeedNotMut;72use crate::type_check::free_region_relations::UniversalRegionRelations;73use crate::type_check::{Locations, MirTypeckRegionConstraints, MirTypeckResults};7475mod borrow_set;76mod borrowck_errors;77mod constraints;78mod dataflow;79mod def_use;80mod diagnostics;81mod handle_placeholders;82mod nll;83mod path_utils;84mod place_ext;85mod places_conflict;86mod polonius;87mod prefixes;88mod region_infer;89mod renumber;90mod root_cx;91mod session_diagnostics;92mod type_check;93mod universal_regions;94mod used_muts;9596/// A public API provided for the Rust compiler consumers.97pub mod consumers;9899/// Associate some local constants with the `'tcx` lifetime100struct TyCtxtConsts<'tcx>(PhantomData<&'tcx ()>);101102impl<'tcx> TyCtxtConsts<'tcx> {103 const DEREF_PROJECTION: &'tcx [PlaceElem<'tcx>; 1] = &[ProjectionElem::Deref];104}105106pub fn provide(providers: &mut Providers) {107 *providers = Providers { mir_borrowck, ..*providers };108}109110/// Provider for `query mir_borrowck`. Unlike `typeck`, this must111/// only be called for typeck roots which *similar* to `typeck` will112/// then borrowck all nested bodies as well.113fn mir_borrowck(114 tcx: TyCtxt<'_>,115 def: LocalDefId,116) -> Result<&FxIndexMap<LocalDefId, ty::DefinitionSiteHiddenType<'_>>, ErrorGuaranteed> {117 assert!(!tcx.is_typeck_child(def.to_def_id()));118 let (input_body, _) = tcx.mir_promoted(def);119 debug!("run query mir_borrowck: {}", tcx.def_path_str(def));120121 // We should eagerly check stalled coroutine obligations from HIR typeck.122 // Not doing so leads to silent normalization failures later, which will123 // fail to register opaque types in the next solver.124 tcx.ensure_result().check_coroutine_obligations(def)?;125126 let input_body: &Body<'_> = &input_body.borrow();127 if let Some(guar) = input_body.tainted_by_errors {128 debug!("Skipping borrowck because of tainted body");129 Err(guar)130 } else if input_body.should_skip() {131 debug!("Skipping borrowck because of injected body");132 let opaque_types = Default::default();133 Ok(tcx.arena.alloc(opaque_types))134 } else {135 let mut root_cx = BorrowCheckRootCtxt::new(tcx, def, None);136 root_cx.do_mir_borrowck();137 root_cx.finalize()138 }139}140141/// Data propagated to the typeck parent by nested items.142/// This should always be empty for the typeck root.143#[derive(Debug)]144struct PropagatedBorrowCheckResults<'tcx> {145 closure_requirements: Option<ClosureRegionRequirements<'tcx>>,146 used_mut_upvars: SmallVec<[FieldIdx; 8]>,147}148149type DeferredClosureRequirements<'tcx> = Vec<(LocalDefId, ty::GenericArgsRef<'tcx>, Locations)>;150151/// After we borrow check a closure, we are left with various152/// requirements that we have inferred between the free regions that153/// appear in the closure's signature or on its field types. These154/// requirements are then verified and proved by the closure's155/// creating function. This struct encodes those requirements.156///157/// The requirements are listed as being between various `RegionVid`. The 0th158/// region refers to `'static`; subsequent region vids refer to the free159/// regions that appear in the closure (or coroutine's) type, in order of160/// appearance. (This numbering is actually defined by the `UniversalRegions`161/// struct in the NLL region checker. See for example162/// `UniversalRegions::closure_mapping`.) Note the free regions in the163/// closure's signature and captures are erased.164///165/// Example: If type check produces a closure with the closure args:166///167/// ```text168/// ClosureArgs = [169/// 'a, // From the parent.170/// 'b,171/// i8, // the "closure kind"172/// for<'x> fn(&'<erased> &'x u32) -> &'x u32, // the "closure signature"173/// &'<erased> String, // some upvar174/// ]175/// ```176///177/// We would "renumber" each free region to a unique vid, as follows:178///179/// ```text180/// ClosureArgs = [181/// '1, // From the parent.182/// '2,183/// i8, // the "closure kind"184/// for<'x> fn(&'3 &'x u32) -> &'x u32, // the "closure signature"185/// &'4 String, // some upvar186/// ]187/// ```188///189/// Now the code might impose a requirement like `'1: '2`. When an190/// instance of the closure is created, the corresponding free regions191/// can be extracted from its type and constrained to have the given192/// outlives relationship.193#[derive(Clone, Debug)]194pub struct ClosureRegionRequirements<'tcx> {195 /// The number of external regions defined on the closure. In our196 /// example above, it would be 3 -- one for `'static`, then `'1`197 /// and `'2`. This is just used for a sanity check later on, to198 /// make sure that the number of regions we see at the callsite199 /// matches.200 pub num_external_vids: usize,201202 /// Requirements between the various free regions defined in203 /// indices.204 pub outlives_requirements: Vec<ClosureOutlivesRequirement<'tcx>>,205}206207/// Indicates an outlives-constraint between a type or between two208/// free regions declared on the closure.209#[derive(Copy, Clone, Debug)]210pub struct ClosureOutlivesRequirement<'tcx> {211 // This region or type ...212 pub subject: ClosureOutlivesSubject<'tcx>,213214 // ... must outlive this one.215 pub outlived_free_region: ty::RegionVid,216217 // If not, report an error here ...218 pub blame_span: Span,219220 // ... due to this reason.221 pub category: ConstraintCategory<'tcx>,222}223224// Make sure this enum doesn't unintentionally grow225#[cfg(target_pointer_width = "64")]226rustc_data_structures::static_assert_size!(ConstraintCategory<'_>, 16);227228/// The subject of a `ClosureOutlivesRequirement` -- that is, the thing229/// that must outlive some region.230#[derive(Copy, Clone, Debug)]231pub enum ClosureOutlivesSubject<'tcx> {232 /// Subject is a type, typically a type parameter, but could also233 /// be a projection. Indicates a requirement like `T: 'a` being234 /// passed to the caller, where the type here is `T`.235 Ty(ClosureOutlivesSubjectTy<'tcx>),236237 /// Subject is a free region from the closure. Indicates a requirement238 /// like `'a: 'b` being passed to the caller; the region here is `'a`.239 Region(ty::RegionVid),240}241242/// Represents a `ty::Ty` for use in [`ClosureOutlivesSubject`].243///244/// This abstraction is necessary because the type may include `ReVar` regions,245/// which is what we use internally within NLL code, and they can't be used in246/// a query response.247#[derive(Copy, Clone, Debug)]248pub struct ClosureOutlivesSubjectTy<'tcx> {249 inner: Ty<'tcx>,250}251// DO NOT implement `TypeVisitable` or `TypeFoldable` traits, because this252// type is not recognized as a binder for late-bound region.253impl<'tcx, I> !TypeVisitable<I> for ClosureOutlivesSubjectTy<'tcx> {}254impl<'tcx, I> !TypeFoldable<I> for ClosureOutlivesSubjectTy<'tcx> {}255256impl<'tcx> ClosureOutlivesSubjectTy<'tcx> {257 /// All regions of `ty` must be of kind `ReVar` and must represent258 /// universal regions *external* to the closure.259 pub fn bind(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self {260 let inner = fold_regions(tcx, ty, |r, depth| match r.kind() {261 ty::ReVar(vid) => {262 let br = ty::BoundRegion {263 var: ty::BoundVar::from_usize(vid.index()),264 kind: ty::BoundRegionKind::Anon,265 };266 ty::Region::new_bound(tcx, depth, br)267 }268 _ => bug!("unexpected region in ClosureOutlivesSubjectTy: {r:?}"),269 });270271 Self { inner }272 }273274 pub fn instantiate(275 self,276 tcx: TyCtxt<'tcx>,277 mut map: impl FnMut(ty::RegionVid) -> ty::Region<'tcx>,278 ) -> Ty<'tcx> {279 fold_regions(tcx, self.inner, |r, depth| match r.kind() {280 ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), br) => {281 debug_assert_eq!(debruijn, depth);282 map(ty::RegionVid::from_usize(br.var.index()))283 }284 _ => bug!("unexpected region {r:?}"),285 })286 }287}288289struct CollectRegionConstraintsResult<'tcx> {290 infcx: BorrowckInferCtxt<'tcx>,291 body_owned: Body<'tcx>,292 promoted: IndexVec<Promoted, Body<'tcx>>,293 move_data: MoveData<'tcx>,294 borrow_set: BorrowSet<'tcx>,295 location_table: PoloniusLocationTable,296 location_map: Rc<DenseLocationMap>,297 universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,298 region_bound_pairs: Frozen<RegionBoundPairs<'tcx>>,299 known_type_outlives_obligations: Frozen<Vec<ty::PolyTypeOutlivesPredicate<'tcx>>>,300 constraints: MirTypeckRegionConstraints<'tcx>,301 deferred_closure_requirements: DeferredClosureRequirements<'tcx>,302 deferred_opaque_type_errors: Vec<DeferredOpaqueTypeError<'tcx>>,303 polonius_facts: Option<AllFacts<RustcFacts>>,304 polonius_context: Option<PoloniusContext>,305}306307/// Start borrow checking by collecting the region constraints for308/// the current body. This initializes the relevant data structures309/// and then type checks the MIR body.310fn borrowck_collect_region_constraints<'tcx>(311 root_cx: &mut BorrowCheckRootCtxt<'tcx>,312 def: LocalDefId,313) -> CollectRegionConstraintsResult<'tcx> {314 let tcx = root_cx.tcx;315 let infcx = BorrowckInferCtxt::new(tcx, def, root_cx.root_def_id());316 let (input_body, promoted) = tcx.mir_promoted(def);317 let input_body: &Body<'_> = &input_body.borrow();318 let input_promoted: &IndexSlice<_, _> = &promoted.borrow();319 if let Some(e) = input_body.tainted_by_errors {320 infcx.set_tainted_by_errors(e);321 root_cx.set_tainted_by_errors(e);322 }323324 // Replace all regions with fresh inference variables. This325 // requires first making our own copy of the MIR. This copy will326 // be modified (in place) to contain non-lexical lifetimes. It327 // will have a lifetime tied to the inference context.328 let mut body_owned = input_body.clone();329 let mut promoted = input_promoted.to_owned();330 let universal_regions = nll::replace_regions_in_mir(&infcx, &mut body_owned, &mut promoted);331 let body = &body_owned; // no further changes332333 let location_table = PoloniusLocationTable::new(body);334335 let move_data = MoveData::gather_moves(body, tcx, |_| true);336337 let locals_are_invalidated_at_exit = tcx.hir_body_owner_kind(def).is_fn_or_closure();338 let borrow_set = BorrowSet::build(tcx, body, locals_are_invalidated_at_exit, &move_data);339340 let location_map = Rc::new(DenseLocationMap::new(body));341342 let polonius_input = root_cx.consumer.as_ref().map_or(false, |c| c.polonius_input())343 || infcx.tcx.sess.opts.unstable_opts.polonius.is_legacy_enabled();344 let mut polonius_facts =345 (polonius_input || PoloniusFacts::enabled(infcx.tcx)).then_some(PoloniusFacts::default());346347 // Run the MIR type-checker.348 let MirTypeckResults {349 constraints,350 universal_region_relations,351 region_bound_pairs,352 known_type_outlives_obligations,353 deferred_closure_requirements,354 polonius_context,355 } = type_check::type_check(356 root_cx,357 &infcx,358 body,359 &promoted,360 universal_regions,361 &location_table,362 &borrow_set,363 &mut polonius_facts,364 &move_data,365 Rc::clone(&location_map),366 );367368 CollectRegionConstraintsResult {369 infcx,370 body_owned,371 promoted,372 move_data,373 borrow_set,374 location_table,375 location_map,376 universal_region_relations,377 region_bound_pairs,378 known_type_outlives_obligations,379 constraints,380 deferred_closure_requirements,381 deferred_opaque_type_errors: Default::default(),382 polonius_facts,383 polonius_context,384 }385}386387/// Using the region constraints computed by [borrowck_collect_region_constraints]388/// and the additional constraints from [BorrowCheckRootCtxt::handle_opaque_type_uses],389/// compute the region graph and actually check for any borrowck errors.390fn borrowck_check_region_constraints<'tcx>(391 root_cx: &mut BorrowCheckRootCtxt<'tcx>,392 CollectRegionConstraintsResult {393 infcx,394 body_owned,395 promoted,396 move_data,397 borrow_set,398 location_table,399 location_map,400 universal_region_relations,401 region_bound_pairs: _,402 known_type_outlives_obligations: _,403 constraints,404 deferred_closure_requirements,405 deferred_opaque_type_errors,406 polonius_facts,407 polonius_context,408 }: CollectRegionConstraintsResult<'tcx>,409) -> PropagatedBorrowCheckResults<'tcx> {410 assert!(!infcx.has_opaque_types_in_storage());411 assert!(deferred_closure_requirements.is_empty());412 let tcx = root_cx.tcx;413 let body = &body_owned;414 let def = body.source.def_id().expect_local();415416 // Compute non-lexical lifetimes using the constraints computed417 // by typechecking the MIR body.418 let nll::NllOutput {419 regioncx,420 polonius_input,421 polonius_output,422 opt_closure_req,423 nll_errors,424 polonius_context,425 } = nll::compute_regions(426 root_cx,427 &infcx,428 body,429 &location_table,430 &move_data,431 &borrow_set,432 location_map,433 universal_region_relations,434 constraints,435 polonius_facts,436 polonius_context,437 );438439 // Dump MIR results into a file, if that is enabled. This lets us440 // write unit-tests, as well as helping with debugging.441 nll::dump_nll_mir(&infcx, body, ®ioncx, &opt_closure_req, &borrow_set);442 polonius::dump_polonius_mir(443 &infcx,444 body,445 ®ioncx,446 &opt_closure_req,447 &borrow_set,448 polonius_context.as_ref(),449 );450451 // We also have a `#[rustc_regions]` annotation that causes us to dump452 // information.453 nll::dump_annotation(&infcx, body, ®ioncx, &opt_closure_req);454455 let movable_coroutine = body.coroutine.is_some()456 && tcx.coroutine_movability(def.to_def_id()) == hir::Movability::Movable;457458 let diags_buffer = &mut BorrowckDiagnosticsBuffer::default();459 // While promoteds should mostly be correct by construction, we need to check them for460 // invalid moves to detect moving out of arrays:`struct S; fn main() { &([S][0]); }`.461 for promoted_body in &promoted {462 use rustc_middle::mir::visit::Visitor;463 // This assumes that we won't use some of the fields of the `promoted_mbcx`464 // when detecting and reporting move errors. While it would be nice to move465 // this check out of `MirBorrowckCtxt`, actually doing so is far from trivial.466 let move_data = MoveData::gather_moves(promoted_body, tcx, |_| true);467 let mut promoted_mbcx = MirBorrowckCtxt {468 root_cx,469 infcx: &infcx,470 body: promoted_body,471 move_data: &move_data,472 // no need to create a real location table for the promoted, it is not used473 location_table: &location_table,474 movable_coroutine,475 fn_self_span_reported: Default::default(),476 access_place_error_reported: Default::default(),477 reservation_error_reported: Default::default(),478 uninitialized_error_reported: Default::default(),479 regioncx: ®ioncx,480 used_mut: Default::default(),481 used_mut_upvars: SmallVec::new(),482 borrow_set: &borrow_set,483 upvars: &[],484 local_names: OnceCell::from(IndexVec::from_elem(None, &promoted_body.local_decls)),485 region_names: RefCell::default(),486 next_region_name: RefCell::new(1),487 polonius_output: None,488 move_errors: Vec::new(),489 diags_buffer,490 polonius_context: polonius_context.as_ref(),491 };492 struct MoveVisitor<'a, 'b, 'infcx, 'tcx> {493 ctxt: &'a mut MirBorrowckCtxt<'b, 'infcx, 'tcx>,494 }495496 impl<'tcx> Visitor<'tcx> for MoveVisitor<'_, '_, '_, 'tcx> {497 fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {498 if let Operand::Move(place) = operand {499 self.ctxt.check_movable_place(location, *place);500 }501 }502 }503 MoveVisitor { ctxt: &mut promoted_mbcx }.visit_body(promoted_body);504 promoted_mbcx.report_move_errors();505 }506507 let mut mbcx = MirBorrowckCtxt {508 root_cx,509 infcx: &infcx,510 body,511 move_data: &move_data,512 location_table: &location_table,513 movable_coroutine,514 fn_self_span_reported: Default::default(),515 access_place_error_reported: Default::default(),516 reservation_error_reported: Default::default(),517 uninitialized_error_reported: Default::default(),518 regioncx: ®ioncx,519 used_mut: Default::default(),520 used_mut_upvars: SmallVec::new(),521 borrow_set: &borrow_set,522 upvars: tcx.closure_captures(def),523 local_names: OnceCell::new(),524 region_names: RefCell::default(),525 next_region_name: RefCell::new(1),526 move_errors: Vec::new(),527 diags_buffer,528 polonius_output: polonius_output.as_deref(),529 polonius_context: polonius_context.as_ref(),530 };531532 // Compute and report region errors, if any.533 if nll_errors.is_empty() {534 mbcx.report_opaque_type_errors(deferred_opaque_type_errors);535 } else {536 mbcx.report_region_errors(nll_errors);537 }538539 let flow_results = get_flow_results(tcx, body, &move_data, &borrow_set, ®ioncx);540 visit_results(541 body,542 traversal::reverse_postorder(body).map(|(bb, _)| bb),543 &flow_results,544 &mut mbcx,545 );546547 mbcx.report_move_errors();548549 // For each non-user used mutable variable, check if it's been assigned from550 // a user-declared local. If so, then put that local into the used_mut set.551 // Note that this set is expected to be small - only upvars from closures552 // would have a chance of erroneously adding non-user-defined mutable vars553 // to the set.554 let temporary_used_locals: FxIndexSet<Local> = mbcx555 .used_mut556 .iter()557 .filter(|&local| !mbcx.body.local_decls[*local].is_user_variable())558 .cloned()559 .collect();560 // For the remaining unused locals that are marked as mutable, we avoid linting any that561 // were never initialized. These locals may have been removed as unreachable code; or will be562 // linted as unused variables.563 let unused_mut_locals =564 mbcx.body.mut_vars_iter().filter(|local| !mbcx.used_mut.contains(local)).collect();565 mbcx.gather_used_muts(temporary_used_locals, unused_mut_locals);566567 debug!("mbcx.used_mut: {:?}", mbcx.used_mut);568 mbcx.lint_unused_mut();569 if let Some(guar) = mbcx.emit_errors() {570 mbcx.root_cx.set_tainted_by_errors(guar);571 }572573 let result = PropagatedBorrowCheckResults {574 closure_requirements: opt_closure_req,575 used_mut_upvars: mbcx.used_mut_upvars,576 };577578 if let Some(consumer) = &mut root_cx.consumer {579 consumer.insert_body(580 def,581 BodyWithBorrowckFacts {582 body: body_owned,583 promoted,584 borrow_set,585 region_inference_context: regioncx,586 location_table: polonius_input.as_ref().map(|_| location_table),587 input_facts: polonius_input,588 output_facts: polonius_output,589 },590 );591 }592593 debug!("do_mir_borrowck: result = {:#?}", result);594595 result596}597598fn get_flow_results<'a, 'tcx>(599 tcx: TyCtxt<'tcx>,600 body: &'a Body<'tcx>,601 move_data: &'a MoveData<'tcx>,602 borrow_set: &'a BorrowSet<'tcx>,603 regioncx: &RegionInferenceContext<'tcx>,604) -> Results<'tcx, Borrowck<'a, 'tcx>> {605 // We compute these three analyses individually, but them combine them into606 // a single results so that `mbcx` can visit them all together.607 let borrows = Borrows::new(tcx, body, regioncx, borrow_set).iterate_to_fixpoint(608 tcx,609 body,610 Some("borrowck"),611 );612 let uninits = MaybeUninitializedPlaces::new(tcx, body, move_data).iterate_to_fixpoint(613 tcx,614 body,615 Some("borrowck"),616 );617 let ever_inits = EverInitializedPlaces::new(body, move_data).iterate_to_fixpoint(618 tcx,619 body,620 Some("borrowck"),621 );622623 let analysis = Borrowck {624 borrows: borrows.analysis,625 uninits: uninits.analysis,626 ever_inits: ever_inits.analysis,627 };628629 assert_eq!(borrows.entry_states.len(), uninits.entry_states.len());630 assert_eq!(borrows.entry_states.len(), ever_inits.entry_states.len());631 let entry_states: EntryStates<_> =632 itertools::izip!(borrows.entry_states, uninits.entry_states, ever_inits.entry_states)633 .map(|(borrows, uninits, ever_inits)| BorrowckDomain { borrows, uninits, ever_inits })634 .collect();635636 Results { analysis, entry_states }637}638639pub(crate) struct BorrowckInferCtxt<'tcx> {640 pub(crate) infcx: InferCtxt<'tcx>,641 pub(crate) root_def_id: LocalDefId,642 pub(crate) param_env: ParamEnv<'tcx>,643 pub(crate) reg_var_to_origin: RefCell<FxIndexMap<ty::RegionVid, RegionCtxt>>,644}645646impl<'tcx> BorrowckInferCtxt<'tcx> {647 pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId, root_def_id: LocalDefId) -> Self {648 let typing_mode = if tcx.use_typing_mode_borrowck() {649 TypingMode::borrowck(tcx, def_id)650 } else {651 TypingMode::analysis_in_body(tcx, def_id)652 };653 let infcx = tcx.infer_ctxt().build(typing_mode);654 let param_env = tcx.param_env(def_id);655 BorrowckInferCtxt {656 infcx,657 root_def_id,658 reg_var_to_origin: RefCell::new(Default::default()),659 param_env,660 }661 }662663 pub(crate) fn next_region_var<F>(664 &self,665 origin: RegionVariableOrigin<'tcx>,666 get_ctxt_fn: F,667 ) -> ty::Region<'tcx>668 where669 F: Fn() -> RegionCtxt,670 {671 let next_region = self.infcx.next_region_var(origin);672 let vid = next_region.as_var();673674 if cfg!(debug_assertions) {675 debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin);676 let ctxt = get_ctxt_fn();677 let mut var_to_origin = self.reg_var_to_origin.borrow_mut();678 assert_eq!(var_to_origin.insert(vid, ctxt), None);679 }680681 next_region682 }683684 #[instrument(skip(self, get_ctxt_fn), level = "debug")]685 pub(crate) fn next_nll_region_var<F>(686 &self,687 origin: NllRegionVariableOrigin<'tcx>,688 get_ctxt_fn: F,689 ) -> ty::Region<'tcx>690 where691 F: Fn() -> RegionCtxt,692 {693 let next_region = self.infcx.next_nll_region_var(origin);694 let vid = next_region.as_var();695696 if cfg!(debug_assertions) {697 debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin);698 let ctxt = get_ctxt_fn();699 let mut var_to_origin = self.reg_var_to_origin.borrow_mut();700 assert_eq!(var_to_origin.insert(vid, ctxt), None);701 }702703 next_region704 }705}706707impl<'tcx> Deref for BorrowckInferCtxt<'tcx> {708 type Target = InferCtxt<'tcx>;709710 fn deref(&self) -> &Self::Target {711 &self.infcx712 }713}714715pub(crate) struct MirBorrowckCtxt<'a, 'infcx, 'tcx> {716 root_cx: &'a mut BorrowCheckRootCtxt<'tcx>,717 infcx: &'infcx BorrowckInferCtxt<'tcx>,718 body: &'a Body<'tcx>,719 move_data: &'a MoveData<'tcx>,720721 /// Map from MIR `Location` to `LocationIndex`; created722 /// when MIR borrowck begins.723 location_table: &'a PoloniusLocationTable,724725 movable_coroutine: bool,726 /// This field keeps track of when borrow errors are reported in the access_place function727 /// so that there is no duplicate reporting. This field cannot also be used for the conflicting728 /// borrow errors that is handled by the `reservation_error_reported` field as the inclusion729 /// of the `Span` type (while required to mute some errors) stops the muting of the reservation730 /// errors.731 access_place_error_reported: FxIndexSet<(Place<'tcx>, Span)>,732 /// This field keeps track of when borrow conflict errors are reported733 /// for reservations, so that we don't report seemingly duplicate734 /// errors for corresponding activations.735 //736 // FIXME: ideally this would be a set of `BorrowIndex`, not `Place`s,737 // but it is currently inconvenient to track down the `BorrowIndex`738 // at the time we detect and report a reservation error.739 reservation_error_reported: FxIndexSet<Place<'tcx>>,740 /// This fields keeps track of the `Span`s that we have741 /// used to report extra information for `FnSelfUse`, to avoid742 /// unnecessarily verbose errors.743 fn_self_span_reported: FxIndexSet<Span>,744 /// This field keeps track of errors reported in the checking of uninitialized variables,745 /// so that we don't report seemingly duplicate errors.746 uninitialized_error_reported: FxIndexSet<Local>,747 /// This field keeps track of all the local variables that are declared mut and are mutated.748 /// Used for the warning issued by an unused mutable local variable.749 used_mut: FxIndexSet<Local>,750 /// If the function we're checking is a closure, then we'll need to report back the list of751 /// mutable upvars that have been used. This field keeps track of them.752 used_mut_upvars: SmallVec<[FieldIdx; 8]>,753 /// Region inference context. This contains the results from region inference and lets us e.g.754 /// find out which CFG points are contained in each borrow region.755 regioncx: &'a RegionInferenceContext<'tcx>,756757 /// The set of borrows extracted from the MIR758 borrow_set: &'a BorrowSet<'tcx>,759760 /// Information about upvars not necessarily preserved in types or MIR761 upvars: &'tcx [&'tcx ty::CapturedPlace<'tcx>],762763 /// Names of local (user) variables (extracted from `var_debug_info`).764 local_names: OnceCell<IndexVec<Local, Option<Symbol>>>,765766 /// Record the region names generated for each region in the given767 /// MIR def so that we can reuse them later in help/error messages.768 region_names: RefCell<FxIndexMap<RegionVid, RegionName>>,769770 /// The counter for generating new region names.771 next_region_name: RefCell<usize>,772773 diags_buffer: &'a mut BorrowckDiagnosticsBuffer<'infcx, 'tcx>,774 move_errors: Vec<MoveError<'tcx>>,775776 /// Results of Polonius analysis.777 polonius_output: Option<&'a PoloniusOutput>,778 /// When using `-Zpolonius=next`: the data used to compute errors and diagnostics.779 polonius_context: Option<&'a PoloniusContext>,780}781782// Check that:783// 1. assignments are always made to mutable locations (FIXME: does that still really go here?)784// 2. loans made in overlapping scopes do not conflict785// 3. assignments do not affect things loaned out as immutable786// 4. moves do not affect things loaned out in any way787impl<'a, 'tcx> ResultsVisitor<'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a, '_, 'tcx> {788 fn visit_after_early_statement_effect(789 &mut self,790 _analysis: &Borrowck<'a, 'tcx>,791 state: &BorrowckDomain,792 stmt: &Statement<'tcx>,793 location: Location,794 ) {795 debug!("MirBorrowckCtxt::process_statement({:?}, {:?}): {:?}", location, stmt, state);796 let span = stmt.source_info.span;797798 self.check_activations(location, span, state);799800 match &stmt.kind {801 StatementKind::Assign(box (lhs, rhs)) => {802 self.consume_rvalue(location, (rhs, span), state);803804 self.mutate_place(location, (*lhs, span), Shallow(None), state);805 }806 StatementKind::FakeRead(box (_, place)) => {807 // Read for match doesn't access any memory and is used to808 // assert that a place is safe and live. So we don't have to809 // do any checks here.810 //811 // FIXME: Remove check that the place is initialized. This is812 // needed for now because matches don't have never patterns yet.813 // So this is the only place we prevent814 // let x: !;815 // match x {};816 // from compiling.817 self.check_if_path_or_subpath_is_moved(818 location,819 InitializationRequiringAction::Use,820 (place.as_ref(), span),821 state,822 );823 }824 StatementKind::Intrinsic(box kind) => match kind {825 NonDivergingIntrinsic::Assume(op) => {826 self.consume_operand(location, (op, span), state);827 }828 NonDivergingIntrinsic::CopyNonOverlapping(..) => span_bug!(829 span,830 "Unexpected CopyNonOverlapping, should only appear after lower_intrinsics",831 )832 }833 // Only relevant for mir typeck834 StatementKind::AscribeUserType(..)835 // Only relevant for liveness and unsafeck836 | StatementKind::PlaceMention(..)837 // Doesn't have any language semantics838 | StatementKind::Coverage(..)839 // These do not actually affect borrowck840 | StatementKind::ConstEvalCounter841 | StatementKind::StorageLive(..) => {}842 // This does not affect borrowck843 StatementKind::BackwardIncompatibleDropHint { place, reason: BackwardIncompatibleDropReason::Edition2024 } => {844 self.check_backward_incompatible_drop(location, **place, state);845 }846 StatementKind::StorageDead(local) => {847 self.access_place(848 location,849 (Place::from(*local), span),850 (Shallow(None), Write(WriteKind::StorageDeadOrDrop)),851 LocalMutationIsAllowed::Yes,852 state,853 );854 }855 StatementKind::Nop856 | StatementKind::SetDiscriminant { .. } => {857 bug!("Statement not allowed in this MIR phase")858 }859 }860 }861862 fn visit_after_early_terminator_effect(863 &mut self,864 _analysis: &Borrowck<'a, 'tcx>,865 state: &BorrowckDomain,866 term: &Terminator<'tcx>,867 loc: Location,868 ) {869 debug!("MirBorrowckCtxt::process_terminator({:?}, {:?}): {:?}", loc, term, state);870 let span = term.source_info.span;871872 self.check_activations(loc, span, state);873874 match &term.kind {875 TerminatorKind::SwitchInt { discr, targets: _ } => {876 self.consume_operand(loc, (discr, span), state);877 }878 TerminatorKind::Drop {879 place,880 target: _,881 unwind: _,882 replace,883 drop: _,884 async_fut: _,885 } => {886 debug!(887 "visit_terminator_drop \888 loc: {:?} term: {:?} place: {:?} span: {:?}",889 loc, term, place, span890 );891892 let write_kind =893 if *replace { WriteKind::Replace } else { WriteKind::StorageDeadOrDrop };894 self.access_place(895 loc,896 (*place, span),897 (AccessDepth::Drop, Write(write_kind)),898 LocalMutationIsAllowed::Yes,899 state,900 );901 }902 TerminatorKind::Call {903 func,904 args,905 destination,906 target: _,907 unwind: _,908 call_source: _,909 fn_span: _,910 } => {911 self.consume_operand(loc, (func, span), state);912 for arg in args {913 self.consume_operand(loc, (&arg.node, arg.span), state);914 }915 self.mutate_place(loc, (*destination, span), Deep, state);916 }917 TerminatorKind::TailCall { func, args, fn_span: _ } => {918 self.consume_operand(loc, (func, span), state);919 for arg in args {920 self.consume_operand(loc, (&arg.node, arg.span), state);921 }922 }923 TerminatorKind::Assert { cond, expected: _, msg, target: _, unwind: _ } => {924 self.consume_operand(loc, (cond, span), state);925 if let AssertKind::BoundsCheck { len, index } = &**msg {926 self.consume_operand(loc, (len, span), state);927 self.consume_operand(loc, (index, span), state);928 }929 }930931 TerminatorKind::Yield { value, resume: _, resume_arg, drop: _ } => {932 self.consume_operand(loc, (value, span), state);933 self.mutate_place(loc, (*resume_arg, span), Deep, state);934 }935936 TerminatorKind::InlineAsm {937 asm_macro: _,938 template: _,939 operands,940 options: _,941 line_spans: _,942 targets: _,943 unwind: _,944 } => {945 for op in operands {946 match op {947 InlineAsmOperand::In { reg: _, value } => {948 self.consume_operand(loc, (value, span), state);949 }950 InlineAsmOperand::Out { reg: _, late: _, place, .. } => {951 if let Some(place) = place {952 self.mutate_place(loc, (*place, span), Shallow(None), state);953 }954 }955 InlineAsmOperand::InOut { reg: _, late: _, in_value, out_place } => {956 self.consume_operand(loc, (in_value, span), state);957 if let &Some(out_place) = out_place {958 self.mutate_place(loc, (out_place, span), Shallow(None), state);959 }960 }961 InlineAsmOperand::Const { value: _ }962 | InlineAsmOperand::SymFn { value: _ }963 | InlineAsmOperand::SymStatic { def_id: _ }964 | InlineAsmOperand::Label { target_index: _ } => {}965 }966 }967 }968969 TerminatorKind::Goto { target: _ }970 | TerminatorKind::UnwindTerminate(_)971 | TerminatorKind::Unreachable972 | TerminatorKind::UnwindResume973 | TerminatorKind::Return974 | TerminatorKind::CoroutineDrop975 | TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }976 | TerminatorKind::FalseUnwind { real_target: _, unwind: _ } => {977 // no data used, thus irrelevant to borrowck978 }979 }980 }981982 fn visit_after_primary_terminator_effect(983 &mut self,984 _analysis: &Borrowck<'a, 'tcx>,985 state: &BorrowckDomain,986 term: &Terminator<'tcx>,987 loc: Location,988 ) {989 let span = term.source_info.span;990991 match term.kind {992 TerminatorKind::Yield { value: _, resume: _, resume_arg: _, drop: _ } => {993 if self.movable_coroutine {994 // Look for any active borrows to locals995 for i in state.borrows.iter() {996 let borrow = &self.borrow_set[i];997 self.check_for_local_borrow(borrow, span);998 }999 }1000 }10011002 TerminatorKind::UnwindResume1003 | TerminatorKind::Return1004 | TerminatorKind::TailCall { .. }1005 | TerminatorKind::CoroutineDrop => {1006 match self.borrow_set.locals_state_at_exit() {1007 LocalsStateAtExit::AllAreInvalidated => {1008 // Returning from the function implicitly kills storage for all locals and statics.1009 // Often, the storage will already have been killed by an explicit1010 // StorageDead, but we don't always emit those (notably on unwind paths),1011 // so this "extra check" serves as a kind of backup.1012 for i in state.borrows.iter() {1013 let borrow = &self.borrow_set[i];1014 self.check_for_invalidation_at_exit(loc, borrow, span);1015 }1016 }1017 // If we do not implicitly invalidate all locals on exit,1018 // we check for conflicts when dropping or moving this local.1019 LocalsStateAtExit::SomeAreInvalidated { has_storage_dead_or_moved: _ } => {}1020 }1021 }10221023 TerminatorKind::UnwindTerminate(_)1024 | TerminatorKind::Assert { .. }1025 | TerminatorKind::Call { .. }1026 | TerminatorKind::Drop { .. }1027 | TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }1028 | TerminatorKind::FalseUnwind { real_target: _, unwind: _ }1029 | TerminatorKind::Goto { .. }1030 | TerminatorKind::SwitchInt { .. }1031 | TerminatorKind::Unreachable1032 | TerminatorKind::InlineAsm { .. } => {}1033 }1034 }1035}10361037use self::AccessDepth::{Deep, Shallow};1038use self::ReadOrWrite::{Activation, Read, Reservation, Write};10391040#[derive(Copy, Clone, PartialEq, Eq, Debug)]1041enum ArtificialField {1042 ArrayLength,1043 FakeBorrow,1044}10451046#[derive(Copy, Clone, PartialEq, Eq, Debug)]1047enum AccessDepth {1048 /// From the RFC: "A *shallow* access means that the immediate1049 /// fields reached at P are accessed, but references or pointers1050 /// found within are not dereferenced. Right now, the only access1051 /// that is shallow is an assignment like `x = ...;`, which would1052 /// be a *shallow write* of `x`."1053 Shallow(Option<ArtificialField>),10541055 /// From the RFC: "A *deep* access means that all data reachable1056 /// through the given place may be invalidated or accesses by1057 /// this action."1058 Deep,10591060 /// Access is Deep only when there is a Drop implementation that1061 /// can reach the data behind the reference.1062 Drop,1063}10641065/// Kind of access to a value: read or write1066/// (For informational purposes only)1067#[derive(Copy, Clone, PartialEq, Eq, Debug)]1068enum ReadOrWrite {1069 /// From the RFC: "A *read* means that the existing data may be1070 /// read, but will not be changed."1071 Read(ReadKind),10721073 /// From the RFC: "A *write* means that the data may be mutated to1074 /// new values or otherwise invalidated (for example, it could be1075 /// de-initialized, as in a move operation).1076 Write(WriteKind),10771078 /// For two-phase borrows, we distinguish a reservation (which is treated1079 /// like a Read) from an activation (which is treated like a write), and1080 /// each of those is furthermore distinguished from Reads/Writes above.1081 Reservation(WriteKind),1082 Activation(WriteKind, BorrowIndex),1083}10841085/// Kind of read access to a value1086/// (For informational purposes only)1087#[derive(Copy, Clone, PartialEq, Eq, Debug)]1088enum ReadKind {1089 Borrow(BorrowKind),1090 Copy,1091}10921093/// Kind of write access to a value1094/// (For informational purposes only)1095#[derive(Copy, Clone, PartialEq, Eq, Debug)]1096enum WriteKind {1097 StorageDeadOrDrop,1098 Replace,1099 MutableBorrow(BorrowKind),1100 Mutate,1101 Move,1102}11031104/// When checking permissions for a place access, this flag is used to indicate that an immutable1105/// local place can be mutated.1106//1107// FIXME: @nikomatsakis suggested that this flag could be removed with the following modifications:1108// - Split `is_mutable()` into `is_assignable()` (can be directly assigned) and1109// `is_declared_mutable()`.1110// - Take flow state into consideration in `is_assignable()` for local variables.1111#[derive(Copy, Clone, PartialEq, Eq, Debug)]1112enum LocalMutationIsAllowed {1113 Yes,1114 /// We want use of immutable upvars to cause a "write to immutable upvar"1115 /// error, not an "reassignment" error.1116 ExceptUpvars,1117 No,1118}11191120#[derive(Copy, Clone, Debug)]1121enum InitializationRequiringAction {1122 Borrow,1123 MatchOn,1124 Use,1125 Assignment,1126 PartialAssignment,1127}11281129#[derive(Debug)]1130struct RootPlace<'tcx> {1131 place_local: Local,1132 place_projection: &'tcx [PlaceElem<'tcx>],1133 is_local_mutation_allowed: LocalMutationIsAllowed,1134}11351136impl InitializationRequiringAction {1137 fn as_noun(self) -> &'static str {1138 match self {1139 InitializationRequiringAction::Borrow => "borrow",1140 InitializationRequiringAction::MatchOn => "use", // no good noun1141 InitializationRequiringAction::Use => "use",1142 InitializationRequiringAction::Assignment => "assign",1143 InitializationRequiringAction::PartialAssignment => "assign to part",1144 }1145 }11461147 fn as_verb_in_past_tense(self) -> &'static str {1148 match self {1149 InitializationRequiringAction::Borrow => "borrowed",1150 InitializationRequiringAction::MatchOn => "matched on",1151 InitializationRequiringAction::Use => "used",1152 InitializationRequiringAction::Assignment => "assigned",1153 InitializationRequiringAction::PartialAssignment => "partially assigned",1154 }1155 }11561157 fn as_general_verb_in_past_tense(self) -> &'static str {1158 match self {1159 InitializationRequiringAction::Borrow1160 | InitializationRequiringAction::MatchOn1161 | InitializationRequiringAction::Use => "used",1162 InitializationRequiringAction::Assignment => "assigned",1163 InitializationRequiringAction::PartialAssignment => "partially assigned",1164 }1165 }1166}11671168impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {1169 fn body(&self) -> &'a Body<'tcx> {1170 self.body1171 }11721173 /// Checks an access to the given place to see if it is allowed. Examines the set of borrows1174 /// that are in scope, as well as which paths have been initialized, to ensure that (a) the1175 /// place is initialized and (b) it is not borrowed in some way that would prevent this1176 /// access.1177 ///1178 /// Returns `true` if an error is reported.1179 fn access_place(1180 &mut self,1181 location: Location,1182 place_span: (Place<'tcx>, Span),1183 kind: (AccessDepth, ReadOrWrite),1184 is_local_mutation_allowed: LocalMutationIsAllowed,1185 state: &BorrowckDomain,1186 ) {1187 let (sd, rw) = kind;11881189 if let Activation(_, borrow_index) = rw {1190 if self.reservation_error_reported.contains(&place_span.0) {1191 debug!(1192 "skipping access_place for activation of invalid reservation \1193 place: {:?} borrow_index: {:?}",1194 place_span.0, borrow_index1195 );1196 return;1197 }1198 }11991200 // Check is_empty() first because it's the common case, and doing that1201 // way we avoid the clone() call.1202 if !self.access_place_error_reported.is_empty()1203 && self.access_place_error_reported.contains(&(place_span.0, place_span.1))1204 {1205 debug!(1206 "access_place: suppressing error place_span=`{:?}` kind=`{:?}`",1207 place_span, kind1208 );12091210 // If the place is being mutated, then mark it as such anyway in order to suppress the1211 // `unused_mut` lint, which is likely incorrect once the access place error has been1212 // resolved.1213 if rw == ReadOrWrite::Write(WriteKind::Mutate)1214 && let Ok(root_place) =1215 self.is_mutable(place_span.0.as_ref(), is_local_mutation_allowed)1216 {1217 self.add_used_mut(root_place, state);1218 }12191220 return;1221 }12221223 let mutability_error = self.check_access_permissions(1224 place_span,1225 rw,1226 is_local_mutation_allowed,1227 state,1228 location,1229 );1230 let conflict_error = self.check_access_for_conflict(location, place_span, sd, rw, state);12311232 if conflict_error || mutability_error {1233 debug!("access_place: logging error place_span=`{:?}` kind=`{:?}`", place_span, kind);1234 self.access_place_error_reported.insert((place_span.0, place_span.1));1235 }1236 }12371238 fn borrows_in_scope<'s>(1239 &self,1240 location: Location,1241 state: &'s BorrowckDomain,1242 ) -> Cow<'s, MixedBitSet<BorrowIndex>> {1243 if let Some(polonius) = &self.polonius_output {1244 // Use polonius output if it has been enabled.1245 let location = self.location_table.start_index(location);1246 let mut polonius_output = MixedBitSet::new_empty(self.borrow_set.len());1247 for &idx in polonius.errors_at(location) {1248 polonius_output.insert(idx);1249 }1250 Cow::Owned(polonius_output)1251 } else {1252 Cow::Borrowed(&state.borrows)1253 }1254 }12551256 #[instrument(level = "debug", skip(self, state))]1257 fn check_access_for_conflict(1258 &mut self,1259 location: Location,1260 place_span: (Place<'tcx>, Span),1261 sd: AccessDepth,1262 rw: ReadOrWrite,1263 state: &BorrowckDomain,1264 ) -> bool {1265 let mut error_reported = false;12661267 let borrows_in_scope = self.borrows_in_scope(location, state);12681269 each_borrow_involving_path(1270 self,1271 self.infcx.tcx,1272 self.body,1273 (sd, place_span.0),1274 self.borrow_set,1275 |borrow_index| borrows_in_scope.contains(borrow_index),1276 |this, borrow_index, borrow| match (rw, borrow.kind) {1277 // Obviously an activation is compatible with its own1278 // reservation (or even prior activating uses of same1279 // borrow); so don't check if they interfere.1280 //1281 // NOTE: *reservations* do conflict with themselves;1282 // thus aren't injecting unsoundness w/ this check.)1283 (Activation(_, activating), _) if activating == borrow_index => {1284 debug!(1285 "check_access_for_conflict place_span: {:?} sd: {:?} rw: {:?} \1286 skipping {:?} b/c activation of same borrow_index",1287 place_span,1288 sd,1289 rw,1290 (borrow_index, borrow),1291 );1292 ControlFlow::Continue(())1293 }12941295 (Read(_), BorrowKind::Shared | BorrowKind::Fake(_))1296 | (1297 Read(ReadKind::Borrow(BorrowKind::Fake(FakeBorrowKind::Shallow))),1298 BorrowKind::Mut { .. },1299 ) => ControlFlow::Continue(()),13001301 (Reservation(_), BorrowKind::Fake(_) | BorrowKind::Shared) => {1302 // This used to be a future compatibility warning (to be1303 // disallowed on NLL). See rust-lang/rust#562541304 ControlFlow::Continue(())1305 }13061307 (Write(WriteKind::Move), BorrowKind::Fake(FakeBorrowKind::Shallow)) => {1308 // Handled by initialization checks.1309 ControlFlow::Continue(())1310 }13111312 (Read(kind), BorrowKind::Mut { .. }) => {1313 // Reading from mere reservations of mutable-borrows is OK.1314 if !is_active(this.dominators(), borrow, location) {1315 assert!(borrow.kind.is_two_phase_borrow());1316 return ControlFlow::Continue(());1317 }13181319 error_reported = true;1320 match kind {1321 ReadKind::Copy => {1322 let err = this1323 .report_use_while_mutably_borrowed(location, place_span, borrow);1324 this.buffer_error(err);1325 }1326 ReadKind::Borrow(bk) => {1327 let err =1328 this.report_conflicting_borrow(location, place_span, bk, borrow);1329 this.buffer_error(err);1330 }1331 }1332 ControlFlow::Break(())1333 }13341335 (Reservation(kind) | Activation(kind, _) | Write(kind), _) => {1336 match rw {1337 Reservation(..) => {1338 debug!(1339 "recording invalid reservation of \1340 place: {:?}",1341 place_span.01342 );1343 this.reservation_error_reported.insert(place_span.0);1344 }1345 Activation(_, activating) => {1346 debug!(1347 "observing check_place for activation of \1348 borrow_index: {:?}",1349 activating1350 );1351 }1352 Read(..) | Write(..) => {}1353 }13541355 error_reported = true;1356 match kind {1357 WriteKind::MutableBorrow(bk) => {1358 let err =1359 this.report_conflicting_borrow(location, place_span, bk, borrow);1360 this.buffer_error(err);1361 }1362 WriteKind::StorageDeadOrDrop => this1363 .report_borrowed_value_does_not_live_long_enough(1364 location,1365 borrow,1366 place_span,1367 Some(WriteKind::StorageDeadOrDrop),1368 ),1369 WriteKind::Mutate => {1370 this.report_illegal_mutation_of_borrowed(location, place_span, borrow)1371 }1372 WriteKind::Move => {1373 this.report_move_out_while_borrowed(location, place_span, borrow)1374 }1375 WriteKind::Replace => {1376 this.report_illegal_mutation_of_borrowed(location, place_span, borrow)1377 }1378 }1379 ControlFlow::Break(())1380 }1381 },1382 );13831384 error_reported1385 }13861387 /// Through #123739, `BackwardIncompatibleDropHint`s (BIDs) are introduced.1388 /// We would like to emit lints whether borrow checking fails at these future drop locations.1389 #[instrument(level = "debug", skip(self, state))]1390 fn check_backward_incompatible_drop(1391 &mut self,1392 location: Location,1393 place: Place<'tcx>,1394 state: &BorrowckDomain,1395 ) {1396 let tcx = self.infcx.tcx;1397 // If this type does not need `Drop`, then treat it like a `StorageDead`.1398 // This is needed because we track the borrows of refs to thread locals,1399 // and we'll ICE because we don't track borrows behind shared references.1400 let sd = if place.ty(self.body, tcx).ty.needs_drop(tcx, self.body.typing_env(tcx)) {1401 AccessDepth::Drop1402 } else {1403 AccessDepth::Shallow(None)1404 };14051406 let borrows_in_scope = self.borrows_in_scope(location, state);14071408 // This is a very simplified version of `Self::check_access_for_conflict`.1409 // We are here checking on BIDs and specifically still-live borrows of data involving the BIDs.1410 each_borrow_involving_path(1411 self,1412 self.infcx.tcx,1413 self.body,1414 (sd, place),1415 self.borrow_set,1416 |borrow_index| borrows_in_scope.contains(borrow_index),1417 |this, _borrow_index, borrow| {1418 if matches!(borrow.kind, BorrowKind::Fake(_)) {1419 return ControlFlow::Continue(());1420 }1421 let borrowed = this.retrieve_borrow_spans(borrow).var_or_use_path_span();1422 let explain = this.explain_why_borrow_contains_point(1423 location,1424 borrow,1425 Some((WriteKind::StorageDeadOrDrop, place)),1426 );1427 this.infcx.tcx.emit_node_span_lint(1428 TAIL_EXPR_DROP_ORDER,1429 CRATE_HIR_ID,1430 borrowed,1431 session_diagnostics::TailExprDropOrder {1432 borrowed,1433 callback: |diag| {1434 explain.add_explanation_to_diagnostic(&this, diag, "", None, None);1435 },1436 },1437 );1438 // We may stop at the first case1439 ControlFlow::Break(())1440 },1441 );1442 }14431444 fn mutate_place(1445 &mut self,1446 location: Location,1447 place_span: (Place<'tcx>, Span),1448 kind: AccessDepth,1449 state: &BorrowckDomain,1450 ) {1451 // Write of P[i] or *P requires P init'd.1452 self.check_if_assigned_path_is_moved(location, place_span, state);14531454 self.access_place(1455 location,1456 place_span,1457 (kind, Write(WriteKind::Mutate)),1458 LocalMutationIsAllowed::No,1459 state,1460 );1461 }14621463 fn consume_rvalue(1464 &mut self,1465 location: Location,1466 (rvalue, span): (&Rvalue<'tcx>, Span),1467 state: &BorrowckDomain,1468 ) {1469 match rvalue {1470 &Rvalue::Ref(_ /*rgn*/, bk, place) => {1471 let access_kind = match bk {1472 BorrowKind::Fake(FakeBorrowKind::Shallow) => {1473 (Shallow(Some(ArtificialField::FakeBorrow)), Read(ReadKind::Borrow(bk)))1474 }1475 BorrowKind::Shared | BorrowKind::Fake(FakeBorrowKind::Deep) => {1476 (Deep, Read(ReadKind::Borrow(bk)))1477 }1478 BorrowKind::Mut { .. } => {1479 let wk = WriteKind::MutableBorrow(bk);1480 if bk.is_two_phase_borrow() {1481 (Deep, Reservation(wk))1482 } else {1483 (Deep, Write(wk))1484 }1485 }1486 };14871488 self.access_place(1489 location,1490 (place, span),1491 access_kind,1492 LocalMutationIsAllowed::No,1493 state,1494 );14951496 let action = if bk == BorrowKind::Fake(FakeBorrowKind::Shallow) {1497 InitializationRequiringAction::MatchOn1498 } else {1499 InitializationRequiringAction::Borrow1500 };15011502 self.check_if_path_or_subpath_is_moved(1503 location,1504 action,1505 (place.as_ref(), span),1506 state,1507 );1508 }15091510 &Rvalue::RawPtr(kind, place) => {1511 let access_kind = match kind {1512 RawPtrKind::Mut => (1513 Deep,1514 Write(WriteKind::MutableBorrow(BorrowKind::Mut {1515 kind: MutBorrowKind::Default,1516 })),1517 ),1518 RawPtrKind::Const => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),1519 RawPtrKind::FakeForPtrMetadata => {1520 (Shallow(Some(ArtificialField::ArrayLength)), Read(ReadKind::Copy))1521 }1522 };15231524 self.access_place(1525 location,1526 (place, span),1527 access_kind,1528 LocalMutationIsAllowed::No,1529 state,1530 );15311532 self.check_if_path_or_subpath_is_moved(1533 location,1534 InitializationRequiringAction::Borrow,1535 (place.as_ref(), span),1536 state,1537 );1538 }15391540 Rvalue::ThreadLocalRef(_) => {}15411542 Rvalue::Use(operand, _)1543 | Rvalue::Repeat(operand, _)1544 | Rvalue::UnaryOp(_ /*un_op*/, operand)1545 | Rvalue::Cast(_ /*cast_kind*/, operand, _ /*ty*/) => {1546 self.consume_operand(location, (operand, span), state)1547 }15481549 &Rvalue::Discriminant(place) => {1550 let af = match *rvalue {1551 Rvalue::Discriminant(..) => None,1552 _ => unreachable!(),1553 };1554 self.access_place(1555 location,1556 (place, span),1557 (Shallow(af), Read(ReadKind::Copy)),1558 LocalMutationIsAllowed::No,1559 state,1560 );1561 self.check_if_path_or_subpath_is_moved(1562 location,1563 InitializationRequiringAction::Use,1564 (place.as_ref(), span),1565 state,1566 );1567 }15681569 Rvalue::BinaryOp(_bin_op, box (operand1, operand2)) => {1570 self.consume_operand(location, (operand1, span), state);1571 self.consume_operand(location, (operand2, span), state);1572 }15731574 Rvalue::Aggregate(aggregate_kind, operands) => {1575 // We need to report back the list of mutable upvars that were1576 // moved into the closure and subsequently used by the closure,1577 // in order to populate our used_mut set.1578 match **aggregate_kind {1579 AggregateKind::Closure(def_id, _)1580 | AggregateKind::CoroutineClosure(def_id, _)1581 | AggregateKind::Coroutine(def_id, _) => {1582 let def_id = def_id.expect_local();1583 let used_mut_upvars = self.root_cx.used_mut_upvars(def_id);1584 debug!("{:?} used_mut_upvars={:?}", def_id, used_mut_upvars);1585 // FIXME: We're cloning the `SmallVec` here to avoid borrowing `root_cx`1586 // when calling `propagate_closure_used_mut_upvar`. This should ideally1587 // be unnecessary.1588 for field in used_mut_upvars.clone() {1589 self.propagate_closure_used_mut_upvar(&operands[field]);1590 }1591 }1592 AggregateKind::Adt(..)1593 | AggregateKind::Array(..)1594 | AggregateKind::Tuple { .. }1595 | AggregateKind::RawPtr(..) => (),1596 }15971598 for operand in operands {1599 self.consume_operand(location, (operand, span), state);1600 }1601 }16021603 Rvalue::WrapUnsafeBinder(op, _) => {1604 self.consume_operand(location, (op, span), state);1605 }16061607 Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in borrowck"),1608 }1609 }16101611 fn propagate_closure_used_mut_upvar(&mut self, operand: &Operand<'tcx>) {1612 let propagate_closure_used_mut_place = |this: &mut Self, place: Place<'tcx>| {1613 // We have three possibilities here:1614 // a. We are modifying something through a mut-ref1615 // b. We are modifying something that is local to our parent1616 // c. Current body is a nested closure, and we are modifying path starting from1617 // a Place captured by our parent closure.16181619 // Handle (c), the path being modified is exactly the path captured by our parent1620 if let Some(field) = this.is_upvar_field_projection(place.as_ref()) {1621 this.used_mut_upvars.push(field);1622 return;1623 }16241625 for (place_ref, proj) in place.iter_projections().rev() {1626 // Handle (a)1627 if proj == ProjectionElem::Deref {1628 match place_ref.ty(this.body(), this.infcx.tcx).ty.kind() {1629 // We aren't modifying a variable directly1630 ty::Ref(_, _, hir::Mutability::Mut) => return,16311632 _ => {}1633 }1634 }16351636 // Handle (c)1637 if let Some(field) = this.is_upvar_field_projection(place_ref) {1638 this.used_mut_upvars.push(field);1639 return;1640 }1641 }16421643 // Handle(b)1644 this.used_mut.insert(place.local);1645 };16461647 // This relies on the current way that by-value1648 // captures of a closure are copied/moved directly1649 // when generating MIR.1650 match *operand {1651 Operand::Move(place) | Operand::Copy(place) => {1652 match place.as_local() {1653 Some(local) if !self.body.local_decls[local].is_user_variable() => {1654 if self.body.local_decls[local].ty.is_mutable_ptr() {1655 // The variable will be marked as mutable by the borrow.1656 return;1657 }1658 // This is an edge case where we have a `move` closure1659 // inside a non-move closure, and the inner closure1660 // contains a mutation:1661 //1662 // let mut i = 0;1663 // || { move || { i += 1; }; };1664 //1665 // In this case our usual strategy of assuming that the1666 // variable will be captured by mutable reference is1667 // wrong, since `i` can be copied into the inner1668 // closure from a shared reference.1669 //1670 // As such we have to search for the local that this1671 // capture comes from and mark it as being used as mut.16721673 let Some(temp_mpi) = self.move_data.rev_lookup.find_local(local) else {1674 bug!("temporary should be tracked");1675 };1676 let init = if let [init_index] = *self.move_data.init_path_map[temp_mpi] {1677 &self.move_data.inits[init_index]1678 } else {1679 bug!("temporary should be initialized exactly once")1680 };16811682 let InitLocation::Statement(loc) = init.location else {1683 bug!("temporary initialized in arguments")1684 };16851686 let body = self.body;1687 let bbd = &body[loc.block];1688 let stmt = &bbd.statements[loc.statement_index];1689 debug!("temporary assigned in: stmt={:?}", stmt);16901691 match stmt.kind {1692 StatementKind::Assign(box (1693 _,1694 Rvalue::Ref(_, _, source)1695 | Rvalue::Use(Operand::Copy(source) | Operand::Move(source), _),1696 )) => {1697 propagate_closure_used_mut_place(self, source);1698 }1699 _ => {1700 bug!(1701 "closures should only capture user variables \1702 or references to user variables"1703 );1704 }1705 }1706 }1707 _ => propagate_closure_used_mut_place(self, place),1708 }1709 }1710 Operand::Constant(..) | Operand::RuntimeChecks(_) => {}1711 }1712 }17131714 fn consume_operand(1715 &mut self,1716 location: Location,1717 (operand, span): (&Operand<'tcx>, Span),1718 state: &BorrowckDomain,1719 ) {1720 match *operand {1721 Operand::Copy(place) => {1722 // copy of place: check if this is "copy of frozen path"1723 // (FIXME: see check_loans.rs)1724 self.access_place(1725 location,1726 (place, span),1727 (Deep, Read(ReadKind::Copy)),1728 LocalMutationIsAllowed::No,1729 state,1730 );17311732 // Finally, check if path was already moved.1733 self.check_if_path_or_subpath_is_moved(1734 location,1735 InitializationRequiringAction::Use,1736 (place.as_ref(), span),1737 state,1738 );1739 }1740 Operand::Move(place) => {1741 // Check if moving from this place makes sense.1742 self.check_movable_place(location, place);17431744 // move of place: check if this is move of already borrowed path1745 self.access_place(1746 location,1747 (place, span),1748 (Deep, Write(WriteKind::Move)),1749 LocalMutationIsAllowed::Yes,1750 state,1751 );17521753 // Finally, check if path was already moved.1754 self.check_if_path_or_subpath_is_moved(1755 location,1756 InitializationRequiringAction::Use,1757 (place.as_ref(), span),1758 state,1759 );1760 }1761 Operand::Constant(_) | Operand::RuntimeChecks(_) => {}1762 }1763 }17641765 /// Checks whether a borrow of this place is invalidated when the function1766 /// exits1767 #[instrument(level = "debug", skip(self))]1768 fn check_for_invalidation_at_exit(1769 &mut self,1770 location: Location,1771 borrow: &BorrowData<'tcx>,1772 span: Span,1773 ) {1774 let place = borrow.borrowed_place;1775 let mut root_place = PlaceRef { local: place.local, projection: &[] };17761777 // FIXME(nll-rfc#40): do more precise destructor tracking here. For now1778 // we just know that all locals are dropped at function exit (otherwise1779 // we'll have a memory leak) and assume that all statics have a destructor.1780 //1781 // FIXME: allow thread-locals to borrow other thread locals?1782 let might_be_alive = if self.body.local_decls[root_place.local].is_ref_to_thread_local() {1783 // Thread-locals might be dropped after the function exits1784 // We have to dereference the outer reference because1785 // borrows don't conflict behind shared references.1786 root_place.projection = TyCtxtConsts::DEREF_PROJECTION;1787 true1788 } else {1789 false1790 };17911792 let sd = if might_be_alive { Deep } else { Shallow(None) };17931794 if places_conflict::borrow_conflicts_with_place(1795 self.infcx.tcx,1796 self.body,1797 place,1798 borrow.kind,1799 root_place,1800 sd,1801 places_conflict::PlaceConflictBias::Overlap,1802 ) {1803 debug!("check_for_invalidation_at_exit({:?}): INVALID", place);1804 // FIXME: should be talking about the region lifetime instead1805 // of just a span here.1806 let span = self.infcx.tcx.sess.source_map().end_point(span);1807 self.report_borrowed_value_does_not_live_long_enough(1808 location,1809 borrow,1810 (place, span),1811 None,1812 )1813 }1814 }18151816 /// Reports an error if this is a borrow of local data.1817 /// This is called for all Yield expressions on movable coroutines1818 fn check_for_local_borrow(&mut self, borrow: &BorrowData<'tcx>, yield_span: Span) {1819 debug!("check_for_local_borrow({:?})", borrow);18201821 if borrow_of_local_data(borrow.borrowed_place) {1822 let err = self.cannot_borrow_across_coroutine_yield(1823 self.retrieve_borrow_spans(borrow).var_or_use(),1824 yield_span,1825 );18261827 self.buffer_error(err);1828 }1829 }18301831 fn check_activations(&mut self, location: Location, span: Span, state: &BorrowckDomain) {1832 // Two-phase borrow support: For each activation that is newly1833 // generated at this statement, check if it interferes with1834 // another borrow.1835 for &borrow_index in self.borrow_set.activations_at_location(location) {1836 let borrow = &self.borrow_set[borrow_index];18371838 // only mutable borrows should be 2-phase1839 assert!(match borrow.kind {1840 BorrowKind::Shared | BorrowKind::Fake(_) => false,1841 BorrowKind::Mut { .. } => true,1842 });18431844 self.access_place(1845 location,1846 (borrow.borrowed_place, span),1847 (Deep, Activation(WriteKind::MutableBorrow(borrow.kind), borrow_index)),1848 LocalMutationIsAllowed::No,1849 state,1850 );1851 // We do not need to call `check_if_path_or_subpath_is_moved`1852 // again, as we already called it when we made the1853 // initial reservation.1854 }1855 }18561857 fn check_movable_place(&mut self, location: Location, place: Place<'tcx>) {1858 use IllegalMoveOriginKind::*;18591860 let body = self.body;1861 let tcx = self.infcx.tcx;1862 let mut place_ty = PlaceTy::from_ty(body.local_decls[place.local].ty);1863 for (place_ref, elem) in place.iter_projections() {1864 match elem {1865 ProjectionElem::Deref => match place_ty.ty.kind() {1866 ty::Ref(..) | ty::RawPtr(..) => {1867 self.move_errors.push(MoveError::new(1868 place,1869 location,1870 BorrowedContent {1871 target_place: place_ref.project_deeper(&[elem], tcx),1872 },1873 ));1874 return;1875 }1876 ty::Adt(adt, _) => {1877 if !adt.is_box() {1878 bug!("Adt should be a box type when Place is deref");1879 }1880 }1881 ty::Bool1882 | ty::Char1883 | ty::Int(_)1884 | ty::Uint(_)1885 | ty::Float(_)1886 | ty::Foreign(_)1887 | ty::Str1888 | ty::Array(_, _)1889 | ty::Pat(_, _)1890 | ty::Slice(_)1891 | ty::FnDef(_, _)1892 | ty::FnPtr(..)1893 | ty::Dynamic(_, _)1894 | ty::Closure(_, _)1895 | ty::CoroutineClosure(_, _)1896 | ty::Coroutine(_, _)1897 | ty::CoroutineWitness(..)1898 | ty::Never1899 | ty::Tuple(_)1900 | ty::UnsafeBinder(_)1901 | ty::Alias(_)1902 | ty::Param(_)1903 | ty::Bound(_, _)1904 | ty::Infer(_)1905 | ty::Error(_)1906 | ty::Placeholder(_) => {1907 bug!("When Place is Deref it's type shouldn't be {place_ty:#?}")1908 }1909 },1910 ProjectionElem::Field(_, _) => match place_ty.ty.kind() {1911 ty::Adt(adt, _) => {1912 if adt.has_dtor(tcx) {1913 self.move_errors.push(MoveError::new(1914 place,1915 location,1916 InteriorOfTypeWithDestructor { container_ty: place_ty.ty },1917 ));1918 return;1919 }1920 }1921 ty::Closure(..)1922 | ty::CoroutineClosure(..)1923 | ty::Coroutine(_, _)1924 | ty::Tuple(_) => (),1925 ty::Bool1926 | ty::Char1927 | ty::Int(_)1928 | ty::Uint(_)1929 | ty::Float(_)1930 | ty::Foreign(_)1931 | ty::Str1932 | ty::Array(_, _)1933 | ty::Pat(_, _)1934 | ty::Slice(_)1935 | ty::RawPtr(_, _)1936 | ty::Ref(_, _, _)1937 | ty::FnDef(_, _)1938 | ty::FnPtr(..)1939 | ty::Dynamic(_, _)1940 | ty::CoroutineWitness(..)1941 | ty::Never1942 | ty::UnsafeBinder(_)1943 | ty::Alias(_)1944 | ty::Param(_)1945 | ty::Bound(_, _)1946 | ty::Infer(_)1947 | ty::Error(_)1948 | ty::Placeholder(_) => bug!(1949 "When Place contains ProjectionElem::Field it's type shouldn't be {place_ty:#?}"1950 ),1951 },1952 ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {1953 match place_ty.ty.kind() {1954 ty::Slice(_) => {1955 self.move_errors.push(MoveError::new(1956 place,1957 location,1958 InteriorOfSliceOrArray { ty: place_ty.ty, is_index: false },1959 ));1960 return;1961 }1962 ty::Array(_, _) => (),1963 _ => bug!("Unexpected type {:#?}", place_ty.ty),1964 }1965 }1966 ProjectionElem::Index(_) => match place_ty.ty.kind() {1967 ty::Array(..) | ty::Slice(..) => {1968 self.move_errors.push(MoveError::new(1969 place,1970 location,1971 InteriorOfSliceOrArray { ty: place_ty.ty, is_index: true },1972 ));1973 return;1974 }1975 _ => bug!("Unexpected type {place_ty:#?}"),1976 },1977 // `OpaqueCast`: only transmutes the type, so no moves there.1978 // `Downcast` : only changes information about a `Place` without moving.1979 // So it's safe to skip these.1980 ProjectionElem::OpaqueCast(_)1981 | ProjectionElem::Downcast(_, _)1982 | ProjectionElem::UnwrapUnsafeBinder(_) => (),1983 }19841985 place_ty = place_ty.projection_ty(tcx, elem);1986 }1987 }19881989 fn check_if_full_path_is_moved(1990 &mut self,1991 location: Location,1992 desired_action: InitializationRequiringAction,1993 place_span: (PlaceRef<'tcx>, Span),1994 state: &BorrowckDomain,1995 ) {1996 let maybe_uninits = &state.uninits;19971998 // Bad scenarios:1999 //2000 // 1. Move of `a.b.c`, use of `a.b.c`
Findings
✓ No findings reported for this file.