]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/borrow_check/member_constraints.rs
Auto merge of #69227 - Marwes:buffer_stderr, r=varkor
[rust.git] / src / librustc_mir / borrow_check / member_constraints.rs
1 use crate::rustc::ty::{self, Ty};
2 use rustc::infer::MemberConstraint;
3 use rustc_data_structures::fx::FxHashMap;
4 use rustc_hir::def_id::DefId;
5 use rustc_index::vec::IndexVec;
6 use rustc_span::Span;
7 use std::hash::Hash;
8 use std::ops::Index;
9
10 /// Compactly stores a set of `R0 member of [R1...Rn]` constraints,
11 /// indexed by the region `R0`.
12 crate struct MemberConstraintSet<'tcx, R>
13 where
14     R: Copy + Eq,
15 {
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>,
19
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>>,
24
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
27     /// slice.
28     choice_regions: Vec<ty::RegionVid>,
29 }
30
31 /// Represents a `R0 member of [R1..Rn]` constraint
32 crate struct NllMemberConstraint<'tcx> {
33     next_constraint: Option<NllMemberConstraintIndex>,
34
35     /// The opaque type whose hidden type is being inferred. (Used in error reporting.)
36     crate opaque_type_def_id: DefId,
37
38     /// The span where the hidden type was instantiated.
39     crate definition_span: Span,
40
41     /// The hidden type in which `R0` appears. (Used in error reporting.)
42     crate hidden_ty: Ty<'tcx>,
43
44     /// The region `R0`.
45     crate member_region_vid: ty::RegionVid,
46
47     /// Index of `R1` in `choice_regions` vector from `MemberConstraintSet`.
48     start_index: usize,
49
50     /// Index of `Rn` in `choice_regions` vector from `MemberConstraintSet`.
51     end_index: usize,
52 }
53
54 rustc_index::newtype_index! {
55     crate struct NllMemberConstraintIndex {
56         DEBUG_FORMAT = "MemberConstraintIndex({})"
57     }
58 }
59
60 impl Default for MemberConstraintSet<'tcx, ty::RegionVid> {
61     fn default() -> Self {
62         Self {
63             first_constraints: Default::default(),
64             constraints: Default::default(),
65             choice_regions: Default::default(),
66         }
67     }
68 }
69
70 impl<'tcx> MemberConstraintSet<'tcx, ty::RegionVid> {
71     /// Pushes a member constraint into the set.
72     ///
73     /// The input member constraint `m_c` is in the form produced by
74     /// the the `rustc::infer` code.
75     ///
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(
81         &mut self,
82         m_c: &MemberConstraint<'tcx>,
83         mut to_region_vid: impl FnMut(ty::Region<'tcx>) -> ty::RegionVid,
84     ) {
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 {
92             next_constraint,
93             member_region_vid,
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,
97             start_index,
98             end_index,
99         });
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)));
102     }
103 }
104
105 impl<R1> MemberConstraintSet<'tcx, R1>
106 where
107     R1: Copy + Hash + Eq,
108 {
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>(
115         self,
116         mut map_fn: impl FnMut(R1) -> R2,
117     ) -> MemberConstraintSet<'tcx, R2>
118     where
119         R2: Copy + Hash + Eq,
120     {
121         // We can re-use most of the original data, just tweaking the
122         // linked list links a bit.
123         //
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 othe
129         // other (see `append_list`).
130
131         let MemberConstraintSet { first_constraints, mut constraints, choice_regions } = self;
132
133         let mut first_constraints2 = FxHashMap::default();
134         first_constraints2.reserve(first_constraints.len());
135
136         for (r1, start1) in first_constraints {
137             let r2 = map_fn(r1);
138             if let Some(&start2) = first_constraints2.get(&r2) {
139                 append_list(&mut constraints, start1, start2);
140             }
141             first_constraints2.insert(r2, start1);
142         }
143
144         MemberConstraintSet { first_constraints: first_constraints2, constraints, choice_regions }
145     }
146 }
147
148 impl<R> MemberConstraintSet<'tcx, R>
149 where
150     R: Copy + Hash + Eq,
151 {
152     crate fn all_indices(&self) -> impl Iterator<Item = NllMemberConstraintIndex> {
153         self.constraints.indices()
154     }
155
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.
159     crate fn indices(
160         &self,
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;
167                 Some(current)
168             } else {
169                 None
170             }
171         })
172     }
173
174     /// Returns the "choice regions" for a given member
175     /// constraint. This is the `R1..Rn` from a constraint like:
176     ///
177     /// ```
178     /// R0 member of [R1..Rn]
179     /// ```
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]
183     }
184 }
185
186 impl<'tcx, R> Index<NllMemberConstraintIndex> for MemberConstraintSet<'tcx, R>
187 where
188     R: Copy + Eq,
189 {
190     type Output = NllMemberConstraint<'tcx>;
191
192     fn index(&self, i: NllMemberConstraintIndex) -> &NllMemberConstraint<'tcx> {
193         &self.constraints[i]
194     }
195 }
196
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`.
200 ///
201 /// Before:
202 ///
203 /// ```
204 /// target_list: A -> B -> C -> (None)
205 /// source_list: D -> E -> F -> (None)
206 /// ```
207 ///
208 /// After:
209 ///
210 /// ```
211 /// target_list: A -> B -> C -> D -> E -> F -> (None)
212 /// ```
213 fn append_list(
214     constraints: &mut IndexVec<NllMemberConstraintIndex, NllMemberConstraint<'_>>,
215     target_list: NllMemberConstraintIndex,
216     source_list: NllMemberConstraintIndex,
217 ) {
218     let mut p = target_list;
219     loop {
220         let mut r = &mut constraints[p];
221         match r.next_constraint {
222             Some(q) => p = q,
223             None => {
224                 r.next_constraint = Some(source_list);
225                 return;
226             }
227         }
228     }
229 }