rustc_middle/traits/
specialization_graph.rs

1use rustc_data_structures::fx::FxIndexMap;
2use rustc_errors::ErrorGuaranteed;
3use rustc_hir::def_id::{DefId, DefIdMap};
4use rustc_macros::{HashStable, TyDecodable, TyEncodable};
5use rustc_span::sym;
6
7use crate::error::StrictCoherenceNeedsNegativeCoherence;
8use crate::ty::fast_reject::SimplifiedType;
9use crate::ty::visit::TypeVisitableExt;
10use crate::ty::{self, TyCtxt};
11
12/// A per-trait graph of impls in specialization order. At the moment, this
13/// graph forms a tree rooted with the trait itself, with all other nodes
14/// representing impls, and parent-child relationships representing
15/// specializations.
16///
17/// The graph provides two key services:
18///
19/// - Construction. This implicitly checks for overlapping impls (i.e., impls
20///   that overlap but where neither specializes the other -- an artifact of the
21///   simple "chain" rule.
22///
23/// - Parent extraction. In particular, the graph can give you the *immediate*
24///   parents of a given specializing impl, which is needed for extracting
25///   default items amongst other things. In the simple "chain" rule, every impl
26///   has at most one parent.
27#[derive(TyEncodable, TyDecodable, HashStable, Debug)]
28pub struct Graph {
29    /// All impls have a parent; the "root" impls have as their parent the `def_id`
30    /// of the trait.
31    pub parent: DefIdMap<DefId>,
32
33    /// The "root" impls are found by looking up the trait's def_id.
34    pub children: DefIdMap<Children>,
35}
36
37impl Graph {
38    pub fn new() -> Graph {
39        Graph { parent: Default::default(), children: Default::default() }
40    }
41
42    /// The parent of a given impl, which is the `DefId` of the trait when the
43    /// impl is a "specialization root".
44    #[track_caller]
45    pub fn parent(&self, child: DefId) -> DefId {
46        *self.parent.get(&child).unwrap_or_else(|| panic!("Failed to get parent for {child:?}"))
47    }
48}
49
50/// What kind of overlap check are we doing -- this exists just for testing and feature-gating
51/// purposes.
52#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable, Debug, TyEncodable, TyDecodable)]
53pub enum OverlapMode {
54    /// The 1.0 rules (either types fail to unify, or where clauses are not implemented for crate-local types)
55    Stable,
56    /// Feature-gated test: Stable, *or* there is an explicit negative impl that rules out one of the where-clauses.
57    WithNegative,
58    /// Just check for negative impls, not for "where clause not implemented": used for testing.
59    Strict,
60}
61
62impl OverlapMode {
63    pub fn get(tcx: TyCtxt<'_>, trait_id: DefId) -> OverlapMode {
64        let with_negative_coherence = tcx.features().with_negative_coherence();
65        let strict_coherence = tcx.has_attr(trait_id, sym::rustc_strict_coherence);
66
67        if with_negative_coherence {
68            if strict_coherence { OverlapMode::Strict } else { OverlapMode::WithNegative }
69        } else {
70            if strict_coherence {
71                let attr_span = trait_id
72                    .as_local()
73                    .into_iter()
74                    .flat_map(|local_def_id| {
75                        tcx.hir().attrs(tcx.local_def_id_to_hir_id(local_def_id))
76                    })
77                    .find(|attr| attr.has_name(sym::rustc_strict_coherence))
78                    .map(|attr| attr.span);
79                tcx.dcx().emit_err(StrictCoherenceNeedsNegativeCoherence {
80                    span: tcx.def_span(trait_id),
81                    attr_span,
82                });
83            }
84            OverlapMode::Stable
85        }
86    }
87
88    pub fn use_negative_impl(&self) -> bool {
89        *self == OverlapMode::Strict || *self == OverlapMode::WithNegative
90    }
91
92    pub fn use_implicit_negative(&self) -> bool {
93        *self == OverlapMode::Stable || *self == OverlapMode::WithNegative
94    }
95}
96
97/// Children of a given impl, grouped into blanket/non-blanket varieties as is
98/// done in `TraitDef`.
99#[derive(Default, TyEncodable, TyDecodable, Debug, HashStable)]
100pub struct Children {
101    // Impls of a trait (or specializations of a given impl). To allow for
102    // quicker lookup, the impls are indexed by a simplified version of their
103    // `Self` type: impls with a simplifiable `Self` are stored in
104    // `non_blanket_impls` keyed by it, while all other impls are stored in
105    // `blanket_impls`.
106    //
107    // A similar division is used within `TraitDef`, but the lists there collect
108    // together *all* the impls for a trait, and are populated prior to building
109    // the specialization graph.
110    /// Impls of the trait.
111    pub non_blanket_impls: FxIndexMap<SimplifiedType, Vec<DefId>>,
112
113    /// Blanket impls associated with the trait.
114    pub blanket_impls: Vec<DefId>,
115}
116
117/// A node in the specialization graph is either an impl or a trait
118/// definition; either can serve as a source of item definitions.
119/// There is always exactly one trait definition node: the root.
120#[derive(Debug, Copy, Clone)]
121pub enum Node {
122    Impl(DefId),
123    Trait(DefId),
124}
125
126impl Node {
127    pub fn is_from_trait(&self) -> bool {
128        matches!(self, Node::Trait(..))
129    }
130
131    /// Tries to find the associated item that implements `trait_item_def_id`
132    /// defined in this node.
133    ///
134    /// If this returns `None`, the item can potentially still be found in
135    /// parents of this node.
136    pub fn item<'tcx>(&self, tcx: TyCtxt<'tcx>, trait_item_def_id: DefId) -> Option<ty::AssocItem> {
137        match *self {
138            Node::Trait(_) => Some(tcx.associated_item(trait_item_def_id)),
139            Node::Impl(impl_def_id) => {
140                let id = tcx.impl_item_implementor_ids(impl_def_id).get(&trait_item_def_id)?;
141                Some(tcx.associated_item(*id))
142            }
143        }
144    }
145
146    pub fn def_id(&self) -> DefId {
147        match *self {
148            Node::Impl(did) => did,
149            Node::Trait(did) => did,
150        }
151    }
152}
153
154#[derive(Copy, Clone)]
155pub struct Ancestors<'tcx> {
156    trait_def_id: DefId,
157    specialization_graph: &'tcx Graph,
158    current_source: Option<Node>,
159}
160
161impl Iterator for Ancestors<'_> {
162    type Item = Node;
163    fn next(&mut self) -> Option<Node> {
164        let cur = self.current_source.take();
165        if let Some(Node::Impl(cur_impl)) = cur {
166            let parent = self.specialization_graph.parent(cur_impl);
167
168            self.current_source = if parent == self.trait_def_id {
169                Some(Node::Trait(parent))
170            } else {
171                Some(Node::Impl(parent))
172            };
173        }
174        cur
175    }
176}
177
178/// Information about the most specialized definition of an associated item.
179#[derive(Debug)]
180pub struct LeafDef {
181    /// The associated item described by this `LeafDef`.
182    pub item: ty::AssocItem,
183
184    /// The node in the specialization graph containing the definition of `item`.
185    pub defining_node: Node,
186
187    /// The "top-most" (ie. least specialized) specialization graph node that finalized the
188    /// definition of `item`.
189    ///
190    /// Example:
191    ///
192    /// ```
193    /// #![feature(specialization)]
194    /// trait Tr {
195    ///     fn assoc(&self);
196    /// }
197    ///
198    /// impl<T> Tr for T {
199    ///     default fn assoc(&self) {}
200    /// }
201    ///
202    /// impl Tr for u8 {}
203    /// ```
204    ///
205    /// If we start the leaf definition search at `impl Tr for u8`, that impl will be the
206    /// `finalizing_node`, while `defining_node` will be the generic impl.
207    ///
208    /// If the leaf definition search is started at the generic impl, `finalizing_node` will be
209    /// `None`, since the most specialized impl we found still allows overriding the method
210    /// (doesn't finalize it).
211    pub finalizing_node: Option<Node>,
212}
213
214impl LeafDef {
215    /// Returns whether this definition is known to not be further specializable.
216    pub fn is_final(&self) -> bool {
217        self.finalizing_node.is_some()
218    }
219}
220
221impl<'tcx> Ancestors<'tcx> {
222    /// Finds the bottom-most (ie. most specialized) definition of an associated
223    /// item.
224    pub fn leaf_def(mut self, tcx: TyCtxt<'tcx>, trait_item_def_id: DefId) -> Option<LeafDef> {
225        let mut finalizing_node = None;
226
227        self.find_map(|node| {
228            if let Some(item) = node.item(tcx, trait_item_def_id) {
229                if finalizing_node.is_none() {
230                    let is_specializable = item.defaultness(tcx).is_default()
231                        || tcx.defaultness(node.def_id()).is_default();
232
233                    if !is_specializable {
234                        finalizing_node = Some(node);
235                    }
236                }
237
238                Some(LeafDef { item, defining_node: node, finalizing_node })
239            } else {
240                // Item not mentioned. This "finalizes" any defaulted item provided by an ancestor.
241                finalizing_node = Some(node);
242                None
243            }
244        })
245    }
246}
247
248/// Walk up the specialization ancestors of a given impl, starting with that
249/// impl itself.
250///
251/// Returns `Err` if an error was reported while building the specialization
252/// graph.
253pub fn ancestors(
254    tcx: TyCtxt<'_>,
255    trait_def_id: DefId,
256    start_from_impl: DefId,
257) -> Result<Ancestors<'_>, ErrorGuaranteed> {
258    let specialization_graph = tcx.specialization_graph_of(trait_def_id)?;
259
260    if let Err(reported) = tcx.type_of(start_from_impl).instantiate_identity().error_reported() {
261        Err(reported)
262    } else {
263        Ok(Ancestors {
264            trait_def_id,
265            specialization_graph,
266            current_source: Some(Node::Impl(start_from_impl)),
267        })
268    }
269}