Skip to content

Commit caacdd2

Browse files
author
Markus Westerlind
committed
Move region_constraint to the unified undo log
1 parent 1506b1f commit caacdd2

File tree

7 files changed

+196
-171
lines changed

7 files changed

+196
-171
lines changed

Cargo.lock

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,17 @@ dependencies = [
987987
[[package]]
988988
name = "ena"
989989
version = "0.13.1"
990-
source = "git+https://github.com/Marwes/ena?branch=detach_undo_log#9b977ea7f209a35f46d65d33cdd74b8f4931fb8a"
990+
source = "registry+https://github.com/rust-lang/crates.io-index"
991+
checksum = "8944dc8fa28ce4a38f778bd46bf7d923fe73eed5a439398507246c8e017e6f36"
992+
dependencies = [
993+
"log",
994+
]
995+
996+
[[package]]
997+
name = "ena"
998+
version = "0.14.0"
999+
source = "registry+https://github.com/rust-lang/crates.io-index"
1000+
checksum = "d7402b94a93c24e742487327a7cd839dc9d36fec9de9fb25b09f2dae459f36c3"
9911001
dependencies = [
9921002
"log",
9931003
]
@@ -3234,7 +3244,7 @@ dependencies = [
32343244
"bitflags",
32353245
"cfg-if",
32363246
"crossbeam-utils 0.7.2",
3237-
"ena",
3247+
"ena 0.13.1",
32383248
"indexmap",
32393249
"jobserver",
32403250
"lazy_static 1.4.0",
@@ -3680,7 +3690,7 @@ dependencies = [
36803690
"bitflags",
36813691
"cfg-if",
36823692
"crossbeam-utils 0.7.2",
3683-
"ena",
3693+
"ena 0.14.0",
36843694
"graphviz",
36853695
"indexmap",
36863696
"jobserver",

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,5 @@ rustc-std-workspace-core = { path = 'src/tools/rustc-std-workspace-core' }
6565
rustc-std-workspace-alloc = { path = 'src/tools/rustc-std-workspace-alloc' }
6666
rustc-std-workspace-std = { path = 'src/tools/rustc-std-workspace-std' }
6767

68-
ena = { version = "0.13.1", git = "https://github.com/Marwes/ena", branch = "detach_undo_log" }
69-
7068
[patch."https://github.com/rust-lang/rust-clippy"]
7169
clippy_lints = { path = "src/tools/clippy/clippy_lints" }

src/librustc_data_structures/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ path = "lib.rs"
1010
doctest = false
1111

1212
[dependencies]
13-
ena = "0.13.1"
13+
ena = "0.14"
1414
indexmap = "1"
1515
log = "0.4"
1616
jobserver_crate = { version = "0.1.13", package = "jobserver" }

src/librustc_infer/infer/mod.rs

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ use self::free_regions::RegionRelations;
4545
use self::lexical_region_resolve::LexicalRegionResolutions;
4646
use self::outlives::env::OutlivesEnvironment;
4747
use self::region_constraints::{GenericKind, RegionConstraintData, VarInfos, VerifyBound};
48-
use self::region_constraints::{RegionConstraintCollector, RegionSnapshot};
48+
use self::region_constraints::{
49+
RegionConstraintCollector, RegionConstraintStorage, RegionSnapshot,
50+
};
4951
use self::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
5052

5153
pub mod at;
@@ -161,7 +163,7 @@ pub struct InferCtxtInner<'tcx> {
161163
/// `resolve_regions_and_report_errors` is invoked, this gets set to `None`
162164
/// -- further attempts to perform unification, etc., may fail if new
163165
/// region constraints would've been added.
164-
region_constraints: Option<RegionConstraintCollector<'tcx>>,
166+
region_constraints: Option<RegionConstraintStorage<'tcx>>,
165167

166168
/// A set of constraints that regionck must validate. Each
167169
/// constraint has the form `T:'a`, meaning "some type `T` must
@@ -206,7 +208,7 @@ impl<'tcx> InferCtxtInner<'tcx> {
206208
const_unification_table: ut::UnificationStorage::new(),
207209
int_unification_table: ut::UnificationStorage::new(),
208210
float_unification_table: ut::UnificationStorage::new(),
209-
region_constraints: Some(RegionConstraintCollector::new()),
211+
region_constraints: Some(RegionConstraintStorage::new()),
210212
region_obligations: vec![],
211213
}
212214
}
@@ -243,8 +245,11 @@ impl<'tcx> InferCtxtInner<'tcx> {
243245
ut::UnificationTable::with_log(&mut self.const_unification_table, &mut self.undo_log)
244246
}
245247

246-
pub fn unwrap_region_constraints(&mut self) -> &mut RegionConstraintCollector<'tcx> {
247-
self.region_constraints.as_mut().expect("region constraints already solved")
248+
pub fn unwrap_region_constraints(&mut self) -> RegionConstraintCollector<'tcx, '_> {
249+
self.region_constraints
250+
.as_mut()
251+
.expect("region constraints already solved")
252+
.with_log(&mut self.undo_log)
248253
}
249254
}
250255

@@ -258,6 +263,14 @@ pub(crate) enum UndoLog<'tcx> {
258263
ConstUnificationTable(sv::UndoLog<ut::Delegate<ty::ConstVid<'tcx>>>),
259264
IntUnificationTable(sv::UndoLog<ut::Delegate<ty::IntVid>>),
260265
FloatUnificationTable(sv::UndoLog<ut::Delegate<ty::FloatVid>>),
266+
RegionConstraintCollector(region_constraints::UndoLog<'tcx>),
267+
RegionUnificationTable(sv::UndoLog<ut::Delegate<ty::RegionVid>>),
268+
}
269+
270+
impl<'tcx> From<region_constraints::UndoLog<'tcx>> for UndoLog<'tcx> {
271+
fn from(l: region_constraints::UndoLog<'tcx>) -> Self {
272+
UndoLog::RegionConstraintCollector(l)
273+
}
261274
}
262275

263276
impl<'tcx> From<sv::UndoLog<ut::Delegate<type_variable::TyVidEqKey<'tcx>>>> for UndoLog<'tcx> {
@@ -308,6 +321,12 @@ impl<'tcx> From<sv::UndoLog<ut::Delegate<ty::FloatVid>>> for UndoLog<'tcx> {
308321
}
309322
}
310323

