Skip to content

Commit 15739b8

Browse files
committed
Nit: rework region obligations to a snapshotted vector
1 parent f722591 commit 15739b8

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

src/librustc/infer/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use std::fmt;
3535
use syntax::ast;
3636
use errors::DiagnosticBuilder;
3737
use syntax_pos::{self, Span, DUMMY_SP};
38-
use util::nodemap::{NodeMap, FxHashMap};
38+
use util::nodemap::FxHashMap;
3939
use arena::DroplessArena;
4040

4141
use self::combine::CombineFields;
@@ -179,7 +179,7 @@ pub struct InferCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
179179
// for each body-id in this map, which will process the
180180
// obligations within. This is expected to be done 'late enough'
181181
// that all type inference variables have been bound and so forth.
182-
region_obligations: RefCell<NodeMap<Vec<RegionObligation<'tcx>>>>,
182+
region_obligations: RefCell<Vec<(ast::NodeId, RegionObligation<'tcx>)>>,
183183
}
184184

185185
/// A map returned by `skolemize_late_bound_regions()` indicating the skolemized
@@ -450,7 +450,7 @@ impl<'a, 'gcx, 'tcx> InferCtxtBuilder<'a, 'gcx, 'tcx> {
450450
tainted_by_errors_flag: Cell::new(false),
451451
err_count_on_creation: tcx.sess.err_count(),
452452
in_snapshot: Cell::new(false),
453-
region_obligations: RefCell::new(NodeMap()),
453+
region_obligations: RefCell::new(vec![]),
454454
}))
455455
}
456456
}
@@ -478,6 +478,7 @@ pub struct CombinedSnapshot<'a, 'tcx:'a> {
478478
int_snapshot: unify::Snapshot<ty::IntVid>,
479479
float_snapshot: unify::Snapshot<ty::FloatVid>,
480480
region_constraints_snapshot: RegionSnapshot,
481+
region_obligations_snapshot: usize,
481482
was_in_snapshot: bool,
482483
_in_progress_tables: Option<Ref<'a, ty::TypeckTables<'tcx>>>,
483484
}
@@ -786,6 +787,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
786787
int_snapshot: self.int_unification_table.borrow_mut().snapshot(),
787788
float_snapshot: self.float_unification_table.borrow_mut().snapshot(),
788789
region_constraints_snapshot: self.borrow_region_constraints().start_snapshot(),
790+
region_obligations_snapshot: self.region_obligations.borrow().len(),
789791
was_in_snapshot: in_snapshot,
790792
// Borrow tables "in progress" (i.e. during typeck)
791793
// to ban writes from within a snapshot to them.
@@ -802,6 +804,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
802804
int_snapshot,
803805
float_snapshot,
804806
region_constraints_snapshot,
807+
region_obligations_snapshot,
805808
was_in_snapshot,
806809
_in_progress_tables } = snapshot;
807810

@@ -819,6 +822,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
819822
self.float_unification_table
820823
.borrow_mut()
821824
.rollback_to(float_snapshot);
825+
self.region_obligations
826+
.borrow_mut()
827+
.truncate(region_obligations_snapshot);
822828
self.borrow_region_constraints()
823829
.rollback_to(region_constraints_snapshot);
824830
}
@@ -830,6 +836,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
830836
int_snapshot,
831837
float_snapshot,
832838
region_constraints_snapshot,
839+
region_obligations_snapshot: _,
833840
was_in_snapshot,
834841
_in_progress_tables } = snapshot;
835842

src/librustc/infer/outlives/obligations.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
8686
) {
8787
self.region_obligations
8888
.borrow_mut()
89-
.entry(body_id)
90-
.or_insert(vec![])
91-
.push(obligation);
89+
.push((body_id, obligation));
9290
}
9391

9492
/// Process the region obligations that must be proven (during
@@ -131,10 +129,16 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
131129
param_env: ty::ParamEnv<'tcx>,
132130
body_id: ast::NodeId,
133131
) {
134-
let region_obligations = match self.region_obligations.borrow_mut().remove(&body_id) {
135-
None => vec![],
136-
Some(vec) => vec,
137-
};
132+
assert!(!self.in_snapshot.get(), "cannot process registered region obligations in a snapshot");
133+
134+
// pull out the region obligations with the given `body_id` (leaving the rest)
135+
let mut my_region_obligations = Vec::with_capacity(self.region_obligations.borrow().len());
136+
{
137+
let mut r_o = self.region_obligations.borrow_mut();
138+
for (_, obligation) in r_o.drain_filter(|(ro_body_id, _)| *ro_body_id == body_id) {
139+
my_region_obligations.push(obligation);
140+
}
141+
}
138142

139143
let outlives =
140144
TypeOutlives::new(self, region_bound_pairs, implicit_region_bound, param_env);
@@ -143,7 +147,7 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
143147
sup_type,
144148
sub_region,
145149
cause,
146-
} in region_obligations
150+
} in my_region_obligations
147151
{
148152
let origin = SubregionOrigin::from_obligation_cause(
149153
&cause,
@@ -170,11 +174,13 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
170174
outlives.type_must_outlive(origin, ty, region);
171175
}
172176

173-
/// Ignore the region obligations for a given `body_id`, not bothering to
174-
/// prove them. This function should not really exist; it is used to accommodate some older
175-
/// code for the time being.
176-
pub fn ignore_region_obligations(&self, body_id: ast::NodeId) {
177-
self.region_obligations.borrow_mut().remove(&body_id);
177+
/// Ignore the region obligations, not bothering to prove
178+
/// them. This function should not really exist; it is used to
179+
/// accommodate some older code for the time being.
180+
pub fn ignore_region_obligations(&self) {
181+
assert!(!self.in_snapshot.get(), "cannot ignore registered region obligations in a snapshot");
182+
183+
self.region_obligations.borrow_mut().clear();
178184
}
179185
}
180186

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#![feature(conservative_impl_trait)]
4646
#![feature(const_fn)]
4747
#![feature(core_intrinsics)]
48+
#![feature(drain_filter)]
4849
#![feature(i128_type)]
4950
#![feature(match_default_bindings)]
5051
#![feature(inclusive_range_syntax)]

src/librustc/traits/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,6 @@ pub fn normalize_param_env_or_error<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
511511
unnormalized_env.reveal);
512512

513513
tcx.infer_ctxt().enter(|infcx| {
514-
let body_id = cause.body_id;
515514
let predicates = match fully_normalize(
516515
&infcx,
517516
cause,
@@ -546,7 +545,7 @@ pub fn normalize_param_env_or_error<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
546545
// properly, and that code is currently largely confined to
547546
// regionck (though I made some efforts to extract it
548547
// out). -nmatsakis
549-
let _ = infcx.ignore_region_obligations(body_id);
548+
let _ = infcx.ignore_region_obligations();
550549

551550
infcx.resolve_regions_and_report_errors(region_context, &region_scope_tree, &free_regions);
552551
let predicates = match infcx.fully_resolve(&predicates) {

0 commit comments

Comments
 (0)