Function rustc_hir_analysis::impl_wf_check::check_impl_wf

source ·
pub(crate) fn check_impl_wf(
    tcx: TyCtxt<'_>,
    impl_def_id: LocalDefId,
) -> Result<(), ErrorGuaranteed>
Expand description

Checks that all the type/lifetime parameters on an impl also appear in the trait ref or self type (or are constrained by a where-clause). These rules are needed to ensure that, given a trait ref like <T as Trait<U>>, we can derive the values of all parameters on the impl (which is needed to make specialization possible).

However, in the case of lifetimes, we only enforce these rules if the lifetime parameter is used in an associated type. This is a concession to backwards compatibility; see comment at the end of the fn for details.

Example:

impl<T> Trait<Foo> for Bar { ... }
//   ^ T does not appear in `Foo` or `Bar`, error!

impl<T> Trait<Foo<T>> for Bar { ... }
//   ^ T appears in `Foo<T>`, ok.

impl<T> Trait<Foo> for Bar where Bar: Iterator<Item = T> { ... }
//   ^ T is bound to `<Bar as Iterator>::Item`, ok.

impl<'a> Trait<Foo> for Bar { }
//   ^ 'a is unused, but for back-compat we allow it

impl<'a> Trait<Foo> for Bar { type X = &'a i32; }
//   ^ 'a is unused and appears in assoc type, error