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::Name;
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, which 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(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 replaced an existing impl that specializes it.
79 /// The impl is a specialization of an existing child.
80 ShouldRecurseOn(DefId),
83 impl<'a, 'gcx, 'tcx> Children {
84 fn new() -> Children {
86 nonblanket_impls: FxHashMap(),
87 blanket_impls: vec![],
91 /// Insert an impl into this set of children without comparing to any existing impls
92 fn insert_blindly(&mut self,
93 tcx: TyCtxt<'a, 'gcx, 'tcx>,
95 let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
96 if let Some(sty) = fast_reject::simplify_type(tcx, trait_ref.self_ty(), false) {
97 self.nonblanket_impls.entry(sty).or_insert(vec![]).push(impl_def_id)
99 self.blanket_impls.push(impl_def_id)
103 /// Attempt to insert an impl into this set of children, while comparing for
104 /// specialization relationships.
106 tcx: TyCtxt<'a, 'gcx, 'tcx>,
108 simplified_self: Option<SimplifiedType>)
109 -> Result<Inserted, OverlapError>
111 let mut last_lint = None;
113 for slot in match simplified_self {
114 Some(sty) => self.filtered_mut(sty),
115 None => self.iter_mut(),
117 let possible_sibling = *slot;
119 let overlap_error = |overlap: traits::coherence::OverlapResult| {
120 // overlap, but no specialization; error out
121 let trait_ref = overlap.impl_header.trait_ref.unwrap();
122 let self_ty = trait_ref.self_ty();
124 with_impl: possible_sibling,
125 trait_desc: trait_ref.to_string(),
126 // only report the Self type if it has at least
127 // some outer concrete shell; otherwise, it's
128 // not adding much information.
129 self_desc: if self_ty.has_concrete_skeleton() {
130 Some(self_ty.to_string())
134 intercrate_ambiguity_causes: overlap.intercrate_ambiguity_causes,
138 let tcx = tcx.global_tcx();
139 let (le, ge) = traits::overlapping_impls(
143 traits::IntercrateMode::Issue43355,
145 if tcx.impls_are_allowed_to_overlap(impl_def_id, possible_sibling) {
146 return Ok((false, false));
149 let le = tcx.specializes((impl_def_id, possible_sibling));
150 let ge = tcx.specializes((possible_sibling, impl_def_id));
153 Err(overlap_error(overlap))
158 || Ok((false, false)),
162 debug!("descending as child of TraitRef {:?}",
163 tcx.impl_trait_ref(possible_sibling).unwrap());
165 // the impl specializes possible_sibling
166 return Ok(Inserted::ShouldRecurseOn(possible_sibling));
167 } else if ge && !le {
168 debug!("placing as parent of TraitRef {:?}",
169 tcx.impl_trait_ref(possible_sibling).unwrap());
171 // possible_sibling specializes the impl
173 return Ok(Inserted::Replaced(possible_sibling));
175 if !tcx.impls_are_allowed_to_overlap(impl_def_id, possible_sibling) {
176 traits::overlapping_impls(
180 traits::IntercrateMode::Fixed,
181 |overlap| last_lint = Some(overlap_error(overlap)),
186 // no overlap (error bailed already via ?)
190 // no overlap with any potential siblings, so add as a new sibling
191 debug!("placing as new sibling");
192 self.insert_blindly(tcx, impl_def_id);
193 Ok(Inserted::BecameNewSibling(last_lint))
196 fn iter_mut(&'a mut self) -> Box<dyn Iterator<Item = &'a mut DefId> + 'a> {
197 let nonblanket = self.nonblanket_impls.iter_mut().flat_map(|(_, v)| v.iter_mut());
198 Box::new(self.blanket_impls.iter_mut().chain(nonblanket))
201 fn filtered_mut(&'a mut self, sty: SimplifiedType)
202 -> Box<dyn Iterator<Item = &'a mut DefId> + 'a> {
203 let nonblanket = self.nonblanket_impls.entry(sty).or_insert(vec![]).iter_mut();
204 Box::new(self.blanket_impls.iter_mut().chain(nonblanket))
208 impl<'a, 'gcx, 'tcx> Graph {
209 pub fn new() -> Graph {
211 parent: Default::default(),
212 children: Default::default(),
216 /// Insert a local impl into the specialization graph. If an existing impl
217 /// conflicts with it (has overlap, but neither specializes the other),
218 /// information about the area of overlap is returned in the `Err`.
219 pub fn insert(&mut self,
220 tcx: TyCtxt<'a, 'gcx, 'tcx>,
222 -> Result<Option<OverlapError>, OverlapError> {
223 assert!(impl_def_id.is_local());
225 let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
226 let trait_def_id = trait_ref.def_id;
228 debug!("insert({:?}): inserting TraitRef {:?} into specialization graph",
229 impl_def_id, trait_ref);
231 // if the reference itself contains an earlier error (e.g., due to a
232 // resolution failure), then we just insert the impl at the top level of
233 // the graph and claim that there's no overlap (in order to suppress
235 if trait_ref.references_error() {
236 debug!("insert: inserting dummy node for erroneous TraitRef {:?}, \
237 impl_def_id={:?}, trait_def_id={:?}",
238 trait_ref, impl_def_id, trait_def_id);
240 self.parent.insert(impl_def_id, trait_def_id);
241 self.children.entry(trait_def_id).or_insert(Children::new())
242 .insert_blindly(tcx, impl_def_id);
246 let mut parent = trait_def_id;
247 let mut last_lint = None;
248 let simplified = fast_reject::simplify_type(tcx, trait_ref.self_ty(), false);
250 // Descend the specialization tree, where `parent` is the current parent node
252 use self::Inserted::*;
254 let insert_result = self.children.entry(parent).or_insert(Children::new())
255 .insert(tcx, impl_def_id, simplified)?;
257 match insert_result {
258 BecameNewSibling(opt_lint) => {
259 last_lint = opt_lint;
262 Replaced(new_child) => {
263 self.parent.insert(new_child, impl_def_id);
264 let mut new_children = Children::new();
265 new_children.insert_blindly(tcx, new_child);
266 self.children.insert(impl_def_id, new_children);
269 ShouldRecurseOn(new_parent) => {
275 self.parent.insert(impl_def_id, parent);
279 /// Insert cached metadata mapping from a child impl back to its parent.
280 pub fn record_impl_from_cstore(&mut self,
281 tcx: TyCtxt<'a, 'gcx, 'tcx>,
284 if self.parent.insert(child, parent).is_some() {
285 bug!("When recording an impl from the crate store, information about its parent \
286 was already present.");
289 self.children.entry(parent).or_insert(Children::new()).insert_blindly(tcx, child);
292 /// The parent of a given impl, which is the def id of the trait when the
293 /// impl is a "specialization root".
294 pub fn parent(&self, child: DefId) -> DefId {
295 *self.parent.get(&child).unwrap()
299 /// A node in the specialization graph is either an impl or a trait
300 /// definition; either can serve as a source of item definitions.
301 /// There is always exactly one trait definition node: the root.
302 #[derive(Debug, Copy, Clone)]
308 impl<'a, 'gcx, 'tcx> Node {
309 pub fn is_from_trait(&self) -> bool {
311 Node::Trait(..) => true,
316 /// Iterate over the items defined directly by the given (impl or trait) node.
319 tcx: TyCtxt<'a, 'gcx, 'tcx>,
320 ) -> impl Iterator<Item = ty::AssociatedItem> + 'a {
321 tcx.associated_items(self.def_id())
324 pub fn def_id(&self) -> DefId {
326 Node::Impl(did) => did,
327 Node::Trait(did) => did,
332 pub struct Ancestors {
334 specialization_graph: Lrc<Graph>,
335 current_source: Option<Node>,
338 impl Iterator for Ancestors {
340 fn next(&mut self) -> Option<Node> {
341 let cur = self.current_source.take();
342 if let Some(Node::Impl(cur_impl)) = cur {
343 let parent = self.specialization_graph.parent(cur_impl);
344 if parent == self.trait_def_id {
345 self.current_source = Some(Node::Trait(parent));
347 self.current_source = Some(Node::Impl(parent));
354 pub struct NodeItem<T> {
359 impl<T> NodeItem<T> {
360 pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> NodeItem<U> {
368 impl<'a, 'gcx, 'tcx> Ancestors {
369 /// Search the items from the given ancestors, returning each definition
370 /// with the given name and the given kind.
371 #[inline] // FIXME(#35870) Avoid closures being unexported due to impl Trait.
374 tcx: TyCtxt<'a, 'gcx, 'tcx>,
375 trait_item_name: Name,
376 trait_item_kind: ty::AssociatedKind,
378 ) -> impl Iterator<Item = NodeItem<ty::AssociatedItem>> + Captures<'gcx> + Captures<'tcx> + 'a {
379 self.flat_map(move |node| {
380 node.items(tcx).filter(move |impl_item| {
381 impl_item.kind == trait_item_kind &&
382 tcx.hygienic_eq(impl_item.name, trait_item_name, trait_def_id)
383 }).map(move |item| NodeItem { node: node, item: item })
388 /// Walk up the specialization ancestors of a given impl, starting with that
390 pub fn ancestors(tcx: TyCtxt,
392 start_from_impl: DefId)
394 let specialization_graph = tcx.specialization_graph_of(trait_def_id);
397 specialization_graph,
398 current_source: Some(Node::Impl(start_from_impl)),
402 impl<'a> HashStable<StableHashingContext<'a>> for Children {
403 fn hash_stable<W: StableHasherResult>(&self,
404 hcx: &mut StableHashingContext<'a>,
405 hasher: &mut StableHasher<W>) {
407 ref nonblanket_impls,
411 ich::hash_stable_trait_impls(hcx, hasher, blanket_impls, nonblanket_impls);
415 impl_stable_hash_for!(struct self::Graph {