1use std::iter;
2
3use rustc_data_structures::fx::FxIndexMap;
4use rustc_errors::ErrorGuaranteed;
5use rustc_hir::def::DefKind;
6use rustc_hir::def_id::{DefId, LOCAL_CRATE};
7use rustc_hir::{self as hir, find_attr};
8use rustc_macros::{Decodable, Encodable, HashStable};
9use rustc_span::Span;
10use tracing::debug;
11
12use crate::query::LocalCrate;
13use crate::traits::specialization_graph;
14use crate::ty::fast_reject::{self, SimplifiedType, TreatParams};
15use crate::ty::print::{with_crate_prefix, with_no_trimmed_paths};
16use crate::ty::{Ident, Ty, TyCtxt};
17
18#[derive(const _: () =
{
impl<'__ctx>
::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
for TraitDef {
#[inline]
fn hash_stable(&self,
__hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
TraitDef {
def_id: ref __binding_0,
safety: ref __binding_1,
constness: ref __binding_2,
impl_restriction: ref __binding_3,
paren_sugar: ref __binding_4,
has_auto_impl: ref __binding_5,
is_marker: ref __binding_6,
is_coinductive: ref __binding_7,
is_fundamental: ref __binding_8,
skip_array_during_method_dispatch: ref __binding_9,
skip_boxed_slice_during_method_dispatch: ref __binding_10,
specialization_kind: ref __binding_11,
must_implement_one_of: ref __binding_12,
force_dyn_incompatible: ref __binding_13,
deny_explicit_impl: ref __binding_14 } => {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
{ __binding_2.hash_stable(__hcx, __hasher); }
{ __binding_3.hash_stable(__hcx, __hasher); }
{ __binding_4.hash_stable(__hcx, __hasher); }
{ __binding_5.hash_stable(__hcx, __hasher); }
{ __binding_6.hash_stable(__hcx, __hasher); }
{ __binding_7.hash_stable(__hcx, __hasher); }
{ __binding_8.hash_stable(__hcx, __hasher); }
{ __binding_9.hash_stable(__hcx, __hasher); }
{ __binding_10.hash_stable(__hcx, __hasher); }
{ __binding_11.hash_stable(__hcx, __hasher); }
{ __binding_12.hash_stable(__hcx, __hasher); }
{ __binding_13.hash_stable(__hcx, __hasher); }
{ __binding_14.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for TraitDef {
fn encode(&self, __encoder: &mut __E) {
match *self {
TraitDef {
def_id: ref __binding_0,
safety: ref __binding_1,
constness: ref __binding_2,
impl_restriction: ref __binding_3,
paren_sugar: ref __binding_4,
has_auto_impl: ref __binding_5,
is_marker: ref __binding_6,
is_coinductive: ref __binding_7,
is_fundamental: ref __binding_8,
skip_array_during_method_dispatch: ref __binding_9,
skip_boxed_slice_during_method_dispatch: ref __binding_10,
specialization_kind: ref __binding_11,
must_implement_one_of: ref __binding_12,
force_dyn_incompatible: ref __binding_13,
deny_explicit_impl: ref __binding_14 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_2,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_3,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_4,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_5,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_6,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_7,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_8,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_9,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_10,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_11,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_12,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_13,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_14,
__encoder);
}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for TraitDef {
fn decode(__decoder: &mut __D) -> Self {
TraitDef {
def_id: ::rustc_serialize::Decodable::decode(__decoder),
safety: ::rustc_serialize::Decodable::decode(__decoder),
constness: ::rustc_serialize::Decodable::decode(__decoder),
impl_restriction: ::rustc_serialize::Decodable::decode(__decoder),
paren_sugar: ::rustc_serialize::Decodable::decode(__decoder),
has_auto_impl: ::rustc_serialize::Decodable::decode(__decoder),
is_marker: ::rustc_serialize::Decodable::decode(__decoder),
is_coinductive: ::rustc_serialize::Decodable::decode(__decoder),
is_fundamental: ::rustc_serialize::Decodable::decode(__decoder),
skip_array_during_method_dispatch: ::rustc_serialize::Decodable::decode(__decoder),
skip_boxed_slice_during_method_dispatch: ::rustc_serialize::Decodable::decode(__decoder),
specialization_kind: ::rustc_serialize::Decodable::decode(__decoder),
must_implement_one_of: ::rustc_serialize::Decodable::decode(__decoder),
force_dyn_incompatible: ::rustc_serialize::Decodable::decode(__decoder),
deny_explicit_impl: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable)]
20pub struct TraitDef {
21 pub def_id: DefId,
22
23 pub safety: hir::Safety,
24
25 pub constness: hir::Constness,
27
28 pub impl_restriction: ImplRestrictionKind,
30
31 pub paren_sugar: bool,
36
37 pub has_auto_impl: bool,
38
39 pub is_marker: bool,
43
44 pub is_coinductive: bool,
52
53 pub is_fundamental: bool,
57
58 pub skip_array_during_method_dispatch: bool,
62
63 pub skip_boxed_slice_during_method_dispatch: bool,
67
68 pub specialization_kind: TraitSpecializationKind,
71
72 pub must_implement_one_of: Option<Box<[Ident]>>,
75
76 pub force_dyn_incompatible: Option<Span>,
79
80 pub deny_explicit_impl: bool,
84}
85
86#[derive(const _: () =
{
impl<'__ctx>
::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
for TraitSpecializationKind {
#[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 {
TraitSpecializationKind::None => {}
TraitSpecializationKind::Marker => {}
TraitSpecializationKind::AlwaysApplicable => {}
}
}
}
};HashStable, #[automatically_derived]
impl ::core::cmp::PartialEq for TraitSpecializationKind {
#[inline]
fn eq(&self, other: &TraitSpecializationKind) -> 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::clone::Clone for TraitSpecializationKind {
#[inline]
fn clone(&self) -> TraitSpecializationKind { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for TraitSpecializationKind { }Copy, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for TraitSpecializationKind {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
TraitSpecializationKind::None => { 0usize }
TraitSpecializationKind::Marker => { 1usize }
TraitSpecializationKind::AlwaysApplicable => { 2usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
TraitSpecializationKind::None => {}
TraitSpecializationKind::Marker => {}
TraitSpecializationKind::AlwaysApplicable => {}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for TraitSpecializationKind {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => { TraitSpecializationKind::None }
1usize => { TraitSpecializationKind::Marker }
2usize => { TraitSpecializationKind::AlwaysApplicable }
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `TraitSpecializationKind`, expected 0..3, actual {0}",
n));
}
}
}
}
};Decodable)]
89pub enum TraitSpecializationKind {
90 None,
92 Marker,
97 AlwaysApplicable,
102}
103
104#[derive(const _: () =
{
impl<'__ctx>
::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
for ImplRestrictionKind {
#[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 {
ImplRestrictionKind::Unrestricted => {}
ImplRestrictionKind::Restricted(ref __binding_0,
ref __binding_1) => {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable, #[automatically_derived]
impl ::core::cmp::PartialEq for ImplRestrictionKind {
#[inline]
fn eq(&self, other: &ImplRestrictionKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(ImplRestrictionKind::Restricted(__self_0, __self_1),
ImplRestrictionKind::Restricted(__arg1_0, __arg1_1)) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl ::core::clone::Clone for ImplRestrictionKind {
#[inline]
fn clone(&self) -> ImplRestrictionKind {
let _: ::core::clone::AssertParamIsClone<DefId>;
let _: ::core::clone::AssertParamIsClone<Span>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for ImplRestrictionKind { }Copy, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for ImplRestrictionKind {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
ImplRestrictionKind::Unrestricted => { 0usize }
ImplRestrictionKind::Restricted(ref __binding_0,
ref __binding_1) => {
1usize
}
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
ImplRestrictionKind::Unrestricted => {}
ImplRestrictionKind::Restricted(ref __binding_0,
ref __binding_1) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for ImplRestrictionKind {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => { ImplRestrictionKind::Unrestricted }
1usize => {
ImplRestrictionKind::Restricted(::rustc_serialize::Decodable::decode(__decoder),
::rustc_serialize::Decodable::decode(__decoder))
}
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `ImplRestrictionKind`, expected 0..2, actual {0}",
n));
}
}
}
}
};Decodable)]
106pub enum ImplRestrictionKind {
107 Unrestricted,
109 Restricted(DefId, Span),
111}
112
113impl ImplRestrictionKind {
114 pub fn is_allowed_in(self, module: DefId, tcx: TyCtxt<'_>) -> bool {
117 match self {
118 ImplRestrictionKind::Unrestricted => true,
119 ImplRestrictionKind::Restricted(restricted_to, _) => {
120 tcx.is_descendant_of(module, restricted_to)
121 }
122 }
123 }
124
125 pub fn expect_span(self) -> Span {
127 match self {
128 ImplRestrictionKind::Unrestricted => {
129 crate::util::bug::bug_fmt(format_args!("called `expect_span` on an unrestricted item"))bug!("called `expect_span` on an unrestricted item")
130 }
131 ImplRestrictionKind::Restricted(_, span) => span,
132 }
133 }
134
135 pub fn restriction_path(self, tcx: TyCtxt<'_>) -> String {
137 match self {
138 ImplRestrictionKind::Unrestricted => String::new(),
139 ImplRestrictionKind::Restricted(restricted_to, _) => {
140 if restricted_to.krate == rustc_hir::def_id::LOCAL_CRATE {
141 {
let _guard = CratePrefixGuard::new();
{ let _guard = NoTrimmedGuard::new(); tcx.def_path_str(restricted_to) }
}with_crate_prefix!(with_no_trimmed_paths!(tcx.def_path_str(restricted_to)))
142 } else {
143 tcx.def_path_str(restricted_to.krate.as_mod_def_id())
144 }
145 }
146 }
147 }
148}
149
150#[derive(#[automatically_derived]
impl ::core::default::Default for TraitImpls {
#[inline]
fn default() -> TraitImpls {
TraitImpls {
blanket_impls: ::core::default::Default::default(),
non_blanket_impls: ::core::default::Default::default(),
}
}
}Default, #[automatically_derived]
impl ::core::fmt::Debug for TraitImpls {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "TraitImpls",
"blanket_impls", &self.blanket_impls, "non_blanket_impls",
&&self.non_blanket_impls)
}
}Debug, const _: () =
{
impl<'__ctx>
::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
for TraitImpls {
#[inline]
fn hash_stable(&self,
__hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
TraitImpls {
blanket_impls: ref __binding_0,
non_blanket_impls: ref __binding_1 } => {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable)]
151pub struct TraitImpls {
152 blanket_impls: Vec<DefId>,
153 non_blanket_impls: FxIndexMap<SimplifiedType, Vec<DefId>>,
155}
156
157impl TraitImpls {
158 pub fn is_empty(&self) -> bool {
159 self.blanket_impls.is_empty() && self.non_blanket_impls.is_empty()
160 }
161
162 pub fn blanket_impls(&self) -> &[DefId] {
163 self.blanket_impls.as_slice()
164 }
165
166 pub fn non_blanket_impls(&self) -> &FxIndexMap<SimplifiedType, Vec<DefId>> {
167 &self.non_blanket_impls
168 }
169}
170
171impl<'tcx> TraitDef {
172 pub fn ancestors(
173 &self,
174 tcx: TyCtxt<'tcx>,
175 of_impl: DefId,
176 ) -> Result<specialization_graph::Ancestors<'tcx>, ErrorGuaranteed> {
177 specialization_graph::ancestors(tcx, self.def_id, of_impl)
178 }
179}
180
181impl<'tcx> TyCtxt<'tcx> {
182 pub fn for_each_relevant_impl(
186 self,
187 trait_def_id: DefId,
188 self_ty: Ty<'tcx>,
189 mut f: impl FnMut(DefId),
190 ) {
191 let impls = self.trait_impls_of(trait_def_id);
197
198 for &impl_def_id in impls.blanket_impls.iter() {
199 f(impl_def_id);
200 }
201
202 if let Some(simp) = fast_reject::simplify_type(self, self_ty, TreatParams::AsRigid) {
209 if let Some(impls) = impls.non_blanket_impls.get(&simp) {
210 for &impl_def_id in impls {
211 f(impl_def_id);
212 }
213 }
214 } else {
215 for &impl_def_id in impls.non_blanket_impls.values().flatten() {
216 f(impl_def_id);
217 }
218 }
219 }
220
221 pub fn non_blanket_impls_for_ty(
223 self,
224 trait_def_id: DefId,
225 self_ty: Ty<'tcx>,
226 ) -> impl Iterator<Item = DefId> {
227 let impls = self.trait_impls_of(trait_def_id);
228 if let Some(simp) =
229 fast_reject::simplify_type(self, self_ty, TreatParams::InstantiateWithInfer)
230 {
231 if let Some(impls) = impls.non_blanket_impls.get(&simp) {
232 return impls.iter().copied();
233 }
234 }
235
236 [].iter().copied()
237 }
238
239 pub fn all_impls(self, trait_def_id: DefId) -> impl Iterator<Item = DefId> {
243 let TraitImpls { blanket_impls, non_blanket_impls } = self.trait_impls_of(trait_def_id);
244
245 blanket_impls.iter().chain(non_blanket_impls.iter().flat_map(|(_, v)| v)).cloned()
246 }
247}
248
249pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> TraitImpls {
251 let mut impls = TraitImpls::default();
252
253 if !trait_id.is_local() {
256 for &cnum in tcx.crates(()).iter() {
257 for &(impl_def_id, simplified_self_ty) in
258 tcx.implementations_of_trait((cnum, trait_id)).iter()
259 {
260 if let Some(simplified_self_ty) = simplified_self_ty {
261 impls
262 .non_blanket_impls
263 .entry(simplified_self_ty)
264 .or_default()
265 .push(impl_def_id);
266 } else {
267 impls.blanket_impls.push(impl_def_id);
268 }
269 }
270 }
271 }
272
273 for &impl_def_id in tcx.local_trait_impls(trait_id) {
274 let impl_def_id = impl_def_id.to_def_id();
275
276 let impl_self_ty = tcx.type_of(impl_def_id).instantiate_identity().skip_norm_wip();
277
278 if let Some(simplified_self_ty) =
279 fast_reject::simplify_type(tcx, impl_self_ty, TreatParams::InstantiateWithInfer)
280 {
281 impls.non_blanket_impls.entry(simplified_self_ty).or_default().push(impl_def_id);
282 } else {
283 impls.blanket_impls.push(impl_def_id);
284 }
285 }
286
287 impls
288}
289
290pub(super) fn incoherent_impls_provider(tcx: TyCtxt<'_>, simp: SimplifiedType) -> &[DefId] {
292 if let Some(def_id) = simp.def()
293 && !{
{
'done:
{
for i in ::rustc_hir::attrs::HasAttrs::get_attrs(def_id, &tcx)
{
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcHasIncoherentInherentImpls)
=> {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}.is_some()find_attr!(tcx, def_id, RustcHasIncoherentInherentImpls)
294 {
295 return &[];
296 }
297
298 let mut impls = Vec::new();
299 for cnum in iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) {
300 for &impl_def_id in tcx.crate_incoherent_impls((cnum, simp)) {
301 impls.push(impl_def_id)
302 }
303 }
304 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/ty/trait_def.rs:304",
"rustc_middle::ty::trait_def", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/ty/trait_def.rs"),
::tracing_core::__macro_support::Option::Some(304u32),
::tracing_core::__macro_support::Option::Some("rustc_middle::ty::trait_def"),
::tracing_core::field::FieldSet::new(&["impls"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&impls) as
&dyn Value))])
});
} else { ; }
};debug!(?impls);
305
306 tcx.arena.alloc_slice(&impls)
307}
308
309pub(super) fn traits_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> &[DefId] {
310 let mut traits = Vec::new();
311 for id in tcx.hir_free_items() {
312 if #[allow(non_exhaustive_omitted_patterns)] match tcx.def_kind(id.owner_id) {
DefKind::Trait | DefKind::TraitAlias => true,
_ => false,
}matches!(tcx.def_kind(id.owner_id), DefKind::Trait | DefKind::TraitAlias) {
313 traits.push(id.owner_id.to_def_id())
314 }
315 }
316
317 tcx.arena.alloc_slice(&traits)
318}
319
320pub(super) fn trait_impls_in_crate_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> &[DefId] {
321 let mut trait_impls = Vec::new();
322 for id in tcx.hir_free_items() {
323 if tcx.def_kind(id.owner_id) == (DefKind::Impl { of_trait: true }) {
324 trait_impls.push(id.owner_id.to_def_id())
325 }
326 }
327
328 tcx.arena.alloc_slice(&trait_impls)
329}