Skip to main content

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_hir::find_attr;
5use rustc_macros::{HashStable, TyDecodable, TyEncodable};
6
7use crate::error::StrictCoherenceNeedsNegativeCoherence;
8use crate::ty::fast_reject::SimplifiedType;
9use crate::ty::{self, TyCtxt, TypeVisitableExt};
10
11/// A per-trait graph of impls in specialization order. At the moment, this
12/// graph forms a tree rooted with the trait itself, with all other nodes
13/// representing impls, and parent-child relationships representing
14/// specializations.
15///
16/// The graph provides two key services:
17///
18/// - Construction. This implicitly checks for overlapping impls (i.e., impls
19///   that overlap but where neither specializes the other -- an artifact of the
20///   simple "chain" rule.
21///
22/// - Parent extraction. In particular, the graph can give you the *immediate*
23///   parents of a given specializing impl, which is needed for extracting
24///   default items amongst other things. In the simple "chain" rule, every impl
25///   has at most one parent.
26#[derive(const _: () =
    {
        impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
            ::rustc_serialize::Encodable<__E> for Graph {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    Graph { parent: ref __binding_0, children: ref __binding_1 }
                        => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                }
            }
        }
    };TyEncodable, const _: () =
    {
        impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
            ::rustc_serialize::Decodable<__D> for Graph {
            fn decode(__decoder: &mut __D) -> Self {
                Graph {
                    parent: ::rustc_serialize::Decodable::decode(__decoder),
                    children: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };TyDecodable, const _: () =
    {
        impl<'__ctx>
            ::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
            for Graph {
            #[inline]
            fn hash_stable(&self,
                __hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                match *self {
                    Graph { parent: ref __binding_0, children: ref __binding_1 }
                        => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable, #[automatically_derived]
impl ::core::fmt::Debug for Graph {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "Graph",
            "parent", &self.parent, "children", &&self.children)
    }
}Debug)]
27pub struct Graph {
28    /// All impls have a parent; the "root" impls have as their parent the `def_id`
29    /// of the trait.
30    pub parent: DefIdMap<DefId>,
31
32    /// The "root" impls are found by looking up the trait's def_id.
33    pub children: DefIdMap<Children>,
34}
35
36impl Graph {
37    pub fn new() -> Graph {
38        Graph { parent: Default::default(), children: Default::default() }
39    }
40
41    /// The parent of a given impl, which is the `DefId` of the trait when the
42    /// impl is a "specialization root".
43    #[track_caller]
44    pub fn parent(&self, child: DefId) -> DefId {
45        *self.parent.get(&child).unwrap_or_else(|| {
    ::core::panicking::panic_fmt(format_args!("Failed to get parent for {0:?}",
            child));
}panic!("Failed to get parent for {child:?}"))
46    }
47}
48
49/// What kind of overlap check are we doing -- this exists just for testing and feature-gating
50/// purposes.
51#[derive(#[automatically_derived]
impl ::core::marker::Copy for OverlapMode { }Copy, #[automatically_derived]
impl ::core::clone::Clone for OverlapMode {
    #[inline]
    fn clone(&self) -> OverlapMode { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for OverlapMode {
    #[inline]
    fn eq(&self, other: &OverlapMode) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for OverlapMode {
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for OverlapMode {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state)
    }
}Hash, const _: () =
    {
        impl<'__ctx>
            ::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
            for OverlapMode {
            #[inline]
            fn hash_stable(&self,
                __hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
                match *self {
                    OverlapMode::Stable => {}
                    OverlapMode::WithNegative => {}
                    OverlapMode::Strict => {}
                }
            }
        }
    };HashStable, #[automatically_derived]
impl ::core::fmt::Debug for OverlapMode {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                OverlapMode::Stable => "Stable",
                OverlapMode::WithNegative => "WithNegative",
                OverlapMode::Strict => "Strict",
            })
    }
}Debug, const _: () =
    {
        impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
            ::rustc_serialize::Encodable<__E> for OverlapMode {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        OverlapMode::Stable => { 0usize }
                        OverlapMode::WithNegative => { 1usize }
                        OverlapMode::Strict => { 2usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
                match *self {
                    OverlapMode::Stable => {}
                    OverlapMode::WithNegative => {}
                    OverlapMode::Strict => {}
                }
            }
        }
    };TyEncodable, const _: () =
    {
        impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
            ::rustc_serialize::Decodable<__D> for OverlapMode {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => { OverlapMode::Stable }
                    1usize => { OverlapMode::WithNegative }
                    2usize => { OverlapMode::Strict }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `OverlapMode`, expected 0..3, actual {0}",
                                n));
                    }
                }
            }
        }
    };TyDecodable)]
