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