Skip to content

Commit 2f79f73

Browse files
committed
collect region contexts during mir renumbering
1 parent cb35a7b commit 2f79f73

File tree

9 files changed

+364
-42
lines changed

9 files changed

+364
-42
lines changed

Cargo.lock

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ dependencies = [
782782
"declare_clippy_lint",
783783
"if_chain",
784784
"itertools",
785-
"pulldown-cmark",
785+
"pulldown-cmark 0.9.2",
786786
"quine-mc_cluskey",
787787
"regex-syntax",
788788
"rustc-semver",
@@ -2003,9 +2003,9 @@ dependencies = [
20032003

20042004
[[package]]
20052005
name = "http-auth"
2006-
version = "0.1.6"
2006+
version = "0.1.8"
20072007
source = "registry+https://github.com/rust-lang/crates.io-index"
2008-
checksum = "c0b40b39d66c28829a0cf4d09f7e139ff8201f7500a5083732848ed3b4b4d850"
2008+
checksum = "5430cacd7a1f9a02fbeb350dfc81a0e5ed42d81f3398cb0ba184017f85bdcfbc"
20092009
dependencies = [
20102010
"memchr",
20112011
]
@@ -2555,7 +2555,7 @@ dependencies = [
25552555
"memchr",
25562556
"once_cell",
25572557
"opener",
2558-
"pulldown-cmark",
2558+
"pulldown-cmark 0.9.2",
25592559
"regex",
25602560
"serde",
25612561
"serde_json",
@@ -2572,7 +2572,7 @@ dependencies = [
25722572
"anyhow",
25732573
"handlebars 3.5.5",
25742574
"pretty_assertions",
2575-
"pulldown-cmark",
2575+
"pulldown-cmark 0.7.2",
25762576
"same-file",
25772577
"serde_json",
25782578
"url",
@@ -3269,6 +3269,17 @@ dependencies = [
32693269
"cc",
32703270
]
32713271

3272+
[[package]]
3273+
name = "pulldown-cmark"
3274+
version = "0.7.2"
3275+
source = "registry+https://github.com/rust-lang/crates.io-index"
3276+
checksum = "ca36dea94d187597e104a5c8e4b07576a8a45aa5db48a65e12940d3eb7461f55"
3277+
dependencies = [
3278+
"bitflags",
3279+
"memchr",
3280+
"unicase",
3281+
]
3282+
32723283
[[package]]
32733284
name = "pulldown-cmark"
32743285
version = "0.9.2"
@@ -4572,7 +4583,7 @@ name = "rustc_resolve"
45724583
version = "0.0.0"
45734584
dependencies = [
45744585
"bitflags",
4575-
"pulldown-cmark",
4586+
"pulldown-cmark 0.9.2",
45764587
"rustc_arena",
45774588
"rustc_ast",
45784589
"rustc_ast_pretty",

compiler/rustc_borrowck/src/lib.rs

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use rustc_hir as hir;
2525
use rustc_hir::def_id::LocalDefId;
2626
use rustc_index::bit_set::ChunkedBitSet;
2727
use rustc_index::vec::IndexVec;
28-
use rustc_infer::infer::{DefiningAnchor, InferCtxt, NllRegionVariableOrigin, TyCtxtInferExt};
28+
use rustc_infer::infer::{
29+
DefiningAnchor, InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin, TyCtxtInferExt,
30+
};
2931
use rustc_middle::mir::{
3032
traversal, Body, ClearCrossCrate, Local, Location, Mutability, NonDivergingIntrinsic, Operand,
3133
Place, PlaceElem, PlaceRef, VarDebugInfoContents,
@@ -95,6 +97,7 @@ use nll::{PoloniusOutput, ToRegionVid};
9597
use place_ext::PlaceExt;
9698
use places_conflict::{places_conflict, PlaceConflictBias};
9799
use region_infer::RegionInferenceContext;
100+
use renumber::RegionCtxt;
98101

99102
// FIXME(eddyb) perhaps move this somewhere more centrally.
100103
#[derive(Debug)]
@@ -168,10 +171,10 @@ fn do_mir_borrowck<'tcx>(
168171
return_body_with_facts: bool,
169172
) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
170173
let def = input_body.source.with_opt_param().as_local().unwrap();
171-
172174
debug!(?def);
173175

174176
let tcx = infcx.tcx;
177+
let infcx = BorrowckInferCtxt::new(infcx);
175178
let param_env = tcx.param_env(def.did);
176179

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

225228
let location_table_owned = LocationTable::new(body);
@@ -257,7 +260,7 @@ fn do_mir_borrowck<'tcx>(
257260
opt_closure_req,
258261
nll_errors,
259262
} = nll::compute_regions(
260-
infcx,
263+
&infcx,
261264
free_regions,
262265
body,
263266
&promoted,
@@ -272,12 +275,12 @@ fn do_mir_borrowck<'tcx>(
272275

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

277280
// We also have a `#[rustc_regions]` annotation that causes us to dump
278281
// information.
279282
nll::dump_annotation(
280-
infcx,
283+
&infcx,
281284
&body,
282285
&regioncx,
283286
&opt_closure_req,
@@ -321,7 +324,7 @@ fn do_mir_borrowck<'tcx>(
321324

322325
if let Err((move_data, move_errors)) = move_data_results {
323326
let mut promoted_mbcx = MirBorrowckCtxt {
324-
infcx,
327+
infcx: &infcx,
325328
param_env,
326329
body: promoted_body,
327330
move_data: &move_data,
@@ -350,7 +353,7 @@ fn do_mir_borrowck<'tcx>(
350353
}
351354

352355
let mut mbcx = MirBorrowckCtxt {
353-
infcx,
356+
infcx: &infcx,
354357
param_env,
355358
body,
356359
move_data: &mdpe.move_data,
@@ -482,22 +485,81 @@ pub struct BodyWithBorrowckFacts<'tcx> {
482485
pub location_table: LocationTable,
483486
}
484487

485-
struct BorrowckInferCtxt<'cx, 'tcx> {
488+
pub struct BorrowckInferCtxt<'cx, 'tcx> {
486489
pub(crate) infcx: &'cx InferCtxt<'tcx>,
487490

488491
#[cfg(debug_assertions)]
489-
pub(crate) _reg_var_to_origin: RefCell<FxHashMap<ty::Region<'tcx>, NllRegionVariableOrigin>>,
492+
pub(crate) reg_var_to_origin: RefCell<FxHashMap<ty::RegionVid, RegionCtxt>>,
490493
}
491494

492495
impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> {
493496
#[cfg(not(debug_assertions))]
494-
pub(crate) fn _new(infcx: &'cx InferCtxt<'tcx>) -> Self {
497+
pub(crate) fn new(infcx: &'cx InferCtxt<'tcx>) -> Self {
495498
BorrowckInferCtxt { infcx }
496499
}
497500

498501
#[cfg(debug_assertions)]
499-
pub(crate) fn _new(infcx: &'cx InferCtxt<'tcx>) -> Self {
500-
BorrowckInferCtxt { infcx, _reg_var_to_origin: RefCell::new(Default::default()) }
502+
pub(crate) fn new(infcx: &'cx InferCtxt<'tcx>) -> Self {
503+
BorrowckInferCtxt { infcx, reg_var_to_origin: RefCell::new(Default::default()) }
504+
}
505+
506+
#[cfg(not(debug_assertions))]
507+
pub(crate) fn next_region_var(&self, origin: RegionVariableOrigin) -> ty::Region<'tcx> {
508+
self.infcx.next_region_var(origin)
509+
}
510+
511+
#[cfg(debug_assertions)]
512+
pub(crate) fn next_region_var(
513+
&self,
514+
origin: RegionVariableOrigin,
515+
ctxt: RegionCtxt,
516+
) -> ty::Region<'tcx> {
517+
let next_region = self.infcx.next_region_var(origin);
518+
let vid = next_region
519+
.try_get_var()
520+
.unwrap_or_else(|| bug!("expected RegionKind::RegionVar on {:?}", next_region));
521+
522+
debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin);
523+
let mut var_to_origin = self.reg_var_to_origin.borrow_mut();
524+
let prev = var_to_origin.insert(vid, ctxt);
525+
debug!("var_to_origin after insertion: {:?}", var_to_origin);
526+
527+
// This only makes sense if not called in a canonicalization context. If this
528+
// ever changes we either want to get rid of `BorrowckInferContext::reg_var_to_origin`
529+
// or modify how we track nll region vars for that map.
530+
assert!(matches!(prev, None));
531+
532+
next_region
533+
}
534+
535+
#[cfg(not(debug_assertions))]
536+
pub(crate) fn next_nll_region_var(&self, origin: NllRegionVariableOrigin) -> ty::Region<'tcx> {
537+
self.infcx.next_nll_region_var(origin)
538+
}
539+
540+
#[cfg(debug_assertions)]
541+
#[instrument(skip(self), level = "debug")]
542+
pub(crate) fn next_nll_region_var(
543+
&self,
544+
origin: NllRegionVariableOrigin,
545+
ctxt: RegionCtxt,
546+
) -> ty::Region<'tcx> {
547+
let next_region = self.infcx.next_nll_region_var(origin.clone());
548+
let vid = next_region
549+
.try_get_var()
550+
.unwrap_or_else(|| bug!("expected RegionKind::RegionVar on {:?}", next_region));
551+
552+
debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin);
553+
let mut var_to_origin = self.reg_var_to_origin.borrow_mut();
554+
let prev = var_to_origin.insert(vid, ctxt);
555+
debug!("var_to_origin after insertion: {:?}", var_to_origin);
556+
557+
// This only makes sense if not called in a canonicalization context. If this
558+
// ever changes we either want to get rid of `BorrowckInferContext::reg_var_to_origin`
559+
// or modify how we track nll region vars for that map.
560+
assert!(matches!(prev, None));
561+
562+
next_region
501563
}
502564
}
503565

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

512574
struct MirBorrowckCtxt<'cx, 'tcx> {
513-
infcx: &'cx InferCtxt<'tcx>,
575+
infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>,
514576
param_env: ParamEnv<'tcx>,
515577
body: &'cx Body<'tcx>,
516578
move_data: &'cx MoveData<'tcx>,

compiler/rustc_borrowck/src/nll.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use rustc_data_structures::vec_map::VecMap;
66
use rustc_hir::def_id::LocalDefId;
77
use rustc_index::vec::IndexVec;
8-
use rustc_infer::infer::InferCtxt;
98
use rustc_middle::mir::{create_dump_file, dump_enabled, dump_mir, PassWhere};
109
use rustc_middle::mir::{
1110
BasicBlock, Body, ClosureOutlivesSubject, ClosureRegionRequirements, LocalKind, Location,
@@ -37,7 +36,7 @@ use crate::{
3736
renumber,
3837
type_check::{self, MirTypeckRegionConstraints, MirTypeckResults},
3938
universal_regions::UniversalRegions,
40-
Upvar,
39+
BorrowckInferCtxt, Upvar,
4140
};
4241

4342
pub type PoloniusOutput = Output<RustcFacts>;
@@ -58,7 +57,7 @@ pub(crate) struct NllOutput<'tcx> {
5857
/// `compute_regions`.
5958
#[instrument(skip(infcx, param_env, body, promoted), level = "debug")]
6059
pub(crate) fn replace_regions_in_mir<'tcx>(
61-
infcx: &InferCtxt<'tcx>,
60+
infcx: &BorrowckInferCtxt<'_, 'tcx>,
6261
param_env: ty::ParamEnv<'tcx>,
6362
body: &mut Body<'tcx>,
6463
promoted: &mut IndexVec<Promoted, Body<'tcx>>,
@@ -157,7 +156,7 @@ fn populate_polonius_move_facts(
157156
///
158157
/// This may result in errors being reported.
159158
pub(crate) fn compute_regions<'cx, 'tcx>(
160-
infcx: &InferCtxt<'tcx>,
159+
infcx: &BorrowckInferCtxt<'_, 'tcx>,
161160
universal_regions: UniversalRegions<'tcx>,
162161
body: &Body<'tcx>,
163162
promoted: &IndexVec<Promoted, Body<'tcx>>,
@@ -258,6 +257,11 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
258257
borrow_set,
259258
);
260259

260+
if cfg!(debug_assertions) {
261+
let var_to_origin = infcx.reg_var_to_origin.borrow();
262+
debug!("var_to_origin: {:#?}", var_to_origin);
263+
}
264+
261265
let mut regioncx = RegionInferenceContext::new(
262266
var_origins,
263267
universal_regions,
@@ -322,7 +326,7 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
322326
}
323327

324328
pub(super) fn dump_mir_results<'tcx>(
325-
infcx: &InferCtxt<'tcx>,
329+
infcx: &BorrowckInferCtxt<'_, 'tcx>,
326330
body: &Body<'tcx>,
327331
regioncx: &RegionInferenceContext<'tcx>,
328332
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
@@ -372,7 +376,7 @@ pub(super) fn dump_mir_results<'tcx>(
372376
#[allow(rustc::diagnostic_outside_of_impl)]
373377
#[allow(rustc::untranslatable_diagnostic)]
374378
pub(super) fn dump_annotation<'tcx>(
375-
infcx: &InferCtxt<'tcx>,
379+
infcx: &BorrowckInferCtxt<'_, 'tcx>,
376380
body: &Body<'tcx>,
377381
regioncx: &RegionInferenceContext<'tcx>,
378382
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,

0 commit comments

Comments
 (0)