Skip to main content

rustc_trait_selection/traits/
dyn_compatibility.rs

1//! "Dyn-compatibility"[^1] refers to the ability for a trait to be converted
2//! to a trait object. In general, traits may only be converted to a trait
3//! object if certain criteria are met.
4//!
5//! [^1]: Formerly known as "object safety".
6
7use std::ops::ControlFlow;
8
9use rustc_errors::FatalError;
10use rustc_hir::def::DefKind;
11use rustc_hir::def_id::DefId;
12use rustc_hir::{self as hir, LangItem};
13use rustc_middle::query::Providers;
14use rustc_middle::ty::{
15    self, EarlyBinder, GenericArgs, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
16    TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode, Unnormalized,
17    Upcast, elaborate,
18};
19use rustc_span::{DUMMY_SP, Span};
20use smallvec::SmallVec;
21use tracing::{debug, instrument};
22
23use super::elaborate;
24use crate::infer::TyCtxtInferExt;
25pub use crate::traits::DynCompatibilityViolation;
26use crate::traits::query::evaluate_obligation::InferCtxtExt;
27use crate::traits::{
28    AssocConstViolation, MethodViolation, Obligation, ObligationCause,
29    normalize_param_env_or_error, util,
30};
31
32/// Returns the dyn-compatibility violations that affect HIR ty lowering.
33///
34/// Currently that is `Self` in supertraits. This is needed
35/// because `dyn_compatibility_violations` can't be used during
36/// type collection, as type collection is needed for `dyn_compatibility_violations` itself.
37x;#[instrument(level = "debug", skip(tcx), ret)]
38pub fn hir_ty_lowering_dyn_compatibility_violations(
39    tcx: TyCtxt<'_>,
40    trait_def_id: DefId,
41) -> Vec<DynCompatibilityViolation> {
42    debug_assert!(tcx.generics_of(trait_def_id).has_self);
43    elaborate::supertrait_def_ids(tcx, trait_def_id)
44        .map(|def_id| predicates_reference_self(tcx, def_id, true))
45        .filter(|spans| !spans.is_empty())
46        .map(DynCompatibilityViolation::SupertraitSelf)
47        .collect()
48}
49
50fn dyn_compatibility_violations(
51    tcx: TyCtxt<'_>,
52    trait_def_id: DefId,
53) -> &'_ [DynCompatibilityViolation] {
54    if true {
    if !tcx.generics_of(trait_def_id).has_self {
        ::core::panicking::panic("assertion failed: tcx.generics_of(trait_def_id).has_self")
    };
};debug_assert!(tcx.generics_of(trait_def_id).has_self);
55    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs:55",
                        "rustc_trait_selection::traits::dyn_compatibility",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs"),
                        ::tracing_core::__macro_support::Option::Some(55u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::dyn_compatibility"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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(&format_args!("dyn_compatibility_violations: {0:?}",
                                                    trait_def_id) as &dyn Value))])
            });
    } else { ; }
};debug!("dyn_compatibility_violations: {:?}", trait_def_id);
56    tcx.arena.alloc_from_iter(
57        elaborate::supertrait_def_ids(tcx, trait_def_id)
58            .flat_map(|def_id| dyn_compatibility_violations_for_trait(tcx, def_id)),
59    )
60}
61
62fn is_dyn_compatible(tcx: TyCtxt<'_>, trait_def_id: DefId) -> bool {
63    tcx.dyn_compatibility_violations(trait_def_id).is_empty()
64}
65
66/// We say a method is *vtable safe* if it can be invoked on a trait
67/// object. Note that dyn-compatible traits can have some
68/// non-vtable-safe methods, so long as they require `Self: Sized` or
69/// otherwise ensure that they cannot be used when `Self = Trait`.
70pub fn is_vtable_safe_method(tcx: TyCtxt<'_>, trait_def_id: DefId, method: ty::AssocItem) -> bool {
71    if true {
    if !tcx.generics_of(trait_def_id).has_self {
        ::core::panicking::panic("assertion failed: tcx.generics_of(trait_def_id).has_self")
    };
};debug_assert!(tcx.generics_of(trait_def_id).has_self);
72    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs:72",
                        "rustc_trait_selection::traits::dyn_compatibility",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs"),
                        ::tracing_core::__macro_support::Option::Some(72u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::dyn_compatibility"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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(&format_args!("is_vtable_safe_method({0:?}, {1:?})",
                                                    trait_def_id, method) as &dyn Value))])
            });
    } else { ; }
};debug!("is_vtable_safe_method({:?}, {:?})", trait_def_id, method);
73    // Any method that has a `Self: Sized` bound cannot be called.
74    if tcx.generics_require_sized_self(method.def_id) {
75        return false;
76    }
77
78    virtual_call_violations_for_method(tcx, trait_def_id, method).is_empty()
79}
80
81x;#[instrument(level = "debug", skip(tcx), ret)]
82fn dyn_compatibility_violations_for_trait(
83    tcx: TyCtxt<'_>,
84    trait_def_id: DefId,
85) -> Vec<DynCompatibilityViolation> {
86    // Check assoc items for violations.
87    let mut violations: Vec<_> = tcx
88        .associated_items(trait_def_id)
89        .in_definition_order()
90        .flat_map(|&item| dyn_compatibility_violations_for_assoc_item(tcx, trait_def_id, item))
91        .collect();
92
93    // Check the trait itself.
94    if trait_has_sized_self(tcx, trait_def_id) {
95        // We don't want to include the requirement from `Sized` itself to be `Sized` in the list.
96        let spans = get_sized_bounds(tcx, trait_def_id);
97        violations.push(DynCompatibilityViolation::SizedSelf(spans));
98    } else if let Some(span) = tcx.trait_def(trait_def_id).force_dyn_incompatible {
99        violations.push(DynCompatibilityViolation::ExplicitlyDynIncompatible([span].into()));
100    }
101
102    let spans = predicates_reference_self(tcx, trait_def_id, false);
103    if !spans.is_empty() {
104        violations.push(DynCompatibilityViolation::SupertraitSelf(spans));
105    }
106    let spans = bounds_reference_self(tcx, trait_def_id);
107    if !spans.is_empty() {
108        violations.push(DynCompatibilityViolation::SupertraitSelf(spans));
109    }
110    let spans = super_predicates_have_non_lifetime_binders(tcx, trait_def_id);
111    if !spans.is_empty() {
112        violations.push(DynCompatibilityViolation::SupertraitNonLifetimeBinder(spans));
113    }
114    let spans = super_predicates_are_unconditionally_const(tcx, trait_def_id);
115    if !spans.is_empty() {
116        violations.push(DynCompatibilityViolation::SupertraitConst(spans));
117    }
118
119    violations
120}
121
122fn sized_trait_bound_spans<'tcx>(
123    tcx: TyCtxt<'tcx>,
124    bounds: hir::GenericBounds<'tcx>,
125) -> impl 'tcx + Iterator<Item = Span> {
126    bounds.iter().filter_map(move |b| match b {
127        hir::GenericBound::Trait(trait_ref)
128            if trait_has_sized_self(
129                tcx,
130                trait_ref.trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise()),
131            ) =>
132        {
133            // Fetch spans for supertraits that are `Sized`: `trait T: Super`
134            Some(trait_ref.span)
135        }
136        _ => None,
137    })
138}
139
140fn get_sized_bounds(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SmallVec<[Span; 1]> {
141    tcx.hir_get_if_local(trait_def_id)
142        .and_then(|node| match node {
143            hir::Node::Item(hir::Item {
144                kind: hir::ItemKind::Trait { generics, bounds, .. },
145                ..
146            }) => Some(
147                generics
148                    .predicates
149                    .iter()
150                    .filter_map(|pred| {
151                        match pred.kind {
152                            hir::WherePredicateKind::BoundPredicate(pred)
153                                if pred.bounded_ty.hir_id.owner.to_def_id() == trait_def_id =>
154                            {
155                                // Fetch spans for trait bounds that are Sized:
156                                // `trait T where Self: Pred`
157                                Some(sized_trait_bound_spans(tcx, pred.bounds))
158                            }
159                            _ => None,
160                        }
161                    })
162                    .flatten()
163                    // Fetch spans for supertraits that are `Sized`: `trait T: Super`.
164                    .chain(sized_trait_bound_spans(tcx, bounds))
165                    .collect::<SmallVec<[Span; 1]>>(),
166            ),
167            _ => None,
168        })
169        .unwrap_or_else(SmallVec::new)
170}
171
172fn predicates_reference_self(
173    tcx: TyCtxt<'_>,
174    trait_def_id: DefId,
175    supertraits_only: bool,
176) -> SmallVec<[Span; 1]> {
177    let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(tcx, trait_def_id));
178    let predicates = if supertraits_only {
179        tcx.explicit_super_predicates_of(trait_def_id).skip_binder()
180    } else {
181        tcx.predicates_of(trait_def_id).predicates
182    };
183    predicates
184        .iter()
185        .map(|&(predicate, sp)| (predicate.instantiate_supertrait(tcx, trait_ref), sp))
186        .filter_map(|(clause, sp)| {
187            // Super predicates cannot allow self projections, since they're
188            // impossible to make into existential bounds without eager resolution
189            // or something.
190            // e.g. `trait A: B<Item = Self::Assoc>`.
191            predicate_references_self(tcx, trait_def_id, clause, sp, AllowSelfProjections::No)
192        })
193        .collect()
194}
195
196fn bounds_reference_self(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SmallVec<[Span; 1]> {
197    tcx.associated_items(trait_def_id)
198        .in_definition_order()
199        // We're only looking at associated type bounds
200        .filter(|item| item.is_type())
201        // Ignore GATs with `Self: Sized`
202        .filter(|item| !tcx.generics_require_sized_self(item.def_id))
203        .flat_map(|item| {
204            tcx.explicit_item_bounds(item.def_id)
205                .iter_identity_copied()
206                .map(Unnormalized::skip_norm_wip)
207        })
208        .filter_map(|(clause, sp)| {
209            // Item bounds *can* have self projections, since they never get
210            // their self type erased.
211            predicate_references_self(tcx, trait_def_id, clause, sp, AllowSelfProjections::Yes)
212        })
213        .collect()
214}
215
216fn predicate_references_self<'tcx>(
217    tcx: TyCtxt<'tcx>,
218    trait_def_id: DefId,
219    predicate: ty::Clause<'tcx>,
220    sp: Span,
221    allow_self_projections: AllowSelfProjections,
222) -> Option<Span> {
223    match predicate.kind().skip_binder() {
224        ty::ClauseKind::Trait(ref data) => {
225            // In the case of a trait predicate, we can skip the "self" type.
226            data.trait_ref.args[1..]
227                .iter()
228                .any(|&arg| {
229                    contains_illegal_self_type_reference(
230                        tcx,
231                        trait_def_id,
232                        arg,
233                        allow_self_projections,
234                    )
235                })
236                .then_some(sp)
237        }
238        ty::ClauseKind::Projection(ref data) => {
239            // And similarly for projections. This should be redundant with
240            // the previous check because any projection should have a
241            // matching `Trait` predicate with the same inputs, but we do
242            // the check to be safe.
243            //
244            // It's also won't be redundant if we allow type-generic associated
245            // types for trait objects.
246            //
247            // Note that we *do* allow projection *outputs* to contain
248            // `Self` (i.e., `trait Foo: Bar<Output=Self::Result> { type Result; }`),
249            // we just require the user to specify *both* outputs
250            // in the object type (i.e., `dyn Foo<Output=(), Result=()>`).
251            //
252            // This is ALT2 in issue #56288, see that for discussion of the
253            // possible alternatives.
254            data.projection_term.args[1..]
255                .iter()
256                .any(|&arg| {
257                    contains_illegal_self_type_reference(
258                        tcx,
259                        trait_def_id,
260                        arg,
261                        allow_self_projections,
262                    )
263                })
264                .then_some(sp)
265        }
266        ty::ClauseKind::ConstArgHasType(_ct, ty) => {
267            contains_illegal_self_type_reference(tcx, trait_def_id, ty, allow_self_projections)
268                .then_some(sp)
269        }
270
271        ty::ClauseKind::WellFormed(..)
272        | ty::ClauseKind::TypeOutlives(..)
273        | ty::ClauseKind::RegionOutlives(..)
274        | ty::ClauseKind::HostEffect(..)
275        | ty::ClauseKind::UnstableFeature(_) => None,
276
277        // FIXME(generic_const_exprs): this can mention `Self`
278        ty::ClauseKind::ConstEvaluatable(..) => None,
279    }
280}
281
282fn super_predicates_have_non_lifetime_binders(
283    tcx: TyCtxt<'_>,
284    trait_def_id: DefId,
285) -> SmallVec<[Span; 1]> {
286    tcx.explicit_super_predicates_of(trait_def_id)
287        .iter_identity_copied()
288        .map(Unnormalized::skip_norm_wip)
289        .filter_map(|(pred, span)| pred.has_non_region_bound_vars().then_some(span))
290        .collect()
291}
292
293/// Checks for `const Trait` supertraits. We're okay with `[const] Trait`,
294/// supertraits since for a non-const instantiation of that trait, the
295/// conditionally-const supertrait is also not required to be const.
296fn super_predicates_are_unconditionally_const(
297    tcx: TyCtxt<'_>,
298    trait_def_id: DefId,
299) -> SmallVec<[Span; 1]> {
300    tcx.explicit_super_predicates_of(trait_def_id)
301        .iter_identity_copied()
302        .map(Unnormalized::skip_norm_wip)
303        .filter_map(|(pred, span)| {
304            if let ty::ClauseKind::HostEffect(_) = pred.kind().skip_binder() {
305                Some(span)
306            } else {
307                None
308            }
309        })
310        .collect()
311}
312
313fn trait_has_sized_self(tcx: TyCtxt<'_>, trait_def_id: DefId) -> bool {
314    tcx.generics_require_sized_self(trait_def_id)
315}
316
317fn generics_require_sized_self(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
318    let Some(sized_def_id) = tcx.lang_items().sized_trait() else {
319        return false; /* No Sized trait, can't require it! */
320    };
321
322    // Search for a predicate like `Self: Sized` amongst the trait bounds.
323    let predicates: Vec<_> = tcx
324        .predicates_of(def_id)
325        .instantiate_identity(tcx)
326        .predicates
327        .into_iter()
328        .map(Unnormalized::skip_norm_wip)
329        .collect();
330    elaborate(tcx, predicates).any(|pred| match pred.kind().skip_binder() {
331        ty::ClauseKind::Trait(ref trait_pred) => {
332            trait_pred.def_id() == sized_def_id && trait_pred.self_ty().is_param(0)
333        }
334        ty::ClauseKind::RegionOutlives(_)
335        | ty::ClauseKind::TypeOutlives(_)
336        | ty::ClauseKind::Projection(_)
337        | ty::ClauseKind::ConstArgHasType(_, _)
338        | ty::ClauseKind::WellFormed(_)
339        | ty::ClauseKind::ConstEvaluatable(_)
340        | ty::ClauseKind::UnstableFeature(_)
341        | ty::ClauseKind::HostEffect(..) => false,
342    })
343}
344
345x;#[instrument(level = "debug", skip(tcx), ret)]
346pub fn dyn_compatibility_violations_for_assoc_item(
347    tcx: TyCtxt<'_>,
348    trait_def_id: DefId,
349    item: ty::AssocItem,
350) -> Vec<DynCompatibilityViolation> {
351    // `final` assoc functions don't prevent a trait from being dyn-compatible
352    if tcx.defaultness(item.def_id).is_final() {
353        return Vec::new();
354    }
355
356    // Any item that has a `Self: Sized` requisite is otherwise exempt from the regulations.
357    if tcx.generics_require_sized_self(item.def_id) {
358        return Vec::new();
359    }
360
361    let span = || item.ident(tcx).span;
362
363    match item.kind {
364        ty::AssocKind::Const { name, is_type_const } => {
365            // We will permit type associated consts if they are explicitly mentioned in the
366            // trait object type. We can't check this here, as here we only check if it is
367            // guaranteed to not be possible.
368
369            let mut errors = Vec::new();
370
371            if tcx.features().min_generic_const_args() {
372                if !tcx.generics_of(item.def_id).is_own_empty() {
373                    errors.push(AssocConstViolation::Generic);
374                } else if !is_type_const {
375                    errors.push(AssocConstViolation::NonType);
376                }
377
378                let ty = ty::Binder::dummy(
379                    tcx.type_of(item.def_id).instantiate_identity().skip_norm_wip(),
380                );
381                if contains_illegal_self_type_reference(
382                    tcx,
383                    trait_def_id,
384                    ty,
385                    AllowSelfProjections::Yes,
386                ) {
387                    errors.push(AssocConstViolation::TypeReferencesSelf);
388                }
389            } else {
390                errors.push(AssocConstViolation::FeatureNotEnabled);
391            }
392
393            errors
394                .into_iter()
395                .map(|error| DynCompatibilityViolation::AssocConst(name, error, span()))
396                .collect()
397        }
398        ty::AssocKind::Fn { name, .. } => {
399            virtual_call_violations_for_method(tcx, trait_def_id, item)
400                .into_iter()
401                .map(|v| {
402                    let node = tcx.hir_get_if_local(item.def_id);
403                    // Get an accurate span depending on the violation.
404                    let span = match (&v, node) {
405                        (MethodViolation::ReferencesSelfInput(Some(span)), _) => *span,
406                        (MethodViolation::UndispatchableReceiver(Some(span)), _) => *span,
407                        (MethodViolation::ReferencesImplTraitInTrait(span), _) => *span,
408                        (MethodViolation::ReferencesSelfOutput, Some(node)) => {
409                            node.fn_decl().map_or(item.ident(tcx).span, |decl| decl.output.span())
410                        }
411                        _ => span(),
412                    };
413
414                    DynCompatibilityViolation::Method(name, v, span)
415                })
416                .collect()
417        }
418        ty::AssocKind::Type { data } => {
419            if !tcx.generics_of(item.def_id).is_own_empty()
420                && let ty::AssocTypeData::Normal(name) = data
421            {
422                vec![DynCompatibilityViolation::GenericAssocTy(name, span())]
423            } else {
424                // We will permit associated types if they are explicitly mentioned in the trait
425                // object type. We can't check this here, as here we only check if it is
426                // guaranteed to not be possible.
427                Vec::new()
428            }
429        }
430    }
431}
432
433/// Returns `Some(_)` if this method cannot be called on a trait
434/// object; this does not necessarily imply that the enclosing trait
435/// is dyn-incompatible, because the method might have a where clause
436/// `Self: Sized`.
437fn virtual_call_violations_for_method<'tcx>(
438    tcx: TyCtxt<'tcx>,
439    trait_def_id: DefId,
440    method: ty::AssocItem,
441) -> Vec<MethodViolation> {
442    let sig = tcx.fn_sig(method.def_id).instantiate_identity().skip_norm_wip();
443
444    // The method's first parameter must be named `self`
445    if !method.is_method() {
446        let sugg = if let Some(hir::Node::TraitItem(hir::TraitItem {
447            generics,
448            kind: hir::TraitItemKind::Fn(sig, _),
449            ..
450        })) = tcx.hir_get_if_local(method.def_id).as_ref()
451        {
452            let sm = tcx.sess.source_map();
453            Some((
454                (
455                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("&self{0}",
                if sig.decl.inputs.is_empty() { "" } else { ", " }))
    })format!("&self{}", if sig.decl.inputs.is_empty() { "" } else { ", " }),
456                    sm.span_through_char(sig.span, '(').shrink_to_hi(),
457                ),
458                (
459                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} Self: Sized",
                generics.add_where_or_trailing_comma()))
    })format!("{} Self: Sized", generics.add_where_or_trailing_comma()),
