Skip to content

Commit 2e1e119

Browse files
committed
Move RegionTracker to region_infer
In terms of code organisation, this is a lot cleaner and allows tighter access modifiers.
1 parent 3bdcb9d commit 2e1e119

File tree

2 files changed

+93
-95
lines changed
  • compiler/rustc_borrowck/src

2 files changed

+93
-95
lines changed

compiler/rustc_borrowck/src/constraints/mod.rs

+1-94
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,13 @@
1-
use crate::region_infer::RegionDefinition;
21
use crate::type_check::Locations;
3-
use rustc_data_structures::graph::scc::{self, Sccs};
42
use rustc_index::{IndexSlice, IndexVec};
53
use rustc_middle::mir::ConstraintCategory;
6-
use rustc_middle::ty::{RegionVid, TyCtxt, UniverseIndex, VarianceDiagInfo};
4+
use rustc_middle::ty::{RegionVid, TyCtxt, VarianceDiagInfo};
75
use rustc_span::Span;
86
use std::fmt;
97
use std::ops::Index;
108

119
pub(crate) mod graph;
1210

13-
pub type ConstraintSccs = Sccs<RegionVid, ConstraintSccIndex, RegionTracker>;
14-
15-
/// An annotation for region graph SCCs that tracks
16-
/// the values of its elements.
17-
#[derive(Copy, Debug, Clone)]
18-
pub struct RegionTracker {
19-
/// The largest universe of a placeholder reached from this SCC.
20-
/// This includes placeholders within this SCC.
21-
max_placeholder_universe_reached: UniverseIndex,
22-
23-
/// The smallest universe index reachable form the nodes of this SCC.
24-
min_reachable_universe: UniverseIndex,
25-
26-
/// The representative Region Variable Id for this SCC. We prefer
27-
/// placeholders over existentially quantified variables, otherwise
28-
/// it's the one with the smallest Region Variable ID.
29-
pub representative: RegionVid,
30-
31-
/// Is the current representative a placeholder?
32-
representative_is_placeholder: bool,
33-
34-
/// Is the current representative existentially quantified?
35-
representative_is_existential: bool,
36-
}
37-
38-
impl scc::Annotation for RegionTracker {
39-
fn merge_scc(mut self, mut other: Self) -> Self {
40-
// Prefer any placeholder over any existential
41-
if other.representative_is_placeholder && self.representative_is_existential {
42-
other.merge_min_max_seen(&self);
43-
return other;
44-
}
45-
46-
if self.representative_is_placeholder && other.representative_is_existential
47-
|| (self.representative <= other.representative)
48-
{
49-
self.merge_min_max_seen(&other);
50-
return self;
51-
}
52-
other.merge_min_max_seen(&self);
53-
other
54-
}
55-
56-
fn merge_reached(mut self, other: Self) -> Self {
57-
// No update to in-component values, only add seen values.
58-
self.merge_min_max_seen(&other);
59-
self
60-
}
61-
}
62-
63-
impl RegionTracker {
64-
pub fn new(rvid: RegionVid, definition: &RegionDefinition<'_>) -> Self {
65-
let (representative_is_placeholder, representative_is_existential) = match definition.origin
66-
{
67-
rustc_infer::infer::NllRegionVariableOrigin::FreeRegion => (false, false),
68-
rustc_infer::infer::NllRegionVariableOrigin::Placeholder(_) => (true, false),
69-
rustc_infer::infer::NllRegionVariableOrigin::Existential { .. } => (false, true),
70-
};
71-
72-
let placeholder_universe =
73-
if representative_is_placeholder { definition.universe } else { UniverseIndex::ROOT };
74-
75-
Self {
76-
max_placeholder_universe_reached: placeholder_universe,
77-
min_reachable_universe: definition.universe,
78-
representative: rvid,
79-
representative_is_placeholder,
80-
representative_is_existential,
81-
}
82-
}
83-
pub fn universe(self) -> UniverseIndex {
84-
self.min_reachable_universe
85-
}
86-
87-
fn merge_min_max_seen(&mut self, other: &Self) {
88-
self.max_placeholder_universe_reached = std::cmp::max(
89-
self.max_placeholder_universe_reached,
90-
other.max_placeholder_universe_reached,
91-
);
92-
93-
self.min_reachable_universe =
94-
std::cmp::min(self.min_reachable_universe, other.min_reachable_universe);
95-
}
96-
97-
/// Returns `true` if during the annotated SCC reaches a placeholder
98-
/// with a universe larger than the smallest reachable one, `false` otherwise.
99-
pub fn has_incompatible_universes(&self) -> bool {
100-
self.universe().cannot_name(self.max_placeholder_universe_reached)
101-
}
102-
}
103-
10411
/// A set of NLL region constraints. These include "outlives"
10512
/// constraints of the form `R1: R2`. Each constraint is identified by
10613
/// a unique `OutlivesConstraintIndex` and you can index into the set

