1use rustc_data_structures::fx::FxIndexMap;
2use rustc_errors::ErrorGuaranteed;
3use rustc_hir::attrs::AttributeKind;
4use rustc_hir::def_id::{DefId, DefIdMap};
5use rustc_hir::find_attr;
6use rustc_macros::{HashStable, TyDecodable, TyEncodable};
7
8use crate::error::StrictCoherenceNeedsNegativeCoherence;
9use crate::ty::fast_reject::SimplifiedType;
10use crate::ty::{self, TyCtxt, TypeVisitableExt};
11
12#[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_query_system::ich::StableHashingContext<'__ctx>>
for Graph {
#[inline]
fn hash_stable(&self,
__hcx:
&mut ::rustc_query_system::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)]
28pub struct Graph {
29 pub parent: DefIdMap<DefId>,
32
33 pub children: DefIdMap<Children>,
35}
36
37impl Graph {
38 pub fn new() -> Graph {
39 Graph { parent: Default::default(), children: Default::default() }
40 }
41
42 #[track_caller]
45 pub fn parent(&self, child: DefId) -> DefId {
46 *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:?}"))
47 }
48}
49
50#[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 {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_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_query_system::ich::StableHashingContext<'__ctx>>
for OverlapMode {
#[inline]
fn hash_stable(&self,
__hcx:
&mut ::rustc_query_system::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)]
53pub enum OverlapMode {
54 Stable,
56 WithNegative,
58 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 = {
'done:
{
for i in tcx.get_all_attrs(trait_id) {
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(AttributeKind::RustcStrictCoherence(span))
=> {
break 'done Some(*span);
}
_ => {}
}
}
None
}
}find_attr!(tcx.get_all_attrs(trait_id), AttributeKind::RustcStrictCoherence(span) => *span);
66
67 if with_negative_coherence {
68 if strict_coherence.is_some() { OverlapMode::Strict } else { OverlapMode::WithNegative }
69 } else {
70 if let Some(span) = strict_coherence {
71 tcx.dcx().emit_err(StrictCoherenceNeedsNegativeCoherence {
72 span: tcx.def_span(trait_id),
73 attr_span: span,
74 });
75 }
76 OverlapMode::Stable
77 }
78 }
79
80 pub fn use_negative_impl(&self) -> bool {
81 *self == OverlapMode::Strict || *self == OverlapMode::WithNegative
82 }
83
84 pub fn use_implicit_negative(&self) -> bool {
85 *self == OverlapMode::Stable || *self == OverlapMode::WithNegative
86 }
87}
88
89#[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_query_system::ich::StableHashingContext<'__ctx>>
for Children {
#[inline]
fn hash_stable(&self,
__hcx:
&mut ::rustc_query_system::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)]
92pub struct Children {
93 pub non_blanket_impls: FxIndexMap<SimplifiedType, Vec<DefId>>,
104
105 pub blanket_impls: Vec<DefId>,
107}
108
109#[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)]
113pub enum Node {
114 Impl(DefId),
115 Trait(DefId),
116}
117
118impl Node {
119 pub fn is_from_trait(&self) -> bool {
120 #[allow(non_exhaustive_omitted_patterns)] match self {
Node::Trait(..) => true,
_ => false,
}matches!(self, Node::Trait(..))
121 }
122
123 pub fn item<'tcx>(&self, tcx: TyCtxt<'tcx>, trait_item_def_id: DefId) -> Option<ty::AssocItem> {
129 match *self {
130 Node::Trait(_) => Some(tcx.associated_item(trait_item_def_id)),
131 Node::Impl(impl_def_id) => {
132 let id = tcx.impl_item_implementor_ids(impl_def_id).get(&trait_item_def_id)?;
133 Some(tcx.associated_item(*id))
134 }
135 }
136 }
137
138 pub fn def_id(&self) -> DefId {
139 match *self {
140 Node::Impl(did) => did,
141 Node::Trait(did) => did,
142 }
143 }
144}
145
146#[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)]
147pub struct Ancestors<'tcx> {
148 trait_def_id: DefId,
149 specialization_graph: &'tcx Graph,
150 current_source: Option<Node>,
151}
152
153impl Iterator for Ancestors<'_> {
154 type Item = Node;
155 fn next(&mut self) -> Option<Node> {
156 let cur = self.current_source.take();
157 if let Some(Node::Impl(cur_impl)) = cur {
158 let parent = self.specialization_graph.parent(cur_impl);
159
160 self.current_source = if parent == self.trait_def_id {
161 Some(Node::Trait(parent))
162 } else {
163 Some(Node::Impl(parent))
164 };
165 }
166 cur
167 }
168}
169
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)]
172pub struct LeafDef {
173 pub item: ty::AssocItem,
175
176 pub defining_node: Node,
178
179 pub finalizing_node: Option<Node>,
204}
205
206impl LeafDef {
207 pub fn is_final(&self) -> bool {
209 self.finalizing_node.is_some()
210 }
211}
212
213impl<'tcx> Ancestors<'tcx> {
214 pub fn leaf_def(mut self, tcx: TyCtxt<'tcx>, trait_item_def_id: DefId) -> Option<LeafDef> {
217 let mut finalizing_node = None;
218
219 self.find_map(|node| {
220 if let Some(item) = node.item(tcx, trait_item_def_id) {
221 if finalizing_node.is_none() {
222 let is_specializable = item.defaultness(tcx).is_default()
223 || tcx.defaultness(node.def_id()).is_default();
224
225 if !is_specializable {
226 finalizing_node = Some(node);
227 }
228 }
229
230 Some(LeafDef { item, defining_node: node, finalizing_node })
231 } else {
232 finalizing_node = Some(node);
234 None
235 }
236 })
237 }
238}
239
240pub fn ancestors(
246 tcx: TyCtxt<'_>,
247 trait_def_id: DefId,
248 start_from_impl: DefId,
249) -> Result<Ancestors<'_>, ErrorGuaranteed> {
250 let specialization_graph = tcx.specialization_graph_of(trait_def_id)?;
251
252 if let Err(reported) = tcx.type_of(start_from_impl).instantiate_identity().error_reported() {
253 Err(reported)
254 } else {
255 Ok(Ancestors {
256 trait_def_id,
257 specialization_graph,
258 current_source: Some(Node::Impl(start_from_impl)),
259 })
260 }
261}