324+
impl<'tcx> From<sv::UndoLog<ut::Delegate<ty::RegionVid>>> for UndoLog<'tcx> {
325+
fn from(l: sv::UndoLog<ut::Delegate<ty::RegionVid>>) -> Self {
326+
Self::RegionUnificationTable(l)
327+
}
328+
}
329+
311330
pub(crate) type UnificationTable<'a, 'tcx, T> =
312331
ut::UnificationTable<ut::InPlace<T, &'a mut ut::UnificationStorage<T>, &'a mut Logs<'tcx>>>;
313332

@@ -316,6 +335,7 @@ struct RollbackView<'tcx, 'a> {
316335
const_unification_table: &'a mut ut::UnificationStorage<ty::ConstVid<'tcx>>,
317336
int_unification_table: &'a mut ut::UnificationStorage<ty::IntVid>,
318337
float_unification_table: &'a mut ut::UnificationStorage<ty::FloatVid>,
338+
region_constraints: &'a mut RegionConstraintStorage<'tcx>,
319339
}
320340

321341
impl<'tcx> Rollback<UndoLog<'tcx>> for RollbackView<'tcx, '_> {
@@ -325,6 +345,10 @@ impl<'tcx> Rollback<UndoLog<'tcx>> for RollbackView<'tcx, '_> {
325345
UndoLog::ConstUnificationTable(undo) => self.const_unification_table.reverse(undo),
326346
UndoLog::IntUnificationTable(undo) => self.int_unification_table.reverse(undo),
327347
UndoLog::FloatUnificationTable(undo) => self.float_unification_table.reverse(undo),
348+
UndoLog::RegionConstraintCollector(undo) => self.region_constraints.reverse(undo),
349+
UndoLog::RegionUnificationTable(undo) => {
350+
self.region_constraints.unification_table.reverse(undo)
351+
}
328352
}
329353
}
330354
}
@@ -408,6 +432,16 @@ impl<'tcx> Snapshots<UndoLog<'tcx>> for Logs<'tcx> {
408432
}
409433

410434
impl<'tcx> Logs<'tcx> {
435+
pub(crate) fn region_constraints(
436+
&self,
437+
after: usize,
438+
) -> impl Iterator<Item = &'_ region_constraints::UndoLog<'tcx>> + Clone {
439+
self.logs[after..].iter().filter_map(|log| match log {
440+
UndoLog::RegionConstraintCollector(log) => Some(log),
441+
_ => None,
442+
})
443+
}
444+
411445
fn assert_open_snapshot(&self, snapshot: &Snapshot<'tcx>) {
412446
// Failures here may indicate a failure to follow a stack discipline.
413447
assert!(self.logs.len() >= snapshot.undo_len);
@@ -1004,7 +1038,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10041038
const_snapshot: _,
10051039
int_snapshot: _,
10061040
float_snapshot: _,
1007-
region_constraints_snapshot,
1041+
region_constraints_snapshot: _,
10081042
region_obligations_snapshot,
10091043
universe,
10101044
was_in_snapshot,
@@ -1023,6 +1057,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10231057
const_unification_table,
10241058
int_unification_table,
10251059
float_unification_table,
1060+
region_constraints,
10261061
..
10271062
} = inner;
10281063
inner.undo_log.rollback_to(
@@ -1031,11 +1066,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10311066
const_unification_table,
10321067
int_unification_table,
10331068
float_unification_table,
1069+
region_constraints: region_constraints.as_mut().unwrap(),
10341070
},
10351071
undo_snapshot,
10361072
);
10371073
inner.projection_cache.rollback_to(projection_cache_snapshot);
1038-
inner.unwrap_region_constraints().rollback_to(region_constraints_snapshot);
10391074
inner.region_obligations.truncate(region_obligations_snapshot);
10401075
}
10411076