compiler/rustc_borrowck/src/region_infer/mod.rs

+92-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::rc::Rc;
44
use rustc_data_structures::binary_search_util;
55
use rustc_data_structures::frozen::Frozen;
66
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
7+
use rustc_data_structures::graph::scc::{self, Sccs};
78
use rustc_errors::Diag;
89
use rustc_hir::def_id::CRATE_DEF_ID;
910
use rustc_index::IndexVec;
@@ -23,7 +24,6 @@ use rustc_mir_dataflow::points::DenseLocationMap;
2324
use rustc_span::Span;
2425

2526
use crate::constraints::graph::{self, NormalConstraintGraph, RegionGraph};
26-
use crate::constraints::{ConstraintSccs, RegionTracker};
2727
use crate::dataflow::BorrowIndex;
2828
use crate::{
2929
constraints::{ConstraintSccIndex, OutlivesConstraint, OutlivesConstraintSet},
@@ -46,6 +46,97 @@ mod reverse_sccs;
4646

4747
pub mod values;
4848

49+
pub type ConstraintSccs = Sccs<RegionVid, ConstraintSccIndex, RegionTracker>;
50+
51+
/// An annotation for region graph SCCs that tracks
52+
/// the values of its elements.
53+
#[derive(Copy, Debug, Clone)]
54+
pub struct RegionTracker {
55+
/// The largest universe of a placeholder reached from this SCC.
56+
/// This includes placeholders within this SCC.
57+
max_placeholder_universe_reached: UniverseIndex,
58+
59+
/// The smallest universe index reachable form the nodes of this SCC.
60+
min_reachable_universe: UniverseIndex,
61+
62+
/// The representative Region Variable Id for this SCC. We prefer
63+
/// placeholders over existentially quantified variables, otherwise
64+
/// it's the one with the smallest Region Variable ID.
65+
representative: RegionVid,
66+
67+
/// Is the current representative a placeholder?
68+
representative_is_placeholder: bool,
69+
70+
/// Is the current representative existentially quantified?
71+
representative_is_existential: bool,
72+
}
73+
74+
impl scc::Annotation for RegionTracker {
75+
fn merge_scc(mut self, mut other: Self) -> Self {
76+
// Prefer any placeholder over any existential
77+
if other.representative_is_placeholder && self.representative_is_existential {
78+
other.merge_min_max_seen(&self);
79+
return other;
80+
}
81+
82+
if self.representative_is_placeholder && other.representative_is_existential
83+
|| (self.representative <= other.representative)
84+
{
85+
self.merge_min_max_seen(&other);
86+
return self;
87+
}
88+
other.merge_min_max_seen(&self);
89+
other
90+
}
91+
92+
fn merge_reached(mut self, other: Self) -> Self {
93+
// No update to in-component values, only add seen values.
94+
self.merge_min_max_seen(&other);
95+
self
96+
}
97+
}
98+
99+
impl RegionTracker {
100+
fn new(rvid: RegionVid, definition: &RegionDefinition<'_>) -> Self {
101+
let (representative_is_placeholder, representative_is_existential) = match definition.origin
102+
{
103+
rustc_infer::infer::NllRegionVariableOrigin::FreeRegion => (false, false),
104+
rustc_infer::infer::NllRegionVariableOrigin::Placeholder(_) => (true, false),
105+
rustc_infer::infer::NllRegionVariableOrigin::Existential { .. } => (false, true),
106+
};
107+
108+
let placeholder_universe =
109+
if representative_is_placeholder { definition.universe } else { UniverseIndex::ROOT };
110+
111+
Self {
112+
max_placeholder_universe_reached: placeholder_universe,
113+
min_reachable_universe: definition.universe,
114+
representative: rvid,
115+
representative_is_placeholder,
116+
representative_is_existential,
117+
}
118+
}
119+
fn universe(self) -> UniverseIndex {
120+
self.min_reachable_universe
121+
}
122+
123+
fn merge_min_max_seen(&mut self, other: &Self) {
124+
self.max_placeholder_universe_reached = std::cmp::max(
125+
self.max_placeholder_universe_reached,
126+
other.max_placeholder_universe_reached,
127+
);
128+
129+
self.min_reachable_universe =
130+
std::cmp::min(self.min_reachable_universe, other.min_reachable_universe);
131+
}
132+
133+
/// Returns `true` if during the annotated SCC reaches a placeholder
134+
/// with a universe larger than the smallest reachable one, `false` otherwise.
135+
pub fn has_incompatible_universes(&self) -> bool {
136+
self.universe().cannot_name(self.max_placeholder_universe_reached)
137+
}
138+
}
139+
49140
pub struct RegionInferenceContext<'tcx> {
50141
pub var_infos: VarInfos,
51142

0 commit comments

Comments
 (0)