1use std::assert_matches::assert_matches;
2
3use hir::Node;
4use rustc_data_structures::fx::FxIndexSet;
5use rustc_hir as hir;
6use rustc_hir::def::DefKind;
7use rustc_hir::def_id::{DefId, LocalDefId};
8use rustc_middle::ty::{
9 self, GenericPredicates, ImplTraitInTraitData, Ty, TyCtxt, TypeVisitable, TypeVisitor, Upcast,
10};
11use rustc_middle::{bug, span_bug};
12use rustc_span::{DUMMY_SP, Ident, Span};
13use tracing::{debug, instrument, trace};
14
15use super::item_bounds::explicit_item_bounds_with_filter;
16use crate::collect::ItemCtxt;
17use crate::constrained_generic_params as cgp;
18use crate::delegation::inherit_predicates_for_delegation_item;
19use crate::hir_ty_lowering::{HirTyLowerer, PredicateFilter, RegionInferReason};
20
21#[instrument(level = "debug", skip(tcx))]
25pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
26 let mut result = tcx.explicit_predicates_of(def_id);
27 debug!("predicates_of: explicit_predicates_of({:?}) = {:?}", def_id, result);
28
29 let inferred_outlives = tcx.inferred_outlives_of(def_id);
30 if !inferred_outlives.is_empty() {
31 debug!("predicates_of: inferred_outlives_of({:?}) = {:?}", def_id, inferred_outlives,);
32 let inferred_outlives_iter =
33 inferred_outlives.iter().map(|(clause, span)| ((*clause).upcast(tcx), *span));
34 if result.predicates.is_empty() {
35 result.predicates = tcx.arena.alloc_from_iter(inferred_outlives_iter);
36 } else {
37 result.predicates = tcx.arena.alloc_from_iter(
38 result.predicates.into_iter().copied().chain(inferred_outlives_iter),
39 );
40 }
41 }
42
43 if tcx.is_trait(def_id) {
44 let span = DUMMY_SP;
62
63 result.predicates = tcx.arena.alloc_from_iter(
64 result
65 .predicates
66 .iter()
67 .copied()
68 .chain(std::iter::once((ty::TraitRef::identity(tcx, def_id).upcast(tcx), span))),
69 );
70 }
71
72 debug!("predicates_of({:?}) = {:?}", def_id, result);
73 result
74}
75
76#[instrument(level = "trace", skip(tcx), ret)]
79fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::GenericPredicates<'_> {
80 use rustc_hir::*;
81
82 match tcx.opt_rpitit_info(def_id.to_def_id()) {
83 Some(ImplTraitInTraitData::Trait { fn_def_id, .. }) => {
84 let mut predicates = Vec::new();
85
86 let identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
91 predicates
92 .extend(tcx.explicit_predicates_of(fn_def_id).instantiate_own(tcx, identity_args));
93
94 compute_bidirectional_outlives_predicates(
99 tcx,
100 &tcx.generics_of(def_id.to_def_id()).own_params
101 [tcx.generics_of(fn_def_id).own_params.len()..],
102 &mut predicates,
103 );
104
105 return ty::GenericPredicates {
106 parent: Some(tcx.parent(def_id.to_def_id())),
107 predicates: tcx.arena.alloc_from_iter(predicates),
108 };
109 }
110
111 Some(ImplTraitInTraitData::Impl { fn_def_id }) => {
112 let assoc_item = tcx.associated_item(def_id);
113 let trait_assoc_predicates =
114 tcx.explicit_predicates_of(assoc_item.trait_item_def_id.unwrap());
115
116 let impl_assoc_identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
117 let impl_def_id = tcx.parent(fn_def_id);
118 let impl_trait_ref_args =
119 tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity().args;
120
121 let impl_assoc_args =
122 impl_assoc_identity_args.rebase_onto(tcx, impl_def_id, impl_trait_ref_args);
123
124 let impl_predicates = trait_assoc_predicates.instantiate_own(tcx, impl_assoc_args);
125
126 return ty::GenericPredicates {
127 parent: Some(impl_def_id),
128 predicates: tcx.arena.alloc_from_iter(impl_predicates),
129 };
130 }
131
132 None => {}
133 }
134
135 let hir_id = tcx.local_def_id_to_hir_id(def_id);
136 let node = tcx.hir_node(hir_id);
137
138 if let Some(sig) = node.fn_sig()
139 && let Some(sig_id) = sig.decl.opt_delegation_sig_id()
140 {
141 return inherit_predicates_for_delegation_item(tcx, def_id, sig_id);
142 }
143
144 let mut is_trait = None;
145 let mut is_default_impl_trait = None;
146
147 let icx = ItemCtxt::new(tcx, def_id);
148
149 const NO_GENERICS: &hir::Generics<'_> = hir::Generics::empty();
150
151 let mut predicates: FxIndexSet<(ty::Clause<'_>, Span)> = FxIndexSet::default();
154
155 let hir_generics = node.generics().unwrap_or(NO_GENERICS);
156 if let Node::Item(item) = node {
157 match item.kind {
158 ItemKind::Impl(impl_) => {
159 if impl_.defaultness.is_default() {
160 is_default_impl_trait = tcx
161 .impl_trait_ref(def_id)
162 .map(|t| ty::Binder::dummy(t.instantiate_identity()));
163 }
164 }
165 ItemKind::Trait(_, _, _, _, self_bounds, ..)
166 | ItemKind::TraitAlias(_, _, self_bounds) => {
167 is_trait = Some((self_bounds, item.span));
168 }
169 _ => {}
170 }
171 };
172
173 if let Node::TraitItem(item) = node {
174 let mut bounds = Vec::new();
175 icx.lowerer().add_default_trait_item_bounds(item, &mut bounds);
176 predicates.extend(bounds);
177 }
178
179 let generics = tcx.generics_of(def_id);
180
181 if let Some((self_bounds, span)) = is_trait {
186 let mut bounds = Vec::new();
187 icx.lowerer().lower_bounds(
188 tcx.types.self_param,
189 self_bounds,
190 &mut bounds,
191 ty::List::empty(),
192 PredicateFilter::All,
193 );
194 icx.lowerer().add_sizedness_bounds(
195 &mut bounds,
196 tcx.types.self_param,
197 self_bounds,
198 None,
199 Some(def_id),
200 span,
201 );
202 icx.lowerer().add_default_super_traits(
203 def_id,
204 &mut bounds,
205 self_bounds,
206 hir_generics,
207 span,
208 );
209 predicates.extend(bounds);
210 }
211
212 if let Some(trait_ref) = is_default_impl_trait {
221 predicates.insert((trait_ref.upcast(tcx), tcx.def_span(def_id)));
222 }
223
224 for param in hir_generics.params {
228 match param.kind {
229 GenericParamKind::Lifetime { .. } => (),
230 GenericParamKind::Type { .. } => {
231 let param_ty = icx.lowerer().lower_ty_param(param.hir_id);
232 let mut bounds = Vec::new();
233 icx.lowerer().add_sizedness_bounds(
235 &mut bounds,
236 param_ty,
237 &[],
238 Some((param.def_id, hir_generics.predicates)),
239 None,
240 param.span,
241 );
242 icx.lowerer().add_default_traits(
243 &mut bounds,
244 param_ty,
245 &[],
246 Some((param.def_id, hir_generics.predicates)),
247 param.span,
248 );
249 trace!(?bounds);
250 predicates.extend(bounds);
251 trace!(?predicates);
252 }
253 hir::GenericParamKind::Const { .. } => {
254 let param_def_id = param.def_id.to_def_id();
255 let ct_ty = tcx.type_of(param_def_id).instantiate_identity();
256 let ct = icx.lowerer().lower_const_param(param_def_id, param.hir_id);
257 predicates
258 .insert((ty::ClauseKind::ConstArgHasType(ct, ct_ty).upcast(tcx), param.span));
259 }
260 }
261 }
262
263 trace!(?predicates);
264 for predicate in hir_generics.predicates {
266 match predicate.kind {
267 hir::WherePredicateKind::BoundPredicate(bound_pred) => {
268 let ty = icx.lowerer().lower_ty_maybe_return_type_notation(bound_pred.bounded_ty);
269
270 let bound_vars = tcx.late_bound_vars(predicate.hir_id);
271 if bound_pred.bounds.is_empty() {
275 if let ty::Param(_) = ty.kind() {
276 } else {
283 let span = bound_pred.bounded_ty.span;
284 let predicate = ty::Binder::bind_with_vars(
285 ty::ClauseKind::WellFormed(ty.into()),
286 bound_vars,
287 );
288 predicates.insert((predicate.upcast(tcx), span));
289 }
290 }
291
292 let mut bounds = Vec::new();
293 icx.lowerer().lower_bounds(
294 ty,
295 bound_pred.bounds,
296 &mut bounds,
297 bound_vars,
298 PredicateFilter::All,
299 );
300 predicates.extend(bounds);
301 }
302
303 hir::WherePredicateKind::RegionPredicate(region_pred) => {
304 let r1 = icx
305 .lowerer()
306 .lower_lifetime(region_pred.lifetime, RegionInferReason::RegionPredicate);
307 predicates.extend(region_pred.bounds.iter().map(|bound| {
308 let (r2, span) = match bound {
309 hir::GenericBound::Outlives(lt) => (
310 icx.lowerer().lower_lifetime(lt, RegionInferReason::RegionPredicate),
311 lt.ident.span,
312 ),
313 bound => {
314 span_bug!(
315 bound.span(),
316 "lifetime param bounds must be outlives, but found {bound:?}"
317 )
318 }
319 };
320 let pred =
321 ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(r1, r2)).upcast(tcx);
322 (pred, span)
323 }))
324 }
325
326 hir::WherePredicateKind::EqPredicate(..) => {
327 }
329 }
330 }
331
332 if tcx.features().generic_const_exprs() {
333 predicates.extend(const_evaluatable_predicates_of(tcx, def_id, &predicates));
334 }
335
336 let mut predicates: Vec<_> = predicates.into_iter().collect();
337
338 if let Node::Item(&Item { kind: ItemKind::Impl { .. }, .. }) = node {
344 let self_ty = tcx.type_of(def_id).instantiate_identity();
345 let trait_ref = tcx.impl_trait_ref(def_id).map(ty::EarlyBinder::instantiate_identity);
346 cgp::setup_constraining_predicates(
347 tcx,
348 &mut predicates,
349 trait_ref,
350 &mut cgp::parameters_for_impl(tcx, self_ty, trait_ref),
351 );
352 }
353
354 if let Node::OpaqueTy(..) = node {
358 compute_bidirectional_outlives_predicates(tcx, &generics.own_params, &mut predicates);
359 debug!(?predicates);
360 }
361
362 ty::GenericPredicates {
363 parent: generics.parent,
364 predicates: tcx.arena.alloc_from_iter(predicates),
365 }
366}
367
368fn compute_bidirectional_outlives_predicates<'tcx>(
371 tcx: TyCtxt<'tcx>,
372 opaque_own_params: &[ty::GenericParamDef],
373 predicates: &mut Vec<(ty::Clause<'tcx>, Span)>,
374) {
375 for param in opaque_own_params {
376 let orig_lifetime = tcx.map_opaque_lifetime_to_parent_lifetime(param.def_id.expect_local());
377 if let ty::ReEarlyParam(..) = orig_lifetime.kind() {
378 let dup_lifetime = ty::Region::new_early_param(
379 tcx,
380 ty::EarlyParamRegion { index: param.index, name: param.name },
381 );
382 let span = tcx.def_span(param.def_id);
383 predicates.push((
384 ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(orig_lifetime, dup_lifetime))
385 .upcast(tcx),
386 span,
387 ));
388 predicates.push((
389 ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(dup_lifetime, orig_lifetime))
390 .upcast(tcx),
391 span,
392 ));
393 }
394 }
395}
396
397#[instrument(level = "debug", skip(tcx, predicates), ret)]
398fn const_evaluatable_predicates_of<'tcx>(
399 tcx: TyCtxt<'tcx>,
400 def_id: LocalDefId,
401 predicates: &FxIndexSet<(ty::Clause<'tcx>, Span)>,
402) -> FxIndexSet<(ty::Clause<'tcx>, Span)> {
403 struct ConstCollector<'tcx> {
404 tcx: TyCtxt<'tcx>,
405 preds: FxIndexSet<(ty::Clause<'tcx>, Span)>,
406 }
407
408 fn is_const_param_default(tcx: TyCtxt<'_>, def: LocalDefId) -> bool {
409 let hir_id = tcx.local_def_id_to_hir_id(def);
410 let (_, parent_node) = tcx
411 .hir_parent_iter(hir_id)
412 .skip_while(|(_, n)| matches!(n, Node::ConstArg(..)))
413 .next()
414 .unwrap();
415 matches!(
416 parent_node,
417 Node::GenericParam(hir::GenericParam { kind: hir::GenericParamKind::Const { .. }, .. })
418 )
419 }
420
421 impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ConstCollector<'tcx> {
422 fn visit_const(&mut self, c: ty::Const<'tcx>) {
423 if let ty::ConstKind::Unevaluated(uv) = c.kind() {
424 if let Some(local) = uv.def.as_local()
425 && is_const_param_default(self.tcx, local)
426 {
427 return;
435 }
436
437 let span = self.tcx.def_span(uv.def);
438 self.preds.insert((ty::ClauseKind::ConstEvaluatable(c).upcast(self.tcx), span));
439 }
440 }
441 }
442
443 let hir_id = tcx.local_def_id_to_hir_id(def_id);
444 let node = tcx.hir_node(hir_id);
445
446 let mut collector = ConstCollector { tcx, preds: FxIndexSet::default() };
447
448 for (clause, _sp) in predicates {
449 clause.visit_with(&mut collector);
450 }
451
452 if let hir::Node::Item(item) = node
453 && let hir::ItemKind::Impl(_) = item.kind
454 {
455 if let Some(of_trait) = tcx.impl_trait_ref(def_id) {
456 debug!("visit impl trait_ref");
457 of_trait.instantiate_identity().visit_with(&mut collector);
458 }
459
460 debug!("visit self_ty");
461 let self_ty = tcx.type_of(def_id);
462 self_ty.instantiate_identity().visit_with(&mut collector);
463 }
464
465 if let Some(_) = tcx.hir_fn_sig_by_hir_id(hir_id) {
466 debug!("visit fn sig");
467 let fn_sig = tcx.fn_sig(def_id);
468 let fn_sig = fn_sig.instantiate_identity();
469 debug!(?fn_sig);
470 fn_sig.visit_with(&mut collector);
471 }
472
473 collector.preds
474}
475
476pub(super) fn trait_explicit_predicates_and_bounds(
477 tcx: TyCtxt<'_>,
478 def_id: LocalDefId,
479) -> ty::GenericPredicates<'_> {
480 assert_eq!(tcx.def_kind(def_id), DefKind::Trait);
481 gather_explicit_predicates_of(tcx, def_id)
482}
483
484pub(super) fn explicit_predicates_of<'tcx>(
485 tcx: TyCtxt<'tcx>,
486 def_id: LocalDefId,
487) -> ty::GenericPredicates<'tcx> {
488 let def_kind = tcx.def_kind(def_id);
489 if let DefKind::Trait = def_kind {
490 let predicates_and_bounds = tcx.trait_explicit_predicates_and_bounds(def_id);
493 let trait_identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
494
495 let is_assoc_item_ty = |ty: Ty<'tcx>| {
496 if let ty::Alias(ty::Projection, projection) = ty.kind() {
506 projection.args == trait_identity_args
507 && !tcx.is_impl_trait_in_trait(projection.def_id)
509 && tcx.associated_item(projection.def_id).container_id(tcx)
510 == def_id.to_def_id()
511 } else {
512 false
513 }
514 };
515
516 let predicates: Vec<_> = predicates_and_bounds
517 .predicates
518 .iter()
519 .copied()
520 .filter(|(pred, _)| match pred.kind().skip_binder() {
521 ty::ClauseKind::Trait(tr) => !is_assoc_item_ty(tr.self_ty()),
522 ty::ClauseKind::Projection(proj) => {
523 !is_assoc_item_ty(proj.projection_term.self_ty())
524 }
525 ty::ClauseKind::TypeOutlives(outlives) => !is_assoc_item_ty(outlives.0),
526 _ => true,
527 })
528 .collect();
529 if predicates.len() == predicates_and_bounds.predicates.len() {
530 predicates_and_bounds
531 } else {
532 ty::GenericPredicates {
533 parent: predicates_and_bounds.parent,
534 predicates: tcx.arena.alloc_slice(&predicates),
535 }
536 }
537 } else {
538 if matches!(def_kind, DefKind::AnonConst)
539 && tcx.features().generic_const_exprs()
540 && let Some(defaulted_param_def_id) =
541 tcx.hir_opt_const_param_default_param_def_id(tcx.local_def_id_to_hir_id(def_id))
542 {
543 let parent_def_id = tcx.local_parent(def_id);
556 let parent_preds = tcx.explicit_predicates_of(parent_def_id);
557
558 let filtered_predicates = parent_preds
562 .predicates
563 .into_iter()
564 .filter(|(pred, _)| {
565 if let ty::ClauseKind::ConstArgHasType(ct, _) = pred.kind().skip_binder() {
566 match ct.kind() {
567 ty::ConstKind::Param(param_const) => {
568 let defaulted_param_idx = tcx
569 .generics_of(parent_def_id)
570 .param_def_id_to_index[&defaulted_param_def_id.to_def_id()];
571 param_const.index < defaulted_param_idx
572 }
573 _ => bug!(
574 "`ConstArgHasType` in `predicates_of`\
575 that isn't a `Param` const"
576 ),
577 }
578 } else {
579 true
580 }
581 })
582 .cloned();
583 return GenericPredicates {
584 parent: parent_preds.parent,
585 predicates: { tcx.arena.alloc_from_iter(filtered_predicates) },
586 };
587 }
588 gather_explicit_predicates_of(tcx, def_id)
589 }
590}
591
592pub(super) fn explicit_super_predicates_of<'tcx>(
596 tcx: TyCtxt<'tcx>,
597 trait_def_id: LocalDefId,
598) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
599 implied_predicates_with_filter(tcx, trait_def_id.to_def_id(), PredicateFilter::SelfOnly)
600}
601
602pub(super) fn explicit_supertraits_containing_assoc_item<'tcx>(
603 tcx: TyCtxt<'tcx>,
604 (trait_def_id, assoc_ident): (DefId, Ident),
605) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
606 implied_predicates_with_filter(
607 tcx,
608 trait_def_id,
609 PredicateFilter::SelfTraitThatDefines(assoc_ident),
610 )
611}
612
613pub(super) fn explicit_implied_predicates_of<'tcx>(
614 tcx: TyCtxt<'tcx>,
615 trait_def_id: LocalDefId,
616) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
617 implied_predicates_with_filter(
618 tcx,
619 trait_def_id.to_def_id(),
620 if tcx.is_trait_alias(trait_def_id.to_def_id()) {
621 PredicateFilter::All
622 } else {
623 PredicateFilter::SelfAndAssociatedTypeBounds
624 },
625 )
626}
627
628pub(super) fn implied_predicates_with_filter<'tcx>(
632 tcx: TyCtxt<'tcx>,
633 trait_def_id: DefId,
634 filter: PredicateFilter,
635) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
636 let Some(trait_def_id) = trait_def_id.as_local() else {
637 assert_matches!(filter, PredicateFilter::SelfTraitThatDefines(_));
640 return tcx.explicit_super_predicates_of(trait_def_id);
641 };
642
643 let Node::Item(item) = tcx.hir_node_by_def_id(trait_def_id) else {
644 bug!("trait_def_id {trait_def_id:?} is not an item");
645 };
646
647 let (generics, superbounds) = match item.kind {
648 hir::ItemKind::Trait(.., generics, supertraits, _) => (generics, supertraits),
649 hir::ItemKind::TraitAlias(_, generics, supertraits) => (generics, supertraits),
650 _ => span_bug!(item.span, "super_predicates invoked on non-trait"),
651 };
652
653 let icx = ItemCtxt::new(tcx, trait_def_id);
654
655 let self_param_ty = tcx.types.self_param;
656 let mut bounds = Vec::new();
657 icx.lowerer().lower_bounds(self_param_ty, superbounds, &mut bounds, ty::List::empty(), filter);
658 match filter {
659 PredicateFilter::All
660 | PredicateFilter::SelfOnly
661 | PredicateFilter::SelfTraitThatDefines(_)
662 | PredicateFilter::SelfAndAssociatedTypeBounds => {
663 icx.lowerer().add_default_super_traits(
664 trait_def_id,
665 &mut bounds,
666 superbounds,
667 generics,
668 item.span,
669 );
670 }
671 PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
673 }
674
675 let where_bounds_that_match =
676 icx.probe_ty_param_bounds_in_generics(generics, item.owner_id.def_id, filter);
677
678 let implied_bounds =
680 &*tcx.arena.alloc_from_iter(bounds.into_iter().chain(where_bounds_that_match));
681 debug!(?implied_bounds);
682
683 match filter {
688 PredicateFilter::SelfOnly => {
689 for &(pred, span) in implied_bounds {
690 debug!("superbound: {:?}", pred);
691 if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
692 && bound.polarity == ty::PredicatePolarity::Positive
693 {
694 tcx.at(span).explicit_super_predicates_of(bound.def_id());
695 }
696 }
697 }
698 PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
699 for &(pred, span) in implied_bounds {
700 debug!("superbound: {:?}", pred);
701 if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
702 && bound.polarity == ty::PredicatePolarity::Positive
703 {
704 tcx.at(span).explicit_implied_predicates_of(bound.def_id());
705 }
706 }
707 }
708 _ => {}
709 }
710
711 assert_only_contains_predicates_from(filter, implied_bounds, tcx.types.self_param);
712
713 ty::EarlyBinder::bind(implied_bounds)
714}
715
716pub(super) fn assert_only_contains_predicates_from<'tcx>(
720 filter: PredicateFilter,
721 bounds: &'tcx [(ty::Clause<'tcx>, Span)],
722 ty: Ty<'tcx>,
723) {
724 if !cfg!(debug_assertions) {
725 return;
726 }
727
728 match filter {
729 PredicateFilter::SelfOnly => {
730 for (clause, _) in bounds {
731 match clause.kind().skip_binder() {
732 ty::ClauseKind::Trait(trait_predicate) => {
733 assert_eq!(
734 trait_predicate.self_ty(),
735 ty,
736 "expected `Self` predicate when computing \
737 `{filter:?}` implied bounds: {clause:?}"
738 );
739 }
740 ty::ClauseKind::Projection(projection_predicate) => {
741 assert_eq!(
742 projection_predicate.self_ty(),
743 ty,
744 "expected `Self` predicate when computing \
745 `{filter:?}` implied bounds: {clause:?}"
746 );
747 }
748 ty::ClauseKind::TypeOutlives(outlives_predicate) => {
749 assert_eq!(
750 outlives_predicate.0, ty,
751 "expected `Self` predicate when computing \
752 `{filter:?}` implied bounds: {clause:?}"
753 );
754 }
755 ty::ClauseKind::HostEffect(host_effect_predicate) => {
756 assert_eq!(
757 host_effect_predicate.self_ty(),
758 ty,
759 "expected `Self` predicate when computing \
760 `{filter:?}` implied bounds: {clause:?}"
761 );
762 }
763
764 ty::ClauseKind::RegionOutlives(_)
765 | ty::ClauseKind::ConstArgHasType(_, _)
766 | ty::ClauseKind::WellFormed(_)
767 | ty::ClauseKind::ConstEvaluatable(_) => {
768 bug!(
769 "unexpected non-`Self` predicate when computing \
770 `{filter:?}` implied bounds: {clause:?}"
771 );
772 }
773 }
774 }
775 }
776 PredicateFilter::SelfTraitThatDefines(_) => {
777 for (clause, _) in bounds {
778 match clause.kind().skip_binder() {
779 ty::ClauseKind::Trait(trait_predicate) => {
780 assert_eq!(
781 trait_predicate.self_ty(),
782 ty,
783 "expected `Self` predicate when computing \
784 `{filter:?}` implied bounds: {clause:?}"
785 );
786 }
787
788 ty::ClauseKind::Projection(_)
789 | ty::ClauseKind::TypeOutlives(_)
790 | ty::ClauseKind::RegionOutlives(_)
791 | ty::ClauseKind::ConstArgHasType(_, _)
792 | ty::ClauseKind::WellFormed(_)
793 | ty::ClauseKind::ConstEvaluatable(_)
794 | ty::ClauseKind::HostEffect(..) => {
795 bug!(
796 "unexpected non-`Self` predicate when computing \
797 `{filter:?}` implied bounds: {clause:?}"
798 );
799 }
800 }
801 }
802 }
803 PredicateFilter::ConstIfConst => {
804 for (clause, _) in bounds {
805 match clause.kind().skip_binder() {
806 ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
807 trait_ref: _,
808 constness: ty::BoundConstness::Maybe,
809 }) => {}
810 _ => {
811 bug!(
812 "unexpected non-`HostEffect` predicate when computing \
813 `{filter:?}` implied bounds: {clause:?}"
814 );
815 }
816 }
817 }
818 }
819 PredicateFilter::SelfConstIfConst => {
820 for (clause, _) in bounds {
821 match clause.kind().skip_binder() {
822 ty::ClauseKind::HostEffect(pred) => {
823 assert_eq!(
824 pred.constness,
825 ty::BoundConstness::Maybe,
826 "expected `[const]` predicate when computing `{filter:?}` \
827 implied bounds: {clause:?}",
828 );
829 assert_eq!(
830 pred.trait_ref.self_ty(),
831 ty,
832 "expected `Self` predicate when computing `{filter:?}` \
833 implied bounds: {clause:?}"
834 );
835 }
836 _ => {
837 bug!(
838 "unexpected non-`HostEffect` predicate when computing \
839 `{filter:?}` implied bounds: {clause:?}"
840 );
841 }
842 }
843 }
844 }
845 PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {}
846 }
847}
848
849#[instrument(level = "trace", skip(tcx))]
852pub(super) fn type_param_predicates<'tcx>(
853 tcx: TyCtxt<'tcx>,
854 (item_def_id, def_id, assoc_ident): (LocalDefId, LocalDefId, Ident),
855) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
856 match tcx.opt_rpitit_info(item_def_id.to_def_id()) {
857 Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
858 return tcx.type_param_predicates((opaque_def_id.expect_local(), def_id, assoc_ident));
859 }
860 Some(ty::ImplTraitInTraitData::Impl { .. }) => {
861 unreachable!("should not be lowering bounds on RPITIT in impl")
862 }
863 None => {}
864 }
865
866 let param_id = tcx.local_def_id_to_hir_id(def_id);
871 let param_owner = tcx.hir_ty_param_owner(def_id);
872
873 let parent = if item_def_id == param_owner {
875 None
877 } else {
878 tcx.generics_of(item_def_id).parent.map(|def_id| def_id.expect_local())
879 };
880
881 let result = if let Some(parent) = parent {
882 let icx = ItemCtxt::new(tcx, parent);
883 icx.probe_ty_param_bounds(DUMMY_SP, def_id, assoc_ident)
884 } else {
885 ty::EarlyBinder::bind(&[] as &[_])
886 };
887 let mut extend = None;
888
889 let item_hir_id = tcx.local_def_id_to_hir_id(item_def_id);
890
891 let hir_node = tcx.hir_node(item_hir_id);
892 let Some(hir_generics) = hir_node.generics() else {
893 return result;
894 };
895
896 if let Node::Item(item) = hir_node
897 && let hir::ItemKind::Trait(..) = item.kind
898 && param_id == item_hir_id
900 {
901 let identity_trait_ref = ty::TraitRef::identity(tcx, item_def_id.to_def_id());
902 extend = Some((identity_trait_ref.upcast(tcx), item.span));
903 }
904
905 let icx = ItemCtxt::new(tcx, item_def_id);
906 let extra_predicates = extend.into_iter().chain(icx.probe_ty_param_bounds_in_generics(
907 hir_generics,
908 def_id,
909 PredicateFilter::SelfTraitThatDefines(assoc_ident),
910 ));
911
912 let bounds =
913 &*tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates));
914
915 let self_ty = match tcx.def_kind(def_id) {
917 DefKind::TyParam => Ty::new_param(
918 tcx,
919 tcx.generics_of(item_def_id)
920 .param_def_id_to_index(tcx, def_id.to_def_id())
921 .expect("expected generic param to be owned by item"),
922 tcx.item_name(def_id.to_def_id()),
923 ),
924 DefKind::Trait | DefKind::TraitAlias => tcx.types.self_param,
925 _ => unreachable!(),
926 };
927 assert_only_contains_predicates_from(
928 PredicateFilter::SelfTraitThatDefines(assoc_ident),
929 bounds,
930 self_ty,
931 );
932
933 ty::EarlyBinder::bind(bounds)
934}
935
936impl<'tcx> ItemCtxt<'tcx> {
937 #[instrument(level = "trace", skip(self, hir_generics))]
943 fn probe_ty_param_bounds_in_generics(
944 &self,
945 hir_generics: &'tcx hir::Generics<'tcx>,
946 param_def_id: LocalDefId,
947 filter: PredicateFilter,
948 ) -> Vec<(ty::Clause<'tcx>, Span)> {
949 let mut bounds = Vec::new();
950
951 for predicate in hir_generics.predicates {
952 let hir_id = predicate.hir_id;
953 let hir::WherePredicateKind::BoundPredicate(predicate) = predicate.kind else {
954 continue;
955 };
956
957 match filter {
958 _ if predicate.is_param_bound(param_def_id.to_def_id()) => {
959 }
961 PredicateFilter::All => {
962 }
964 PredicateFilter::SelfOnly
965 | PredicateFilter::SelfTraitThatDefines(_)
966 | PredicateFilter::SelfConstIfConst
967 | PredicateFilter::SelfAndAssociatedTypeBounds => continue,
968 PredicateFilter::ConstIfConst => unreachable!(),
969 }
970
971 let bound_ty = self.lowerer().lower_ty_maybe_return_type_notation(predicate.bounded_ty);
972
973 let bound_vars = self.tcx.late_bound_vars(hir_id);
974 self.lowerer().lower_bounds(
975 bound_ty,
976 predicate.bounds,
977 &mut bounds,
978 bound_vars,
979 filter,
980 );
981 }
982
983 bounds
984 }
985}
986
987pub(super) fn const_conditions<'tcx>(
988 tcx: TyCtxt<'tcx>,
989 def_id: LocalDefId,
990) -> ty::ConstConditions<'tcx> {
991 if !tcx.is_conditionally_const(def_id) {
992 bug!("const_conditions invoked for item that is not conditionally const: {def_id:?}");
993 }
994
995 match tcx.opt_rpitit_info(def_id.to_def_id()) {
996 Some(
998 ty::ImplTraitInTraitData::Impl { fn_def_id }
999 | ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
1000 ) => return tcx.const_conditions(fn_def_id),
1001 None => {}
1002 }
1003
1004 let (generics, trait_def_id_and_supertraits, has_parent) = match tcx.hir_node_by_def_id(def_id)
1005 {
1006 Node::Item(item) => match item.kind {
1007 hir::ItemKind::Impl(impl_) => (impl_.generics, None, false),
1008 hir::ItemKind::Fn { generics, .. } => (generics, None, false),
1009 hir::ItemKind::Trait(_, _, _, generics, supertraits, _) => {
1010 (generics, Some((item.owner_id.def_id, supertraits)), false)
1011 }
1012 _ => bug!("const_conditions called on wrong item: {def_id:?}"),
1013 },
1014 Node::TraitItem(item) => match item.kind {
1019 hir::TraitItemKind::Fn(_, _) | hir::TraitItemKind::Type(_, _) => {
1020 (item.generics, None, true)
1021 }
1022 _ => bug!("const_conditions called on wrong item: {def_id:?}"),
1023 },
1024 Node::ImplItem(item) => match item.kind {
1025 hir::ImplItemKind::Fn(_, _) | hir::ImplItemKind::Type(_) => {
1026 (item.generics, None, tcx.is_conditionally_const(tcx.local_parent(def_id)))
1027 }
1028 _ => bug!("const_conditions called on wrong item: {def_id:?}"),
1029 },
1030 Node::ForeignItem(item) => match item.kind {
1031 hir::ForeignItemKind::Fn(_, _, generics) => (generics, None, false),
1032 _ => bug!("const_conditions called on wrong item: {def_id:?}"),
1033 },
1034 Node::OpaqueTy(opaque) => match opaque.origin {
1035 hir::OpaqueTyOrigin::FnReturn { parent, .. } => return tcx.const_conditions(parent),
1036 hir::OpaqueTyOrigin::AsyncFn { .. } | hir::OpaqueTyOrigin::TyAlias { .. } => {
1037 unreachable!()
1038 }
1039 },
1040 Node::Ctor(hir::VariantData::Tuple { .. }) => return Default::default(),
1042 _ => bug!("const_conditions called on wrong item: {def_id:?}"),
1043 };
1044
1045 let icx = ItemCtxt::new(tcx, def_id);
1046 let mut bounds = Vec::new();
1047
1048 for pred in generics.predicates {
1049 match pred.kind {
1050 hir::WherePredicateKind::BoundPredicate(bound_pred) => {
1051 let ty = icx.lowerer().lower_ty_maybe_return_type_notation(bound_pred.bounded_ty);
1052 let bound_vars = tcx.late_bound_vars(pred.hir_id);
1053 icx.lowerer().lower_bounds(
1054 ty,
1055 bound_pred.bounds.iter(),
1056 &mut bounds,
1057 bound_vars,
1058 PredicateFilter::ConstIfConst,
1059 );
1060 }
1061 _ => {}
1062 }
1063 }
1064
1065 if let Some((def_id, supertraits)) = trait_def_id_and_supertraits {
1066 bounds.push((
1068 ty::Binder::dummy(ty::TraitRef::identity(tcx, def_id.to_def_id()))
1069 .to_host_effect_clause(tcx, ty::BoundConstness::Maybe),
1070 DUMMY_SP,
1071 ));
1072
1073 icx.lowerer().lower_bounds(
1074 tcx.types.self_param,
1075 supertraits,
1076 &mut bounds,
1077 ty::List::empty(),
1078 PredicateFilter::ConstIfConst,
1079 );
1080 }
1081
1082 ty::ConstConditions {
1083 parent: has_parent.then(|| tcx.local_parent(def_id).to_def_id()),
1084 predicates: tcx.arena.alloc_from_iter(bounds.into_iter().map(|(clause, span)| {
1085 (
1086 clause.kind().map_bound(|clause| match clause {
1087 ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
1088 trait_ref,
1089 constness: ty::BoundConstness::Maybe,
1090 }) => trait_ref,
1091 _ => bug!("converted {clause:?}"),
1092 }),
1093 span,
1094 )
1095 })),
1096 }
1097}
1098
1099pub(super) fn explicit_implied_const_bounds<'tcx>(
1100 tcx: TyCtxt<'tcx>,
1101 def_id: LocalDefId,
1102) -> ty::EarlyBinder<'tcx, &'tcx [(ty::PolyTraitRef<'tcx>, Span)]> {
1103 if !tcx.is_conditionally_const(def_id) {
1104 bug!(
1105 "explicit_implied_const_bounds invoked for item that is not conditionally const: {def_id:?}"
1106 );
1107 }
1108
1109 let bounds = match tcx.opt_rpitit_info(def_id.to_def_id()) {
1110 Some(ty::ImplTraitInTraitData::Trait { .. }) => {
1113 explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
1114 }
1115 Some(ty::ImplTraitInTraitData::Impl { .. }) => {
1116 span_bug!(tcx.def_span(def_id), "RPITIT in impl should not have item bounds")
1117 }
1118 None => match tcx.hir_node_by_def_id(def_id) {
1119 Node::Item(hir::Item { kind: hir::ItemKind::Trait(..), .. }) => {
1120 implied_predicates_with_filter(
1121 tcx,
1122 def_id.to_def_id(),
1123 PredicateFilter::SelfConstIfConst,
1124 )
1125 }
1126 Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. })
1127 | Node::OpaqueTy(_) => {
1128 explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
1129 }
1130 _ => bug!("explicit_implied_const_bounds called on wrong item: {def_id:?}"),
1131 },
1132 };
1133
1134 bounds.map_bound(|bounds| {
1135 &*tcx.arena.alloc_from_iter(bounds.iter().copied().map(|(clause, span)| {
1136 (
1137 clause.kind().map_bound(|clause| match clause {
1138 ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
1139 trait_ref,
1140 constness: ty::BoundConstness::Maybe,
1141 }) => trait_ref,
1142 _ => bug!("converted {clause:?}"),
1143 }),
1144 span,
1145 )
1146 }))
1147 })
1148}