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 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 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 false,
154 );
155 let errors_compat = infcx_compat.resolve_regions_with_outlives_env(&outlives_env);
156 if errors_compat.is_empty() {
157 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#[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 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 match header.map(|h| h.polarity) {
245 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 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 }
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 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 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
383fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
396 let mut required_bounds_by_item = FxIndexMap::default();
398 let associated_items = tcx.associated_items(trait_def_id);
399
400 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 gat_item.kind != ty::AssocKind::Type {
412 continue;
413 }
414 let gat_generics = tcx.generics_of(gat_def_id);
415 if gat_generics.is_own_empty() {
417 continue;
418 }
419
420 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 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 ty::AssocKind::Fn => {
436 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 &sig.inputs().iter().copied().collect(),
451 gat_def_id,
452 gat_generics,
453 )
454 }
455 ty::AssocKind::Type => {
457 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 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 should_continue = true;
500 }
501 }
502 }
503 if !should_continue {
508 break;
509 }
510 }
511
512 for (gat_def_id, required_bounds) in required_bounds_by_item {
513 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
575fn 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 ty::ParamEnv::new(bounds)
595}
596
597fn 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 let mut bounds = FxIndexSet::default();
618
619 let (regions, types) = GATArgsCollector::visit(gat_def_id.to_def_id(), to_check);
620
621 if types.is_empty() && regions.is_empty() {
627 return None;
628 }
629
630 for (region_a, region_a_idx) in ®ions {
631 if let ty::ReStatic | ty::ReError(_) = **region_a {
635 continue;
636 }
637 for (ty, ty_idx) in &types {
642 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 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 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 bounds.insert(
661 ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty_param, region_param))
662 .upcast(tcx),
663 );
664 }
665 }
666
667 for (region_b, region_b_idx) in ®ions {
672 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 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 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 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
708fn 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
727fn 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
742fn 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 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 errors.is_empty()
765}
766
767struct GATArgsCollector<'tcx> {
772 gat: DefId,
773 regions: FxIndexSet<(ty::Region<'tcx>, usize)>,
775 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
823fn 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 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 hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. } => Ok(()),
935
936 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 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 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 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 ty::Array(ty, ..) => ty_is_local(*ty),
1026 ty::Slice(ty) => ty_is_local(*ty),
1027 ty::Ref(_, ty, ast::Mutability::Not) => ty_is_local(*ty),
1030 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 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 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
1119fn 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 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 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 } else {
1149 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 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 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 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 if let hir::ItemKind::Trait(..) = item.kind {
1262 check_gat_where_clauses(tcx, item.owner_id.def_id);
1263 }
1264 res
1265}
1266
1267fn 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 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 let trait_ref = tcx.impl_trait_ref(item.owner_id).unwrap().instantiate_identity();
1379 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 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 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#[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 for param in &generics.own_params {
1477 if let Some(default) = param.default_value(tcx).map(ty::EarlyBinder::instantiate_identity) {
1478 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 let args = GenericArgs::for_item(tcx, def_id.to_def_id(), |param, _| {
1504 if param.index >= generics.parent_count as u32
1505 && let Some(default) = param.default_value(tcx).map(ty::EarlyBinder::instantiate_identity)
1507 && !default.has_param()
1509 {
1510 return default;
1512 }
1513 tcx.mk_param_from_def(param)
1514 });
1515
1516 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 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 None
1558 } else {
1559 Some((instantiated_pred, sp))
1560 }
1561 })
1562 .map(|(pred, sp)| {
1563 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 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 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 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 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 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#[derive(Clone, Copy, PartialEq)]
1705enum ArbitrarySelfTypesLevel {
1706 Basic, WithPointers, }
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 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 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 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 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 {
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
1839enum ReceiverValidityError {
1843 DoesNotDeref,
1846 MethodGenericParamUsed,
1848}
1849
1850fn 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
1864fn 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 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 if arbitrary_self_types_enabled.is_some() {
1903 autoderef = autoderef.use_receiver_trait();
1904 }
1905
1906 if arbitrary_self_types_enabled == Some(ArbitrarySelfTypesLevel::WithPointers) {
1908 autoderef = autoderef.include_raw_pointers();
1909 }
1910
1911 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 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 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 break;
1944 }
1945
1946 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 }
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 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(¶meter) {
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 tcx.dcx().span_delayed_bug(
2052 hir_param.span,
2053 "hir generics and ty generics in different order",
2054 );
2055 continue;
2056 }
2057
2058 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 }
2072 _ => {
2073 let has_explicit_bounds = explicitly_bounded_params.contains(¶meter);
2074 report_bivariance(tcx, hir_param, has_explicit_bounds, item);
2075 }
2076 }
2077 }
2078}
2079
2080struct 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 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 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
2193struct 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
2241struct 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 }
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 #[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 let implied_obligations = traits::elaborate(tcx, predicates_with_span);
2285
2286 for (pred, obligation_span) in implied_obligations {
2287 if let ty::ClauseKind::WellFormed(..) = pred.kind().skip_binder() {
2291 continue;
2292 }
2293 if pred.is_global() && !pred.has_type_flags(TypeFlags::HAS_BINDER_VARS) {
2295 let pred = self.normalize(span, None, pred);
2296
2297 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 .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 }
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 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 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 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 let mut shadowed = FxHashSet::default();
2417
2418 for (idx, &candidate) in lifetimes.iter().enumerate() {
2419 if shadowed.contains(&candidate) {
2424 continue;
2425 }
2426
2427 for &victim in &lifetimes[(idx + 1)..] {
2428 let Some(def_id) = victim.opt_param_def_id(tcx, owner_id.to_def_id()) else {
2436 continue;
2437 };
2438
2439 if tcx.parent(def_id) != owner_id.to_def_id() {
2444 continue;
2445 }
2446
2447 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 victim: ty::Region<'tcx>,
2469 candidate: ty::Region<'tcx>,
2471}
2472
2473pub fn provide(providers: &mut Providers) {
2474 *providers = Providers { check_mod_type_wf, check_well_formed, ..*providers };
2475}