// Copyright 2012-2017 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. use borrow_check::borrow_set::{BorrowSet, BorrowData}; use borrow_check::place_ext::PlaceExt; use rustc; use rustc::hir; use rustc::hir::def_id::DefId; use rustc::middle::region; use rustc::mir::{self, Location, Place, Mir}; use rustc::ty::{Region, TyCtxt}; use rustc::ty::RegionKind; use rustc::ty::RegionKind::ReScope; use rustc_data_structures::bitslice::{BitwiseOperator}; use rustc_data_structures::indexed_set::{IdxSet}; use rustc_data_structures::indexed_vec::{Idx, IndexVec}; use rustc_data_structures::sync::Lrc; use dataflow::{BitDenotation, BlockSets, InitialFlow}; pub use dataflow::indexes::{BorrowIndex, ReserveOrActivateIndex}; use borrow_check::nll::region_infer::RegionInferenceContext; use borrow_check::nll::ToRegionVid; use syntax_pos::Span; use std::rc::Rc; /// `Borrows` stores the data used in the analyses that track the flow /// of borrows. /// /// It uniquely identifies every borrow (`Rvalue::Ref`) by a /// `BorrowIndex`, and maps each such index to a `BorrowData` /// describing the borrow. These indexes are used for representing the /// borrows in compact bitvectors. pub struct Borrows<'a, 'gcx: 'tcx, 'tcx: 'a> { tcx: TyCtxt<'a, 'gcx, 'tcx>, mir: &'a Mir<'tcx>, scope_tree: Lrc, root_scope: Option, borrow_set: BorrowSet<'tcx>, /// NLL region inference context with which NLL queries should be resolved nonlexical_regioncx: Option>>, } impl ReserveOrActivateIndex { fn reserved(i: BorrowIndex) -> Self { ReserveOrActivateIndex::new(i.index() * 2) } fn active(i: BorrowIndex) -> Self { ReserveOrActivateIndex::new((i.index() * 2) + 1) } pub(crate) fn is_reservation(self) -> bool { self.index() % 2 == 0 } pub(crate) fn is_activation(self) -> bool { self.index() % 2 == 1} pub(crate) fn kind(self) -> &'static str { if self.is_reservation() { "reserved" } else { "active" } } pub(crate) fn borrow_index(self) -> BorrowIndex { BorrowIndex::new(self.index() / 2) } } impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> { pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>, mir: &'a Mir<'tcx>, nonlexical_regioncx: Option>>, def_id: DefId, body_id: Option) -> Self { let scope_tree = tcx.region_scope_tree(def_id); let root_scope = body_id.map(|body_id| { region::Scope::CallSite(tcx.hir.body(body_id).value.hir_id.local_id) }); let borrow_set = BorrowSet::build(tcx, mir); Borrows { tcx: tcx, mir: mir, borrow_set, scope_tree, root_scope, nonlexical_regioncx, } } /// Returns the span for the "end point" given region. This will /// return `None` if NLL is enabled, since that concept has no /// meaning there. Otherwise, return region span if it exists and /// span for end of the function if it doesn't exist. pub(crate) fn opt_region_end_span(&self, region: &Region) -> Option { match self.nonlexical_regioncx { Some(_) => None, None => { match self.borrow_set.region_span_map.get(region) { Some(span) => Some(self.tcx.sess.codemap().end_point(*span)), None => Some(self.tcx.sess.codemap().end_point(self.mir.span)) } } } } crate fn borrows(&self) -> &IndexVec> { &self.borrow_set.borrows } pub fn scope_tree(&self) -> &Lrc { &self.scope_tree } pub fn location(&self, idx: BorrowIndex) -> &Location { &self.borrow_set.borrows[idx].reserve_location } /// Add all borrows to the kill set, if those borrows are out of scope at `location`. /// That means either they went out of either a nonlexical scope, if we care about those /// at the moment, or the location represents a lexical EndRegion fn kill_loans_out_of_scope_at_location(&self, sets: &mut BlockSets, location: Location) { if let Some(ref regioncx) = self.nonlexical_regioncx { // NOTE: The state associated with a given `location` // reflects the dataflow on entry to the statement. If it // does not contain `borrow_region`, then then that means // that the statement at `location` kills the borrow. // // We are careful always to call this function *before* we // set up the gen-bits for the statement or // termanator. That way, if the effect of the statement or // terminator *does* introduce a new loan of the same // region, then setting that gen-bit will override any // potential kill introduced here. for (borrow_index, borrow_data) in self.borrow_set.borrows.iter_enumerated() { let borrow_region = borrow_data.region.to_region_vid(); if !regioncx.region_contains_point(borrow_region, location) { sets.kill(&ReserveOrActivateIndex::reserved(borrow_index)); sets.kill(&ReserveOrActivateIndex::active(borrow_index)); } } } } fn kill_borrows_on_local(&self, sets: &mut BlockSets, local: &rustc::mir::Local) { if let Some(borrow_indexes) = self.borrow_set.local_map.get(local) { sets.kill_all(borrow_indexes.iter() .map(|b| ReserveOrActivateIndex::reserved(*b))); sets.kill_all(borrow_indexes.iter() .map(|b| ReserveOrActivateIndex::active(*b))); } } /// Performs the activations for a given location fn perform_activations_at_location(&self, sets: &mut BlockSets, location: Location) { // Handle activations match self.borrow_set.activation_map.get(&location) { Some(activations) => { for activated in activations { debug!("activating borrow {:?}", activated); sets.gen(&ReserveOrActivateIndex::active(*activated)) } } None => {} } } } impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> { type Idx = ReserveOrActivateIndex; fn name() -> &'static str { "borrows" } fn bits_per_block(&self) -> usize { self.borrow_set.borrows.len() * 2 } fn start_block_effect(&self, _entry_set: &mut IdxSet) { // no borrows of code region_scopes have been taken prior to // function execution, so this method has no effect on // `_sets`. } fn before_statement_effect(&self, sets: &mut BlockSets, location: Location) { debug!("Borrows::before_statement_effect sets: {:?} location: {:?}", sets, location); self.kill_loans_out_of_scope_at_location(sets, location); } fn statement_effect(&self, sets: &mut BlockSets, location: Location) { debug!("Borrows::statement_effect sets: {:?} location: {:?}", sets, location); let block = &self.mir.basic_blocks().get(location.block).unwrap_or_else(|| { panic!("could not find block at location {:?}", location); }); let stmt = block.statements.get(location.statement_index).unwrap_or_else(|| { panic!("could not find statement at location {:?}"); }); self.perform_activations_at_location(sets, location); self.kill_loans_out_of_scope_at_location(sets, location); match stmt.kind { // EndRegion kills any borrows (reservations and active borrows both) mir::StatementKind::EndRegion(region_scope) => { if let Some(borrow_indexes) = self.borrow_set.region_map.get(&ReScope(region_scope)) { assert!(self.nonlexical_regioncx.is_none()); for idx in borrow_indexes { sets.kill(&ReserveOrActivateIndex::reserved(*idx)); sets.kill(&ReserveOrActivateIndex::active(*idx)); } } else { // (if there is no entry, then there are no borrows to be tracked) } } mir::StatementKind::Assign(ref lhs, ref rhs) => { // Make sure there are no remaining borrows for variables // that are assigned over. if let Place::Local(ref local) = *lhs { // FIXME: Handle the case in which we're assigning over // a projection (`foo.bar`). self.kill_borrows_on_local(sets, local); } // NOTE: if/when the Assign case is revised to inspect // the assigned_place here, make sure to also // re-consider the current implementations of the // propagate_call_return method. if let mir::Rvalue::Ref(region, _, ref place) = *rhs { if place.is_unsafe_place(self.tcx, self.mir) { return; } let index = self.borrow_set.location_map.get(&location).unwrap_or_else(|| { panic!("could not find BorrowIndex for location {:?}", location); }); if let RegionKind::ReEmpty = region { // If the borrowed value dies before the borrow is used, the region for // the borrow can be empty. Don't track the borrow in that case. sets.kill(&ReserveOrActivateIndex::active(*index)); return } assert!(self.borrow_set.region_map.get(region).unwrap_or_else(|| { panic!("could not find BorrowIndexs for region {:?}", region); }).contains(&index)); sets.gen(&ReserveOrActivateIndex::reserved(*index)); // Issue #46746: Two-phase borrows handles // stmts of form `Tmp = &mut Borrow` ... match lhs { Place::Local(..) | Place::Static(..) => {} // okay Place::Projection(..) => { // ... can assign into projections, // e.g. `box (&mut _)`. Current // conservative solution: force // immediate activation here. sets.gen(&ReserveOrActivateIndex::active(*index)); } } } } mir::StatementKind::StorageDead(local) => { // Make sure there are no remaining borrows for locals that // are gone out of scope. self.kill_borrows_on_local(sets, &local) } mir::StatementKind::InlineAsm { ref outputs, ref asm, .. } => { for (output, kind) in outputs.iter().zip(&asm.outputs) { if !kind.is_indirect && !kind.is_rw { // Make sure there are no remaining borrows for direct // output variables. if let Place::Local(ref local) = *output { // FIXME: Handle the case in which we're assigning over // a projection (`foo.bar`). self.kill_borrows_on_local(sets, local); } } } } mir::StatementKind::SetDiscriminant { .. } | mir::StatementKind::StorageLive(..) | mir::StatementKind::Validate(..) | mir::StatementKind::UserAssertTy(..) | mir::StatementKind::Nop => {} } } fn before_terminator_effect(&self, sets: &mut BlockSets, location: Location) { debug!("Borrows::before_terminator_effect sets: {:?} location: {:?}", sets, location); self.kill_loans_out_of_scope_at_location(sets, location); } fn terminator_effect(&self, sets: &mut BlockSets, location: Location) { debug!("Borrows::terminator_effect sets: {:?} location: {:?}", sets, location); let block = &self.mir.basic_blocks().get(location.block).unwrap_or_else(|| { panic!("could not find block at location {:?}", location); }); let term = block.terminator(); self.perform_activations_at_location(sets, location); self.kill_loans_out_of_scope_at_location(sets, location); match term.kind { mir::TerminatorKind::Resume | mir::TerminatorKind::Return | mir::TerminatorKind::GeneratorDrop => { // When we return from the function, then all `ReScope`-style regions // are guaranteed to have ended. // Normally, there would be `EndRegion` statements that come before, // and hence most of these loans will already be dead -- but, in some cases // like unwind paths, we do not always emit `EndRegion` statements, so we // add some kills here as a "backup" and to avoid spurious error messages. for (borrow_index, borrow_data) in self.borrow_set.borrows.iter_enumerated() { if let ReScope(scope) = borrow_data.region { // Check that the scope is not actually a scope from a function that is // a parent of our closure. Note that the CallSite scope itself is // *outside* of the closure, for some weird reason. if let Some(root_scope) = self.root_scope { if *scope != root_scope && self.scope_tree.is_subscope_of(*scope, root_scope) { sets.kill(&ReserveOrActivateIndex::reserved(borrow_index)); sets.kill(&ReserveOrActivateIndex::active(borrow_index)); } } } } } mir::TerminatorKind::Abort | mir::TerminatorKind::SwitchInt {..} | mir::TerminatorKind::Drop {..} | mir::TerminatorKind::DropAndReplace {..} | mir::TerminatorKind::Call {..} | mir::TerminatorKind::Assert {..} | mir::TerminatorKind::Yield {..} | mir::TerminatorKind::Goto {..} | mir::TerminatorKind::FalseEdges {..} | mir::TerminatorKind::FalseUnwind {..} | mir::TerminatorKind::Unreachable => {} } } fn propagate_call_return(&self, _in_out: &mut IdxSet, _call_bb: mir::BasicBlock, _dest_bb: mir::BasicBlock, _dest_place: &mir::Place) { // there are no effects on borrows from method call return... // // ... but if overwriting a place can affect flow state, then // latter is not true; see NOTE on Assign case in // statement_effect_on_borrows. } } impl<'a, 'gcx, 'tcx> BitwiseOperator for Borrows<'a, 'gcx, 'tcx> { #[inline] fn join(&self, pred1: usize, pred2: usize) -> usize { pred1 | pred2 // union effects of preds when computing reservations } } impl<'a, 'gcx, 'tcx> InitialFlow for Borrows<'a, 'gcx, 'tcx> { #[inline] fn bottom_value() -> bool { false // bottom = nothing is reserved or activated yet } }