]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/member_constraints.rs
Auto merge of #98754 - jyn514:non-trivial-drop, r=compiler-errors
[rust.git] / compiler / rustc_borrowck / src / member_constraints.rs
1 use rustc_data_structures::captures::Captures;
2 use rustc_data_structures::fx::FxHashMap;
3 use rustc_index::vec::IndexVec;
4 use rustc_middle::infer::MemberConstraint;
5 use rustc_middle::ty::{self, Ty};
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 pub(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 pub(crate) struct NllMemberConstraint<'tcx> {
33     next_constraint: Option<NllMemberConstraintIndex>,
34
35     /// The span where the hidden type was instantiated.
36     pub(crate) definition_span: Span,
37
38     /// The hidden type in which `R0` appears. (Used in error reporting.)
39     pub(crate) hidden_ty: Ty<'tcx>,
40
41     /// The region `R0`.
42     pub(crate) member_region_vid: ty::RegionVid,
43
44     /// Index of `R1` in `choice_regions` vector from `MemberConstraintSet`.
45     start_index: usize,
46
47     /// Index of `Rn` in `choice_regions` vector from `MemberConstraintSet`.
48     end_index: usize,
49 }
50
51 rustc_index::newtype_index! {
52     pub(crate) struct NllMemberConstraintIndex {
53         DEBUG_FORMAT = "MemberConstraintIndex({})"
54     }
55 }
56
57 impl Default for MemberConstraintSet<'_, ty::RegionVid> {
58     fn default() -> Self {
59         Self {
60             first_constraints: Default::default(),
61             constraints: Default::default(),
62             choice_regions: Default::default(),
63         }
64     }
65 }
66
67 impl<'tcx> MemberConstraintSet<'tcx, ty::RegionVid> {
68     /// Pushes a member constraint into the set.
69     ///
70     /// The input member constraint `m_c` is in the form produced by
71     /// the `rustc_middle::infer` code.
72     ///
73     /// The `to_region_vid` callback fn is used to convert the regions
74     /// within into `RegionVid` format -- it typically consults the
75     /// `UniversalRegions` data structure that is known to the caller
76     /// (but which this code is unaware of).
77     pub(crate) fn push_constraint(
78         &mut self,
79         m_c: &MemberConstraint<'tcx>,
80         mut to_region_vid: impl FnMut(ty::Region<'tcx>) -> ty::RegionVid,
81     ) {
82         debug!("push_constraint(m_c={:?})", m_c);
83         let member_region_vid: ty::RegionVid = to_region_vid(m_c.member_region);
84         let next_constraint = self.first_constraints.get(&member_region_vid).cloned();
85         let start_index = self.choice_regions.len();
86         let end_index = start_index + m_c.choice_regions.len();
87         debug!("push_constraint: member_region_vid={:?}", member_region_vid);
88         let constraint_index = self.constraints.push(NllMemberConstraint {
89             next_constraint,
90             member_region_vid,
91             definition_span: m_c.definition_span,
92             hidden_ty: m_c.hidden_ty,
93             start_index,
94             end_index,
95         });
96         self.first_constraints.insert(member_region_vid, constraint_index);
97         self.choice_regions.extend(m_c.choice_regions.iter().map(|&r| to_region_vid(r)));
98     }
99 }
100
101 impl<'tcx, R1> MemberConstraintSet<'tcx, R1>
102 where
103     R1: Copy + Hash + Eq,
104 {
105     /// Remap the "member region" key using `map_fn`, producing a new
106     /// member constraint set.  This is used in the NLL code to map from
107     /// the original `RegionVid` to an scc index. In some cases, we
108     /// may have multiple `R1` values mapping to the same `R2` key -- that
109     /// is ok, the two sets will be merged.
110     pub(crate) fn into_mapped<R2>(
111         self,
112         mut map_fn: impl FnMut(R1) -> R2,
113     ) -> MemberConstraintSet<'tcx, R2>
114     where
115         R2: Copy + Hash + Eq,
116     {
117         // We can re-use most of the original data, just tweaking the
118         // linked list links a bit.
119         //
120         // For example if we had two keys `Ra` and `Rb` that both now
121         // wind up mapped to the same key `S`, we would append the
122         // linked list for `Ra` onto the end of the linked list for
123         // `Rb` (or vice versa) -- this basically just requires
124         // rewriting the final link from one list to point at the other
125         // other (see `append_list`).
126
127         let MemberConstraintSet { first_constraints, mut constraints, choice_regions } = self;
128
129         let mut first_constraints2 = FxHashMap::default();
130         first_constraints2.reserve(first_constraints.len());
131
132         for (r1, start1) in first_constraints {
133             let r2 = map_fn(r1);
134             if let Some(&start2) = first_constraints2.get(&r2) {
135                 append_list(&mut constraints, start1, start2);
136             }
137             first_constraints2.insert(r2, start1);
138         }
139
140         MemberConstraintSet { first_constraints: first_constraints2, constraints, choice_regions }
141     }
142 }
143
144 impl<'tcx, R> MemberConstraintSet<'tcx, R>
145 where
146     R: Copy + Hash + Eq,
147 {
148     pub(crate) fn all_indices(
149         &self,
150     ) -> impl Iterator<Item = NllMemberConstraintIndex> + Captures<'tcx> + '_ {
151         self.constraints.indices()
152     }
153
154     /// Iterate down the constraint indices associated with a given
155     /// peek-region.  You can then use `choice_regions` and other
156     /// methods to access data.
157     pub(crate) fn indices(
158         &self,
159         member_region_vid: R,
160     ) -> impl Iterator<Item = NllMemberConstraintIndex> + Captures<'tcx> + '_ {
161         let mut next = self.first_constraints.get(&member_region_vid).cloned();
162         std::iter::from_fn(move || -> Option<NllMemberConstraintIndex> {
163             if let Some(current) = next {
164                 next = self.constraints[current].next_constraint;
165                 Some(current)
166             } else {
167                 None
168             }
169         })
170     }
171
172     /// Returns the "choice regions" for a given member
173     /// constraint. This is the `R1..Rn` from a constraint like:
174     ///
175     /// ```text
176     /// R0 member of [R1..Rn]
177     /// ```
178     pub(crate) fn choice_regions(&self, pci: NllMemberConstraintIndex) -> &[ty::RegionVid] {
179         let NllMemberConstraint { start_index, end_index, .. } = &self.constraints[pci];
180         &self.choice_regions[*start_index..*end_index]
181     }
182 }
183
184 impl<'tcx, R> Index<NllMemberConstraintIndex> for MemberConstraintSet<'tcx, R>
185 where
186     R: Copy + Eq,
187 {
188     type Output = NllMemberConstraint<'tcx>;
189
190     fn index(&self, i: NllMemberConstraintIndex) -> &NllMemberConstraint<'tcx> {
191         &self.constraints[i]
192     }
193 }
194
195 /// Given a linked list starting at `source_list` and another linked
196 /// list starting at `target_list`, modify `target_list` so that it is
197 /// followed by `source_list`.
198 ///
199 /// Before:
200 ///
201 /// ```text
202 /// target_list: A -> B -> C -> (None)
203 /// source_list: D -> E -> F -> (None)
204 /// ```
205 ///
206 /// After:
207 ///
208 /// ```text
209 /// target_list: A -> B -> C -> D -> E -> F -> (None)
210 /// ```
211 fn append_list(
212     constraints: &mut IndexVec<NllMemberConstraintIndex, NllMemberConstraint<'_>>,
213     target_list: NllMemberConstraintIndex,
214     source_list: NllMemberConstraintIndex,
215 ) {
216     let mut p = target_list;
217     loop {
218         let mut r = &mut constraints[p];
219         match r.next_constraint {
220             Some(q) => p = q,
221             None => {
222                 r.next_constraint = Some(source_list);
223                 return;
224             }
225         }
226     }
227 }