rustc_hir_analysis/check/
wfcheck.rs

1use std::cell::LazyCell;
2use std::ops::{ControlFlow, Deref};
3
4use hir::intravisit::{self, Visitor};
5use rustc_abi::ExternAbi;
6use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
7use rustc_errors::codes::*;
8use rustc_errors::{Applicability, ErrorGuaranteed, pluralize, struct_span_code_err};
9use rustc_hir::def::{DefKind, Res};
10use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
11use rustc_hir::lang_items::LangItem;
12use rustc_hir::{AmbigArg, ItemKind};
13use rustc_infer::infer::outlives::env::OutlivesEnvironment;
14use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt};
15use rustc_lint_defs::builtin::SUPERTRAIT_ITEM_SHADOWING_DEFINITION;
16use rustc_macros::LintDiagnostic;
17use rustc_middle::mir::interpret::ErrorHandled;
18use rustc_middle::query::Providers;
19use rustc_middle::traits::solve::NoSolution;
20use rustc_middle::ty::trait_def::TraitSpecializationKind;
21use rustc_middle::ty::{
22    self, AdtKind, GenericArgKind, GenericArgs, GenericParamDefKind, Ty, TyCtxt, TypeFlags,
23    TypeFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode,
24    Upcast,
25};
26use rustc_middle::{bug, span_bug};
27use rustc_session::parse::feature_err;
28use rustc_span::{DUMMY_SP, Ident, Span, sym};
29use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
30use rustc_trait_selection::regions::{InferCtxtRegionExt, OutlivesEnvironmentBuildExt};
31use rustc_trait_selection::traits::misc::{
32    ConstParamTyImplementationError, type_allowed_to_implement_const_param_ty,
33};
34use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
35use rustc_trait_selection::traits::{
36    self, FulfillmentError, Obligation, ObligationCause, ObligationCauseCode, ObligationCtxt,
37    WellFormedLoc,
38};
39use tracing::{debug, instrument};
40use {rustc_ast as ast, rustc_hir as hir};
41
42use crate::autoderef::Autoderef;
43use crate::collect::CollectItemTypesVisitor;
44use crate::constrained_generic_params::{Parameter, identify_constrained_generic_params};
45use crate::errors::InvalidReceiverTyHint;
46use crate::{errors, fluent_generated as fluent};
47
48pub(super) struct WfCheckingCtxt<'a, 'tcx> {
49    pub(super) ocx: ObligationCtxt<'a, 'tcx, FulfillmentError<'tcx>>,
50    span: Span,
51    body_def_id: LocalDefId,
52    param_env: ty::ParamEnv<'tcx>,
53}
54impl<'a, 'tcx> Deref for WfCheckingCtxt<'a, 'tcx> {
55    type Target = ObligationCtxt<'a, 'tcx, FulfillmentError<'tcx>>;
56    fn deref(&self) -> &Self::Target {
57        &self.ocx
58    }
59}
60
61impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
62    fn tcx(&self) -> TyCtxt<'tcx> {
63        self.ocx.infcx.tcx
64    }
65
66    // Convenience function to normalize during wfcheck. This performs
67    // `ObligationCtxt::normalize`, but provides a nice `ObligationCauseCode`.
68    fn normalize<T>(&self, span: Span, loc: Option<WellFormedLoc>, value: T) -> T
69    where
70        T: TypeFoldable<TyCtxt<'tcx>>,
71    {
72        self.ocx.normalize(
73            &ObligationCause::new(span, self.body_def_id, ObligationCauseCode::WellFormed(loc)),
74            self.param_env,
75            value,
76        )
77    }
78
79    fn register_wf_obligation(
80        &self,
81        span: Span,
82        loc: Option<WellFormedLoc>,
83        arg: ty::GenericArg<'tcx>,
84    ) {
85        let cause = traits::ObligationCause::new(
86            span,
87            self.body_def_id,
88            ObligationCauseCode::WellFormed(loc),
89        );
90        self.ocx.register_obligation(Obligation::new(
91            self.tcx(),
92            cause,
93            self.param_env,
94            ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg))),
95        ));
96    }
97}
98
99pub(super) fn enter_wf_checking_ctxt<'tcx, F>(
100    tcx: TyCtxt<'tcx>,
101    span: Span,
102    body_def_id: LocalDefId,
103    f: F,
104) -> Result<(), ErrorGuaranteed>
105where
106    F: for<'a> FnOnce(&WfCheckingCtxt<'a, 'tcx>) -> Result<(), ErrorGuaranteed>,
107{
108    let param_env = tcx.param_env(body_def_id);
109    let infcx = &tcx.infer_ctxt().build(TypingMode::non_body_analysis());
110    let ocx = ObligationCtxt::new_with_diagnostics(infcx);
111
112    let mut wfcx = WfCheckingCtxt { ocx, span, body_def_id, param_env };
113
114    if !tcx.features().trivial_bounds() {
115        wfcx.check_false_global_bounds()
116    }
117    f(&mut wfcx)?;
118
119    let errors = wfcx.select_all_or_error();
120    if !errors.is_empty() {
121        return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
122    }
123
124    let assumed_wf_types = wfcx.ocx.assumed_wf_types_and_report_errors(param_env, body_def_id)?;
125    debug!(?assumed_wf_types);
126
127    let infcx_compat = infcx.fork();
128
129    // We specifically want to *disable* the implied bounds hack, first,
130    // so we can detect when failures are due to bevy's implied bounds.
131    let outlives_env = OutlivesEnvironment::new_with_implied_bounds_compat(
132        &infcx,
133        body_def_id,
134        param_env,
135        assumed_wf_types.iter().copied(),
136        true,
137    );
138
139    lint_redundant_lifetimes(tcx, body_def_id, &outlives_env);
140
141    let errors = infcx.resolve_regions_with_outlives_env(&outlives_env);
142    if errors.is_empty() {
143        return Ok(());
144    }
145
146    let outlives_env = OutlivesEnvironment::new_with_implied_bounds_compat(
147        &infcx_compat,
148        body_def_id,
149        param_env,
150        assumed_wf_types,
151        // Don't *disable* the implied bounds hack; though this will only apply
152        // the implied bounds hack if this contains `bevy_ecs`'s `ParamSet` type.
153        false,
154    );
155    let errors_compat = infcx_compat.resolve_regions_with_outlives_env(&outlives_env);
156    if errors_compat.is_empty() {
157        // FIXME: Once we fix bevy, this would be the place to insert a warning
158        // to upgrade bevy.
159        Ok(())
160    } else {
161        Err(infcx_compat.err_ctxt().report_region_errors(body_def_id, &errors_compat))
162    }
163}
164
165fn check_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
166    let node = tcx.hir_node_by_def_id(def_id);
167    let mut res = match node {
168        hir::Node::Crate(_) => bug!("check_well_formed cannot be applied to the crate root"),
169        hir::Node::Item(item) => check_item(tcx, item),
170        hir::Node::TraitItem(item) => check_trait_item(tcx, item),
171        hir::Node::ImplItem(item) => check_impl_item(tcx, item),
172        hir::Node::ForeignItem(item) => check_foreign_item(tcx, item),
173        hir::Node::OpaqueTy(_) => Ok(crate::check::check::check_item_type(tcx, def_id)),
174        _ => unreachable!("{node:?}"),
175    };
176
177    if let Some(generics) = node.generics() {
178        for param in generics.params {
179            res = res.and(check_param_wf(tcx, param));
180        }
181    }
182
183    res
184}
185
186/// Checks that the field types (in a struct def'n) or argument types (in an enum def'n) are
187/// well-formed, meaning that they do not require any constraints not declared in the struct
188/// definition itself. For example, this definition would be illegal:
189///
190/// ```rust
191/// struct Ref<'a, T> { x: &'a T }
192/// ```
193///
194/// because the type did not declare that `T:'a`.
195///
196/// We do this check as a pre-pass before checking fn bodies because if these constraints are
197/// not included it frequently leads to confusing errors in fn bodies. So it's better to check
198/// the types first.
199#[instrument(skip(tcx), level = "debug")]
200fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<(), ErrorGuaranteed> {
201    let def_id = item.owner_id.def_id;
202
203    debug!(
204        ?item.owner_id,
205        item.name = ? tcx.def_path_str(def_id)
206    );
207    CollectItemTypesVisitor { tcx }.visit_item(item);
208
209    let res = match item.kind {
210        // Right now we check that every default trait implementation
211        // has an implementation of itself. Basically, a case like:
212        //
213        //     impl Trait for T {}
214        //
215        // has a requirement of `T: Trait` which was required for default
216        // method implementations. Although this could be improved now that
217        // there's a better infrastructure in place for this, it's being left
218        // for a follow-up work.
219        //
220        // Since there's such a requirement, we need to check *just* positive
221        // implementations, otherwise things like:
222        //
223        //     impl !Send for T {}
224        //
225        // won't be allowed unless there's an *explicit* implementation of `Send`
226        // for `T`
227        hir::ItemKind::Impl(impl_) => {
228            let header = tcx.impl_trait_header(def_id);
229            let is_auto = header
230                .is_some_and(|header| tcx.trait_is_auto(header.trait_ref.skip_binder().def_id));
231
232            crate::impl_wf_check::check_impl_wf(tcx, def_id)?;
233            let mut res = Ok(());
234            if let (hir::Defaultness::Default { .. }, true) = (impl_.defaultness, is_auto) {
235                let sp = impl_.of_trait.as_ref().map_or(item.span, |t| t.path.span);
236                res = Err(tcx
237                    .dcx()
238                    .struct_span_err(sp, "impls of auto traits cannot be default")
239                    .with_span_labels(impl_.defaultness_span, "default because of this")
240                    .with_span_label(sp, "auto trait")
241                    .emit());
242            }
243            // We match on both `ty::ImplPolarity` and `ast::ImplPolarity` just to get the `!` span.
244            match header.map(|h| h.polarity) {
245                // `None` means this is an inherent impl
246                Some(ty::ImplPolarity::Positive) | None => {
247                    res = res.and(check_impl(tcx, item, impl_.self_ty, &impl_.of_trait));
248                }
249                Some(ty::ImplPolarity::Negative) => {
250                    let ast::ImplPolarity::Negative(span) = impl_.polarity else {
251                        bug!("impl_polarity query disagrees with impl's polarity in HIR");
252                    };
253                    // FIXME(#27579): what amount of WF checking do we need for neg impls?
254                    if let hir::Defaultness::Default { .. } = impl_.defaultness {
255                        let mut spans = vec![span];
256                        spans.extend(impl_.defaultness_span);
257                        res = Err(struct_span_code_err!(
258                            tcx.dcx(),
259                            spans,
260                            E0750,
261                            "negative impls cannot be default impls"
262                        )
263                        .emit());
264                    }
265                }
266                Some(ty::ImplPolarity::Reservation) => {
267                    // FIXME: what amount of WF checking do we need for reservation impls?
268                }
269            }
270            res
271        }
272        hir::ItemKind::Fn { ident, sig, .. } => {
273            check_item_fn(tcx, def_id, ident, item.span, sig.decl)
274        }
275        hir::ItemKind::Static(_, ty, ..) => {
276            check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
277        }
278        hir::ItemKind::Const(_, ty, ..) => {
279            check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
280        }
281        hir::ItemKind::Struct(_, _, hir_generics) => {
282            let res = check_type_defn(tcx, item, false);
283            check_variances_for_type_defn(tcx, item, hir_generics);
284            res
285        }
286        hir::ItemKind::Union(_, _, hir_generics) => {
287            let res = check_type_defn(tcx, item, true);
288            check_variances_for_type_defn(tcx, item, hir_generics);
289            res
290        }
291        hir::ItemKind::Enum(_, _, hir_generics) => {
292            let res = check_type_defn(tcx, item, true);
293            check_variances_for_type_defn(tcx, item, hir_generics);
294            res
295        }
296        hir::ItemKind::Trait(..) => check_trait(tcx, item),
297        hir::ItemKind::TraitAlias(..) => check_trait(tcx, item),
298        // `ForeignItem`s are handled separately.
299        hir::ItemKind::ForeignMod { .. } => Ok(()),
300        hir::ItemKind::TyAlias(_, hir_ty, hir_generics)
301            if tcx.type_alias_is_lazy(item.owner_id) =>
302        {
303            let res = enter_wf_checking_ctxt(tcx, item.span, def_id, |wfcx| {
304                let ty = tcx.type_of(def_id).instantiate_identity();
305                let item_ty = wfcx.normalize(hir_ty.span, Some(WellFormedLoc::Ty(def_id)), ty);
306                wfcx.register_wf_obligation(
307                    hir_ty.span,
308                    Some(WellFormedLoc::Ty(def_id)),
309                    item_ty.into(),
310                );
311                check_where_clauses(wfcx, item.span, def_id);
312                Ok(())
313            });
314            check_variances_for_type_defn(tcx, item, hir_generics);
315            res
316        }
317        _ => Ok(()),
318    };
319
320    crate::check::check::check_item_type(tcx, def_id);
321
322    res
323}
324
325fn check_foreign_item<'tcx>(
326    tcx: TyCtxt<'tcx>,
327    item: &'tcx hir::ForeignItem<'tcx>,
328) -> Result<(), ErrorGuaranteed> {
329    let def_id = item.owner_id.def_id;
330
331    CollectItemTypesVisitor { tcx }.visit_foreign_item(item);
332
333    debug!(
334        ?item.owner_id,
335        item.name = ? tcx.def_path_str(def_id)
336    );
337
338    match item.kind {
339        hir::ForeignItemKind::Fn(sig, ..) => {
340            check_item_fn(tcx, def_id, item.ident, item.span, sig.decl)
341        }
342        hir::ForeignItemKind::Static(ty, ..) => {
343            check_item_type(tcx, def_id, ty.span, UnsizedHandling::AllowIfForeignTail)
344        }
345        hir::ForeignItemKind::Type => Ok(()),
346    }
347}
348
349fn check_trait_item<'tcx>(
350    tcx: TyCtxt<'tcx>,
351    trait_item: &'tcx hir::TraitItem<'tcx>,
352) -> Result<(), ErrorGuaranteed> {
353    let def_id = trait_item.owner_id.def_id;
354
355    CollectItemTypesVisitor { tcx }.visit_trait_item(trait_item);
356
357    let (method_sig, span) = match trait_item.kind {
358        hir::TraitItemKind::Fn(ref sig, _) => (Some(sig), trait_item.span),
359        hir::TraitItemKind::Type(_bounds, Some(ty)) => (None, ty.span),
360        _ => (None, trait_item.span),
361    };
362
363    check_dyn_incompatible_self_trait_by_name(tcx, trait_item);
364
365    // Check that an item definition in a subtrait is shadowing a supertrait item.
366    lint_item_shadowing_supertrait_item(tcx, def_id);
367
368    let mut res = check_associated_item(tcx, def_id, span, method_sig);
369
370    if matches!(trait_item.kind, hir::TraitItemKind::Fn(..)) {
371        for &assoc_ty_def_id in tcx.associated_types_for_impl_traits_in_associated_fn(def_id) {
372            res = res.and(check_associated_item(
373                tcx,
374                assoc_ty_def_id.expect_local(),
375                tcx.def_span(assoc_ty_def_id),
376                None,
377            ));
378        }
379    }
380    res
381}
382
383/// Require that the user writes where clauses on GATs for the implicit
384/// outlives bounds involving trait parameters in trait functions and
385/// lifetimes passed as GAT args. See `self-outlives-lint` test.
386///
387/// We use the following trait as an example throughout this function:
388/// ```rust,ignore (this code fails due to this lint)
389/// trait IntoIter {
390///     type Iter<'a>: Iterator<Item = Self::Item<'a>>;
391///     type Item<'a>;
392///     fn into_iter<'a>(&'a self) -> Self::Iter<'a>;
393/// }
394/// ```
395fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
396    // Associates every GAT's def_id to a list of possibly missing bounds detected by this lint.
397    let mut required_bounds_by_item = FxIndexMap::default();
398    let associated_items = tcx.associated_items(trait_def_id);
399
400    // Loop over all GATs together, because if this lint suggests adding a where-clause bound
401    // to one GAT, it might then require us to an additional bound on another GAT.
402    // In our `IntoIter` example, we discover a missing `Self: 'a` bound on `Iter<'a>`, which
403    // then in a second loop adds a `Self: 'a` bound to `Item` due to the relationship between
404    // those GATs.
405    loop {
406        let mut should_continue = false;
407        for gat_item in associated_items.in_definition_order() {
408            let gat_def_id = gat_item.def_id.expect_local();
409            let gat_item = tcx.associated_item(gat_def_id);
410            // If this item is not an assoc ty, or has no args, then it's not a GAT
411            if gat_item.kind != ty::AssocKind::Type {
412                continue;
413            }
414            let gat_generics = tcx.generics_of(gat_def_id);
415            // FIXME(jackh726): we can also warn in the more general case
416            if gat_generics.is_own_empty() {
417                continue;
418            }
419
420            // Gather the bounds with which all other items inside of this trait constrain the GAT.
421            // This is calculated by taking the intersection of the bounds that each item
422            // constrains the GAT with individually.
423            let mut new_required_bounds: Option<FxIndexSet<ty::Clause<'_>>> = None;
424            for item in associated_items.in_definition_order() {
425                let item_def_id = item.def_id.expect_local();
426                // Skip our own GAT, since it does not constrain itself at all.
427                if item_def_id == gat_def_id {
428                    continue;
429                }
430
431                let param_env = tcx.param_env(item_def_id);
432
433                let item_required_bounds = match tcx.associated_item(item_def_id).kind {
434                    // In our example, this corresponds to `into_iter` method
435                    ty::AssocKind::Fn => {
436                        // For methods, we check the function signature's return type for any GATs
437                        // to constrain. In the `into_iter` case, we see that the return type
438                        // `Self::Iter<'a>` is a GAT we want to gather any potential missing bounds from.
439                        let sig: ty::FnSig<'_> = tcx.liberate_late_bound_regions(
440                            item_def_id.to_def_id(),
441                            tcx.fn_sig(item_def_id).instantiate_identity(),
442                        );
443                        gather_gat_bounds(
444                            tcx,
445                            param_env,
446                            item_def_id,
447                            sig.inputs_and_output,
448                            // We also assume that all of the function signature's parameter types
449                            // are well formed.
450                            &sig.inputs().iter().copied().collect(),
451                            gat_def_id,
452                            gat_generics,
453                        )
454                    }
455                    // In our example, this corresponds to the `Iter` and `Item` associated types
456                    ty::AssocKind::Type => {
457                        // If our associated item is a GAT with missing bounds, add them to
458                        // the param-env here. This allows this GAT to propagate missing bounds
459                        // to other GATs.
460                        let param_env = augment_param_env(
461                            tcx,
462                            param_env,
463                            required_bounds_by_item.get(&item_def_id),
464                        );
465                        gather_gat_bounds(
466                            tcx,
467                            param_env,
468                            item_def_id,
469                            tcx.explicit_item_bounds(item_def_id)
470                                .iter_identity_copied()
471                                .collect::<Vec<_>>(),
472                            &FxIndexSet::default(),
473                            gat_def_id,
474                            gat_generics,
475                        )
476                    }
477                    ty::AssocKind::Const => None,
478                };
479
480                if let Some(item_required_bounds) = item_required_bounds {
481                    // Take the intersection of the required bounds for this GAT, and
482                    // the item_required_bounds which are the ones implied by just
483                    // this item alone.
484                    // This is why we use an Option<_>, since we need to distinguish
485                    // the empty set of bounds from the _uninitialized_ set of bounds.
486                    if let Some(new_required_bounds) = &mut new_required_bounds {
487                        new_required_bounds.retain(|b| item_required_bounds.contains(b));
488                    } else {
489                        new_required_bounds = Some(item_required_bounds);
490                    }
491                }
492            }
493
494            if let Some(new_required_bounds) = new_required_bounds {
495                let required_bounds = required_bounds_by_item.entry(gat_def_id).or_default();
496                if new_required_bounds.into_iter().any(|p| required_bounds.insert(p)) {
497                    // Iterate until our required_bounds no longer change
498                    // Since they changed here, we should continue the loop
499                    should_continue = true;
500                }
501            }
502        }
503        // We know that this loop will eventually halt, since we only set `should_continue` if the
504        // `required_bounds` for this item grows. Since we are not creating any new region or type
505        // variables, the set of all region and type bounds that we could ever insert are limited
506        // by the number of unique types and regions we observe in a given item.
507        if !should_continue {
508            break;
509        }
510    }
511
512    for (gat_def_id, required_bounds) in required_bounds_by_item {
513        // Don't suggest adding `Self: 'a` to a GAT that can't be named
514        if tcx.is_impl_trait_in_trait(gat_def_id.to_def_id()) {
515            continue;
516        }
517
518        let gat_item_hir = tcx.hir_expect_trait_item(gat_def_id);
519        debug!(?required_bounds);
520        let param_env = tcx.param_env(gat_def_id);
521
522        let unsatisfied_bounds: Vec<_> = required_bounds
523            .into_iter()
524            .filter(|clause| match clause.kind().skip_binder() {
525                ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => {
526                    !region_known_to_outlive(
527                        tcx,
528                        gat_def_id,
529                        param_env,
530                        &FxIndexSet::default(),
531                        a,
532                        b,
533                    )
534                }
535                ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(a, b)) => {
536                    !ty_known_to_outlive(tcx, gat_def_id, param_env, &FxIndexSet::default(), a, b)
537                }
538                _ => bug!("Unexpected ClauseKind"),
539            })
540            .map(|clause| clause.to_string())
541            .collect();
542
543        if !unsatisfied_bounds.is_empty() {
544            let plural = pluralize!(unsatisfied_bounds.len());
545            let suggestion = format!(
546                "{} {}",
547                gat_item_hir.generics.add_where_or_trailing_comma(),
548                unsatisfied_bounds.join(", "),
549            );
550            let bound =
551                if unsatisfied_bounds.len() > 1 { "these bounds are" } else { "this bound is" };
552            tcx.dcx()
553                .struct_span_err(
554                    gat_item_hir.span,
555                    format!("missing required bound{} on `{}`", plural, gat_item_hir.ident),
556                )
557                .with_span_suggestion(
558                    gat_item_hir.generics.tail_span_for_predicate_suggestion(),
559                    format!("add the required where clause{plural}"),
560                    suggestion,
561                    Applicability::MachineApplicable,
562                )
563                .with_note(format!(
564                    "{bound} currently required to ensure that impls have maximum flexibility"
565                ))
566                .with_note(
567                    "we are soliciting feedback, see issue #87479 \
568                     <https://github.com/rust-lang/rust/issues/87479> for more information",
569                )
570                .emit();
571        }
572    }
573}
574
575/// Add a new set of predicates to the caller_bounds of an existing param_env.
576fn augment_param_env<'tcx>(
577    tcx: TyCtxt<'tcx>,
578    param_env: ty::ParamEnv<'tcx>,
579    new_predicates: Option<&FxIndexSet<ty::Clause<'tcx>>>,
580) -> ty::ParamEnv<'tcx> {
581    let Some(new_predicates) = new_predicates else {
582        return param_env;
583    };
584
585    if new_predicates.is_empty() {
586        return param_env;
587    }
588
589    let bounds = tcx.mk_clauses_from_iter(
590        param_env.caller_bounds().iter().chain(new_predicates.iter().cloned()),
591    );
592    // FIXME(compiler-errors): Perhaps there is a case where we need to normalize this
593    // i.e. traits::normalize_param_env_or_error
594    ty::ParamEnv::new(bounds)
595}
596
597/// We use the following trait as an example throughout this function.
598/// Specifically, let's assume that `to_check` here is the return type
599/// of `into_iter`, and the GAT we are checking this for is `Iter`.
600/// ```rust,ignore (this code fails due to this lint)
601/// trait IntoIter {
602///     type Iter<'a>: Iterator<Item = Self::Item<'a>>;
603///     type Item<'a>;
604///     fn into_iter<'a>(&'a self) -> Self::Iter<'a>;
605/// }
606/// ```
607fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
608    tcx: TyCtxt<'tcx>,
609    param_env: ty::ParamEnv<'tcx>,
610    item_def_id: LocalDefId,
611    to_check: T,
612    wf_tys: &FxIndexSet<Ty<'tcx>>,
613    gat_def_id: LocalDefId,
614    gat_generics: &'tcx ty::Generics,
615) -> Option<FxIndexSet<ty::Clause<'tcx>>> {
616    // The bounds we that we would require from `to_check`
617    let mut bounds = FxIndexSet::default();
618
619    let (regions, types) = GATArgsCollector::visit(gat_def_id.to_def_id(), to_check);
620
621    // If both regions and types are empty, then this GAT isn't in the
622    // set of types we are checking, and we shouldn't try to do clause analysis
623    // (particularly, doing so would end up with an empty set of clauses,
624    // since the current method would require none, and we take the
625    // intersection of requirements of all methods)
626    if types.is_empty() && regions.is_empty() {
627        return None;
628    }
629
630    for (region_a, region_a_idx) in &regions {
631        // Ignore `'static` lifetimes for the purpose of this lint: it's
632        // because we know it outlives everything and so doesn't give meaningful
633        // clues. Also ignore `ReError`, to avoid knock-down errors.
634        if let ty::ReStatic | ty::ReError(_) = **region_a {
635            continue;
636        }
637        // For each region argument (e.g., `'a` in our example), check for a
638        // relationship to the type arguments (e.g., `Self`). If there is an
639        // outlives relationship (`Self: 'a`), then we want to ensure that is
640        // reflected in a where clause on the GAT itself.
641        for (ty, ty_idx) in &types {
642            // In our example, requires that `Self: 'a`
643            if ty_known_to_outlive(tcx, item_def_id, param_env, wf_tys, *ty, *region_a) {
644                debug!(?ty_idx, ?region_a_idx);
645                debug!("required clause: {ty} must outlive {region_a}");
646                // Translate into the generic parameters of the GAT. In
647                // our example, the type was `Self`, which will also be
648                // `Self` in the GAT.
649                let ty_param = gat_generics.param_at(*ty_idx, tcx);
650                let ty_param = Ty::new_param(tcx, ty_param.index, ty_param.name);
651                // Same for the region. In our example, 'a corresponds
652                // to the 'me parameter.
653                let region_param = gat_generics.param_at(*region_a_idx, tcx);
654                let region_param = ty::Region::new_early_param(
655                    tcx,
656                    ty::EarlyParamRegion { index: region_param.index, name: region_param.name },
657                );
658                // The predicate we expect to see. (In our example,
659                // `Self: 'me`.)
660                bounds.insert(
661                    ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty_param, region_param))
662                        .upcast(tcx),
663                );
664            }
665        }
666
667        // For each region argument (e.g., `'a` in our example), also check for a
668        // relationship to the other region arguments. If there is an outlives
669        // relationship, then we want to ensure that is reflected in the where clause
670        // on the GAT itself.
671        for (region_b, region_b_idx) in &regions {
672            // Again, skip `'static` because it outlives everything. Also, we trivially
673            // know that a region outlives itself. Also ignore `ReError`, to avoid
674            // knock-down errors.
675            if matches!(**region_b, ty::ReStatic | ty::ReError(_)) || region_a == region_b {
676                continue;
677            }
678            if region_known_to_outlive(tcx, item_def_id, param_env, wf_tys, *region_a, *region_b) {
679                debug!(?region_a_idx, ?region_b_idx);
680                debug!("required clause: {region_a} must outlive {region_b}");
681                // Translate into the generic parameters of the GAT.
682                let region_a_param = gat_generics.param_at(*region_a_idx, tcx);
683                let region_a_param = ty::Region::new_early_param(
684                    tcx,
685                    ty::EarlyParamRegion { index: region_a_param.index, name: region_a_param.name },
686                );
687                // Same for the region.
688                let region_b_param = gat_generics.param_at(*region_b_idx, tcx);
689                let region_b_param = ty::Region::new_early_param(
690                    tcx,
691                    ty::EarlyParamRegion { index: region_b_param.index, name: region_b_param.name },
692                );
693                // The predicate we expect to see.
694                bounds.insert(
695                    ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(
696                        region_a_param,
697                        region_b_param,
698                    ))
699                    .upcast(tcx),
700                );
701            }
702        }
703    }
704
705    Some(bounds)
706}
707
708/// Given a known `param_env` and a set of well formed types, can we prove that
709/// `ty` outlives `region`.
710fn ty_known_to_outlive<'tcx>(
711    tcx: TyCtxt<'tcx>,
712    id: LocalDefId,
713    param_env: ty::ParamEnv<'tcx>,
714    wf_tys: &FxIndexSet<Ty<'tcx>>,
715    ty: Ty<'tcx>,
716    region: ty::Region<'tcx>,
717) -> bool {
718    test_region_obligations(tcx, id, param_env, wf_tys, |infcx| {
719        infcx.register_region_obligation(infer::RegionObligation {
720            sub_region: region,
721            sup_type: ty,
722            origin: infer::RelateParamBound(DUMMY_SP, ty, None),
723        });
724    })
725}
726
727/// Given a known `param_env` and a set of well formed types, can we prove that
728/// `region_a` outlives `region_b`
729fn region_known_to_outlive<'tcx>(
730    tcx: TyCtxt<'tcx>,
731    id: LocalDefId,
732    param_env: ty::ParamEnv<'tcx>,
733    wf_tys: &FxIndexSet<Ty<'tcx>>,
734    region_a: ty::Region<'tcx>,
735    region_b: ty::Region<'tcx>,
736) -> bool {
737    test_region_obligations(tcx, id, param_env, wf_tys, |infcx| {
738        infcx.sub_regions(infer::RelateRegionParamBound(DUMMY_SP, None), region_b, region_a);
739    })
740}
741
742/// Given a known `param_env` and a set of well formed types, set up an
743/// `InferCtxt`, call the passed function (to e.g. set up region constraints
744/// to be tested), then resolve region and return errors
745fn test_region_obligations<'tcx>(
746    tcx: TyCtxt<'tcx>,
747    id: LocalDefId,
748    param_env: ty::ParamEnv<'tcx>,
749    wf_tys: &FxIndexSet<Ty<'tcx>>,
750    add_constraints: impl FnOnce(&InferCtxt<'tcx>),
751) -> bool {
752    // Unfortunately, we have to use a new `InferCtxt` each call, because
753    // region constraints get added and solved there and we need to test each
754    // call individually.
755    let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
756
757    add_constraints(&infcx);
758
759    let errors = infcx.resolve_regions(id, param_env, wf_tys.iter().copied());
760    debug!(?errors, "errors");
761
762    // If we were able to prove that the type outlives the region without
763    // an error, it must be because of the implied or explicit bounds...
764    errors.is_empty()
765}
766
767/// TypeVisitor that looks for uses of GATs like
768/// `<P0 as Trait<P1..Pn>>::GAT<Pn..Pm>` and adds the arguments `P0..Pm` into
769/// the two vectors, `regions` and `types` (depending on their kind). For each
770/// parameter `Pi` also track the index `i`.
771struct GATArgsCollector<'tcx> {
772    gat: DefId,
773    // Which region appears and which parameter index its instantiated with
774    regions: FxIndexSet<(ty::Region<'tcx>, usize)>,
775    // Which params appears and which parameter index its instantiated with
776    types: FxIndexSet<(Ty<'tcx>, usize)>,
777}
778
779impl<'tcx> GATArgsCollector<'tcx> {
780    fn visit<T: TypeFoldable<TyCtxt<'tcx>>>(
781        gat: DefId,
782        t: T,
783    ) -> (FxIndexSet<(ty::Region<'tcx>, usize)>, FxIndexSet<(Ty<'tcx>, usize)>) {
784        let mut visitor =
785            GATArgsCollector { gat, regions: FxIndexSet::default(), types: FxIndexSet::default() };
786        t.visit_with(&mut visitor);
787        (visitor.regions, visitor.types)
788    }
789}
790
791impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GATArgsCollector<'tcx> {
792    fn visit_ty(&mut self, t: Ty<'tcx>) {
793        match t.kind() {
794            ty::Alias(ty::Projection, p) if p.def_id == self.gat => {
795                for (idx, arg) in p.args.iter().enumerate() {
796                    match arg.unpack() {
797                        GenericArgKind::Lifetime(lt) if !lt.is_bound() => {
798                            self.regions.insert((lt, idx));
799                        }
800                        GenericArgKind::Type(t) => {
801                            self.types.insert((t, idx));
802                        }
803                        _ => {}
804                    }
805                }
806            }
807            _ => {}
808        }
809        t.super_visit_with(self)
810    }
811}
812
813fn could_be_self(trait_def_id: LocalDefId, ty: &hir::Ty<'_>) -> bool {
814    match ty.kind {
815        hir::TyKind::TraitObject([trait_ref], ..) => match trait_ref.trait_ref.path.segments {
816            [s] => s.res.opt_def_id() == Some(trait_def_id.to_def_id()),
817            _ => false,
818        },
819        _ => false,
820    }
821}
822
823/// Detect when a dyn-incompatible trait is referring to itself in one of its associated items.
824///
825/// In such cases, suggest using `Self` instead.
826fn check_dyn_incompatible_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem<'_>) {
827    let (trait_ident, trait_def_id) =
828        match tcx.hir_node_by_def_id(tcx.hir_get_parent_item(item.hir_id()).def_id) {
829            hir::Node::Item(item) => match item.kind {
830                hir::ItemKind::Trait(_, _, ident, ..) => (ident, item.owner_id),
831                _ => return,
832            },
833            _ => return,
834        };
835    let mut trait_should_be_self = vec![];
836    match &item.kind {
837        hir::TraitItemKind::Const(ty, _) | hir::TraitItemKind::Type(_, Some(ty))
838            if could_be_self(trait_def_id.def_id, ty) =>
839        {
840            trait_should_be_self.push(ty.span)
841        }
842        hir::TraitItemKind::Fn(sig, _) => {
843            for ty in sig.decl.inputs {
844                if could_be_self(trait_def_id.def_id, ty) {
845                    trait_should_be_self.push(ty.span);
846                }
847            }
848            match sig.decl.output {
849                hir::FnRetTy::Return(ty) if could_be_self(trait_def_id.def_id, ty) => {
850                    trait_should_be_self.push(ty.span);
851                }
852                _ => {}
853            }
854        }
855        _ => {}
856    }
857    if !trait_should_be_self.is_empty() {
858        if tcx.is_dyn_compatible(trait_def_id) {
859            return;
860        }
861        let sugg = trait_should_be_self.iter().map(|span| (*span, "Self".to_string())).collect();
862        tcx.dcx()
863            .struct_span_err(
864                trait_should_be_self,
865                "associated item referring to unboxed trait object for its own trait",
866            )
867            .with_span_label(trait_ident.span, "in this trait")
868            .with_multipart_suggestion(
869                "you might have meant to use `Self` to refer to the implementing type",
870                sugg,
871                Applicability::MachineApplicable,
872            )
873            .emit();
874    }
875}
876
877fn lint_item_shadowing_supertrait_item<'tcx>(tcx: TyCtxt<'tcx>, trait_item_def_id: LocalDefId) {
878    let item_name = tcx.item_name(trait_item_def_id.to_def_id());
879    let trait_def_id = tcx.local_parent(trait_item_def_id);
880
881    let shadowed: Vec<_> = traits::supertrait_def_ids(tcx, trait_def_id.to_def_id())
882        .skip(1)
883        .flat_map(|supertrait_def_id| {
884            tcx.associated_items(supertrait_def_id).filter_by_name_unhygienic(item_name)
885        })
886        .collect();
887    if !shadowed.is_empty() {
888        let shadowee = if let [shadowed] = shadowed[..] {
889            errors::SupertraitItemShadowee::Labeled {
890                span: tcx.def_span(shadowed.def_id),
891                supertrait: tcx.item_name(shadowed.trait_container(tcx).unwrap()),
892            }
893        } else {
894            let (traits, spans): (Vec<_>, Vec<_>) = shadowed
895                .iter()
896                .map(|item| {
897                    (tcx.item_name(item.trait_container(tcx).unwrap()), tcx.def_span(item.def_id))
898                })
899                .unzip();
900            errors::SupertraitItemShadowee::Several { traits: traits.into(), spans: spans.into() }
901        };
902
903        tcx.emit_node_span_lint(
904            SUPERTRAIT_ITEM_SHADOWING_DEFINITION,
905            tcx.local_def_id_to_hir_id(trait_item_def_id),
906            tcx.def_span(trait_item_def_id),
907            errors::SupertraitItemShadowing {
908                item: item_name,
909                subtrait: tcx.item_name(trait_def_id.to_def_id()),
910                shadowee,
911            },
912        );
913    }
914}
915
916fn check_impl_item<'tcx>(
917    tcx: TyCtxt<'tcx>,
918    impl_item: &'tcx hir::ImplItem<'tcx>,
919) -> Result<(), ErrorGuaranteed> {
920    CollectItemTypesVisitor { tcx }.visit_impl_item(impl_item);
921
922    let (method_sig, span) = match impl_item.kind {
923        hir::ImplItemKind::Fn(ref sig, _) => (Some(sig), impl_item.span),
924        // Constrain binding and overflow error spans to `<Ty>` in `type foo = <Ty>`.
925        hir::ImplItemKind::Type(ty) if ty.span != DUMMY_SP => (None, ty.span),
926        _ => (None, impl_item.span),
927    };
928    check_associated_item(tcx, impl_item.owner_id.def_id, span, method_sig)
929}
930
931fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(), ErrorGuaranteed> {
932    match param.kind {
933        // We currently only check wf of const params here.
934        hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. } => Ok(()),
935
936        // Const parameters are well formed if their type is structural match.
937        hir::GenericParamKind::Const { ty: hir_ty, default: _, synthetic: _ } => {
938            let ty = tcx.type_of(param.def_id).instantiate_identity();
939
940            if tcx.features().unsized_const_params() {
941                enter_wf_checking_ctxt(tcx, hir_ty.span, tcx.local_parent(param.def_id), |wfcx| {
942                    wfcx.register_bound(
943                        ObligationCause::new(
944                            hir_ty.span,
945                            param.def_id,
946                            ObligationCauseCode::ConstParam(ty),
947                        ),
948                        wfcx.param_env,
949                        ty,
950                        tcx.require_lang_item(LangItem::UnsizedConstParamTy, Some(hir_ty.span)),
951                    );
952                    Ok(())
953                })
954            } else if tcx.features().adt_const_params() {
955                enter_wf_checking_ctxt(tcx, hir_ty.span, tcx.local_parent(param.def_id), |wfcx| {
956                    wfcx.register_bound(
957                        ObligationCause::new(
958                            hir_ty.span,
959                            param.def_id,
960                            ObligationCauseCode::ConstParam(ty),
961                        ),
962                        wfcx.param_env,
963                        ty,
964                        tcx.require_lang_item(LangItem::ConstParamTy, Some(hir_ty.span)),
965                    );
966                    Ok(())
967                })
968            } else {
969                let mut diag = match ty.kind() {
970                    ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Error(_) => return Ok(()),
971                    ty::FnPtr(..) => tcx.dcx().struct_span_err(
972                        hir_ty.span,
973                        "using function pointers as const generic parameters is forbidden",
974                    ),
975                    ty::RawPtr(_, _) => tcx.dcx().struct_span_err(
976                        hir_ty.span,
977                        "using raw pointers as const generic parameters is forbidden",
978                    ),
979                    _ => {
980                        // Avoid showing "{type error}" to users. See #118179.
981                        ty.error_reported()?;
982
983                        tcx.dcx().struct_span_err(
984                            hir_ty.span,
985                            format!(
986                                "`{ty}` is forbidden as the type of a const generic parameter",
987                            ),
988                        )
989                    }
990                };
991
992                diag.note("the only supported types are integers, `bool`, and `char`");
993
994                let cause = ObligationCause::misc(hir_ty.span, param.def_id);
995                let adt_const_params_feature_string =
996                    " more complex and user defined types".to_string();
997                let may_suggest_feature = match type_allowed_to_implement_const_param_ty(
998                    tcx,
999                    tcx.param_env(param.def_id),
1000                    ty,
1001                    LangItem::ConstParamTy,
1002                    cause,
1003                ) {
1004                    // Can never implement `ConstParamTy`, don't suggest anything.
1005                    Err(
1006                        ConstParamTyImplementationError::NotAnAdtOrBuiltinAllowed
1007                        | ConstParamTyImplementationError::InvalidInnerTyOfBuiltinTy(..),
1008                    ) => None,
1009                    Err(ConstParamTyImplementationError::UnsizedConstParamsFeatureRequired) => {
1010                        Some(vec![
1011                            (adt_const_params_feature_string, sym::adt_const_params),
1012                            (
1013                                " references to implement the `ConstParamTy` trait".into(),
1014                                sym::unsized_const_params,
1015                            ),
1016                        ])
1017                    }
1018                    // May be able to implement `ConstParamTy`. Only emit the feature help
1019                    // if the type is local, since the user may be able to fix the local type.
1020                    Err(ConstParamTyImplementationError::InfrigingFields(..)) => {
1021                        fn ty_is_local(ty: Ty<'_>) -> bool {
1022                            match ty.kind() {
1023                                ty::Adt(adt_def, ..) => adt_def.did().is_local(),
1024                                // Arrays and slices use the inner type's `ConstParamTy`.
1025                                ty::Array(ty, ..) => ty_is_local(*ty),
1026                                ty::Slice(ty) => ty_is_local(*ty),
1027                                // `&` references use the inner type's `ConstParamTy`.
1028                                // `&mut` are not supported.
1029                                ty::Ref(_, ty, ast::Mutability::Not) => ty_is_local(*ty),
1030                                // Say that a tuple is local if any of its components are local.
1031                                // This is not strictly correct, but it's likely that the user can fix the local component.
1032                                ty::Tuple(tys) => tys.iter().any(|ty| ty_is_local(ty)),
1033                                _ => false,
1034                            }
1035                        }
1036
1037                        ty_is_local(ty).then_some(vec![(
1038                            adt_const_params_feature_string,
1039                            sym::adt_const_params,
1040                        )])
1041                    }
1042                    // Implements `ConstParamTy`, suggest adding the feature to enable.
1043                    Ok(..) => Some(vec![(adt_const_params_feature_string, sym::adt_const_params)]),
1044                };
1045                if let Some(features) = may_suggest_feature {
1046                    tcx.disabled_nightly_features(&mut diag, Some(param.hir_id), features);
1047                }
1048
1049                Err(diag.emit())
1050            }
1051        }
1052    }
1053}
1054
1055#[instrument(level = "debug", skip(tcx, span, sig_if_method))]
1056fn check_associated_item(
1057    tcx: TyCtxt<'_>,
1058    item_id: LocalDefId,
1059    span: Span,
1060    sig_if_method: Option<&hir::FnSig<'_>>,
1061) -> Result<(), ErrorGuaranteed> {
1062    let loc = Some(WellFormedLoc::Ty(item_id));
1063    enter_wf_checking_ctxt(tcx, span, item_id, |wfcx| {
1064        let item = tcx.associated_item(item_id);
1065
1066        // Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
1067        // other `Foo` impls are incoherent.
1068        tcx.ensure_ok()
1069            .coherent_trait(tcx.parent(item.trait_item_def_id.unwrap_or(item_id.into())))?;
1070
1071        let self_ty = match item.container {
1072            ty::AssocItemContainer::Trait => tcx.types.self_param,
1073            ty::AssocItemContainer::Impl => {
1074                tcx.type_of(item.container_id(tcx)).instantiate_identity()
1075            }
1076        };
1077
1078        match item.kind {
1079            ty::AssocKind::Const => {
1080                let ty = tcx.type_of(item.def_id).instantiate_identity();
1081                let ty = wfcx.normalize(span, Some(WellFormedLoc::Ty(item_id)), ty);
1082                wfcx.register_wf_obligation(span, loc, ty.into());
1083                check_sized_if_body(
1084                    wfcx,
1085                    item.def_id.expect_local(),
1086                    ty,
1087                    Some(span),
1088                    ObligationCauseCode::SizedConstOrStatic,
1089                );
1090                Ok(())
1091            }
1092            ty::AssocKind::Fn => {
1093                let sig = tcx.fn_sig(item.def_id).instantiate_identity();
1094                let hir_sig = sig_if_method.expect("bad signature for method");
1095                check_fn_or_method(
1096                    wfcx,
1097                    item.ident(tcx).span,
1098                    sig,
1099                    hir_sig.decl,
1100                    item.def_id.expect_local(),
1101                );
1102                check_method_receiver(wfcx, hir_sig, item, self_ty)
1103            }
1104            ty::AssocKind::Type => {
1105                if let ty::AssocItemContainer::Trait = item.container {
1106                    check_associated_type_bounds(wfcx, item, span)
1107                }
1108                if item.defaultness(tcx).has_value() {
1109                    let ty = tcx.type_of(item.def_id).instantiate_identity();
1110                    let ty = wfcx.normalize(span, Some(WellFormedLoc::Ty(item_id)), ty);
1111                    wfcx.register_wf_obligation(span, loc, ty.into());
1112                }
1113                Ok(())
1114            }
1115        }
1116    })
1117}
1118
1119/// In a type definition, we check that to ensure that the types of the fields are well-formed.
1120fn check_type_defn<'tcx>(
1121    tcx: TyCtxt<'tcx>,
1122    item: &hir::Item<'tcx>,
1123    all_sized: bool,
1124) -> Result<(), ErrorGuaranteed> {
1125    let _ = tcx.representability(item.owner_id.def_id);
1126    let adt_def = tcx.adt_def(item.owner_id);
1127
1128    enter_wf_checking_ctxt(tcx, item.span, item.owner_id.def_id, |wfcx| {
1129        let variants = adt_def.variants();
1130        let packed = adt_def.repr().packed();
1131
1132        for variant in variants.iter() {
1133            // All field types must be well-formed.
1134            for field in &variant.fields {
1135                if let Some(def_id) = field.value
1136                    && let Some(_ty) = tcx.type_of(def_id).no_bound_vars()
1137                {
1138                    // FIXME(generic_const_exprs, default_field_values): this is a hack and needs to
1139                    // be refactored to check the instantiate-ability of the code better.
1140                    if let Some(def_id) = def_id.as_local()
1141                        && let hir::Node::AnonConst(anon) = tcx.hir_node_by_def_id(def_id)
1142                        && let expr = &tcx.hir_body(anon.body).value
1143                        && let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
1144                        && let Res::Def(DefKind::ConstParam, _def_id) = path.res
1145                    {
1146                        // Do not evaluate bare `const` params, as those would ICE and are only
1147                        // usable if `#![feature(generic_const_exprs)]` is enabled.
1148                    } else {
1149                        // Evaluate the constant proactively, to emit an error if the constant has
1150                        // an unconditional error. We only do so if the const has no type params.
1151                        let _ = tcx.const_eval_poly(def_id);
1152                    }
1153                }
1154                let field_id = field.did.expect_local();
1155                let hir::FieldDef { ty: hir_ty, .. } =
1156                    tcx.hir_node_by_def_id(field_id).expect_field();
1157                let ty = wfcx.normalize(
1158                    hir_ty.span,
1159                    None,
1160                    tcx.type_of(field.did).instantiate_identity(),
1161                );
1162                wfcx.register_wf_obligation(
1163                    hir_ty.span,
1164                    Some(WellFormedLoc::Ty(field_id)),
1165                    ty.into(),
1166                )
1167            }
1168
1169            // For DST, or when drop needs to copy things around, all
1170            // intermediate types must be sized.
1171            let needs_drop_copy = || {
1172                packed && {
1173                    let ty = tcx.type_of(variant.tail().did).instantiate_identity();
1174                    let ty = tcx.erase_regions(ty);
1175                    assert!(!ty.has_infer());
1176                    ty.needs_drop(tcx, wfcx.infcx.typing_env(wfcx.param_env))
1177                }
1178            };
1179            // All fields (except for possibly the last) should be sized.
1180            let all_sized = all_sized || variant.fields.is_empty() || needs_drop_copy();
1181            let unsized_len = if all_sized { 0 } else { 1 };
1182            for (idx, field) in
1183                variant.fields.raw[..variant.fields.len() - unsized_len].iter().enumerate()
1184            {
1185                let last = idx == variant.fields.len() - 1;
1186                let field_id = field.did.expect_local();
1187                let hir::FieldDef { ty: hir_ty, .. } =
1188                    tcx.hir_node_by_def_id(field_id).expect_field();
1189                let ty = wfcx.normalize(
1190                    hir_ty.span,
1191                    None,
1192                    tcx.type_of(field.did).instantiate_identity(),
1193                );
1194                wfcx.register_bound(
1195                    traits::ObligationCause::new(
1196                        hir_ty.span,
1197                        wfcx.body_def_id,
1198                        ObligationCauseCode::FieldSized {
1199                            adt_kind: match &item.kind {
1200                                ItemKind::Struct(..) => AdtKind::Struct,
1201                                ItemKind::Union(..) => AdtKind::Union,
1202                                ItemKind::Enum(..) => AdtKind::Enum,
1203                                kind => span_bug!(
1204                                    item.span,
1205                                    "should be wfchecking an ADT, got {kind:?}"
1206                                ),
1207                            },
1208                            span: hir_ty.span,
1209                            last,
1210                        },
1211                    ),
1212                    wfcx.param_env,
1213                    ty,
1214                    tcx.require_lang_item(LangItem::Sized, Some(hir_ty.span)),
1215                );
1216            }
1217
1218            // Explicit `enum` discriminant values must const-evaluate successfully.
1219            if let ty::VariantDiscr::Explicit(discr_def_id) = variant.discr {
1220                match tcx.const_eval_poly(discr_def_id) {
1221                    Ok(_) => {}
1222                    Err(ErrorHandled::Reported(..)) => {}
1223                    Err(ErrorHandled::TooGeneric(sp)) => {
1224                        span_bug!(sp, "enum variant discr was too generic to eval")
1225                    }
1226                }
1227            }
1228        }
1229
1230        check_where_clauses(wfcx, item.span, item.owner_id.def_id);
1231        Ok(())
1232    })
1233}
1234
1235#[instrument(skip(tcx, item))]
1236fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) -> Result<(), ErrorGuaranteed> {
1237    debug!(?item.owner_id);
1238
1239    let def_id = item.owner_id.def_id;
1240    let trait_def = tcx.trait_def(def_id);
1241    if trait_def.is_marker
1242        || matches!(trait_def.specialization_kind, TraitSpecializationKind::Marker)
1243    {
1244        for associated_def_id in &*tcx.associated_item_def_ids(def_id) {
1245            struct_span_code_err!(
1246                tcx.dcx(),
1247                tcx.def_span(*associated_def_id),
1248                E0714,
1249                "marker traits cannot have associated items",
1250            )
1251            .emit();
1252        }
1253    }
1254
1255    let res = enter_wf_checking_ctxt(tcx, item.span, def_id, |wfcx| {
1256        check_where_clauses(wfcx, item.span, def_id);
1257        Ok(())
1258    });
1259
1260    // Only check traits, don't check trait aliases
1261    if let hir::ItemKind::Trait(..) = item.kind {
1262        check_gat_where_clauses(tcx, item.owner_id.def_id);
1263    }
1264    res
1265}
1266
1267/// Checks all associated type defaults of trait `trait_def_id`.
1268///
1269/// Assuming the defaults are used, check that all predicates (bounds on the
1270/// assoc type and where clauses on the trait) hold.
1271fn check_associated_type_bounds(wfcx: &WfCheckingCtxt<'_, '_>, item: ty::AssocItem, span: Span) {
1272    let bounds = wfcx.tcx().explicit_item_bounds(item.def_id);
1273
1274    debug!("check_associated_type_bounds: bounds={:?}", bounds);
1275    let wf_obligations = bounds.iter_identity_copied().flat_map(|(bound, bound_span)| {
1276        let normalized_bound = wfcx.normalize(span, None, bound);
1277        traits::wf::clause_obligations(
1278            wfcx.infcx,
1279            wfcx.param_env,
1280            wfcx.body_def_id,
1281            normalized_bound,
1282            bound_span,
1283        )
1284    });
1285
1286    wfcx.register_obligations(wf_obligations);
1287}
1288
1289fn check_item_fn(
1290    tcx: TyCtxt<'_>,
1291    def_id: LocalDefId,
1292    ident: Ident,
1293    span: Span,
1294    decl: &hir::FnDecl<'_>,
1295) -> Result<(), ErrorGuaranteed> {
1296    enter_wf_checking_ctxt(tcx, span, def_id, |wfcx| {
1297        let sig = tcx.fn_sig(def_id).instantiate_identity();
1298        check_fn_or_method(wfcx, ident.span, sig, decl, def_id);
1299        Ok(())
1300    })
1301}
1302
1303enum UnsizedHandling {
1304    Forbid,
1305    AllowIfForeignTail,
1306}
1307
1308fn check_item_type(
1309    tcx: TyCtxt<'_>,
1310    item_id: LocalDefId,
1311    ty_span: Span,
1312    unsized_handling: UnsizedHandling,
1313) -> Result<(), ErrorGuaranteed> {
1314    debug!("check_item_type: {:?}", item_id);
1315
1316    enter_wf_checking_ctxt(tcx, ty_span, item_id, |wfcx| {
1317        let ty = tcx.type_of(item_id).instantiate_identity();
1318        let item_ty = wfcx.normalize(ty_span, Some(WellFormedLoc::Ty(item_id)), ty);
1319
1320        let forbid_unsized = match unsized_handling {
1321            UnsizedHandling::Forbid => true,
1322            UnsizedHandling::AllowIfForeignTail => {
1323                let tail =
1324                    tcx.struct_tail_for_codegen(item_ty, wfcx.infcx.typing_env(wfcx.param_env));
1325                !matches!(tail.kind(), ty::Foreign(_))
1326            }
1327        };
1328
1329        wfcx.register_wf_obligation(ty_span, Some(WellFormedLoc::Ty(item_id)), item_ty.into());
1330        if forbid_unsized {
1331            wfcx.register_bound(
1332                traits::ObligationCause::new(
1333                    ty_span,
1334                    wfcx.body_def_id,
1335                    ObligationCauseCode::SizedConstOrStatic,
1336                ),
1337                wfcx.param_env,
1338                item_ty,
1339                tcx.require_lang_item(LangItem::Sized, Some(ty_span)),
1340            );
1341        }
1342
1343        // Ensure that the end result is `Sync` in a non-thread local `static`.
1344        let should_check_for_sync = tcx.static_mutability(item_id.to_def_id())
1345            == Some(hir::Mutability::Not)
1346            && !tcx.is_foreign_item(item_id.to_def_id())
1347            && !tcx.is_thread_local_static(item_id.to_def_id());
1348
1349        if should_check_for_sync {
1350            wfcx.register_bound(
1351                traits::ObligationCause::new(
1352                    ty_span,
1353                    wfcx.body_def_id,
1354                    ObligationCauseCode::SharedStatic,
1355                ),
1356                wfcx.param_env,
1357                item_ty,
1358                tcx.require_lang_item(LangItem::Sync, Some(ty_span)),
1359            );
1360        }
1361        Ok(())
1362    })
1363}
1364
1365#[instrument(level = "debug", skip(tcx, hir_self_ty, hir_trait_ref))]
1366fn check_impl<'tcx>(
1367    tcx: TyCtxt<'tcx>,
1368    item: &'tcx hir::Item<'tcx>,
1369    hir_self_ty: &hir::Ty<'_>,
1370    hir_trait_ref: &Option<hir::TraitRef<'_>>,
1371) -> Result<(), ErrorGuaranteed> {
1372    enter_wf_checking_ctxt(tcx, item.span, item.owner_id.def_id, |wfcx| {
1373        match hir_trait_ref {
1374            Some(hir_trait_ref) => {
1375                // `#[rustc_reservation_impl]` impls are not real impls and
1376                // therefore don't need to be WF (the trait's `Self: Trait` predicate
1377                // won't hold).
1378                let trait_ref = tcx.impl_trait_ref(item.owner_id).unwrap().instantiate_identity();
1379                // Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
1380                // other `Foo` impls are incoherent.
1381                tcx.ensure_ok().coherent_trait(trait_ref.def_id)?;
1382                let trait_span = hir_trait_ref.path.span;
1383                let trait_ref = wfcx.normalize(
1384                    trait_span,
1385                    Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)),
1386                    trait_ref,
1387                );
1388                let trait_pred =
1389                    ty::TraitPredicate { trait_ref, polarity: ty::PredicatePolarity::Positive };
1390                let mut obligations = traits::wf::trait_obligations(
1391                    wfcx.infcx,
1392                    wfcx.param_env,
1393                    wfcx.body_def_id,
1394                    trait_pred,
1395                    trait_span,
1396                    item,
1397                );
1398                for obligation in &mut obligations {
1399                    if obligation.cause.span != trait_span {
1400                        // We already have a better span.
1401                        continue;
1402                    }
1403                    if let Some(pred) = obligation.predicate.as_trait_clause()
1404                        && pred.skip_binder().self_ty() == trait_ref.self_ty()
1405                    {
1406                        obligation.cause.span = hir_self_ty.span;
1407                    }
1408                    if let Some(pred) = obligation.predicate.as_projection_clause()
1409                        && pred.skip_binder().self_ty() == trait_ref.self_ty()
1410                    {
1411                        obligation.cause.span = hir_self_ty.span;
1412                    }
1413                }
1414
1415                // Ensure that the `~const` where clauses of the trait hold for the impl.
1416                if tcx.is_conditionally_const(item.owner_id.def_id) {
1417                    for (bound, _) in
1418                        tcx.const_conditions(trait_ref.def_id).instantiate(tcx, trait_ref.args)
1419                    {
1420                        let bound = wfcx.normalize(
1421                            item.span,
1422                            Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)),
1423                            bound,
1424                        );
1425                        wfcx.register_obligation(Obligation::new(
1426                            tcx,
1427                            ObligationCause::new(
1428                                hir_self_ty.span,
1429                                wfcx.body_def_id,
1430                                ObligationCauseCode::WellFormed(None),
1431                            ),
1432                            wfcx.param_env,
1433                            bound.to_host_effect_clause(tcx, ty::BoundConstness::Maybe),
1434                        ))
1435                    }
1436                }
1437
1438                debug!(?obligations);
1439                wfcx.register_obligations(obligations);
1440            }
1441            None => {
1442                let self_ty = tcx.type_of(item.owner_id).instantiate_identity();
1443                let self_ty = wfcx.normalize(
1444                    item.span,
1445                    Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)),
1446                    self_ty,
1447                );
1448                wfcx.register_wf_obligation(
1449                    hir_self_ty.span,
1450                    Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)),
1451                    self_ty.into(),
1452                );
1453            }
1454        }
1455
1456        check_where_clauses(wfcx, item.span, item.owner_id.def_id);
1457        Ok(())
1458    })
1459}
1460
1461/// Checks where-clauses and inline bounds that are declared on `def_id`.
1462#[instrument(level = "debug", skip(wfcx))]
1463fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id: LocalDefId) {
1464    let infcx = wfcx.infcx;
1465    let tcx = wfcx.tcx();
1466
1467    let predicates = tcx.predicates_of(def_id.to_def_id());
1468    let generics = tcx.generics_of(def_id);
1469
1470    // Check that concrete defaults are well-formed. See test `type-check-defaults.rs`.
1471    // For example, this forbids the declaration:
1472    //
1473    //     struct Foo<T = Vec<[u32]>> { .. }
1474    //
1475    // Here, the default `Vec<[u32]>` is not WF because `[u32]: Sized` does not hold.
1476    for param in &generics.own_params {
1477        if let Some(default) = param.default_value(tcx).map(ty::EarlyBinder::instantiate_identity) {
1478            // Ignore dependent defaults -- that is, where the default of one type
1479            // parameter includes another (e.g., `<T, U = T>`). In those cases, we can't
1480            // be sure if it will error or not as user might always specify the other.
1481            // FIXME(generic_const_exprs): This is incorrect when dealing with unused const params.
1482            // E.g: `struct Foo<const N: usize, const M: usize = { 1 - 2 }>;`. Here, we should
1483            // eagerly error but we don't as we have `ConstKind::Unevaluated(.., [N, M])`.
1484            if !default.has_param() {
1485                wfcx.register_wf_obligation(
1486                    tcx.def_span(param.def_id),
1487                    matches!(param.kind, GenericParamDefKind::Type { .. })
1488                        .then(|| WellFormedLoc::Ty(param.def_id.expect_local())),
1489                    default,
1490                );
1491            }
1492        }
1493    }
1494
1495    // Check that trait predicates are WF when params are instantiated with their defaults.
1496    // We don't want to overly constrain the predicates that may be written but we want to
1497    // catch cases where a default my never be applied such as `struct Foo<T: Copy = String>`.
1498    // Therefore we check if a predicate which contains a single type param
1499    // with a concrete default is WF with that default instantiated.
1500    // For more examples see tests `defaults-well-formedness.rs` and `type-check-defaults.rs`.
1501    //
1502    // First we build the defaulted generic parameters.
1503    let args = GenericArgs::for_item(tcx, def_id.to_def_id(), |param, _| {
1504        if param.index >= generics.parent_count as u32
1505            // If the param has a default, ...
1506            && let Some(default) = param.default_value(tcx).map(ty::EarlyBinder::instantiate_identity)
1507            // ... and it's not a dependent default, ...
1508            && !default.has_param()
1509        {
1510            // ... then instantiate it with the default.
1511            return default;
1512        }
1513        tcx.mk_param_from_def(param)
1514    });
1515
1516    // Now we build the instantiated predicates.
1517    let default_obligations = predicates
1518        .predicates
1519        .iter()
1520        .flat_map(|&(pred, sp)| {
1521            #[derive(Default)]
1522            struct CountParams {
1523                params: FxHashSet<u32>,
1524            }
1525            impl<'tcx> ty::TypeVisitor<TyCtxt<'tcx>> for CountParams {
1526                type Result = ControlFlow<()>;
1527                fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
1528                    if let ty::Param(param) = t.kind() {
1529                        self.params.insert(param.index);
1530                    }
1531                    t.super_visit_with(self)
1532                }
1533
1534                fn visit_region(&mut self, _: ty::Region<'tcx>) -> Self::Result {
1535                    ControlFlow::Break(())
1536                }
1537
1538                fn visit_const(&mut self, c: ty::Const<'tcx>) -> Self::Result {
1539                    if let ty::ConstKind::Param(param) = c.kind() {
1540                        self.params.insert(param.index);
1541                    }
1542                    c.super_visit_with(self)
1543                }
1544            }
1545            let mut param_count = CountParams::default();
1546            let has_region = pred.visit_with(&mut param_count).is_break();
1547            let instantiated_pred = ty::EarlyBinder::bind(pred).instantiate(tcx, args);
1548            // Don't check non-defaulted params, dependent defaults (including lifetimes)
1549            // or preds with multiple params.
1550            if instantiated_pred.has_non_region_param()
1551                || param_count.params.len() > 1
1552                || has_region
1553            {
1554                None
1555            } else if predicates.predicates.iter().any(|&(p, _)| p == instantiated_pred) {
1556                // Avoid duplication of predicates that contain no parameters, for example.
1557                None
1558            } else {
1559                Some((instantiated_pred, sp))
1560            }
1561        })
1562        .map(|(pred, sp)| {
1563            // Convert each of those into an obligation. So if you have
1564            // something like `struct Foo<T: Copy = String>`, we would
1565            // take that predicate `T: Copy`, instantiated with `String: Copy`
1566            // (actually that happens in the previous `flat_map` call),
1567            // and then try to prove it (in this case, we'll fail).
1568            //
1569            // Note the subtle difference from how we handle `predicates`
1570            // below: there, we are not trying to prove those predicates
1571            // to be *true* but merely *well-formed*.
1572            let pred = wfcx.normalize(sp, None, pred);
1573            let cause = traits::ObligationCause::new(
1574                sp,
1575                wfcx.body_def_id,
1576                ObligationCauseCode::WhereClause(def_id.to_def_id(), DUMMY_SP),
1577            );
1578            Obligation::new(tcx, cause, wfcx.param_env, pred)
1579        });
1580
1581    let predicates = predicates.instantiate_identity(tcx);
1582
1583    let predicates = wfcx.normalize(span, None, predicates);
1584
1585    debug!(?predicates.predicates);
1586    assert_eq!(predicates.predicates.len(), predicates.spans.len());
1587    let wf_obligations = predicates.into_iter().flat_map(|(p, sp)| {
1588        traits::wf::clause_obligations(infcx, wfcx.param_env, wfcx.body_def_id, p, sp)
1589    });
1590    let obligations: Vec<_> = wf_obligations.chain(default_obligations).collect();
1591    wfcx.register_obligations(obligations);
1592}
1593
1594#[instrument(level = "debug", skip(wfcx, span, hir_decl))]
1595fn check_fn_or_method<'tcx>(
1596    wfcx: &WfCheckingCtxt<'_, 'tcx>,
1597    span: Span,
1598    sig: ty::PolyFnSig<'tcx>,
1599    hir_decl: &hir::FnDecl<'_>,
1600    def_id: LocalDefId,
1601) {
1602    let tcx = wfcx.tcx();
1603    let mut sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), sig);
1604
1605    // Normalize the input and output types one at a time, using a different
1606    // `WellFormedLoc` for each. We cannot call `normalize_associated_types`
1607    // on the entire `FnSig`, since this would use the same `WellFormedLoc`
1608    // for each type, preventing the HIR wf check from generating
1609    // a nice error message.
1610    let arg_span =
1611        |idx| hir_decl.inputs.get(idx).map_or(hir_decl.output.span(), |arg: &hir::Ty<'_>| arg.span);
1612
1613    sig.inputs_and_output =
1614        tcx.mk_type_list_from_iter(sig.inputs_and_output.iter().enumerate().map(|(idx, ty)| {
1615            wfcx.normalize(
1616                arg_span(idx),
1617                Some(WellFormedLoc::Param {
1618                    function: def_id,
1619                    // Note that the `param_idx` of the output type is
1620                    // one greater than the index of the last input type.
1621                    param_idx: idx,
1622                }),
1623                ty,
1624            )
1625        }));
1626
1627    for (idx, ty) in sig.inputs_and_output.iter().enumerate() {
1628        wfcx.register_wf_obligation(
1629            arg_span(idx),
1630            Some(WellFormedLoc::Param { function: def_id, param_idx: idx }),
1631            ty.into(),
1632        );
1633    }
1634
1635    check_where_clauses(wfcx, span, def_id);
1636
1637    if sig.abi == ExternAbi::RustCall {
1638        let span = tcx.def_span(def_id);
1639        let has_implicit_self = hir_decl.implicit_self != hir::ImplicitSelfKind::None;
1640        let mut inputs = sig.inputs().iter().skip(if has_implicit_self { 1 } else { 0 });
1641        // Check that the argument is a tuple and is sized
1642        if let Some(ty) = inputs.next() {
1643            wfcx.register_bound(
1644                ObligationCause::new(span, wfcx.body_def_id, ObligationCauseCode::RustCall),
1645                wfcx.param_env,
1646                *ty,
1647                tcx.require_lang_item(hir::LangItem::Tuple, Some(span)),
1648            );
1649            wfcx.register_bound(
1650                ObligationCause::new(span, wfcx.body_def_id, ObligationCauseCode::RustCall),
1651                wfcx.param_env,
1652                *ty,
1653                tcx.require_lang_item(hir::LangItem::Sized, Some(span)),
1654            );
1655        } else {
1656            tcx.dcx().span_err(
1657                hir_decl.inputs.last().map_or(span, |input| input.span),
1658                "functions with the \"rust-call\" ABI must take a single non-self tuple argument",
1659            );
1660        }
1661        // No more inputs other than the `self` type and the tuple type
1662        if inputs.next().is_some() {
1663            tcx.dcx().span_err(
1664                hir_decl.inputs.last().map_or(span, |input| input.span),
1665                "functions with the \"rust-call\" ABI must take a single non-self tuple argument",
1666            );
1667        }
1668    }
1669
1670    // If the function has a body, additionally require that the return type is sized.
1671    check_sized_if_body(
1672        wfcx,
1673        def_id,
1674        sig.output(),
1675        match hir_decl.output {
1676            hir::FnRetTy::Return(ty) => Some(ty.span),
1677            hir::FnRetTy::DefaultReturn(_) => None,
1678        },
1679        ObligationCauseCode::SizedReturnType,
1680    );
1681}
1682
1683fn check_sized_if_body<'tcx>(
1684    wfcx: &WfCheckingCtxt<'_, 'tcx>,
1685    def_id: LocalDefId,
1686    ty: Ty<'tcx>,
1687    maybe_span: Option<Span>,
1688    code: ObligationCauseCode<'tcx>,
1689) {
1690    let tcx = wfcx.tcx();
1691    if let Some(body) = tcx.hir_maybe_body_owned_by(def_id) {
1692        let span = maybe_span.unwrap_or(body.value.span);
1693
1694        wfcx.register_bound(
1695            ObligationCause::new(span, def_id, code),
1696            wfcx.param_env,
1697            ty,
1698            tcx.require_lang_item(LangItem::Sized, Some(span)),
1699        );
1700    }
1701}
1702
1703/// The `arbitrary_self_types_pointers` feature implies `arbitrary_self_types`.
1704#[derive(Clone, Copy, PartialEq)]
1705enum ArbitrarySelfTypesLevel {
1706    Basic,        // just arbitrary_self_types
1707    WithPointers, // both arbitrary_self_types and arbitrary_self_types_pointers
1708}
1709
1710#[instrument(level = "debug", skip(wfcx))]
1711fn check_method_receiver<'tcx>(
1712    wfcx: &WfCheckingCtxt<'_, 'tcx>,
1713    fn_sig: &hir::FnSig<'_>,
1714    method: ty::AssocItem,
1715    self_ty: Ty<'tcx>,
1716) -> Result<(), ErrorGuaranteed> {
1717    let tcx = wfcx.tcx();
1718
1719    if !method.fn_has_self_parameter {
1720        return Ok(());
1721    }
1722
1723    let span = fn_sig.decl.inputs[0].span;
1724
1725    let sig = tcx.fn_sig(method.def_id).instantiate_identity();
1726    let sig = tcx.liberate_late_bound_regions(method.def_id, sig);
1727    let sig = wfcx.normalize(span, None, sig);
1728
1729    debug!("check_method_receiver: sig={:?}", sig);
1730
1731    let self_ty = wfcx.normalize(span, None, self_ty);
1732
1733    let receiver_ty = sig.inputs()[0];
1734    let receiver_ty = wfcx.normalize(span, None, receiver_ty);
1735
1736    // If the receiver already has errors reported, consider it valid to avoid
1737    // unnecessary errors (#58712).
1738    if receiver_ty.references_error() {
1739        return Ok(());
1740    }
1741
1742    let arbitrary_self_types_level = if tcx.features().arbitrary_self_types_pointers() {
1743        Some(ArbitrarySelfTypesLevel::WithPointers)
1744    } else if tcx.features().arbitrary_self_types() {
1745        Some(ArbitrarySelfTypesLevel::Basic)
1746    } else {
1747        None
1748    };
1749    let generics = tcx.generics_of(method.def_id);
1750
1751    let receiver_validity =
1752        receiver_is_valid(wfcx, span, receiver_ty, self_ty, arbitrary_self_types_level, generics);
1753    if let Err(receiver_validity_err) = receiver_validity {
1754        return Err(match arbitrary_self_types_level {
1755            // Wherever possible, emit a message advising folks that the features
1756            // `arbitrary_self_types` or `arbitrary_self_types_pointers` might
1757            // have helped.
1758            None if receiver_is_valid(
1759                wfcx,
1760                span,
1761                receiver_ty,
1762                self_ty,
1763                Some(ArbitrarySelfTypesLevel::Basic),
1764                generics,
1765            )
1766            .is_ok() =>
1767            {
1768                // Report error; would have worked with `arbitrary_self_types`.
1769                feature_err(
1770                    &tcx.sess,
1771                    sym::arbitrary_self_types,
1772                    span,
1773                    format!(
1774                        "`{receiver_ty}` cannot be used as the type of `self` without \
1775                            the `arbitrary_self_types` feature",
1776                    ),
1777                )
1778                .with_help(fluent::hir_analysis_invalid_receiver_ty_help)
1779                .emit()
1780            }
1781            None | Some(ArbitrarySelfTypesLevel::Basic)
1782                if receiver_is_valid(
1783                    wfcx,
1784                    span,
1785                    receiver_ty,
1786                    self_ty,
1787                    Some(ArbitrarySelfTypesLevel::WithPointers),
1788                    generics,
1789                )
1790                .is_ok() =>
1791            {
1792                // Report error; would have worked with `arbitrary_self_types_pointers`.
1793                feature_err(
1794                    &tcx.sess,
1795                    sym::arbitrary_self_types_pointers,
1796                    span,
1797                    format!(
1798                        "`{receiver_ty}` cannot be used as the type of `self` without \
1799                            the `arbitrary_self_types_pointers` feature",
1800                    ),
1801                )
1802                .with_help(fluent::hir_analysis_invalid_receiver_ty_help)
1803                .emit()
1804            }
1805            _ =>
1806            // Report error; would not have worked with `arbitrary_self_types[_pointers]`.
1807            {
1808                match receiver_validity_err {
1809                    ReceiverValidityError::DoesNotDeref if arbitrary_self_types_level.is_some() => {
1810                        let hint = match receiver_ty
1811                            .builtin_deref(false)
1812                            .unwrap_or(receiver_ty)
1813                            .ty_adt_def()
1814                            .and_then(|adt_def| tcx.get_diagnostic_name(adt_def.did()))
1815                        {
1816                            Some(sym::RcWeak | sym::ArcWeak) => Some(InvalidReceiverTyHint::Weak),
1817                            Some(sym::NonNull) => Some(InvalidReceiverTyHint::NonNull),
1818                            _ => None,
1819                        };
1820
1821                        tcx.dcx().emit_err(errors::InvalidReceiverTy { span, receiver_ty, hint })
1822                    }
1823                    ReceiverValidityError::DoesNotDeref => {
1824                        tcx.dcx().emit_err(errors::InvalidReceiverTyNoArbitrarySelfTypes {
1825                            span,
1826                            receiver_ty,
1827                        })
1828                    }
1829                    ReceiverValidityError::MethodGenericParamUsed => {
1830                        tcx.dcx().emit_err(errors::InvalidGenericReceiverTy { span, receiver_ty })
1831                    }
1832                }
1833            }
1834        });
1835    }
1836    Ok(())
1837}
1838
1839/// Error cases which may be returned from `receiver_is_valid`. These error
1840/// cases are generated in this function as they may be unearthed as we explore
1841/// the `autoderef` chain, but they're converted to diagnostics in the caller.
1842enum ReceiverValidityError {
1843    /// The self type does not get to the receiver type by following the
1844    /// autoderef chain.
1845    DoesNotDeref,
1846    /// A type was found which is a method type parameter, and that's not allowed.
1847    MethodGenericParamUsed,
1848}
1849
1850/// Confirms that a type is not a type parameter referring to one of the
1851/// method's type params.
1852fn confirm_type_is_not_a_method_generic_param(
1853    ty: Ty<'_>,
1854    method_generics: &ty::Generics,
1855) -> Result<(), ReceiverValidityError> {
1856    if let ty::Param(param) = ty.kind() {
1857        if (param.index as usize) >= method_generics.parent_count {
1858            return Err(ReceiverValidityError::MethodGenericParamUsed);
1859        }
1860    }
1861    Ok(())
1862}
1863
1864/// Returns whether `receiver_ty` would be considered a valid receiver type for `self_ty`. If
1865/// `arbitrary_self_types` is enabled, `receiver_ty` must transitively deref to `self_ty`, possibly
1866/// through a `*const/mut T` raw pointer if  `arbitrary_self_types_pointers` is also enabled.
1867/// If neither feature is enabled, the requirements are more strict: `receiver_ty` must implement
1868/// `Receiver` and directly implement `Deref<Target = self_ty>`.
1869///
1870/// N.B., there are cases this function returns `true` but causes an error to be emitted,
1871/// particularly when `receiver_ty` derefs to a type that is the same as `self_ty` but has the
1872/// wrong lifetime. Be careful of this if you are calling this function speculatively.
1873fn receiver_is_valid<'tcx>(
1874    wfcx: &WfCheckingCtxt<'_, 'tcx>,
1875    span: Span,
1876    receiver_ty: Ty<'tcx>,
1877    self_ty: Ty<'tcx>,
1878    arbitrary_self_types_enabled: Option<ArbitrarySelfTypesLevel>,
1879    method_generics: &ty::Generics,
1880) -> Result<(), ReceiverValidityError> {
1881    let infcx = wfcx.infcx;
1882    let tcx = wfcx.tcx();
1883    let cause =
1884        ObligationCause::new(span, wfcx.body_def_id, traits::ObligationCauseCode::MethodReceiver);
1885
1886    // Special case `receiver == self_ty`, which doesn't necessarily require the `Receiver` lang item.
1887    if let Ok(()) = wfcx.infcx.commit_if_ok(|_| {
1888        let ocx = ObligationCtxt::new(wfcx.infcx);
1889        ocx.eq(&cause, wfcx.param_env, self_ty, receiver_ty)?;
1890        if ocx.select_all_or_error().is_empty() { Ok(()) } else { Err(NoSolution) }
1891    }) {
1892        return Ok(());
1893    }
1894
1895    confirm_type_is_not_a_method_generic_param(receiver_ty, method_generics)?;
1896
1897    let mut autoderef = Autoderef::new(infcx, wfcx.param_env, wfcx.body_def_id, span, receiver_ty);
1898
1899    // The `arbitrary_self_types` feature allows custom smart pointer
1900    // types to be method receivers, as identified by following the Receiver<Target=T>
1901    // chain.
1902    if arbitrary_self_types_enabled.is_some() {
1903        autoderef = autoderef.use_receiver_trait();
1904    }
1905
1906    // The `arbitrary_self_types_pointers` feature allows raw pointer receivers like `self: *const Self`.
1907    if arbitrary_self_types_enabled == Some(ArbitrarySelfTypesLevel::WithPointers) {
1908        autoderef = autoderef.include_raw_pointers();
1909    }
1910
1911    // Keep dereferencing `receiver_ty` until we get to `self_ty`.
1912    while let Some((potential_self_ty, _)) = autoderef.next() {
1913        debug!(
1914            "receiver_is_valid: potential self type `{:?}` to match `{:?}`",
1915            potential_self_ty, self_ty
1916        );
1917
1918        confirm_type_is_not_a_method_generic_param(potential_self_ty, method_generics)?;
1919
1920        // Check if the self type unifies. If it does, then commit the result
1921        // since it may have region side-effects.
1922        if let Ok(()) = wfcx.infcx.commit_if_ok(|_| {
1923            let ocx = ObligationCtxt::new(wfcx.infcx);
1924            ocx.eq(&cause, wfcx.param_env, self_ty, potential_self_ty)?;
1925            if ocx.select_all_or_error().is_empty() { Ok(()) } else { Err(NoSolution) }
1926        }) {
1927            wfcx.register_obligations(autoderef.into_obligations());
1928            return Ok(());
1929        }
1930
1931        // Without `feature(arbitrary_self_types)`, we require that each step in the
1932        // deref chain implement `LegacyReceiver`.
1933        if arbitrary_self_types_enabled.is_none() {
1934            let legacy_receiver_trait_def_id =
1935                tcx.require_lang_item(LangItem::LegacyReceiver, Some(span));
1936            if !legacy_receiver_is_implemented(
1937                wfcx,
1938                legacy_receiver_trait_def_id,
1939                cause.clone(),
1940                potential_self_ty,
1941            ) {
1942                // We cannot proceed.
1943                break;
1944            }
1945
1946            // Register the bound, in case it has any region side-effects.
1947            wfcx.register_bound(
1948                cause.clone(),
1949                wfcx.param_env,
1950                potential_self_ty,
1951                legacy_receiver_trait_def_id,
1952            );
1953        }
1954    }
1955
1956    debug!("receiver_is_valid: type `{:?}` does not deref to `{:?}`", receiver_ty, self_ty);
1957    Err(ReceiverValidityError::DoesNotDeref)
1958}
1959
1960fn legacy_receiver_is_implemented<'tcx>(
1961    wfcx: &WfCheckingCtxt<'_, 'tcx>,
1962    legacy_receiver_trait_def_id: DefId,
1963    cause: ObligationCause<'tcx>,
1964    receiver_ty: Ty<'tcx>,
1965) -> bool {
1966    let tcx = wfcx.tcx();
1967    let trait_ref = ty::TraitRef::new(tcx, legacy_receiver_trait_def_id, [receiver_ty]);
1968
1969    let obligation = Obligation::new(tcx, cause, wfcx.param_env, trait_ref);
1970
1971    if wfcx.infcx.predicate_must_hold_modulo_regions(&obligation) {
1972        true
1973    } else {
1974        debug!(
1975            "receiver_is_implemented: type `{:?}` does not implement `LegacyReceiver` trait",
1976            receiver_ty
1977        );
1978        false
1979    }
1980}
1981
1982fn check_variances_for_type_defn<'tcx>(
1983    tcx: TyCtxt<'tcx>,
1984    item: &'tcx hir::Item<'tcx>,
1985    hir_generics: &hir::Generics<'tcx>,
1986) {
1987    match item.kind {
1988        ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => {
1989            // Ok
1990        }
1991        ItemKind::TyAlias(..) => {
1992            assert!(
1993                tcx.type_alias_is_lazy(item.owner_id),
1994                "should not be computing variance of non-weak type alias"
1995            );
1996        }
1997        kind => span_bug!(item.span, "cannot compute the variances of {kind:?}"),
1998    }
1999
2000    let ty_predicates = tcx.predicates_of(item.owner_id);
2001    assert_eq!(ty_predicates.parent, None);
2002    let variances = tcx.variances_of(item.owner_id);
2003
2004    let mut constrained_parameters: FxHashSet<_> = variances
2005        .iter()
2006        .enumerate()
2007        .filter(|&(_, &variance)| variance != ty::Bivariant)
2008        .map(|(index, _)| Parameter(index as u32))
2009        .collect();
2010
2011    identify_constrained_generic_params(tcx, ty_predicates, None, &mut constrained_parameters);
2012
2013    // Lazily calculated because it is only needed in case of an error.
2014    let explicitly_bounded_params = LazyCell::new(|| {
2015        let icx = crate::collect::ItemCtxt::new(tcx, item.owner_id.def_id);
2016        hir_generics
2017            .predicates
2018            .iter()
2019            .filter_map(|predicate| match predicate.kind {
2020                hir::WherePredicateKind::BoundPredicate(predicate) => {
2021                    match icx.lower_ty(predicate.bounded_ty).kind() {
2022                        ty::Param(data) => Some(Parameter(data.index)),
2023                        _ => None,
2024                    }
2025                }
2026                _ => None,
2027            })
2028            .collect::<FxHashSet<_>>()
2029    });
2030
2031    let ty_generics = tcx.generics_of(item.owner_id);
2032
2033    for (index, _) in variances.iter().enumerate() {
2034        let parameter = Parameter(index as u32);
2035
2036        if constrained_parameters.contains(&parameter) {
2037            continue;
2038        }
2039
2040        let ty_param = &ty_generics.own_params[index];
2041        let hir_param = &hir_generics.params[index];
2042
2043        if ty_param.def_id != hir_param.def_id.into() {
2044            // Valid programs always have lifetimes before types in the generic parameter list.
2045            // ty_generics are normalized to be in this required order, and variances are built
2046            // from ty generics, not from hir generics. but we need hir generics to get
2047            // a span out.
2048            //
2049            // If they aren't in the same order, then the user has written invalid code, and already
2050            // got an error about it (or I'm wrong about this).
2051            tcx.dcx().span_delayed_bug(
2052                hir_param.span,
2053                "hir generics and ty generics in different order",
2054            );
2055            continue;
2056        }
2057
2058        // Look for `ErrorGuaranteed` deeply within this type.
2059        if let ControlFlow::Break(ErrorGuaranteed { .. }) = tcx
2060            .type_of(item.owner_id)
2061            .instantiate_identity()
2062            .visit_with(&mut HasErrorDeep { tcx, seen: Default::default() })
2063        {
2064            continue;
2065        }
2066
2067        match hir_param.name {
2068            hir::ParamName::Error(_) => {
2069                // Don't report a bivariance error for a lifetime that isn't
2070                // even valid to name.
2071            }
2072            _ => {
2073                let has_explicit_bounds = explicitly_bounded_params.contains(&parameter);
2074                report_bivariance(tcx, hir_param, has_explicit_bounds, item);
2075            }
2076        }
2077    }
2078}
2079
2080/// Look for `ErrorGuaranteed` deeply within structs' (unsubstituted) fields.
2081struct HasErrorDeep<'tcx> {
2082    tcx: TyCtxt<'tcx>,
2083    seen: FxHashSet<DefId>,
2084}
2085impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for HasErrorDeep<'tcx> {
2086    type Result = ControlFlow<ErrorGuaranteed>;
2087
2088    fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
2089        match *ty.kind() {
2090            ty::Adt(def, _) => {
2091                if self.seen.insert(def.did()) {
2092                    for field in def.all_fields() {
2093                        self.tcx.type_of(field.did).instantiate_identity().visit_with(self)?;
2094                    }
2095                }
2096            }
2097            ty::Error(guar) => return ControlFlow::Break(guar),
2098            _ => {}
2099        }
2100        ty.super_visit_with(self)
2101    }
2102
2103    fn visit_region(&mut self, r: ty::Region<'tcx>) -> Self::Result {
2104        if let Err(guar) = r.error_reported() {
2105            ControlFlow::Break(guar)
2106        } else {
2107            ControlFlow::Continue(())
2108        }
2109    }
2110
2111    fn visit_const(&mut self, c: ty::Const<'tcx>) -> Self::Result {
2112        if let Err(guar) = c.error_reported() {
2113            ControlFlow::Break(guar)
2114        } else {
2115            ControlFlow::Continue(())
2116        }
2117    }
2118}
2119
2120fn report_bivariance<'tcx>(
2121    tcx: TyCtxt<'tcx>,
2122    param: &'tcx hir::GenericParam<'tcx>,
2123    has_explicit_bounds: bool,
2124    item: &'tcx hir::Item<'tcx>,
2125) -> ErrorGuaranteed {
2126    let param_name = param.name.ident();
2127
2128    let help = match item.kind {
2129        ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => {
2130            if let Some(def_id) = tcx.lang_items().phantom_data() {
2131                errors::UnusedGenericParameterHelp::Adt {
2132                    param_name,
2133                    phantom_data: tcx.def_path_str(def_id),
2134                }
2135            } else {
2136                errors::UnusedGenericParameterHelp::AdtNoPhantomData { param_name }
2137            }
2138        }
2139        ItemKind::TyAlias(..) => errors::UnusedGenericParameterHelp::TyAlias { param_name },
2140        item_kind => bug!("report_bivariance: unexpected item kind: {item_kind:?}"),
2141    };
2142
2143    let mut usage_spans = vec![];
2144    intravisit::walk_item(
2145        &mut CollectUsageSpans { spans: &mut usage_spans, param_def_id: param.def_id.to_def_id() },
2146        item,
2147    );
2148
2149    if !usage_spans.is_empty() {
2150        // First, check if the ADT/LTA is (probably) cyclical. We say probably here, since we're
2151        // not actually looking into substitutions, just walking through fields / the "RHS".
2152        // We don't recurse into the hidden types of opaques or anything else fancy.
2153        let item_def_id = item.owner_id.to_def_id();
2154        let is_probably_cyclical =
2155            IsProbablyCyclical { tcx, item_def_id, seen: Default::default() }
2156                .visit_def(item_def_id)
2157                .is_break();
2158        // If the ADT/LTA is cyclical, then if at least one usage of the type parameter or
2159        // the `Self` alias is present in the, then it's probably a cyclical struct/ type
2160        // alias, and we should call those parameter usages recursive rather than just saying
2161        // they're unused...
2162        //
2163        // We currently report *all* of the parameter usages, since computing the exact
2164        // subset is very involved, and the fact we're mentioning recursion at all is
2165        // likely to guide the user in the right direction.
2166        if is_probably_cyclical {
2167            return tcx.dcx().emit_err(errors::RecursiveGenericParameter {
2168                spans: usage_spans,
2169                param_span: param.span,
2170                param_name,
2171                param_def_kind: tcx.def_descr(param.def_id.to_def_id()),
2172                help,
2173                note: (),
2174            });
2175        }
2176    }
2177
2178    let const_param_help =
2179        matches!(param.kind, hir::GenericParamKind::Type { .. } if !has_explicit_bounds);
2180
2181    let mut diag = tcx.dcx().create_err(errors::UnusedGenericParameter {
2182        span: param.span,
2183        param_name,
2184        param_def_kind: tcx.def_descr(param.def_id.to_def_id()),
2185        usage_spans,
2186        help,
2187        const_param_help,
2188    });
2189    diag.code(E0392);
2190    diag.emit()
2191}
2192
2193/// Detects cases where an ADT/LTA is trivially cyclical -- we want to detect this so
2194/// we only mention that its parameters are used cyclically if the ADT/LTA is truly
2195/// cyclical.
2196///
2197/// Notably, we don't consider substitutions here, so this may have false positives.
2198struct IsProbablyCyclical<'tcx> {
2199    tcx: TyCtxt<'tcx>,
2200    item_def_id: DefId,
2201    seen: FxHashSet<DefId>,
2202}
2203
2204impl<'tcx> IsProbablyCyclical<'tcx> {
2205    fn visit_def(&mut self, def_id: DefId) -> ControlFlow<(), ()> {
2206        match self.tcx.def_kind(def_id) {
2207            DefKind::Struct | DefKind::Enum | DefKind::Union => {
2208                self.tcx.adt_def(def_id).all_fields().try_for_each(|field| {
2209                    self.tcx.type_of(field.did).instantiate_identity().visit_with(self)
2210                })
2211            }
2212            DefKind::TyAlias if self.tcx.type_alias_is_lazy(def_id) => {
2213                self.tcx.type_of(def_id).instantiate_identity().visit_with(self)
2214            }
2215            _ => ControlFlow::Continue(()),
2216        }
2217    }
2218}
2219
2220impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for IsProbablyCyclical<'tcx> {
2221    type Result = ControlFlow<(), ()>;
2222
2223    fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<(), ()> {
2224        let def_id = match ty.kind() {
2225            ty::Adt(adt_def, _) => Some(adt_def.did()),
2226            ty::Alias(ty::Weak, alias_ty) => Some(alias_ty.def_id),
2227            _ => None,
2228        };
2229        if let Some(def_id) = def_id {
2230            if def_id == self.item_def_id {
2231                return ControlFlow::Break(());
2232            }
2233            if self.seen.insert(def_id) {
2234                self.visit_def(def_id)?;
2235            }
2236        }
2237        ty.super_visit_with(self)
2238    }
2239}
2240
2241/// Collect usages of the `param_def_id` and `Res::SelfTyAlias` in the HIR.
2242///
2243/// This is used to report places where the user has used parameters in a
2244/// non-variance-constraining way for better bivariance errors.
2245struct CollectUsageSpans<'a> {
2246    spans: &'a mut Vec<Span>,
2247    param_def_id: DefId,
2248}
2249
2250impl<'tcx> Visitor<'tcx> for CollectUsageSpans<'_> {
2251    type Result = ();
2252
2253    fn visit_generics(&mut self, _g: &'tcx rustc_hir::Generics<'tcx>) -> Self::Result {
2254        // Skip the generics. We only care about fields, not where clause/param bounds.
2255    }
2256
2257    fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx, AmbigArg>) -> Self::Result {
2258        if let hir::TyKind::Path(hir::QPath::Resolved(None, qpath)) = t.kind {
2259            if let Res::Def(DefKind::TyParam, def_id) = qpath.res
2260                && def_id == self.param_def_id
2261            {
2262                self.spans.push(t.span);
2263                return;
2264            } else if let Res::SelfTyAlias { .. } = qpath.res {
2265                self.spans.push(t.span);
2266                return;
2267            }
2268        }
2269        intravisit::walk_ty(self, t);
2270    }
2271}
2272
2273impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
2274    /// Feature gates RFC 2056 -- trivial bounds, checking for global bounds that
2275    /// aren't true.
2276    #[instrument(level = "debug", skip(self))]
2277    fn check_false_global_bounds(&mut self) {
2278        let tcx = self.ocx.infcx.tcx;
2279        let mut span = self.span;
2280        let empty_env = ty::ParamEnv::empty();
2281
2282        let predicates_with_span = tcx.predicates_of(self.body_def_id).predicates.iter().copied();
2283        // Check elaborated bounds.
2284        let implied_obligations = traits::elaborate(tcx, predicates_with_span);
2285
2286        for (pred, obligation_span) in implied_obligations {
2287            // We lower empty bounds like `Vec<dyn Copy>:` as
2288            // `WellFormed(Vec<dyn Copy>)`, which will later get checked by
2289            // regular WF checking
2290            if let ty::ClauseKind::WellFormed(..) = pred.kind().skip_binder() {
2291                continue;
2292            }
2293            // Match the existing behavior.
2294            if pred.is_global() && !pred.has_type_flags(TypeFlags::HAS_BINDER_VARS) {
2295                let pred = self.normalize(span, None, pred);
2296
2297                // only use the span of the predicate clause (#90869)
2298                let hir_node = tcx.hir_node_by_def_id(self.body_def_id);
2299                if let Some(hir::Generics { predicates, .. }) = hir_node.generics() {
2300                    span = predicates
2301                        .iter()
2302                        // There seems to be no better way to find out which predicate we are in
2303                        .find(|pred| pred.span.contains(obligation_span))
2304                        .map(|pred| pred.span)
2305                        .unwrap_or(obligation_span);
2306                }
2307
2308                let obligation = Obligation::new(
2309                    tcx,
2310                    traits::ObligationCause::new(
2311                        span,
2312                        self.body_def_id,
2313                        ObligationCauseCode::TrivialBound,
2314                    ),
2315                    empty_env,
2316                    pred,
2317                );
2318                self.ocx.register_obligation(obligation);
2319            }
2320        }
2321    }
2322}
2323
2324fn check_mod_type_wf(tcx: TyCtxt<'_>, module: LocalModDefId) -> Result<(), ErrorGuaranteed> {
2325    let items = tcx.hir_module_items(module);
2326    let res = items
2327        .par_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id))
2328        .and(items.par_impl_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)))
2329        .and(items.par_trait_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)))
2330        .and(
2331            items.par_foreign_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)),
2332        )
2333        .and(items.par_opaques(|item| tcx.ensure_ok().check_well_formed(item)));
2334    if module == LocalModDefId::CRATE_DEF_ID {
2335        super::entry::check_for_entry_fn(tcx);
2336    }
2337    res
2338}
2339
2340fn lint_redundant_lifetimes<'tcx>(
2341    tcx: TyCtxt<'tcx>,
2342    owner_id: LocalDefId,
2343    outlives_env: &OutlivesEnvironment<'tcx>,
2344) {
2345    let def_kind = tcx.def_kind(owner_id);
2346    match def_kind {
2347        DefKind::Struct
2348        | DefKind::Union
2349        | DefKind::Enum
2350        | DefKind::Trait
2351        | DefKind::TraitAlias
2352        | DefKind::Fn
2353        | DefKind::Const
2354        | DefKind::Impl { of_trait: _ } => {
2355            // Proceed
2356        }
2357        DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => {
2358            let parent_def_id = tcx.local_parent(owner_id);
2359            if matches!(tcx.def_kind(parent_def_id), DefKind::Impl { of_trait: true }) {
2360                // Don't check for redundant lifetimes for associated items of trait
2361                // implementations, since the signature is required to be compatible
2362                // with the trait, even if the implementation implies some lifetimes
2363                // are redundant.
2364                return;
2365            }
2366        }
2367        DefKind::Mod
2368        | DefKind::Variant
2369        | DefKind::TyAlias
2370        | DefKind::ForeignTy
2371        | DefKind::TyParam
2372        | DefKind::ConstParam
2373        | DefKind::Static { .. }
2374        | DefKind::Ctor(_, _)
2375        | DefKind::Macro(_)
2376        | DefKind::ExternCrate
2377        | DefKind::Use
2378        | DefKind::ForeignMod
2379        | DefKind::AnonConst
2380        | DefKind::InlineConst
2381        | DefKind::OpaqueTy
2382        | DefKind::Field
2383        | DefKind::LifetimeParam
2384        | DefKind::GlobalAsm
2385        | DefKind::Closure
2386        | DefKind::SyntheticCoroutineBody => return,
2387    }
2388
2389    // The ordering of this lifetime map is a bit subtle.
2390    //
2391    // Specifically, we want to find a "candidate" lifetime that precedes a "victim" lifetime,
2392    // where we can prove that `'candidate = 'victim`.
2393    //
2394    // `'static` must come first in this list because we can never replace `'static` with
2395    // something else, but if we find some lifetime `'a` where `'a = 'static`, we want to
2396    // suggest replacing `'a` with `'static`.
2397    let mut lifetimes = vec![tcx.lifetimes.re_static];
2398    lifetimes.extend(
2399        ty::GenericArgs::identity_for_item(tcx, owner_id).iter().filter_map(|arg| arg.as_region()),
2400    );
2401    // If we are in a function, add its late-bound lifetimes too.
2402    if matches!(def_kind, DefKind::Fn | DefKind::AssocFn) {
2403        for (idx, var) in
2404            tcx.fn_sig(owner_id).instantiate_identity().bound_vars().iter().enumerate()
2405        {
2406            let ty::BoundVariableKind::Region(kind) = var else { continue };
2407            let kind = ty::LateParamRegionKind::from_bound(ty::BoundVar::from_usize(idx), kind);
2408            lifetimes.push(ty::Region::new_late_param(tcx, owner_id.to_def_id(), kind));
2409        }
2410    }
2411    lifetimes.retain(|candidate| candidate.has_name());
2412
2413    // Keep track of lifetimes which have already been replaced with other lifetimes.
2414    // This makes sure that if `'a = 'b = 'c`, we don't say `'c` should be replaced by
2415    // both `'a` and `'b`.
2416    let mut shadowed = FxHashSet::default();
2417
2418    for (idx, &candidate) in lifetimes.iter().enumerate() {
2419        // Don't suggest removing a lifetime twice. We only need to check this
2420        // here and not up in the `victim` loop because equality is transitive,
2421        // so if A = C and B = C, then A must = B, so it'll be shadowed too in
2422        // A's victim loop.
2423        if shadowed.contains(&candidate) {
2424            continue;
2425        }
2426
2427        for &victim in &lifetimes[(idx + 1)..] {
2428            // All region parameters should have a `DefId` available as:
2429            // - Late-bound parameters should be of the`BrNamed` variety,
2430            // since we get these signatures straight from `hir_lowering`.
2431            // - Early-bound parameters unconditionally have a `DefId` available.
2432            //
2433            // Any other regions (ReError/ReStatic/etc.) shouldn't matter, since we
2434            // can't really suggest to remove them.
2435            let Some(def_id) = victim.opt_param_def_id(tcx, owner_id.to_def_id()) else {
2436                continue;
2437            };
2438
2439            // Do not rename lifetimes not local to this item since they'll overlap
2440            // with the lint running on the parent. We still want to consider parent
2441            // lifetimes which make child lifetimes redundant, otherwise we would
2442            // have truncated the `identity_for_item` args above.
2443            if tcx.parent(def_id) != owner_id.to_def_id() {
2444                continue;
2445            }
2446
2447            // If `candidate <: victim` and `victim <: candidate`, then they're equal.
2448            if outlives_env.free_region_map().sub_free_regions(tcx, candidate, victim)
2449                && outlives_env.free_region_map().sub_free_regions(tcx, victim, candidate)
2450            {
2451                shadowed.insert(victim);
2452                tcx.emit_node_span_lint(
2453                    rustc_lint_defs::builtin::REDUNDANT_LIFETIMES,
2454                    tcx.local_def_id_to_hir_id(def_id.expect_local()),
2455                    tcx.def_span(def_id),
2456                    RedundantLifetimeArgsLint { candidate, victim },
2457                );
2458            }
2459        }
2460    }
2461}
2462
2463#[derive(LintDiagnostic)]
2464#[diag(hir_analysis_redundant_lifetime_args)]
2465#[note]
2466struct RedundantLifetimeArgsLint<'tcx> {
2467    /// The lifetime we have found to be redundant.
2468    victim: ty::Region<'tcx>,
2469    // The lifetime we can replace the victim with.
2470    candidate: ty::Region<'tcx>,
2471}
2472
2473pub fn provide(providers: &mut Providers) {
2474    *providers = Providers { check_mod_type_wf, check_well_formed, ..*providers };
2475}