1 use rustc_data_structures::fx::FxHashMap;
2 use rustc_hir::def_id::DefId;
3 use rustc_index::vec::IndexVec;
4 use rustc_middle::infer::MemberConstraint;
5 use rustc_middle::ty::{self, Ty};
10 /// Compactly stores a set of `R0 member of [R1...Rn]` constraints,
11 /// indexed by the region `R0`.
12 crate struct MemberConstraintSet<'tcx, R>
16 /// Stores the first "member" constraint for a given `R0`. This is an
17 /// index into the `constraints` vector below.
18 first_constraints: FxHashMap<R, NllMemberConstraintIndex>,
20 /// Stores the data about each `R0 member of [R1..Rn]` constraint.
21 /// These are organized into a linked list, so each constraint
22 /// contains the index of the next constraint with the same `R0`.
23 constraints: IndexVec<NllMemberConstraintIndex, NllMemberConstraint<'tcx>>,
25 /// Stores the `R1..Rn` regions for *all* sets. For any given
26 /// constraint, we keep two indices so that we can pull out a
28 choice_regions: Vec<ty::RegionVid>,
31 /// Represents a `R0 member of [R1..Rn]` constraint
32 crate struct NllMemberConstraint<'tcx> {
33 next_constraint: Option<NllMemberConstraintIndex>,
35 /// The opaque type whose hidden type is being inferred. (Used in error reporting.)
36 crate opaque_type_def_id: DefId,
38 /// The span where the hidden type was instantiated.
39 crate definition_span: Span,
41 /// The hidden type in which `R0` appears. (Used in error reporting.)
42 crate hidden_ty: Ty<'tcx>,
45 crate member_region_vid: ty::RegionVid,
47 /// Index of `R1` in `choice_regions` vector from `MemberConstraintSet`.
50 /// Index of `Rn` in `choice_regions` vector from `MemberConstraintSet`.
54 rustc_index::newtype_index! {
55 crate struct NllMemberConstraintIndex {
56 DEBUG_FORMAT = "MemberConstraintIndex({})"
60 impl Default for MemberConstraintSet<'tcx, ty::RegionVid> {
61 fn default() -> Self {
63 first_constraints: Default::default(),
64 constraints: Default::default(),
65 choice_regions: Default::default(),
70 impl<'tcx> MemberConstraintSet<'tcx, ty::RegionVid> {
71 /// Pushes a member constraint into the set.
73 /// The input member constraint `m_c` is in the form produced by
74 /// the `rustc_middle::infer` code.
76 /// The `to_region_vid` callback fn is used to convert the regions
77 /// within into `RegionVid` format -- it typically consults the
78 /// `UniversalRegions` data structure that is known to the caller
79 /// (but which this code is unaware of).
80 crate fn push_constraint(
82 m_c: &MemberConstraint<'tcx>,
83 mut to_region_vid: impl FnMut(ty::Region<'tcx>) -> ty::RegionVid,
85 debug!("push_constraint(m_c={:?})", m_c);
86 let member_region_vid: ty::RegionVid = to_region_vid(m_c.member_region);
87 let next_constraint = self.first_constraints.get(&member_region_vid).cloned();
88 let start_index = self.choice_regions.len();
89 let end_index = start_index + m_c.choice_regions.len();
90 debug!("push_constraint: member_region_vid={:?}", member_region_vid);
91 let constraint_index = self.constraints.push(NllMemberConstraint {
94 opaque_type_def_id: m_c.opaque_type_def_id,
95 definition_span: m_c.definition_span,
96 hidden_ty: m_c.hidden_ty,
100 self.first_constraints.insert(member_region_vid, constraint_index);
101 self.choice_regions.extend(m_c.choice_regions.iter().map(|&r| to_region_vid(r)));
105 impl<R1> MemberConstraintSet<'tcx, R1>
107 R1: Copy + Hash + Eq,
109 /// Remap the "member region" key using `map_fn`, producing a new
110 /// member constraint set. This is used in the NLL code to map from
111 /// the original `RegionVid` to an scc index. In some cases, we
112 /// may have multiple `R1` values mapping to the same `R2` key -- that
113 /// is ok, the two sets will be merged.
114 crate fn into_mapped<R2>(
116 mut map_fn: impl FnMut(R1) -> R2,
117 ) -> MemberConstraintSet<'tcx, R2>
119 R2: Copy + Hash + Eq,
121 // We can re-use most of the original data, just tweaking the
122 // linked list links a bit.
124 // For example if we had two keys `Ra` and `Rb` that both now
125 // wind up mapped to the same key `S`, we would append the
126 // linked list for `Ra` onto the end of the linked list for
127 // `Rb` (or vice versa) -- this basically just requires
128 // rewriting the final link from one list to point at the other
129 // other (see `append_list`).
131 let MemberConstraintSet { first_constraints, mut constraints, choice_regions } = self;
133 let mut first_constraints2 = FxHashMap::default();
134 first_constraints2.reserve(first_constraints.len());
136 for (r1, start1) in first_constraints {
138 if let Some(&start2) = first_constraints2.get(&r2) {
139 append_list(&mut constraints, start1, start2);
141 first_constraints2.insert(r2, start1);
144 MemberConstraintSet { first_constraints: first_constraints2, constraints, choice_regions }
148 impl<R> MemberConstraintSet<'tcx, R>
152 crate fn all_indices(&self) -> impl Iterator<Item = NllMemberConstraintIndex> {
153 self.constraints.indices()
156 /// Iterate down the constraint indices associated with a given
157 /// peek-region. You can then use `choice_regions` and other
158 /// methods to access data.
161 member_region_vid: R,
162 ) -> impl Iterator<Item = NllMemberConstraintIndex> + '_ {
163 let mut next = self.first_constraints.get(&member_region_vid).cloned();
164 std::iter::from_fn(move || -> Option<NllMemberConstraintIndex> {
165 if let Some(current) = next {
166 next = self.constraints[current].next_constraint;
174 /// Returns the "choice regions" for a given member
175 /// constraint. This is the `R1..Rn` from a constraint like:
178 /// R0 member of [R1..Rn]
180 crate fn choice_regions(&self, pci: NllMemberConstraintIndex) -> &[ty::RegionVid] {
181 let NllMemberConstraint { start_index, end_index, .. } = &self.constraints[pci];
182 &self.choice_regions[*start_index..*end_index]
186 impl<'tcx, R> Index<NllMemberConstraintIndex> for MemberConstraintSet<'tcx, R>
190 type Output = NllMemberConstraint<'tcx>;
192 fn index(&self, i: NllMemberConstraintIndex) -> &NllMemberConstraint<'tcx> {
197 /// Given a linked list starting at `source_list` and another linked
198 /// list starting at `target_list`, modify `target_list` so that it is
199 /// followed by `source_list`.
204 /// target_list: A -> B -> C -> (None)
205 /// source_list: D -> E -> F -> (None)
211 /// target_list: A -> B -> C -> D -> E -> F -> (None)
214 constraints: &mut IndexVec<NllMemberConstraintIndex, NllMemberConstraint<'_>>,
215 target_list: NllMemberConstraintIndex,
216 source_list: NllMemberConstraintIndex,
218 let mut p = target_list;
220 let mut r = &mut constraints[p];
221 match r.next_constraint {
224 r.next_constraint = Some(source_list);