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