1 use super::OverlapError;
3 use crate::hir::def_id::DefId;
4 use crate::ich::{self, StableHashingContext};
5 use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
8 use crate::ty::{self, TyCtxt, TypeFoldable};
9 use crate::ty::fast_reject::{self, SimplifiedType};
10 use syntax::ast::Ident;
11 use crate::util::captures::Captures;
12 use crate::util::nodemap::{DefIdMap, FxHashMap};
14 /// A per-trait graph of impls in specialization order. At the moment, this
15 /// graph forms a tree rooted with the trait itself, with all other nodes
16 /// representing impls, and parent-child relationships representing
19 /// The graph provides two key services:
21 /// - Construction. This implicitly checks for overlapping impls (i.e., impls
22 /// that overlap but where neither specializes the other -- an artifact of the
23 /// simple "chain" rule.
25 /// - Parent extraction. In particular, the graph can give you the *immediate*
26 /// parents of a given specializing impl, which is needed for extracting
27 /// default items amongst other things. In the simple "chain" rule, every impl
28 /// has at most one parent.
29 #[derive(RustcEncodable, RustcDecodable)]
31 // All impls have a parent; the "root" impls have as their parent the `def_id`
33 parent: DefIdMap<DefId>,
35 // The "root" impls are found by looking up the trait's def_id.
36 children: DefIdMap<Children>,
39 /// Children of a given impl, grouped into blanket/non-blanket varieties as is
40 /// done in `TraitDef`.
41 #[derive(Default, RustcEncodable, RustcDecodable)]
43 // Impls of a trait (or specializations of a given impl). To allow for
44 // quicker lookup, the impls are indexed by a simplified version of their
45 // `Self` type: impls with a simplifiable `Self` are stored in
46 // `nonblanket_impls` keyed by it, while all other impls are stored in
49 // A similar division is used within `TraitDef`, but the lists there collect
50 // together *all* the impls for a trait, and are populated prior to building
51 // the specialization graph.
53 /// Impls of the trait.
54 nonblanket_impls: FxHashMap<fast_reject::SimplifiedType, Vec<DefId>>,
56 /// Blanket impls associated with the trait.
57 blanket_impls: Vec<DefId>,
60 #[derive(Copy, Clone, Debug)]
61 pub enum FutureCompatOverlapErrorKind {
67 pub struct FutureCompatOverlapError {
68 pub error: OverlapError,
69 pub kind: FutureCompatOverlapErrorKind
72 /// The result of attempting to insert an impl into a group of children.
74 /// The impl was inserted as a new child in this group of children.
75 BecameNewSibling(Option<FutureCompatOverlapError>),
77 /// The impl should replace existing impls [X1, ..], because the impl specializes X1, X2, etc.
78 ReplaceChildren(Vec<DefId>),
80 /// The impl is a specialization of an existing child.
81 ShouldRecurseOn(DefId),
84 impl<'gcx, 'tcx> Children {
85 /// Insert an impl into this set of children without comparing to any existing impls.
86 fn insert_blindly(&mut self, tcx: TyCtxt<'gcx, 'tcx>, impl_def_id: DefId) {
87 let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
88 if let Some(sty) = fast_reject::simplify_type(tcx, trait_ref.self_ty(), false) {
89 debug!("insert_blindly: impl_def_id={:?} sty={:?}", impl_def_id, sty);
90 self.nonblanket_impls.entry(sty).or_default().push(impl_def_id)
92 debug!("insert_blindly: impl_def_id={:?} sty=None", impl_def_id);
93 self.blanket_impls.push(impl_def_id)
97 /// Removes an impl from this set of children. Used when replacing
98 /// an impl with a parent. The impl must be present in the list of
100 fn remove_existing(&mut self, tcx: TyCtxt<'gcx, 'tcx>, impl_def_id: DefId) {
101 let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
102 let vec: &mut Vec<DefId>;
103 if let Some(sty) = fast_reject::simplify_type(tcx, trait_ref.self_ty(), false) {
104 debug!("remove_existing: impl_def_id={:?} sty={:?}", impl_def_id, sty);
105 vec = self.nonblanket_impls.get_mut(&sty).unwrap();
107 debug!("remove_existing: impl_def_id={:?} sty=None", impl_def_id);
108 vec = &mut self.blanket_impls;
111 let index = vec.iter().position(|d| *d == impl_def_id).unwrap();
115 /// Attempt to insert an impl into this set of children, while comparing for
116 /// specialization relationships.
119 tcx: TyCtxt<'gcx, 'tcx>,
121 simplified_self: Option<SimplifiedType>,
122 ) -> Result<Inserted, OverlapError> {
123 let mut last_lint = None;
124 let mut replace_children = Vec::new();
127 "insert(impl_def_id={:?}, simplified_self={:?})",
132 let possible_siblings = match simplified_self {
133 Some(sty) => PotentialSiblings::Filtered(self.filtered(sty)),
134 None => PotentialSiblings::Unfiltered(self.iter()),
137 for possible_sibling in possible_siblings {
139 "insert: impl_def_id={:?}, simplified_self={:?}, possible_sibling={:?}",
145 let overlap_error = |overlap: traits::coherence::OverlapResult<'_>| {
146 // Found overlap, but no specialization; error out.
147 let trait_ref = overlap.impl_header.trait_ref.unwrap();
148 let self_ty = trait_ref.self_ty();
150 with_impl: possible_sibling,
151 trait_desc: trait_ref.to_string(),
152 // Only report the `Self` type if it has at least
153 // some outer concrete shell; otherwise, it's
154 // not adding much information.
155 self_desc: if self_ty.has_concrete_skeleton() {
156 Some(self_ty.to_string())
160 intercrate_ambiguity_causes: overlap.intercrate_ambiguity_causes,
161 involves_placeholder: overlap.involves_placeholder,
165 let tcx = tcx.global_tcx();
166 let (le, ge) = traits::overlapping_impls(
170 traits::IntercrateMode::Issue43355,
172 if let Some(overlap_kind) =
173 tcx.impls_are_allowed_to_overlap(impl_def_id, possible_sibling)
176 ty::ImplOverlapKind::Permitted => {}
177 ty::ImplOverlapKind::Issue33140 => {
178 last_lint = Some(FutureCompatOverlapError {
179 error: overlap_error(overlap),
180 kind: FutureCompatOverlapErrorKind::Issue33140
185 return Ok((false, false));
188 let le = tcx.specializes((impl_def_id, possible_sibling));
189 let ge = tcx.specializes((possible_sibling, impl_def_id));
192 Err(overlap_error(overlap))
197 || Ok((false, false)),
201 debug!("descending as child of TraitRef {:?}",
202 tcx.impl_trait_ref(possible_sibling).unwrap());
204 // The impl specializes `possible_sibling`.
205 return Ok(Inserted::ShouldRecurseOn(possible_sibling));
206 } else if ge && !le {
207 debug!("placing as parent of TraitRef {:?}",
208 tcx.impl_trait_ref(possible_sibling).unwrap());
210 replace_children.push(possible_sibling);
212 if let None = tcx.impls_are_allowed_to_overlap(
213 impl_def_id, possible_sibling)
215 // do future-compat checks for overlap. Have issue #33140
216 // errors overwrite issue #43355 errors when both are present.
218 traits::overlapping_impls(
222 traits::IntercrateMode::Fixed,
224 last_lint = Some(FutureCompatOverlapError {
225 error: overlap_error(overlap),
226 kind: FutureCompatOverlapErrorKind::Issue43355
233 // no overlap (error bailed already via ?)
237 if !replace_children.is_empty() {
238 return Ok(Inserted::ReplaceChildren(replace_children));
241 // No overlap with any potential siblings, so add as a new sibling.
242 debug!("placing as new sibling");
243 self.insert_blindly(tcx, impl_def_id);
244 Ok(Inserted::BecameNewSibling(last_lint))
247 fn iter(&mut self) -> impl Iterator<Item = DefId> + '_ {
248 let nonblanket = self.nonblanket_impls.iter_mut().flat_map(|(_, v)| v.iter());
249 self.blanket_impls.iter().chain(nonblanket).cloned()
252 fn filtered(&mut self, sty: SimplifiedType) -> impl Iterator<Item = DefId> + '_ {
253 let nonblanket = self.nonblanket_impls.entry(sty).or_default().iter();
254 self.blanket_impls.iter().chain(nonblanket).cloned()
258 // A custom iterator used by Children::insert
259 enum PotentialSiblings<I, J>
260 where I: Iterator<Item = DefId>,
261 J: Iterator<Item = DefId>
267 impl<I, J> Iterator for PotentialSiblings<I, J>
268 where I: Iterator<Item = DefId>,
269 J: Iterator<Item = DefId>
273 fn next(&mut self) -> Option<Self::Item> {
275 PotentialSiblings::Unfiltered(ref mut iter) => iter.next(),
276 PotentialSiblings::Filtered(ref mut iter) => iter.next()
281 impl<'gcx, 'tcx> Graph {
282 pub fn new() -> Graph {
284 parent: Default::default(),
285 children: Default::default(),
289 /// Insert a local impl into the specialization graph. If an existing impl
290 /// conflicts with it (has overlap, but neither specializes the other),
291 /// information about the area of overlap is returned in the `Err`.
294 tcx: TyCtxt<'gcx, 'tcx>,
296 ) -> Result<Option<FutureCompatOverlapError>, OverlapError> {
297 assert!(impl_def_id.is_local());
299 let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
300 let trait_def_id = trait_ref.def_id;
302 debug!("insert({:?}): inserting TraitRef {:?} into specialization graph",
303 impl_def_id, trait_ref);
305 // If the reference itself contains an earlier error (e.g., due to a
306 // resolution failure), then we just insert the impl at the top level of
307 // the graph and claim that there's no overlap (in order to suppress
309 if trait_ref.references_error() {
310 debug!("insert: inserting dummy node for erroneous TraitRef {:?}, \
311 impl_def_id={:?}, trait_def_id={:?}",
312 trait_ref, impl_def_id, trait_def_id);
314 self.parent.insert(impl_def_id, trait_def_id);
315 self.children.entry(trait_def_id).or_default()
316 .insert_blindly(tcx, impl_def_id);
320 let mut parent = trait_def_id;
321 let mut last_lint = None;
322 let simplified = fast_reject::simplify_type(tcx, trait_ref.self_ty(), false);
324 // Descend the specialization tree, where `parent` is the current parent node.
326 use self::Inserted::*;
328 let insert_result = self.children.entry(parent).or_default()
329 .insert(tcx, impl_def_id, simplified)?;
331 match insert_result {
332 BecameNewSibling(opt_lint) => {
333 last_lint = opt_lint;
336 ReplaceChildren(grand_children_to_be) => {
343 // and we are inserting the impl N. We want to make it:
351 // Adjust P's list of children: remove G and then add N.
353 let siblings = self.children
356 for &grand_child_to_be in &grand_children_to_be {
357 siblings.remove_existing(tcx, grand_child_to_be);
359 siblings.insert_blindly(tcx, impl_def_id);
362 // Set G's parent to N and N's parent to P.
363 for &grand_child_to_be in &grand_children_to_be {
364 self.parent.insert(grand_child_to_be, impl_def_id);
366 self.parent.insert(impl_def_id, parent);
368 // Add G as N's child.
369 for &grand_child_to_be in &grand_children_to_be {
370 self.children.entry(impl_def_id).or_default()
371 .insert_blindly(tcx, grand_child_to_be);
375 ShouldRecurseOn(new_parent) => {
381 self.parent.insert(impl_def_id, parent);
385 /// Insert cached metadata mapping from a child impl back to its parent.
386 pub fn record_impl_from_cstore(
388 tcx: TyCtxt<'gcx, 'tcx>,
392 if self.parent.insert(child, parent).is_some() {
393 bug!("When recording an impl from the crate store, information about its parent \
394 was already present.");
397 self.children.entry(parent).or_default().insert_blindly(tcx, child);
400 /// The parent of a given impl, which is the `DefId` of the trait when the
401 /// impl is a "specialization root".
402 pub fn parent(&self, child: DefId) -> DefId {
403 *self.parent.get(&child).unwrap()
407 /// A node in the specialization graph is either an impl or a trait
408 /// definition; either can serve as a source of item definitions.
409 /// There is always exactly one trait definition node: the root.
410 #[derive(Debug, Copy, Clone)]
416 impl<'gcx, 'tcx> Node {
417 pub fn is_from_trait(&self) -> bool {
419 Node::Trait(..) => true,
424 /// Iterate over the items defined directly by the given (impl or trait) node.
425 pub fn items(&self, tcx: TyCtxt<'gcx, 'tcx>) -> ty::AssocItemsIterator<'gcx, 'tcx> {
426 tcx.associated_items(self.def_id())
429 pub fn def_id(&self) -> DefId {
431 Node::Impl(did) => did,
432 Node::Trait(did) => did,
437 pub struct Ancestors<'tcx> {
439 specialization_graph: &'tcx Graph,
440 current_source: Option<Node>,
443 impl Iterator for Ancestors<'_> {
445 fn next(&mut self) -> Option<Node> {
446 let cur = self.current_source.take();
447 if let Some(Node::Impl(cur_impl)) = cur {
448 let parent = self.specialization_graph.parent(cur_impl);
450 self.current_source = if parent == self.trait_def_id {
451 Some(Node::Trait(parent))
453 Some(Node::Impl(parent))
460 pub struct NodeItem<T> {
465 impl<T> NodeItem<T> {
466 pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> NodeItem<U> {
474 impl<'gcx, 'tcx> Ancestors<'gcx> {
475 /// Search the items from the given ancestors, returning each definition
476 /// with the given name and the given kind.
477 // FIXME(#35870): avoid closures being unexported due to `impl Trait`.
481 tcx: TyCtxt<'gcx, 'tcx>,
482 trait_item_name: Ident,
483 trait_item_kind: ty::AssocKind,
485 ) -> impl Iterator<Item = NodeItem<ty::AssocItem>> + Captures<'gcx> + 'tcx {
486 self.flat_map(move |node| {
487 use crate::ty::AssocKind::*;
488 node.items(tcx).filter(move |impl_item| match (trait_item_kind, impl_item.kind) {
492 | (Type, Existential)
493 => tcx.hygienic_eq(impl_item.ident, trait_item_name, trait_def_id),
500 }).map(move |item| NodeItem { node: node, item: item })
505 /// Walk up the specialization ancestors of a given impl, starting with that
508 tcx: TyCtxt<'tcx, '_>,
510 start_from_impl: DefId,
511 ) -> Ancestors<'tcx> {
512 let specialization_graph = tcx.specialization_graph_of(trait_def_id);
515 specialization_graph,
516 current_source: Some(Node::Impl(start_from_impl)),
520 impl<'a> HashStable<StableHashingContext<'a>> for Children {
521 fn hash_stable<W: StableHasherResult>(&self,
522 hcx: &mut StableHashingContext<'a>,
523 hasher: &mut StableHasher<W>) {
525 ref nonblanket_impls,
529 ich::hash_stable_trait_impls(hcx, hasher, blanket_impls, nonblanket_impls);
533 impl_stable_hash_for!(struct self::Graph {