]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/specialize/specialization_graph.rs
Show a note where a macro failed to match
[rust.git] / compiler / rustc_trait_selection / src / traits / specialize / specialization_graph.rs
1 use super::OverlapError;
2
3 use crate::traits;
4 use rustc_hir::def_id::DefId;
5 use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams};
6 use rustc_middle::ty::print::with_no_trimmed_paths;
7 use rustc_middle::ty::{self, TyCtxt, TypeVisitable};
8
9 pub use rustc_middle::traits::specialization_graph::*;
10
11 #[derive(Copy, Clone, Debug)]
12 pub enum FutureCompatOverlapErrorKind {
13     Issue33140,
14     LeakCheck,
15 }
16
17 #[derive(Debug)]
18 pub struct FutureCompatOverlapError {
19     pub error: OverlapError,
20     pub kind: FutureCompatOverlapErrorKind,
21 }
22
23 /// The result of attempting to insert an impl into a group of children.
24 enum Inserted {
25     /// The impl was inserted as a new child in this group of children.
26     BecameNewSibling(Option<FutureCompatOverlapError>),
27
28     /// The impl should replace existing impls [X1, ..], because the impl specializes X1, X2, etc.
29     ReplaceChildren(Vec<DefId>),
30
31     /// The impl is a specialization of an existing child.
32     ShouldRecurseOn(DefId),
33 }
34
35 trait ChildrenExt<'tcx> {
36     fn insert_blindly(&mut self, tcx: TyCtxt<'tcx>, impl_def_id: DefId);
37     fn remove_existing(&mut self, tcx: TyCtxt<'tcx>, impl_def_id: DefId);
38
39     fn insert(
40         &mut self,
41         tcx: TyCtxt<'tcx>,
42         impl_def_id: DefId,
43         simplified_self: Option<SimplifiedType>,
44         overlap_mode: OverlapMode,
45     ) -> Result<Inserted, OverlapError>;
46 }
47
48 impl ChildrenExt<'_> for Children {
49     /// Insert an impl into this set of children without comparing to any existing impls.
50     fn insert_blindly(&mut self, tcx: TyCtxt<'_>, impl_def_id: DefId) {
51         let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
52         if let Some(st) = fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::AsInfer)
53         {
54             debug!("insert_blindly: impl_def_id={:?} st={:?}", impl_def_id, st);
55             self.non_blanket_impls.entry(st).or_default().push(impl_def_id)
56         } else {
57             debug!("insert_blindly: impl_def_id={:?} st=None", impl_def_id);
58             self.blanket_impls.push(impl_def_id)
59         }
60     }
61
62     /// Removes an impl from this set of children. Used when replacing
63     /// an impl with a parent. The impl must be present in the list of
64     /// children already.
65     fn remove_existing(&mut self, tcx: TyCtxt<'_>, impl_def_id: DefId) {
66         let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
67         let vec: &mut Vec<DefId>;
68         if let Some(st) = fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::AsInfer)
69         {
70             debug!("remove_existing: impl_def_id={:?} st={:?}", impl_def_id, st);
71             vec = self.non_blanket_impls.get_mut(&st).unwrap();
72         } else {
73             debug!("remove_existing: impl_def_id={:?} st=None", impl_def_id);
74             vec = &mut self.blanket_impls;
75         }
76
77         let index = vec.iter().position(|d| *d == impl_def_id).unwrap();
78         vec.remove(index);
79     }
80
81     /// Attempt to insert an impl into this set of children, while comparing for
82     /// specialization relationships.
83     fn insert(
84         &mut self,
85         tcx: TyCtxt<'_>,
86         impl_def_id: DefId,
87         simplified_self: Option<SimplifiedType>,
88         overlap_mode: OverlapMode,
89     ) -> Result<Inserted, OverlapError> {
90         let mut last_lint = None;
91         let mut replace_children = Vec::new();
92
93         debug!("insert(impl_def_id={:?}, simplified_self={:?})", impl_def_id, simplified_self,);
94
95         let possible_siblings = match simplified_self {
96             Some(st) => PotentialSiblings::Filtered(filtered_children(self, st)),
97             None => PotentialSiblings::Unfiltered(iter_children(self)),
98         };
99
100         for possible_sibling in possible_siblings {
101             debug!(
102                 "insert: impl_def_id={:?}, simplified_self={:?}, possible_sibling={:?}",
103                 impl_def_id, simplified_self, possible_sibling,
104             );
105
106             let create_overlap_error = |overlap: traits::coherence::OverlapResult<'_>| {
107                 let trait_ref = overlap.impl_header.trait_ref.unwrap();
108                 let self_ty = trait_ref.self_ty();
109
110                 // FIXME: should postpone string formatting until we decide to actually emit.
111                 with_no_trimmed_paths!({
112                     OverlapError {
113                         with_impl: possible_sibling,
114                         trait_desc: trait_ref.print_only_trait_path().to_string(),
115                         // Only report the `Self` type if it has at least
116                         // some outer concrete shell; otherwise, it's
117                         // not adding much information.
118                         self_desc: if self_ty.has_concrete_skeleton() {
119                             Some(self_ty.to_string())
120                         } else {
121                             None
122                         },
123                         intercrate_ambiguity_causes: overlap.intercrate_ambiguity_causes,
124                         involves_placeholder: overlap.involves_placeholder,
125                     }
126                 })
127             };
128
129             let report_overlap_error = |overlap: traits::coherence::OverlapResult<'_>,
130                                         last_lint: &mut _| {
131                 // Found overlap, but no specialization; error out or report future-compat warning.
132
133                 // Do we *still* get overlap if we disable the future-incompatible modes?
134                 let should_err = traits::overlapping_impls(
135                     tcx,
136                     possible_sibling,
137                     impl_def_id,
138                     traits::SkipLeakCheck::default(),
139                     overlap_mode,
140                 )
141                 .is_some();
142
143                 let error = create_overlap_error(overlap);
144
145                 if should_err {
146                     Err(error)
147                 } else {
148                     *last_lint = Some(FutureCompatOverlapError {
149                         error,
150                         kind: FutureCompatOverlapErrorKind::LeakCheck,
151                     });
152
153                     Ok((false, false))
154                 }
155             };
156
157             let last_lint_mut = &mut last_lint;
158             let (le, ge) = traits::overlapping_impls(
159                 tcx,
160                 possible_sibling,
161                 impl_def_id,
162                 traits::SkipLeakCheck::Yes,
163                 overlap_mode,
164             )
165             .map_or(Ok((false, false)), |overlap| {
166                 if let Some(overlap_kind) =
167                     tcx.impls_are_allowed_to_overlap(impl_def_id, possible_sibling)
168                 {
169                     match overlap_kind {
170                         ty::ImplOverlapKind::Permitted { marker: _ } => {}
171                         ty::ImplOverlapKind::Issue33140 => {
172                             *last_lint_mut = Some(FutureCompatOverlapError {
173                                 error: create_overlap_error(overlap),
174                                 kind: FutureCompatOverlapErrorKind::Issue33140,
175                             });
176                         }
177                     }
178
179                     return Ok((false, false));
180                 }
181
182                 let le = tcx.specializes((impl_def_id, possible_sibling));
183                 let ge = tcx.specializes((possible_sibling, impl_def_id));
184
185                 if le == ge { report_overlap_error(overlap, last_lint_mut) } else { Ok((le, ge)) }
186             })?;
187
188             if le && !ge {
189                 debug!(
190                     "descending as child of TraitRef {:?}",
191                     tcx.impl_trait_ref(possible_sibling).unwrap()
192                 );
193
194                 // The impl specializes `possible_sibling`.
195                 return Ok(Inserted::ShouldRecurseOn(possible_sibling));
196             } else if ge && !le {
197                 debug!(
198                     "placing as parent of TraitRef {:?}",
199                     tcx.impl_trait_ref(possible_sibling).unwrap()
200                 );
201
202                 replace_children.push(possible_sibling);
203             } else {
204                 // Either there's no overlap, or the overlap was already reported by
205                 // `overlap_error`.
206             }
207         }
208
209         if !replace_children.is_empty() {
210             return Ok(Inserted::ReplaceChildren(replace_children));
211         }
212
213         // No overlap with any potential siblings, so add as a new sibling.
214         debug!("placing as new sibling");
215         self.insert_blindly(tcx, impl_def_id);
216         Ok(Inserted::BecameNewSibling(last_lint))
217     }
218 }
219
220 fn iter_children(children: &mut Children) -> impl Iterator<Item = DefId> + '_ {
221     let nonblanket = children.non_blanket_impls.iter().flat_map(|(_, v)| v.iter());
222     children.blanket_impls.iter().chain(nonblanket).cloned()
223 }
224
225 fn filtered_children(
226     children: &mut Children,
227     st: SimplifiedType,
228 ) -> impl Iterator<Item = DefId> + '_ {
229     let nonblanket = children.non_blanket_impls.entry(st).or_default().iter();
230     children.blanket_impls.iter().chain(nonblanket).cloned()
231 }
232
233 // A custom iterator used by Children::insert
234 enum PotentialSiblings<I, J>
235 where
236     I: Iterator<Item = DefId>,
237     J: Iterator<Item = DefId>,
238 {
239     Unfiltered(I),
240     Filtered(J),
241 }
242
243 impl<I, J> Iterator for PotentialSiblings<I, J>
244 where
245     I: Iterator<Item = DefId>,
246     J: Iterator<Item = DefId>,
247 {
248     type Item = DefId;
249
250     fn next(&mut self) -> Option<Self::Item> {
251         match *self {
252             PotentialSiblings::Unfiltered(ref mut iter) => iter.next(),
253             PotentialSiblings::Filtered(ref mut iter) => iter.next(),
254         }
255     }
256 }
257
258 pub trait GraphExt {
259     /// Insert a local impl into the specialization graph. If an existing impl
260     /// conflicts with it (has overlap, but neither specializes the other),
261     /// information about the area of overlap is returned in the `Err`.
262     fn insert(
263         &mut self,
264         tcx: TyCtxt<'_>,
265         impl_def_id: DefId,
266         overlap_mode: OverlapMode,
267     ) -> Result<Option<FutureCompatOverlapError>, OverlapError>;
268
269     /// Insert cached metadata mapping from a child impl back to its parent.
270     fn record_impl_from_cstore(&mut self, tcx: TyCtxt<'_>, parent: DefId, child: DefId);
271 }
272
273 impl GraphExt for Graph {
274     /// Insert a local impl into the specialization graph. If an existing impl
275     /// conflicts with it (has overlap, but neither specializes the other),
276     /// information about the area of overlap is returned in the `Err`.
277     fn insert(
278         &mut self,
279         tcx: TyCtxt<'_>,
280         impl_def_id: DefId,
281         overlap_mode: OverlapMode,
282     ) -> Result<Option<FutureCompatOverlapError>, OverlapError> {
283         assert!(impl_def_id.is_local());
284
285         let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
286         let trait_def_id = trait_ref.def_id;
287
288         debug!(
289             "insert({:?}): inserting TraitRef {:?} into specialization graph",
290             impl_def_id, trait_ref
291         );
292
293         // If the reference itself contains an earlier error (e.g., due to a
294         // resolution failure), then we just insert the impl at the top level of
295         // the graph and claim that there's no overlap (in order to suppress
296         // bogus errors).
297         if trait_ref.references_error() {
298             debug!(
299                 "insert: inserting dummy node for erroneous TraitRef {:?}, \
300                  impl_def_id={:?}, trait_def_id={:?}",
301                 trait_ref, impl_def_id, trait_def_id
302             );
303
304             self.parent.insert(impl_def_id, trait_def_id);
305             self.children.entry(trait_def_id).or_default().insert_blindly(tcx, impl_def_id);
306             return Ok(None);
307         }
308
309         let mut parent = trait_def_id;
310         let mut last_lint = None;
311         let simplified = fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::AsInfer);
312
313         // Descend the specialization tree, where `parent` is the current parent node.
314         loop {
315             use self::Inserted::*;
316
317             let insert_result = self.children.entry(parent).or_default().insert(
318                 tcx,
319                 impl_def_id,
320                 simplified,
321                 overlap_mode,
322             )?;
323
324             match insert_result {
325                 BecameNewSibling(opt_lint) => {
326                     last_lint = opt_lint;
327                     break;
328                 }
329                 ReplaceChildren(grand_children_to_be) => {
330                     // We currently have
331                     //
332                     //     P
333                     //     |
334                     //     G
335                     //
336                     // and we are inserting the impl N. We want to make it:
337                     //
338                     //     P
339                     //     |
340                     //     N
341                     //     |
342                     //     G
343
344                     // Adjust P's list of children: remove G and then add N.
345                     {
346                         let siblings = self.children.get_mut(&parent).unwrap();
347                         for &grand_child_to_be in &grand_children_to_be {
348                             siblings.remove_existing(tcx, grand_child_to_be);
349                         }
350                         siblings.insert_blindly(tcx, impl_def_id);
351                     }
352
353                     // Set G's parent to N and N's parent to P.
354                     for &grand_child_to_be in &grand_children_to_be {
355                         self.parent.insert(grand_child_to_be, impl_def_id);
356                     }
357                     self.parent.insert(impl_def_id, parent);
358
359                     // Add G as N's child.
360                     for &grand_child_to_be in &grand_children_to_be {
361                         self.children
362                             .entry(impl_def_id)
363                             .or_default()
364                             .insert_blindly(tcx, grand_child_to_be);
365                     }
366                     break;
367                 }
368                 ShouldRecurseOn(new_parent) => {
369                     parent = new_parent;
370                 }
371             }
372         }
373
374         self.parent.insert(impl_def_id, parent);
375         Ok(last_lint)
376     }
377
378     /// Insert cached metadata mapping from a child impl back to its parent.
379     fn record_impl_from_cstore(&mut self, tcx: TyCtxt<'_>, parent: DefId, child: DefId) {
380         if self.parent.insert(child, parent).is_some() {
381             bug!(
382                 "When recording an impl from the crate store, information about its parent \
383                  was already present."
384             );
385         }
386
387         self.children.entry(parent).or_default().insert_blindly(tcx, child);
388     }
389 }