Skip to content

Commit db01b67

Browse files
committed
[nll] Refactor the Edges iterator to return OutlivesConstraints
Part of #53178
1 parent a8c11d2 commit db01b67

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

src/librustc_mir/borrow_check/nll/constraints/graph.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,33 @@ impl<D: ConstraintGraphDirecton> ConstraintGraph<D> {
103103
}
104104

105105
/// Given a region `R`, iterate over all constraints `R: R1`.
106-
crate fn outgoing_edges(&self, region_sup: RegionVid) -> Edges<'_, D> {
106+
crate fn outgoing_edges<'a>(
107+
&'a self,
108+
region_sup: RegionVid,
109+
constraints: &'a ConstraintSet,
110+
) -> Edges<'a, D> {
107111
let first = self.first_constraints[region_sup];
108112
Edges {
109113
graph: self,
114+
constraints,
110115
pointer: first,
111116
}
112117
}
113118
}
114119

115120
crate struct Edges<'s, D: ConstraintGraphDirecton> {
116121
graph: &'s ConstraintGraph<D>,
122+
constraints: &'s ConstraintSet,
117123
pointer: Option<ConstraintIndex>,
118124
}
119125

120126
impl<'s, D: ConstraintGraphDirecton> Iterator for Edges<'s, D> {
121-
type Item = ConstraintIndex;
127+
type Item = OutlivesConstraint;
122128

123129
fn next(&mut self) -> Option<Self::Item> {
124130
if let Some(p) = self.pointer {
125131
self.pointer = self.graph.next_constraints[p];
126-
Some(p)
132+
Some(self.constraints[p])
127133
} else {
128134
None
129135
}
@@ -154,22 +160,20 @@ impl<'s, D: ConstraintGraphDirecton> RegionGraph<'s, D> {
154160
/// there exists a constraint `R: R1`.
155161
crate fn outgoing_regions(&self, region_sup: RegionVid) -> Successors<'_, D> {
156162
Successors {
157-
set: self.set,
158-
edges: self.constraint_graph.outgoing_edges(region_sup),
163+
edges: self.constraint_graph.outgoing_edges(region_sup, self.set),
159164
}
160165
}
161166
}
162167

163168
crate struct Successors<'s, D: ConstraintGraphDirecton> {
164-
set: &'s ConstraintSet,
165169
edges: Edges<'s, D>,
166170
}
167171

168172
impl<'s, D: ConstraintGraphDirecton> Iterator for Successors<'s, D> {
169173
type Item = RegionVid;
170174

171175
fn next(&mut self) -> Option<Self::Item> {
172-
self.edges.next().map(|c| D::end_region(&self.set[c]))
176+
self.edges.next().map(|c| D::end_region(&c))
173177
}
174178
}
175179

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs

