1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use super::OverlapError;
13 use hir::def_id::DefId;
14 use ich::{self, StableHashingContext};
15 use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
18 use ty::{self, TyCtxt, TypeFoldable};
19 use ty::fast_reject::{self, SimplifiedType};
20 use rustc_data_structures::sync::Lrc;
21 use syntax::ast::Ident;
22 use util::captures::Captures;
23 use util::nodemap::{DefIdMap, FxHashMap};
25 /// A per-trait graph of impls in specialization order. At the moment, this
26 /// graph forms a tree rooted with the trait itself, with all other nodes
27 /// representing impls, and parent-child relationships representing
30 /// The graph provides two key services:
32 /// - Construction. This implicitly checks for overlapping impls (i.e., impls
33 /// that overlap but where neither specializes the other -- an artifact of the
34 /// simple "chain" rule.
36 /// - Parent extraction. In particular, the graph can give you the *immediate*
37 /// parents of a given specializing impl, which is needed for extracting
38 /// default items amongst other things. In the simple "chain" rule, every impl
39 /// has at most one parent.
40 #[derive(RustcEncodable, RustcDecodable)]
42 // All impls have a parent; the "root" impls have as their parent the `def_id`
44 parent: DefIdMap<DefId>,
46 // The "root" impls are found by looking up the trait's def_id.
47 children: DefIdMap<Children>,
50 /// Children of a given impl, grouped into blanket/non-blanket varieties as is
51 /// done in `TraitDef`.
52 #[derive(Default, RustcEncodable, RustcDecodable)]
54 // Impls of a trait (or specializations of a given impl). To allow for
55 // quicker lookup, the impls are indexed by a simplified version of their
56 // `Self` type: impls with a simplifiable `Self` are stored in
57 // `nonblanket_impls` keyed by it, while all other impls are stored in
60 // A similar division is used within `TraitDef`, but the lists there collect
61 // together *all* the impls for a trait, and are populated prior to building
62 // the specialization graph.
64 /// Impls of the trait.
65 nonblanket_impls: FxHashMap<fast_reject::SimplifiedType, Vec<DefId>>,
67 /// Blanket impls associated with the trait.
68 blanket_impls: Vec<DefId>,
71 /// The result of attempting to insert an impl into a group of children.
73 /// The impl was inserted as a new child in this group of children.
74 BecameNewSibling(Option<OverlapError>),
76 /// The impl should replace existing impls [X1, ..], because the impl specializes X1, X2, etc.
77 ReplaceChildren(Vec<DefId>),
79 /// The impl is a specialization of an existing child.
80 ShouldRecurseOn(DefId),
83 impl<'a, 'gcx, 'tcx> Children {
84 /// Insert an impl into this set of children without comparing to any existing impls.
85 fn insert_blindly(&mut self,
86 tcx: TyCtxt<'a, 'gcx, 'tcx>,
88 let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
89 if let Some(sty) = fast_reject::simplify_type(tcx, trait_ref.self_ty(), false) {
90 debug!("insert_blindly: impl_def_id={:?} sty={:?}", impl_def_id, sty);
91 self.nonblanket_impls.entry(sty).or_default().push(impl_def_id)
93 debug!("insert_blindly: impl_def_id={:?} sty=None", impl_def_id);
94 self.blanket_impls.push(impl_def_id)
98 /// Remove an impl from this set of children. Used when replacing
99 /// an impl with a parent. The impl must be present in the list of
100 /// children already.
101 fn remove_existing(&mut self,
102 tcx: TyCtxt<'a, 'gcx, 'tcx>,
103 impl_def_id: DefId) {
104 let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
105 let vec: &mut Vec<DefId>;
106 if let Some(sty) = fast_reject::simplify_type(tcx, trait_ref.self_ty(), false) {
107 debug!("remove_existing: impl_def_id={:?} sty={:?}", impl_def_id, sty);
108 vec = self.nonblanket_impls.get_mut(&sty).unwrap();
110 debug!("remove_existing: impl_def_id={:?} sty=None", impl_def_id);
111 vec = &mut self.blanket_impls;
114 let index = vec.iter().position(|d| *d == impl_def_id).unwrap();
118 /// Attempt to insert an impl into this set of children, while comparing for
119 /// specialization relationships.
121 tcx: TyCtxt<'a, 'gcx, 'tcx>,
123 simplified_self: Option<SimplifiedType>)
124 -> Result<Inserted, OverlapError>
126 let mut last_lint = None;
127 let mut replace_children = Vec::new();
130 "insert(impl_def_id={:?}, simplified_self={:?})",
135 for possible_sibling in match simplified_self {
136 Some(sty) => self.filtered(sty),
140 "insert: impl_def_id={:?}, simplified_self={:?}, possible_sibling={:?}",
146 let overlap_error = |overlap: traits::coherence::OverlapResult<'_>| {
147 // Found overlap, but no specialization; error out.
148 let trait_ref = overlap.impl_header.trait_ref.unwrap();
149 let self_ty = trait_ref.self_ty();
151 with_impl: possible_sibling,
152 trait_desc: trait_ref.to_string(),
153 // Only report the `Self` type if it has at least
154 // some outer concrete shell; otherwise, it's
155 // not adding much information.
156 self_desc: if self_ty.has_concrete_skeleton() {
157 Some(self_ty.to_string())
161 intercrate_ambiguity_causes: overlap.intercrate_ambiguity_causes,
165 let tcx = tcx.global_tcx();
166 let (le, ge) = traits::overlapping_impls(
170 traits::IntercrateMode::Issue43355,
172 if tcx.impls_are_allowed_to_overlap(impl_def_id, possible_sibling) {
173 return Ok((false, false));
176 let le = tcx.specializes((impl_def_id, possible_sibling));
177 let ge = tcx.specializes((possible_sibling, impl_def_id));
180 Err(overlap_error(overlap))
185 || Ok((false, false)),
189 debug!("descending as child of TraitRef {:?}",
190 tcx.impl_trait_ref(possible_sibling).unwrap());
192 // The impl specializes `possible_sibling`.
193 return Ok(Inserted::ShouldRecurseOn(possible_sibling));
194 } else if ge && !le {
195 debug!("placing as parent of TraitRef {:?}",
196 tcx.impl_trait_ref(possible_sibling).unwrap());
198 replace_children.push(possible_sibling);
200 if !tcx.impls_are_allowed_to_overlap(impl_def_id, possible_sibling) {
201 traits::overlapping_impls(
205 traits::IntercrateMode::Fixed,
206 |overlap| last_lint = Some(overlap_error(overlap)),
211 // no overlap (error bailed already via ?)
215 if !replace_children.is_empty() {
216 return Ok(Inserted::ReplaceChildren(replace_children));
219 // No overlap with any potential siblings, so add as a new sibling.
220 debug!("placing as new sibling");
221 self.insert_blindly(tcx, impl_def_id);
222 Ok(Inserted::BecameNewSibling(last_lint))
225 fn iter(&mut self) -> Box<dyn Iterator<Item = DefId> + '_> {
226 let nonblanket = self.nonblanket_impls.iter_mut().flat_map(|(_, v)| v.iter());
227 Box::new(self.blanket_impls.iter().chain(nonblanket).cloned())
230 fn filtered(&mut self, sty: SimplifiedType) -> Box<dyn Iterator<Item = DefId> + '_> {
231 let nonblanket = self.nonblanket_impls.entry(sty).or_default().iter();
232 Box::new(self.blanket_impls.iter().chain(nonblanket).cloned())
236 impl<'a, 'gcx, 'tcx> Graph {
237 pub fn new() -> Graph {
239 parent: Default::default(),
240 children: Default::default(),
244 /// Insert a local impl into the specialization graph. If an existing impl
245 /// conflicts with it (has overlap, but neither specializes the other),
246 /// information about the area of overlap is returned in the `Err`.
247 pub fn insert(&mut self,
248 tcx: TyCtxt<'a, 'gcx, 'tcx>,
250 -> Result<Option<OverlapError>, OverlapError> {
251 assert!(impl_def_id.is_local());
253 let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
254 let trait_def_id = trait_ref.def_id;
256 debug!("insert({:?}): inserting TraitRef {:?} into specialization graph",
257 impl_def_id, trait_ref);
259 // If the reference itself contains an earlier error (e.g., due to a
260 // resolution failure), then we just insert the impl at the top level of
261 // the graph and claim that there's no overlap (in order to suppress
263 if trait_ref.references_error() {
264 debug!("insert: inserting dummy node for erroneous TraitRef {:?}, \
265 impl_def_id={:?}, trait_def_id={:?}",
266 trait_ref, impl_def_id, trait_def_id);
268 self.parent.insert(impl_def_id, trait_def_id);
269 self.children.entry(trait_def_id).or_default()
270 .insert_blindly(tcx, impl_def_id);
274 let mut parent = trait_def_id;
275 let mut last_lint = None;
276 let simplified = fast_reject::simplify_type(tcx, trait_ref.self_ty(), false);
278 // Descend the specialization tree, where `parent` is the current parent node.
280 use self::Inserted::*;
282 let insert_result = self.children.entry(parent).or_default()
283 .insert(tcx, impl_def_id, simplified)?;
285 match insert_result {
286 BecameNewSibling(opt_lint) => {
287 last_lint = opt_lint;
290 ReplaceChildren(grand_children_to_be) => {
297 // and we are inserting the impl N. We want to make it:
305 // Adjust P's list of children: remove G and then add N.
307 let siblings = self.children
310 for &grand_child_to_be in &grand_children_to_be {
311 siblings.remove_existing(tcx, grand_child_to_be);
313 siblings.insert_blindly(tcx, impl_def_id);
316 // Set G's parent to N and N's parent to P.
317 for &grand_child_to_be in &grand_children_to_be {
318 self.parent.insert(grand_child_to_be, impl_def_id);
320 self.parent.insert(impl_def_id, parent);
322 // Add G as N's child.
323 for &grand_child_to_be in &grand_children_to_be {
324 self.children.entry(impl_def_id).or_default()
325 .insert_blindly(tcx, grand_child_to_be);
329 ShouldRecurseOn(new_parent) => {
335 self.parent.insert(impl_def_id, parent);
339 /// Insert cached metadata mapping from a child impl back to its parent.
340 pub fn record_impl_from_cstore(&mut self,
341 tcx: TyCtxt<'a, 'gcx, 'tcx>,
344 if self.parent.insert(child, parent).is_some() {
345 bug!("When recording an impl from the crate store, information about its parent \
346 was already present.");
349 self.children.entry(parent).or_default().insert_blindly(tcx, child);
352 /// The parent of a given impl, which is the def id of the trait when the
353 /// impl is a "specialization root".
354 pub fn parent(&self, child: DefId) -> DefId {
355 *self.parent.get(&child).unwrap()
359 /// A node in the specialization graph is either an impl or a trait
360 /// definition; either can serve as a source of item definitions.
361 /// There is always exactly one trait definition node: the root.
362 #[derive(Debug, Copy, Clone)]
368 impl<'a, 'gcx, 'tcx> Node {
369 pub fn is_from_trait(&self) -> bool {
371 Node::Trait(..) => true,
376 /// Iterate over the items defined directly by the given (impl or trait) node.
379 tcx: TyCtxt<'a, 'gcx, 'tcx>,
380 ) -> ty::AssociatedItemsIterator<'a, 'gcx, 'tcx> {
381 tcx.associated_items(self.def_id())
384 pub fn def_id(&self) -> DefId {
386 Node::Impl(did) => did,
387 Node::Trait(did) => did,
392 pub struct Ancestors {
394 specialization_graph: Lrc<Graph>,
395 current_source: Option<Node>,
398 impl Iterator for Ancestors {
400 fn next(&mut self) -> Option<Node> {
401 let cur = self.current_source.take();
402 if let Some(Node::Impl(cur_impl)) = cur {
403 let parent = self.specialization_graph.parent(cur_impl);
405 self.current_source = if parent == self.trait_def_id {
406 Some(Node::Trait(parent))
408 Some(Node::Impl(parent))
415 pub struct NodeItem<T> {
420 impl<T> NodeItem<T> {
421 pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> NodeItem<U> {
429 impl<'a, 'gcx, 'tcx> Ancestors {
430 /// Search the items from the given ancestors, returning each definition
431 /// with the given name and the given kind.
432 // FIXME(#35870): avoid closures being unexported due to `impl Trait`.
436 tcx: TyCtxt<'a, 'gcx, 'tcx>,
437 trait_item_name: Ident,
438 trait_item_kind: ty::AssociatedKind,
440 ) -> impl Iterator<Item = NodeItem<ty::AssociatedItem>> + Captures<'gcx> + Captures<'tcx> + 'a {
441 self.flat_map(move |node| {
442 use ty::AssociatedKind::*;
443 node.items(tcx).filter(move |impl_item| match (trait_item_kind, impl_item.kind) {
447 | (Type, Existential)
448 => tcx.hygienic_eq(impl_item.ident, trait_item_name, trait_def_id),
455 }).map(move |item| NodeItem { node: node, item: item })
460 /// Walk up the specialization ancestors of a given impl, starting with that
462 pub fn ancestors(tcx: TyCtxt<'_, '_, '_>,
464 start_from_impl: DefId)
466 let specialization_graph = tcx.specialization_graph_of(trait_def_id);
469 specialization_graph,
470 current_source: Some(Node::Impl(start_from_impl)),
474 impl<'a> HashStable<StableHashingContext<'a>> for Children {
475 fn hash_stable<W: StableHasherResult>(&self,
476 hcx: &mut StableHashingContext<'a>,
477 hasher: &mut StableHasher<W>) {
479 ref nonblanket_impls,
483 ich::hash_stable_trait_impls(hcx, hasher, blanket_impls, nonblanket_impls);
487 impl_stable_hash_for!(struct self::Graph {