460                    generics.tail_span_for_predicate_suggestion(),
461                ),
462            ))
463        } else {
464            None
465        };
466
467        // Not having `self` parameter messes up the later checks,
468        // so we need to return instead of pushing
469        return ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [MethodViolation::StaticMethod(sugg)]))vec![MethodViolation::StaticMethod(sugg)];
470    }
471
472    let mut errors = Vec::new();
473
474    for (i, &input_ty) in sig.skip_binder().inputs().iter().enumerate().skip(1) {
475        if contains_illegal_self_type_reference(
476            tcx,
477            trait_def_id,
478            sig.rebind(input_ty),
479            AllowSelfProjections::Yes,
480        ) {
481            let span = if let Some(hir::Node::TraitItem(hir::TraitItem {
482                kind: hir::TraitItemKind::Fn(sig, _),
483                ..
484            })) = tcx.hir_get_if_local(method.def_id).as_ref()
485            {
486                Some(sig.decl.inputs[i].span)
487            } else {
488                None
489            };
490            errors.push(MethodViolation::ReferencesSelfInput(span));
491        }
492    }
493    if contains_illegal_self_type_reference(
494        tcx,
495        trait_def_id,
496        sig.output(),
497        AllowSelfProjections::Yes,
498    ) {
499        errors.push(MethodViolation::ReferencesSelfOutput);
500    }
501    if let Some(error) = contains_illegal_impl_trait_in_trait(tcx, method.def_id, sig.output()) {
502        errors.push(error);
503    }
504    if sig.skip_binder().c_variadic() {
505        errors.push(MethodViolation::CVariadic);
506    }
507
508    // We can't monomorphize things like `fn foo<A>(...)`.
509    let own_counts = tcx.generics_of(method.def_id).own_counts();
510    if own_counts.types > 0 || own_counts.consts > 0 {
511        errors.push(MethodViolation::Generic);
512    }
513
514    let receiver_ty = tcx.liberate_late_bound_regions(method.def_id, sig.input(0));
515
516    // `self: Self` can't be dispatched on.
517    // However, this is considered dyn compatible. We allow it as a special case here.
518    // FIXME(mikeyhew) get rid of this `if` statement once `receiver_is_dispatchable` allows
519    // `Receiver: Unsize<Receiver[Self => dyn Trait]>`.
520    if receiver_ty != tcx.types.self_param {
521        if !receiver_is_dispatchable(tcx, method, receiver_ty) {
522            let span = if let Some(hir::Node::TraitItem(hir::TraitItem {
523                kind: hir::TraitItemKind::Fn(sig, _),
524                ..
525            })) = tcx.hir_get_if_local(method.def_id).as_ref()
526            {
527                Some(sig.decl.inputs[0].span)
528            } else {
529                None
530            };
531            errors.push(MethodViolation::UndispatchableReceiver(span));
532        } else {
533            // We confirm that the `receiver_is_dispatchable` is accurate later,
534            // see `check_receiver_correct`. It should be kept in sync with this code.
535        }
536    }
537
538    // NOTE: This check happens last, because it results in a lint, and not a
539    // hard error.
540    if tcx.predicates_of(method.def_id).predicates.iter().any(|&(pred, _span)| {
541        // dyn Trait is okay:
542        //
543        //     trait Trait {
544        //         fn f(&self) where Self: 'static;
545        //     }
546        //
547        // because a trait object can't claim to live longer than the concrete
548        // type. If the lifetime bound holds on dyn Trait then it's guaranteed
549        // to hold as well on the concrete type.
550        if pred.as_type_outlives_clause().is_some() {
551            return false;
552        }
553
554        // dyn Trait is okay:
555        //
556        //     auto trait AutoTrait {}
557        //
558        //     trait Trait {
559        //         fn f(&self) where Self: AutoTrait;
560        //     }
561        //
562        // because `impl AutoTrait for dyn Trait` is disallowed by coherence.
563        // Traits with a default impl are implemented for a trait object if and
564        // only if the autotrait is one of the trait object's trait bounds, like
565        // in `dyn Trait + AutoTrait`. This guarantees that trait objects only
566        // implement auto traits if the underlying type does as well.
567        if let ty::ClauseKind::Trait(ty::TraitPredicate {
568            trait_ref: pred_trait_ref,
569            polarity: ty::PredicatePolarity::Positive,
570        }) = pred.kind().skip_binder()
571            && pred_trait_ref.self_ty() == tcx.types.self_param
572            && tcx.trait_is_auto(pred_trait_ref.def_id)
573        {
574            // Consider bounds like `Self: Bound<Self>`. Auto traits are not
575            // allowed to have generic parameters so `auto trait Bound<T> {}`
576            // would already have reported an error at the definition of the
577            // auto trait.
578            if pred_trait_ref.args.len() != 1 {
579                if !tcx.dcx().has_errors().is_some() {
    {
        ::core::panicking::panic_fmt(format_args!("auto traits cannot have generic parameters"));
    }
};assert!(
580                    tcx.dcx().has_errors().is_some(),
581                    "auto traits cannot have generic parameters"
582                );
583            }
584            return false;
585        }
586
587        contains_illegal_self_type_reference(tcx, trait_def_id, pred, AllowSelfProjections::Yes)
588    }) {
589        errors.push(MethodViolation::WhereClauseReferencesSelf);
590    }
591
592    errors
593}
594
595/// Performs a type instantiation to produce the version of `receiver_ty` when `Self = self_ty`.
596/// For example, for `receiver_ty = Rc<Self>` and `self_ty = Foo`, returns `Rc<Foo>`.
597fn receiver_for_self_ty<'tcx>(
598    tcx: TyCtxt<'tcx>,
599    receiver_ty: Ty<'tcx>,
600    self_ty: Ty<'tcx>,
601    method_def_id: DefId,
602) -> Ty<'tcx> {
603    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs:603",
                        "rustc_trait_selection::traits::dyn_compatibility",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs"),
                        ::tracing_core::__macro_support::Option::Some(603u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::dyn_compatibility"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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(&format_args!("receiver_for_self_ty({0:?}, {1:?}, {2:?})",
                                                    receiver_ty, self_ty, method_def_id) as &dyn Value))])
            });
    } else { ; }
};debug!("receiver_for_self_ty({:?}, {:?}, {:?})", receiver_ty, self_ty, method_def_id);
604    let args = GenericArgs::for_item(tcx, method_def_id, |param, _| {
605        if param.index == 0 { self_ty.into() } else { tcx.mk_param_from_def(param) }
606    });
607
608    let result = EarlyBinder::bind(receiver_ty).instantiate(tcx, args).skip_norm_wip();
609    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs:609",
                        "rustc_trait_selection::traits::dyn_compatibility",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs"),
                        ::tracing_core::__macro_support::Option::Some(609u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::dyn_compatibility"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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(&format_args!("receiver_for_self_ty({0:?}, {1:?}, {2:?}) = {3:?}",
                                                    receiver_ty, self_ty, method_def_id, result) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(
610        "receiver_for_self_ty({:?}, {:?}, {:?}) = {:?}",
611        receiver_ty, self_ty, method_def_id, result
612    );
613    result
614}
615
616/// Checks the method's receiver (the `self` argument) can be dispatched on when `Self` is a
617/// trait object. We require that `DispatchableFromDyn` be implemented for the receiver type
618/// in the following way:
619/// - let `Receiver` be the type of the `self` argument, i.e `Self`, `&Self`, `Rc<Self>`,
620/// - require the following bound:
621///
622///   ```ignore (not-rust)
623///   Receiver[Self => T]: DispatchFromDyn<Receiver[Self => dyn Trait]>
624///   ```
625///
626///   where `Foo[X => Y]` means "the same type as `Foo`, but with `X` replaced with `Y`"
627///   (instantiation notation).
628///
629/// Some examples of receiver types and their required obligation:
630/// - `&'a mut self` requires `&'a mut Self: DispatchFromDyn<&'a mut dyn Trait>`,
631/// - `self: Rc<Self>` requires `Rc<Self>: DispatchFromDyn<Rc<dyn Trait>>`,
632/// - `self: Pin<Box<Self>>` requires `Pin<Box<Self>>: DispatchFromDyn<Pin<Box<dyn Trait>>>`.
633///
634/// The only case where the receiver is not dispatchable, but is still a valid receiver
635/// type (just not dyn compatible), is when there is more than one level of pointer indirection.
636/// E.g., `self: &&Self`, `self: &Rc<Self>`, `self: Box<Box<Self>>`. In these cases, there
637/// is no way, or at least no inexpensive way, to coerce the receiver from the version where
638/// `Self = dyn Trait` to the version where `Self = T`, where `T` is the unknown erased type
639/// contained by the trait object, because the object that needs to be coerced is behind
640/// a pointer.
641///
642/// If lowering already produced an error in the receiver type, we conservatively treat it as
643/// undispatchable instead of asking the solver.
644///
645/// In practice, we cannot use `dyn Trait` explicitly in the obligation because it would result in
646/// a new check that `Trait` is dyn-compatible, creating a cycle.
647/// Instead, we emulate a placeholder by introducing a new type parameter `U` such that
648/// `Self: Unsize<U>` and `U: Trait + MetaSized`, and use `U` in place of `dyn Trait`.
649///
650/// Written as a chalk-style query:
651/// ```ignore (not-rust)
652/// forall (U: Trait + MetaSized) {
653///     if (Self: Unsize<U>) {
654///         Receiver: DispatchFromDyn<Receiver[Self => U]>
655///     }
656/// }
657/// ```
658/// for `self: &'a mut Self`, this means `&'a mut Self: DispatchFromDyn<&'a mut U>`
659/// for `self: Rc<Self>`, this means `Rc<Self>: DispatchFromDyn<Rc<U>>`
660/// for `self: Pin<Box<Self>>`, this means `Pin<Box<Self>>: DispatchFromDyn<Pin<Box<U>>>`
661//
662// FIXME(mikeyhew) when unsized receivers are implemented as part of unsized rvalues, add this
663// fallback query: `Receiver: Unsize<Receiver[Self => U]>` to support receivers like
664// `self: Wrapper<Self>`.
665fn receiver_is_dispatchable<'tcx>(
666    tcx: TyCtxt<'tcx>,
667    method: ty::AssocItem,
668    receiver_ty: Ty<'tcx>,
669) -> bool {
670    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs:670",
                        "rustc_trait_selection::traits::dyn_compatibility",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs"),
                        ::tracing_core::__macro_support::Option::Some(670u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::dyn_compatibility"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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(&format_args!("receiver_is_dispatchable: method = {0:?}, receiver_ty = {1:?}",
                                                    method, receiver_ty) as &dyn Value))])
            });
    } else { ; }
};debug!("receiver_is_dispatchable: method = {:?}, receiver_ty = {:?}", method, receiver_ty);
671
672    if receiver_ty.references_error() {
673        return false;
674    }
675
676    let (Some(unsize_did), Some(dispatch_from_dyn_did)) =
677        (tcx.lang_items().unsize_trait(), tcx.lang_items().dispatch_from_dyn_trait())
678    else {
679        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs:679",
                        "rustc_trait_selection::traits::dyn_compatibility",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs"),
                        ::tracing_core::__macro_support::Option::Some(679u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::dyn_compatibility"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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(&format_args!("receiver_is_dispatchable: Missing `Unsize` or `DispatchFromDyn` traits")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("receiver_is_dispatchable: Missing `Unsize` or `DispatchFromDyn` traits");
680        return false;
681    };
682
683    // the type `U` in the query
684    // use a bogus type parameter to mimic a forall(U) query using u32::MAX for now.
685    let unsized_self_ty: Ty<'tcx> =
686        Ty::new_param(tcx, u32::MAX, rustc_span::sym::RustaceansAreAwesome);
687
688    // `Receiver[Self => U]`
689    let unsized_receiver_ty =
690        receiver_for_self_ty(tcx, receiver_ty, unsized_self_ty, method.def_id);
691
692    // create a modified param env, with `Self: Unsize<U>` and `U: Trait` (and all of
693    // its supertraits) added to caller bounds. `U: MetaSized` is already implied here.
694    let param_env = {
695        // N.B. We generally want to emulate the construction of the `unnormalized_param_env`
696        // in the param-env query here. The fact that we don't just start with the clauses
697        // in the param-env of the method is because those are already normalized, and mixing
698        // normalized and unnormalized copies of predicates in `normalize_param_env_or_error`
699        // will cause ambiguity that the user can't really avoid.
700        //
701        // We leave out certain complexities of the param-env query here. Specifically, we:
702        // 1. Do not add `[const]` bounds since there are no `dyn const Trait`s.
703        // 2. Do not add RPITIT self projection bounds for defaulted methods, since we
704        //    are not constructing a param-env for "inside" of the body of the defaulted
705        //    method, so we don't really care about projecting to a specific RPIT type,
706        //    and because RPITITs are not dyn compatible (yet).
707        let mut predicates: Vec<_> = tcx
708            .predicates_of(method.def_id)
709            .instantiate_identity(tcx)
710            .predicates
711            .into_iter()
712            .map(Unnormalized::skip_norm_wip)
713            .collect();
714
715        // Self: Unsize<U>
716        let unsize_predicate =
717            ty::TraitRef::new(tcx, unsize_did, [tcx.types.self_param, unsized_self_ty]);
718        predicates.push(unsize_predicate.upcast(tcx));
719
720        // U: Trait<Arg1, ..., ArgN>
721        let trait_def_id = method.trait_container(tcx).unwrap();
722        let args = GenericArgs::for_item(tcx, trait_def_id, |param, _| {
723            if param.index == 0 { unsized_self_ty.into() } else { tcx.mk_param_from_def(param) }
724        });
725        let trait_predicate = ty::TraitRef::new_from_args(tcx, trait_def_id, args);
726        predicates.push(trait_predicate.upcast(tcx));
727
728        let meta_sized_predicate = {
729            let meta_sized_did = tcx.require_lang_item(LangItem::MetaSized, DUMMY_SP);
730            ty::TraitRef::new(tcx, meta_sized_did, [unsized_self_ty]).upcast(tcx)
731        };
732        predicates.push(meta_sized_predicate);
733
734        normalize_param_env_or_error(
735            tcx,
736            ty::ParamEnv::new(tcx.mk_clauses(&predicates)),
737            ObligationCause::dummy_with_span(tcx.def_span(method.def_id)),
738        )
739    };
740
741    // Receiver: DispatchFromDyn<Receiver[Self => U]>
742    let obligation = {
743        let predicate =
744            ty::TraitRef::new(tcx, dispatch_from_dyn_did, [receiver_ty, unsized_receiver_ty]);
745
746        Obligation::new(tcx, ObligationCause::dummy(), param_env, predicate)
747    };
748
749    let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
750    // the receiver is dispatchable iff the obligation holds
751    infcx.predicate_must_hold_modulo_regions(&obligation)
752}
753
754#[derive(#[automatically_derived]
impl ::core::marker::Copy for AllowSelfProjections { }Copy, #[automatically_derived]
impl ::core::clone::Clone for AllowSelfProjections {
    #[inline]
    fn clone(&self) -> AllowSelfProjections { *self }
}Clone)]
755enum AllowSelfProjections {
756    Yes,
757    No,
758}
759
760/// Check if the given value contains illegal `Self` references.
761///
762/// This is somewhat subtle. In general, we want to forbid references to `Self` in the
763/// argument and return types, since the value of `Self` is erased.
764///
765/// However, there is one exception: It is ok to reference `Self` in order to access an
766/// associated type of the current trait, since we retain the value of those associated
767/// types in the trait object type itself.
768///
769/// The same thing holds for associated consts under feature `min_generic_const_args`.
770///
771/// ```rust,ignore (example)
772/// trait SuperTrait {
773///     type X;
774/// }
775///
776/// trait Trait : SuperTrait {
777///     type Y;
778///     fn foo(&self, x: Self) // bad
779///     fn foo(&self) -> Self // bad
780///     fn foo(&self) -> Option<Self> // bad
781///     fn foo(&self) -> Self::Y // OK, desugars to next example
782///     fn foo(&self) -> <Self as Trait>::Y // OK
783///     fn foo(&self) -> Self::X // OK, desugars to next example
784///     fn foo(&self) -> <Self as SuperTrait>::X // OK
785/// }
786/// ```
787///
788/// However, it is not as simple as allowing `Self` in a projected
789/// type, because there are illegal ways to use `Self` as well:
790///
791/// ```rust,ignore (example)
792/// trait Trait : SuperTrait {
793///     ...
794///     fn foo(&self) -> <Self as SomeOtherTrait>::X;
795/// }
796/// ```
797///
798/// Here we will not have the type of `X` recorded in the
799/// object type, and we cannot resolve `Self as SomeOtherTrait`
800/// without knowing what `Self` is.
801fn contains_illegal_self_type_reference<'tcx, T: TypeVisitable<TyCtxt<'tcx>>>(
802    tcx: TyCtxt<'tcx>,
803    trait_def_id: DefId,
804    value: T,
805    allow_self_projections: AllowSelfProjections,
806) -> bool {
807    value
808        .visit_with(&mut IllegalSelfTypeVisitor {
809            tcx,
810            trait_def_id,
811            supertraits: None,
812            allow_self_projections,
813        })
814        .is_break()
815}
816
817struct IllegalSelfTypeVisitor<'tcx> {
818    tcx: TyCtxt<'tcx>,
819    trait_def_id: DefId,
820    supertraits: Option<Vec<ty::TraitRef<'tcx>>>,
821    allow_self_projections: AllowSelfProjections,
822}
823
824impl<'tcx> IllegalSelfTypeVisitor<'tcx> {
825    fn is_supertrait_of_current_trait(&mut self, trait_ref: ty::TraitRef<'tcx>) -> bool {
826        // Compute supertraits of current trait lazily.
827        let supertraits = self.supertraits.get_or_insert_with(|| {
828            util::supertraits(
829                self.tcx,
830                ty::Binder::dummy(ty::TraitRef::identity(self.tcx, self.trait_def_id)),
831            )
832            .map(|trait_ref| {
833                self.tcx.erase_and_anonymize_regions(
834                    self.tcx.instantiate_bound_regions_with_erased(trait_ref),
835                )
836            })
837            .collect()
838        });
839
840        // Determine whether the given trait ref is in fact a supertrait of the current trait.
841        // In that case, any derived projections are legal, because the term will be specified
842        // in the trait object type.
843        // Note that we can just use direct equality here because all of these types are part of
844        // the formal parameter listing, and hence there should be no inference variables.
845        let trait_ref = trait_ref
846            .fold_with(&mut EraseEscapingBoundRegions { tcx: self.tcx, binder: ty::INNERMOST });
847        supertraits.contains(&trait_ref)
848    }
849}
850
851impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for IllegalSelfTypeVisitor<'tcx> {
852    type Result = ControlFlow<()>;
853
854    fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
855        match ty.kind() {
856            ty::Param(_) => {
857                if ty == self.tcx.types.self_param {
858                    ControlFlow::Break(())
859                } else {
860                    ControlFlow::Continue(())
861                }
862            }
863            ty::Alias(ty::AliasTy { kind: ty::Projection { def_id }, .. })
864                if self.tcx.is_impl_trait_in_trait(*def_id) =>
865            {
866                // We'll deny these later in their own pass
867                ControlFlow::Continue(())
868            }
869            ty::Alias(proj @ ty::AliasTy { kind: ty::Projection { .. }, .. }) => {
870                match self.allow_self_projections {
871                    AllowSelfProjections::Yes => {
872                        // Only walk contained types if the parent trait is not a supertrait.
873                        if self.is_supertrait_of_current_trait(proj.trait_ref(self.tcx)) {
874                            ControlFlow::Continue(())
875                        } else {
876                            ty.super_visit_with(self)
877                        }
878                    }
879                    AllowSelfProjections::No => ty.super_visit_with(self),
880                }
881            }
882            _ => ty.super_visit_with(self),
883        }
884    }
885
886    fn visit_const(&mut self, ct: ty::Const<'tcx>) -> Self::Result {
887        let ct = self.tcx.expand_abstract_consts(ct);
888
889        match ct.kind() {
890            ty::ConstKind::Unevaluated(proj) if self.tcx.features().min_generic_const_args() => {
891                match self.allow_self_projections {
892                    AllowSelfProjections::Yes
893                        if #[allow(non_exhaustive_omitted_patterns)] match self.tcx.def_kind(proj.def) {
    DefKind::AssocConst { .. } => true,
    _ => false,
}matches!(self.tcx.def_kind(proj.def), DefKind::AssocConst { .. })
894                            && let trait_def_id = self.tcx.parent(proj.def)
895                            && self.tcx.def_kind(trait_def_id) == DefKind::Trait =>
896                    {
897                        let trait_ref = ty::TraitRef::from_assoc(self.tcx, trait_def_id, proj.args);
898
899                        // Only walk contained consts if the parent trait is not a supertrait.
900                        if self.is_supertrait_of_current_trait(trait_ref) {
901                            ControlFlow::Continue(())
902                        } else {
903                            ct.super_visit_with(self)
904                        }
905                    }
906                    _ => ct.super_visit_with(self),
907                }
908            }
909            _ => ct.super_visit_with(self),
910        }
911    }
912}
913
914struct EraseEscapingBoundRegions<'tcx> {
915    tcx: TyCtxt<'tcx>,
916    binder: ty::DebruijnIndex,
917}
918
919impl<'tcx> TypeFolder<TyCtxt<'tcx>> for EraseEscapingBoundRegions<'tcx> {
920    fn cx(&self) -> TyCtxt<'tcx> {
921        self.tcx
922    }
923
924    fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
925    where
926        T: TypeFoldable<TyCtxt<'tcx>>,
927    {
928        self.binder.shift_in(1);
929        let result = t.super_fold_with(self);
930        self.binder.shift_out(1);
931        result
932    }
933
934    fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
935        if let ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), _) = r.kind()
936            && debruijn < self.binder
937        {
938            r
939        } else {
940            self.tcx.lifetimes.re_erased
941        }
942    }
943}
944
945fn contains_illegal_impl_trait_in_trait<'tcx>(
946    tcx: TyCtxt<'tcx>,
947    fn_def_id: DefId,
948    ty: ty::Binder<'tcx, Ty<'tcx>>,
949) -> Option<MethodViolation> {
950    let ty = tcx.liberate_late_bound_regions(fn_def_id, ty);
951
952    if tcx.asyncness(fn_def_id).is_async() {
953        // Rendering the error as a separate `async-specific` message is better.
954        Some(MethodViolation::AsyncFn)
955    } else {
956        ty.visit_with(&mut IllegalRpititVisitor { tcx, allowed: None }).break_value()
957    }
958}
959
960struct IllegalRpititVisitor<'tcx> {
961    tcx: TyCtxt<'tcx>,
962    allowed: Option<ty::AliasTy<'tcx>>,
963}
964
965impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for IllegalRpititVisitor<'tcx> {
966    type Result = ControlFlow<MethodViolation>;
967
968    fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
969        if let ty::Alias(proj @ ty::AliasTy { kind: ty::Projection { def_id }, .. }) = *ty.kind()
970            && Some(proj) != self.allowed
971            && self.tcx.is_impl_trait_in_trait(def_id)
972        {
973            ControlFlow::Break(MethodViolation::ReferencesImplTraitInTrait(
974                self.tcx.def_span(def_id),
975            ))
976        } else {
977            ty.super_visit_with(self)
978        }
979    }
980}
981
982pub(crate) fn provide(providers: &mut Providers) {
983    *providers = Providers {
984        dyn_compatibility_violations,
985        is_dyn_compatible,
986        generics_require_sized_self,
987        ..*providers
988    };
989}