@@ -1048,7 +1083,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10481083
const_snapshot: _,
10491084
int_snapshot: _,
10501085
float_snapshot: _,
1051-
region_constraints_snapshot,
1086+
region_constraints_snapshot: _,
10521087
region_obligations_snapshot: _,
10531088
universe: _,
10541089
was_in_snapshot,
@@ -1062,7 +1097,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10621097
let mut inner = self.inner.borrow_mut();
10631098
inner.undo_log.commit(undo_snapshot);
10641099
inner.projection_cache.commit(projection_cache_snapshot);
1065-
inner.unwrap_region_constraints().commit(region_constraints_snapshot);
10661100
}
10671101

10681102
/// Executes `f` and commit the bindings.
@@ -1135,7 +1169,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11351169
self.inner
11361170
.borrow_mut()
11371171
.unwrap_region_constraints()
1138-
.region_constraints_added_in_snapshot(&snapshot.region_constraints_snapshot)
1172+
.region_constraints_added_in_snapshot(&snapshot.undo_snapshot)
11391173
}
11401174

11411175
pub fn add_given(&self, sub: ty::Region<'tcx>, sup: ty::RegionVid) {
@@ -1466,6 +1500,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14661500
.region_constraints
14671501
.take()
14681502
.expect("regions already resolved")
1503+
.with_log(&mut inner.undo_log)
14691504
.into_infos_and_data();
14701505

14711506
let region_rels = &RegionRelations::new(
@@ -1527,12 +1562,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15271562
/// called. This is used only during NLL processing to "hand off" ownership
15281563
/// of the set of region variables into the NLL region context.
15291564
pub fn take_region_var_origins(&self) -> VarInfos {
1530-
let (var_infos, data) = self
1531-
.inner
1532-
.borrow_mut()
1565+
let mut inner = self.inner.borrow_mut();
1566+
let (var_infos, data) = inner
15331567
.region_constraints
15341568
.take()
15351569
.expect("regions already resolved")
1570+
.with_log(&mut inner.undo_log)
15361571
.into_infos_and_data();
15371572
assert!(data.is_empty());
15381573
var_infos

src/librustc_infer/infer/region_constraints/leak_check.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use super::*;
22
use crate::infer::{CombinedSnapshot, PlaceholderMap};
3+
use rustc_data_structures::undo_log::UndoLogs;
34
use rustc_middle::ty::error::TypeError;
45
use rustc_middle::ty::relate::RelateResult;
56

6-
impl<'tcx> RegionConstraintCollector<'tcx> {
7+
impl<'tcx> RegionConstraintCollector<'tcx, '_> {
78
/// Searches region constraints created since `snapshot` that
89
/// affect one of the placeholders in `placeholder_map`, returning
910
/// an error if any of the placeholders are related to another
@@ -31,7 +32,7 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
3132
) -> RelateResult<'tcx, ()> {
3233
debug!("leak_check(placeholders={:?})", placeholder_map);
3334

34-
assert!(self.in_snapshot());
35+
assert!(UndoLogs::<super::UndoLog<'_>>::in_snapshot(&self.undo_log));
3536

3637
// Go through each placeholder that we created.
3738
for &placeholder_region in placeholder_map.values() {
@@ -45,7 +46,11 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
4546
// in some way. This means any region that either outlives
4647
// or is outlived by a placeholder.
4748
let mut taint_set = TaintSet::new(TaintDirections::both(), placeholder_region);
48-
taint_set.fixed_point(tcx, &self.undo_log, &self.data.verifys);
49+
taint_set.fixed_point(
50+
tcx,
51+
self.undo_log.region_constraints(0),
52+
&self.storage.data.verifys,
53+
);
4954
let tainted_regions = taint_set.into_set();
5055

5156
// Report an error if two placeholders in the same universe
@@ -88,19 +93,21 @@ impl<'tcx> TaintSet<'tcx> {
8893
TaintSet { directions, regions }
8994
}
9095

91-
fn fixed_point(
96+
fn fixed_point<'a>(
9297
&mut self,
9398
tcx: TyCtxt<'tcx>,
94-
undo_log: &[UndoLog<'tcx>],
99+
undo_log: impl IntoIterator<Item = &'a UndoLog<'tcx>> + Clone,
95100
verifys: &[Verify<'tcx>],
96-
) {
101+
) where
102+
'tcx: 'a,
103+
{
97104
let mut prev_len = 0;
98105
while prev_len < self.len() {
99106
debug!("tainted: prev_len = {:?} new_len = {:?}", prev_len, self.len());
100107

101108
prev_len = self.len();
102109

103-
for undo_entry in undo_log {
110+
for undo_entry in undo_log.clone() {
104111
match undo_entry {
105112
&AddConstraint(Constraint::VarSubVar(a, b)) => {
106113
self.add_edge(tcx.mk_region(ReVar(a)), tcx.mk_region(ReVar(b)));

0 commit comments

Comments
 (0)