52pub enum OverlapMode {
53    /// The 1.0 rules (either types fail to unify, or where clauses are not implemented for crate-local types)
54    Stable,
55    /// Feature-gated test: Stable, *or* there is an explicit negative impl that rules out one of the where-clauses.
56    WithNegative,
57    /// Just check for negative impls, not for "where clause not implemented": used for testing.
58    Strict,
59}
60
61impl OverlapMode {
62    pub fn get(tcx: TyCtxt<'_>, trait_id: DefId) -> OverlapMode {
63        let with_negative_coherence = tcx.features().with_negative_coherence();
64        let strict_coherence = {

    #[allow(deprecated)]
    {
        {
            'done:
                {
                for i in tcx.get_all_attrs(trait_id) {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(RustcStrictCoherence(span)) =>
                            {
                            break 'done Some(*span);
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }
}find_attr!(tcx, trait_id, RustcStrictCoherence(span) => *span);
65
66        if with_negative_coherence {
67            if strict_coherence.is_some() { OverlapMode::Strict } else { OverlapMode::WithNegative }
68        } else {
69            if let Some(span) = strict_coherence {
70                tcx.dcx().emit_err(StrictCoherenceNeedsNegativeCoherence {
71                    span: tcx.def_span(trait_id),
72                    attr_span: span,
73                });
74            }
75            OverlapMode::Stable
76        }
77    }
78
79    pub fn use_negative_impl(&self) -> bool {
80        *self == OverlapMode::Strict || *self == OverlapMode::WithNegative
81    }
82
83    pub fn use_implicit_negative(&self) -> bool {
84        *self == OverlapMode::Stable || *self == OverlapMode::WithNegative
85    }
86}
87
88/// Children of a given impl, grouped into blanket/non-blanket varieties as is
89/// done in `TraitDef`.
90#[derive(#[automatically_derived]
impl ::core::default::Default for Children {
    #[inline]
    fn default() -> Children {
        Children {
            non_blanket_impls: ::core::default::Default::default(),
            blanket_impls: ::core::default::Default::default(),
        }
    }
}Default, const _: () =
    {
        impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
            ::rustc_serialize::Encodable<__E> for Children {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    Children {
                        non_blanket_impls: ref __binding_0,
                        blanket_impls: ref __binding_1 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                }
            }
        }
    };TyEncodable, const _: () =
    {
        impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
            ::rustc_serialize::Decodable<__D> for Children {
            fn decode(__decoder: &mut __D) -> Self {
                Children {
                    non_blanket_impls: ::rustc_serialize::Decodable::decode(__decoder),
                    blanket_impls: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };TyDecodable, #[automatically_derived]
impl ::core::fmt::Debug for Children {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "Children",
            "non_blanket_impls", &self.non_blanket_impls, "blanket_impls",
            &&self.blanket_impls)
    }
}Debug, const _: () =
    {
        impl<'__ctx>
            ::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
            for Children {
            #[inline]
            fn hash_stable(&self,
                __hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                match *self {
                    Children {
                        non_blanket_impls: ref __binding_0,
                        blanket_impls: ref __binding_1 } => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable)]
91pub struct Children {
92    // Impls of a trait (or specializations of a given impl). To allow for
93    // quicker lookup, the impls are indexed by a simplified version of their
94    // `Self` type: impls with a simplifiable `Self` are stored in
95    // `non_blanket_impls` keyed by it, while all other impls are stored in
96    // `blanket_impls`.
97    //
98    // A similar division is used within `TraitDef`, but the lists there collect
99    // together *all* the impls for a trait, and are populated prior to building
100    // the specialization graph.
101    /// Impls of the trait.
102    pub non_blanket_impls: FxIndexMap<SimplifiedType, Vec<DefId>>,
103
104    /// Blanket impls associated with the trait.
105    pub blanket_impls: Vec<DefId>,
106}
107
108/// A node in the specialization graph is either an impl or a trait
109/// definition; either can serve as a source of item definitions.
110/// There is always exactly one trait definition node: the root.
111#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Node {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Node::Impl(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Impl",
                    &__self_0),
            Node::Trait(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Trait",
                    &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl ::core::marker::Copy for Node { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Node {
    #[inline]
    fn clone(&self) -> Node {
        let _: ::core::clone::AssertParamIsClone<DefId>;
        *self
    }
}Clone)]
112pub enum Node {
113    Impl(DefId),
114    Trait(DefId),
115}
116
117impl Node {
118    pub fn is_from_trait(&self) -> bool {
119        #[allow(non_exhaustive_omitted_patterns)] match self {
    Node::Trait(..) => true,
    _ => false,
}matches!(self, Node::Trait(..))
120    }
121
122    /// Tries to find the associated item that implements `trait_item_def_id`
123    /// defined in this node.
124    ///
125    /// If this returns `None`, the item can potentially still be found in
126    /// parents of this node.
127    pub fn item<'tcx>(&self, tcx: TyCtxt<'tcx>, trait_item_def_id: DefId) -> Option<ty::AssocItem> {
128        match *self {
129            Node::Trait(_) => Some(tcx.associated_item(trait_item_def_id)),
130            Node::Impl(impl_def_id) => {
131                let id = tcx.impl_item_implementor_ids(impl_def_id).get(&trait_item_def_id)?;
132                Some(tcx.associated_item(*id))
133            }
134        }
135    }
136
137    pub fn def_id(&self) -> DefId {
138        match *self {
139            Node::Impl(did) => did,
140            Node::Trait(did) => did,
141        }
142    }
143}
144
145#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for Ancestors<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for Ancestors<'tcx> {
    #[inline]
    fn clone(&self) -> Ancestors<'tcx> {
        let _: ::core::clone::AssertParamIsClone<DefId>;
        let _: ::core::clone::AssertParamIsClone<&'tcx Graph>;
        let _: ::core::clone::AssertParamIsClone<Option<Node>>;
        *self
    }
}Clone)]
146pub struct Ancestors<'tcx> {
147    trait_def_id: DefId,
148    specialization_graph: &'tcx Graph,
149    current_source: Option<Node>,
150}
151
152impl Iterator for Ancestors<'_> {
153    type Item = Node;
154    fn next(&mut self) -> Option<Node> {
155        let cur = self.current_source.take();
156        if let Some(Node::Impl(cur_impl)) = cur {
157            let parent = self.specialization_graph.parent(cur_impl);
158
159            self.current_source = if parent == self.trait_def_id {
160                Some(Node::Trait(parent))
161            } else {
162                Some(Node::Impl(parent))
163            };
164        }
165        cur
166    }
167}
168
169/// Information about the most specialized definition of an associated item.
170#[derive(#[automatically_derived]
impl ::core::fmt::Debug for LeafDef {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "LeafDef",
            "item", &self.item, "defining_node", &self.defining_node,
            "finalizing_node", &&self.finalizing_node)
    }
}Debug)]
171pub struct LeafDef {
172    /// The associated item described by this `LeafDef`.
173    pub item: ty::AssocItem,
174
175    /// The node in the specialization graph containing the definition of `item`.
176    pub defining_node: Node,
177
178    /// The "top-most" (ie. least specialized) specialization graph node that finalized the
179    /// definition of `item`.
180    ///
181    /// Example:
182    ///
183    /// ```
184    /// #![feature(specialization)]
185    /// trait Tr {
186    ///     fn assoc(&self);
187    /// }
188    ///
189    /// impl<T> Tr for T {
190    ///     default fn assoc(&self) {}
191    /// }
192    ///
193    /// impl Tr for u8 {}
194    /// ```
195    ///
196    /// If we start the leaf definition search at `impl Tr for u8`, that impl will be the
197    /// `finalizing_node`, while `defining_node` will be the generic impl.
198    ///
199    /// If the leaf definition search is started at the generic impl, `finalizing_node` will be
200    /// `None`, since the most specialized impl we found still allows overriding the method
201    /// (doesn't finalize it).
202    pub finalizing_node: Option<Node>,
203}
204
205impl LeafDef {
206    /// Returns whether this definition is known to not be further specializable.
207    pub fn is_final(&self) -> bool {
208        self.finalizing_node.is_some()
209    }
210}
211
212impl<'tcx> Ancestors<'tcx> {
213    /// Finds the bottom-most (ie. most specialized) definition of an associated
214    /// item.
215    pub fn leaf_def(mut self, tcx: TyCtxt<'tcx>, trait_item_def_id: DefId) -> Option<LeafDef> {
216        let mut finalizing_node = None;
217
218        self.find_map(|node| {
219            if let Some(item) = node.item(tcx, trait_item_def_id) {
220                if finalizing_node.is_none() {
221                    let is_specializable = item.defaultness(tcx).is_default()
222                        || tcx.defaultness(node.def_id()).is_default();
223
224                    if !is_specializable {
225                        finalizing_node = Some(node);
226                    }
227                }
228
229                Some(LeafDef { item, defining_node: node, finalizing_node })
230            } else {
231                // Item not mentioned. This "finalizes" any defaulted item provided by an ancestor.
232                finalizing_node = Some(node);
233                None
234            }
235        })
236    }
237}
238
239/// Walk up the specialization ancestors of a given impl, starting with that
240/// impl itself.
241///
242/// Returns `Err` if an error was reported while building the specialization
243/// graph.
244pub fn ancestors(
245    tcx: TyCtxt<'_>,
246    trait_def_id: DefId,
247    start_from_impl: DefId,
248) -> Result<Ancestors<'_>, ErrorGuaranteed> {
249    let specialization_graph = tcx.specialization_graph_of(trait_def_id)?;
250
251    if let Err(reported) = tcx.type_of(start_from_impl).instantiate_identity().error_reported() {
252        Err(reported)
253    } else {
254        Ok(Ancestors {
255            trait_def_id,
256            specialization_graph,
257            current_source: Some(Node::Impl(start_from_impl)),
258        })
259    }
260}