Skip to content

Commit

Permalink
collect region contexts during mir renumbering
Browse files Browse the repository at this point in the history
  • Loading branch information
b-naber committed Feb 19, 2023
1 parent cb35a7b commit 2f79f73
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 42 deletions.
23 changes: 17 additions & 6 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ dependencies = [
"declare_clippy_lint",
"if_chain",
"itertools",
"pulldown-cmark",
"pulldown-cmark 0.9.2",
"quine-mc_cluskey",
"regex-syntax",
"rustc-semver",
Expand Down Expand Up @@ -2003,9 +2003,9 @@ dependencies = [

[[package]]
name = "http-auth"
version = "0.1.6"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0b40b39d66c28829a0cf4d09f7e139ff8201f7500a5083732848ed3b4b4d850"
checksum = "5430cacd7a1f9a02fbeb350dfc81a0e5ed42d81f3398cb0ba184017f85bdcfbc"
dependencies = [
"memchr",
]
Expand Down Expand Up @@ -2555,7 +2555,7 @@ dependencies = [
"memchr",
"once_cell",
"opener",
"pulldown-cmark",
"pulldown-cmark 0.9.2",
"regex",
"serde",
"serde_json",
Expand All @@ -2572,7 +2572,7 @@ dependencies = [
"anyhow",
"handlebars 3.5.5",
"pretty_assertions",
"pulldown-cmark",
"pulldown-cmark 0.7.2",
"same-file",
"serde_json",
"url",
Expand Down Expand Up @@ -3269,6 +3269,17 @@ dependencies = [
"cc",
]

[[package]]
name = "pulldown-cmark"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca36dea94d187597e104a5c8e4b07576a8a45aa5db48a65e12940d3eb7461f55"
dependencies = [
"bitflags",
"memchr",
"unicase",
]

[[package]]
name = "pulldown-cmark"
version = "0.9.2"
Expand Down Expand Up @@ -4572,7 +4583,7 @@ name = "rustc_resolve"
version = "0.0.0"
dependencies = [
"bitflags",
"pulldown-cmark",
"pulldown-cmark 0.9.2",
"rustc_arena",
"rustc_ast",
"rustc_ast_pretty",
Expand Down
90 changes: 76 additions & 14 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
use rustc_index::bit_set::ChunkedBitSet;
use rustc_index::vec::IndexVec;
use rustc_infer::infer::{DefiningAnchor, InferCtxt, NllRegionVariableOrigin, TyCtxtInferExt};
use rustc_infer::infer::{
DefiningAnchor, InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin, TyCtxtInferExt,
};
use rustc_middle::mir::{
traversal, Body, ClearCrossCrate, Local, Location, Mutability, NonDivergingIntrinsic, Operand,
Place, PlaceElem, PlaceRef, VarDebugInfoContents,
Expand Down Expand Up @@ -95,6 +97,7 @@ use nll::{PoloniusOutput, ToRegionVid};
use place_ext::PlaceExt;
use places_conflict::{places_conflict, PlaceConflictBias};
use region_infer::RegionInferenceContext;
use renumber::RegionCtxt;

// FIXME(eddyb) perhaps move this somewhere more centrally.
#[derive(Debug)]
Expand Down Expand Up @@ -168,10 +171,10 @@ fn do_mir_borrowck<'tcx>(
return_body_with_facts: bool,
) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
let def = input_body.source.with_opt_param().as_local().unwrap();

debug!(?def);

let tcx = infcx.tcx;
let infcx = BorrowckInferCtxt::new(infcx);
let param_env = tcx.param_env(def.did);

let mut local_names = IndexVec::from_elem(None, &input_body.local_decls);
Expand Down Expand Up @@ -219,7 +222,7 @@ fn do_mir_borrowck<'tcx>(
let mut body_owned = input_body.clone();
let mut promoted = input_promoted.clone();
let free_regions =
nll::replace_regions_in_mir(infcx, param_env, &mut body_owned, &mut promoted);
nll::replace_regions_in_mir(&infcx, param_env, &mut body_owned, &mut promoted);
let body = &body_owned; // no further changes

let location_table_owned = LocationTable::new(body);
Expand Down Expand Up @@ -257,7 +260,7 @@ fn do_mir_borrowck<'tcx>(
opt_closure_req,
nll_errors,
} = nll::compute_regions(
infcx,
&infcx,
free_regions,
body,
&promoted,
Expand All @@ -272,12 +275,12 @@ fn do_mir_borrowck<'tcx>(

// Dump MIR results into a file, if that is enabled. This let us
// write unit-tests, as well as helping with debugging.
nll::dump_mir_results(infcx, &body, &regioncx, &opt_closure_req);
nll::dump_mir_results(&infcx, &body, &regioncx, &opt_closure_req);

// We also have a `#[rustc_regions]` annotation that causes us to dump
// information.
nll::dump_annotation(
infcx,
&infcx,
&body,
&regioncx,
&opt_closure_req,
Expand Down Expand Up @@ -321,7 +324,7 @@ fn do_mir_borrowck<'tcx>(

if let Err((move_data, move_errors)) = move_data_results {
let mut promoted_mbcx = MirBorrowckCtxt {
infcx,
infcx: &infcx,
param_env,
body: promoted_body,
move_data: &move_data,
Expand Down Expand Up @@ -350,7 +353,7 @@ fn do_mir_borrowck<'tcx>(
}

let mut mbcx = MirBorrowckCtxt {
infcx,
infcx: &infcx,
param_env,
body,
move_data: &mdpe.move_data,
Expand Down Expand Up @@ -482,22 +485,81 @@ pub struct BodyWithBorrowckFacts<'tcx> {
pub location_table: LocationTable,
}

struct BorrowckInferCtxt<'cx, 'tcx> {
pub struct BorrowckInferCtxt<'cx, 'tcx> {
pub(crate) infcx: &'cx InferCtxt<'tcx>,

#[cfg(debug_assertions)]
pub(crate) _reg_var_to_origin: RefCell<FxHashMap<ty::Region<'tcx>, NllRegionVariableOrigin>>,
pub(crate) reg_var_to_origin: RefCell<FxHashMap<ty::RegionVid, RegionCtxt>>,
}

impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> {
#[cfg(not(debug_assertions))]
pub(crate) fn _new(infcx: &'cx InferCtxt<'tcx>) -> Self {
pub(crate) fn new(infcx: &'cx InferCtxt<'tcx>) -> Self {
BorrowckInferCtxt { infcx }
}

#[cfg(debug_assertions)]
pub(crate) fn _new(infcx: &'cx InferCtxt<'tcx>) -> Self {
BorrowckInferCtxt { infcx, _reg_var_to_origin: RefCell::new(Default::default()) }
pub(crate) fn new(infcx: &'cx InferCtxt<'tcx>) -> Self {
BorrowckInferCtxt { infcx, reg_var_to_origin: RefCell::new(Default::default()) }
}

#[cfg(not(debug_assertions))]
pub(crate) fn next_region_var(&self, origin: RegionVariableOrigin) -> ty::Region<'tcx> {
self.infcx.next_region_var(origin)
}

#[cfg(debug_assertions)]
pub(crate) fn next_region_var(
&self,
origin: RegionVariableOrigin,
ctxt: RegionCtxt,
) -> ty::Region<'tcx> {
let next_region = self.infcx.next_region_var(origin);
let vid = next_region
.try_get_var()
.unwrap_or_else(|| bug!("expected RegionKind::RegionVar on {:?}", next_region));

debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin);
let mut var_to_origin = self.reg_var_to_origin.borrow_mut();
let prev = var_to_origin.insert(vid, ctxt);
debug!("var_to_origin after insertion: {:?}", var_to_origin);

// This only makes sense if not called in a canonicalization context. If this
// ever changes we either want to get rid of `BorrowckInferContext::reg_var_to_origin`
// or modify how we track nll region vars for that map.
assert!(matches!(prev, None));

next_region
}

#[cfg(not(debug_assertions))]
pub(crate) fn next_nll_region_var(&self, origin: NllRegionVariableOrigin) -> ty::Region<'tcx> {
self.infcx.next_nll_region_var(origin)
}

#[cfg(debug_assertions)]
#[instrument(skip(self), level = "debug")]
pub(crate) fn next_nll_region_var(
&self,
origin: NllRegionVariableOrigin,
ctxt: RegionCtxt,
) -> ty::Region<'tcx> {
let next_region = self.infcx.next_nll_region_var(origin.clone());
let vid = next_region
.try_get_var()
.unwrap_or_else(|| bug!("expected RegionKind::RegionVar on {:?}", next_region));

debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin);
let mut var_to_origin = self.reg_var_to_origin.borrow_mut();
let prev = var_to_origin.insert(vid, ctxt);
debug!("var_to_origin after insertion: {:?}", var_to_origin);

// This only makes sense if not called in a canonicalization context. If this
// ever changes we either want to get rid of `BorrowckInferContext::reg_var_to_origin`
// or modify how we track nll region vars for that map.
assert!(matches!(prev, None));

next_region
}
}

Expand All @@ -510,7 +572,7 @@ impl<'cx, 'tcx> Deref for BorrowckInferCtxt<'cx, 'tcx> {
}

struct MirBorrowckCtxt<'cx, 'tcx> {
infcx: &'cx InferCtxt<'tcx>,
infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>,
param_env: ParamEnv<'tcx>,
body: &'cx Body<'tcx>,
move_data: &'cx MoveData<'tcx>,
Expand Down
16 changes: 10 additions & 6 deletions compiler/rustc_borrowck/src/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use rustc_data_structures::vec_map::VecMap;
use rustc_hir::def_id::LocalDefId;
use rustc_index::vec::IndexVec;
use rustc_infer::infer::InferCtxt;
use rustc_middle::mir::{create_dump_file, dump_enabled, dump_mir, PassWhere};
use rustc_middle::mir::{
BasicBlock, Body, ClosureOutlivesSubject, ClosureRegionRequirements, LocalKind, Location,
Expand Down Expand Up @@ -37,7 +36,7 @@ use crate::{
renumber,
type_check::{self, MirTypeckRegionConstraints, MirTypeckResults},
universal_regions::UniversalRegions,
Upvar,
BorrowckInferCtxt, Upvar,
};

pub type PoloniusOutput = Output<RustcFacts>;
Expand All @@ -58,7 +57,7 @@ pub(crate) struct NllOutput<'tcx> {
/// `compute_regions`.
#[instrument(skip(infcx, param_env, body, promoted), level = "debug")]
pub(crate) fn replace_regions_in_mir<'tcx>(
infcx: &InferCtxt<'tcx>,
infcx: &BorrowckInferCtxt<'_, 'tcx>,
param_env: ty::ParamEnv<'tcx>,
body: &mut Body<'tcx>,
promoted: &mut IndexVec<Promoted, Body<'tcx>>,
Expand Down Expand Up @@ -157,7 +156,7 @@ fn populate_polonius_move_facts(
///
/// This may result in errors being reported.
pub(crate) fn compute_regions<'cx, 'tcx>(
infcx: &InferCtxt<'tcx>,
infcx: &BorrowckInferCtxt<'_, 'tcx>,
universal_regions: UniversalRegions<'tcx>,
body: &Body<'tcx>,
promoted: &IndexVec<Promoted, Body<'tcx>>,
Expand Down Expand Up @@ -258,6 +257,11 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
borrow_set,
);

if cfg!(debug_assertions) {
let var_to_origin = infcx.reg_var_to_origin.borrow();
debug!("var_to_origin: {:#?}", var_to_origin);
}

let mut regioncx = RegionInferenceContext::new(
var_origins,
universal_regions,
Expand Down Expand Up @@ -322,7 +326,7 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
}

pub(super) fn dump_mir_results<'tcx>(
infcx: &InferCtxt<'tcx>,
infcx: &BorrowckInferCtxt<'_, 'tcx>,
body: &Body<'tcx>,
regioncx: &RegionInferenceContext<'tcx>,
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
Expand Down Expand Up @@ -372,7 +376,7 @@ pub(super) fn dump_mir_results<'tcx>(
#[allow(rustc::diagnostic_outside_of_impl)]
#[allow(rustc::untranslatable_diagnostic)]
pub(super) fn dump_annotation<'tcx>(
infcx: &InferCtxt<'tcx>,
infcx: &BorrowckInferCtxt<'_, 'tcx>,
body: &Body<'tcx>,
regioncx: &RegionInferenceContext<'tcx>,
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
Expand Down
Loading

0 comments on commit 2f79f73

Please sign in to comment.