+19-21
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use borrow_check::nll::region_infer::{ConstraintIndex, RegionInferenceContext};
11+
use borrow_check::nll::constraints::OutlivesConstraint;
12+
use borrow_check::nll::region_infer::RegionInferenceContext;
1213
use borrow_check::nll::type_check::Locations;
1314
use rustc::hir::def_id::DefId;
1415
use rustc::infer::error_reporting::nice_region_error::NiceRegionError;
@@ -53,7 +54,7 @@ impl fmt::Display for ConstraintCategory {
5354
#[derive(Copy, Clone, PartialEq, Eq)]
5455
enum Trace {
5556
StartRegion,
56-
FromConstraint(ConstraintIndex),
57+
FromOutlivesConstraint(OutlivesConstraint),
5758
NotVisited,
5859
}
5960

@@ -80,12 +81,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
8081
debug!(
8182
"best_blame_constraint: path={:#?}",
8283
path.iter()
83-
.map(|&ci| format!(
84-
"{:?}: {:?} ({:?}: {:?})",
85-
ci,
86-
&self.constraints[ci],
87-
self.constraint_sccs.scc(self.constraints[ci].sup),
88-
self.constraint_sccs.scc(self.constraints[ci].sub),
84+
.map(|&c| format!(
85+
"{:?} ({:?}: {:?})",
86+
c,
87+
self.constraint_sccs.scc(c.sup),
88+
self.constraint_sccs.scc(c.sub),
8989
))
9090
.collect::<Vec<_>>()
9191
);
@@ -121,7 +121,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
121121
// highlight (e.g., a call site or something).
122122
let target_scc = self.constraint_sccs.scc(target_region);
123123
let best_choice = (0..path.len()).rev().find(|&i| {
124-
let constraint = &self.constraints[path[i]];
124+
let constraint = path[i];
125125

126126
let constraint_sup_scc = self.constraint_sccs.scc(constraint.sup);
127127

@@ -164,7 +164,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
164164
&self,
165165
from_region: RegionVid,
166166
target_test: impl Fn(RegionVid) -> bool,
167-
) -> Option<(Vec<ConstraintIndex>, RegionVid)> {
167+
) -> Option<(Vec<OutlivesConstraint>, RegionVid)> {
168168
let mut context = IndexVec::from_elem(Trace::NotVisited, &self.definitions);
169169
context[from_region] = Trace::StartRegion;
170170

@@ -185,9 +185,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
185185
Trace::NotVisited => {
186186
bug!("found unvisited region {:?} on path to {:?}", p, r)
187187
}
188-
Trace::FromConstraint(c) => {
188+
Trace::FromOutlivesConstraint(c) => {
189189
result.push(c);
190-
p = self.constraints[c].sup;
190+
p = c.sup;
191191
}
192192

193193
Trace::StartRegion => {
@@ -201,11 +201,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
201201
// Otherwise, walk over the outgoing constraints and
202202
// enqueue any regions we find, keeping track of how we
203203
// reached them.
204-
for constraint in self.constraint_graph.outgoing_edges(r) {
205-
assert_eq!(self.constraints[constraint].sup, r);
206-
let sub_region = self.constraints[constraint].sub;
204+
for constraint in self.constraint_graph.outgoing_edges(r, &self.constraints) {
205+
assert_eq!(constraint.sup, r);
206+
let sub_region = constraint.sub;
207207
if let Trace::NotVisited = context[sub_region] {
208-
context[sub_region] = Trace::FromConstraint(constraint);
208+
context[sub_region] = Trace::FromOutlivesConstraint(constraint);
209209
deque.push_back(sub_region);
210210
}
211211
}
@@ -216,8 +216,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
216216

217217
/// This function will return true if a constraint is interesting and false if a constraint
218218
/// is not. It is useful in filtering constraint paths to only interesting points.
219-
fn constraint_is_interesting(&self, index: ConstraintIndex) -> bool {
220-
let constraint = self.constraints[index];
219+
fn constraint_is_interesting(&self, constraint: OutlivesConstraint) -> bool {
221220
debug!(
222221
"constraint_is_interesting: locations={:?} constraint={:?}",
223222
constraint.locations, constraint
@@ -232,19 +231,18 @@ impl<'tcx> RegionInferenceContext<'tcx> {
232231
/// This function classifies a constraint from a location.
233232
fn classify_constraint(
234233
&self,
235-
index: ConstraintIndex,
234+
constraint: OutlivesConstraint,
236235
mir: &Mir<'tcx>,
237236
tcx: TyCtxt<'_, '_, 'tcx>,
238237
) -> (ConstraintCategory, Span) {
239-
let constraint = self.constraints[index];
240238
debug!("classify_constraint: constraint={:?}", constraint);
241239
let span = constraint.locations.span(mir);
242240
let location = constraint
243241
.locations
244242
.from_location()
245243
.unwrap_or(Location::START);
246244

247-
if !self.constraint_is_interesting(index) {
245+
if !self.constraint_is_interesting(constraint) {
248246
return (ConstraintCategory::Boring, span);
249247
}
250248

src/librustc_mir/borrow_check/nll/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use super::universal_regions::UniversalRegions;
1212
use borrow_check::nll::constraints::graph::NormalConstraintGraph;
1313
use borrow_check::nll::constraints::{
14-
ConstraintIndex, ConstraintSccIndex, ConstraintSet, OutlivesConstraint,
14+
ConstraintSccIndex, ConstraintSet, OutlivesConstraint,
1515
};
1616
use borrow_check::nll::region_infer::values::{RegionElement, ToElementIndex};
1717
use borrow_check::nll::type_check::free_region_relations::UniversalRegionRelations;

0 commit comments

Comments
 (0)