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::{
22 HirTyLowerer, OverlappingAsssocItemConstraints, PredicateFilter, RegionInferReason,
23};
24
25#[instrument(level = "debug", skip(tcx))]
29pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
30 let mut result = tcx.explicit_predicates_of(def_id);
31 debug!("predicates_of: explicit_predicates_of({:?}) = {:?}", def_id, result);
32
33 let inferred_outlives = tcx.inferred_outlives_of(def_id);
34 if !inferred_outlives.is_empty() {
35 debug!("predicates_of: inferred_outlives_of({:?}) = {:?}", def_id, inferred_outlives,);
36 let inferred_outlives_iter =
37 inferred_outlives.iter().map(|(clause, span)| ((*clause).upcast(tcx), *span));
38 if result.predicates.is_empty() {
39 result.predicates = tcx.arena.alloc_from_iter(inferred_outlives_iter);
40 } else {
41 result.predicates = tcx.arena.alloc_from_iter(
42 result.predicates.into_iter().copied().chain(inferred_outlives_iter),
43 );
44 }
45 }
46
47 if tcx.is_trait(def_id) {
48 let span = DUMMY_SP;
66
67 result.predicates = tcx.arena.alloc_from_iter(
68 result
69 .predicates
70 .iter()
71 .copied()
72 .chain(std::iter::once((ty::TraitRef::identity(tcx, def_id).upcast(tcx), span))),
73 );
74 }
75
76 debug!("predicates_of({:?}) = {:?}", def_id, result);
77 result
78}
79
80#[instrument(level = "trace", skip(tcx), ret)]
83fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::GenericPredicates<'_> {
84 use rustc_hir::*;
85
86 match tcx.opt_rpitit_info(def_id.to_def_id()) {
87 Some(ImplTraitInTraitData::Trait { fn_def_id, .. }) => {
88 let mut predicates = Vec::new();
89
90 let identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
95 predicates
96 .extend(tcx.explicit_predicates_of(fn_def_id).instantiate_own(tcx, identity_args));
97
98 compute_bidirectional_outlives_predicates(
103 tcx,
104 &tcx.generics_of(def_id.to_def_id()).own_params
105 [tcx.generics_of(fn_def_id).own_params.len()..],
106 &mut predicates,
107 );
108
109 return ty::GenericPredicates {
110 parent: Some(tcx.parent(def_id.to_def_id())),
111 predicates: tcx.arena.alloc_from_iter(predicates),
112 };
113 }
114
115 Some(ImplTraitInTraitData::Impl { fn_def_id }) => {
116 let trait_item_def_id = tcx.trait_item_of(def_id).unwrap();
117 let trait_assoc_predicates = tcx.explicit_predicates_of(trait_item_def_id);
118
119 let impl_assoc_identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
120 let impl_def_id = tcx.parent(fn_def_id);
121 let impl_trait_ref_args =
122 tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity().args;
123
124 let impl_assoc_args =
125 impl_assoc_identity_args.rebase_onto(tcx, impl_def_id, impl_trait_ref_args);
126
127 let impl_predicates = trait_assoc_predicates.instantiate_own(tcx, impl_assoc_args);
128
129 return ty::GenericPredicates {
130 parent: Some(impl_def_id),
131 predicates: tcx.arena.alloc_from_iter(impl_predicates),
132 };
133 }
134
135 None => {}
136 }
137
138 let hir_id = tcx.local_def_id_to_hir_id(def_id);
139 let node = tcx.hir_node(hir_id);
140
141 if let Some(sig) = node.fn_sig()
142 && let Some(sig_id) = sig.decl.opt_delegation_sig_id()
143 {
144 return inherit_predicates_for_delegation_item(tcx, def_id, sig_id);
145 }
146
147 let mut is_trait = None;
148 let mut is_default_impl_trait = None;
149
150 let icx = ItemCtxt::new(tcx, def_id);
151
152 const NO_GENERICS: &hir::Generics<'_> = hir::Generics::empty();
153
154 let mut predicates: FxIndexSet<(ty::Clause<'_>, Span)> = FxIndexSet::default();
157
158 let hir_generics = node.generics().unwrap_or(NO_GENERICS);
159 if let Node::Item(item) = node {
160 match item.kind {
161 ItemKind::Impl(impl_) => {
162 if let Some(of_trait) = impl_.of_trait
163 && of_trait.defaultness.is_default()
164 {
165 is_default_impl_trait = tcx
166 .impl_trait_ref(def_id)
167 .map(|t| ty::Binder::dummy(t.instantiate_identity()));
168 }
169 }
170 ItemKind::Trait(_, _, _, _, _, self_bounds, ..)
171 | ItemKind::TraitAlias(_, _, self_bounds) => {
172 is_trait = Some((self_bounds, item.span));
173 }
174 _ => {}
175 }
176 };
177
178 let generics = tcx.generics_of(def_id);
179
180 if let Some((self_bounds, span)) = is_trait {
185 let mut bounds = Vec::new();
186 icx.lowerer().lower_bounds(
187 tcx.types.self_param,
188 self_bounds,
189 &mut bounds,
190 ty::List::empty(),
191 PredicateFilter::All,
192 OverlappingAsssocItemConstraints::Allowed,
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 let bound_vars = tcx.late_bound_vars(predicate.hir_id);
270
271 if bound_pred.bounds.is_empty() {
273 if let ty::Param(_) = ty.kind() {
274 } else {
276 let span = bound_pred.bounded_ty.span;
280 let predicate = ty::Binder::bind_with_vars(
281 ty::ClauseKind::WellFormed(ty.into()),
282 bound_vars,
283 );
284 predicates.insert((predicate.upcast(tcx), span));
285 }
286 }
287
288 let mut bounds = Vec::new();
289 icx.lowerer().lower_bounds(
290 ty,
291 bound_pred.bounds,
292 &mut bounds,
293 bound_vars,
294 PredicateFilter::All,
295 OverlappingAsssocItemConstraints::Allowed,
296 );
297 predicates.extend(bounds);
298 }
299
300 hir::WherePredicateKind::RegionPredicate(region_pred) => {
301 let r1 = icx
302 .lowerer()
303 .lower_lifetime(region_pred.lifetime, RegionInferReason::RegionPredicate);
304 predicates.extend(region_pred.bounds.iter().map(|bound| {
305 let (r2, span) = match bound {
306 hir::GenericBound::Outlives(lt) => (
307 icx.lowerer().lower_lifetime(lt, RegionInferReason::RegionPredicate),
308 lt.ident.span,
309 ),
310 bound => {
311 span_bug!(
312 bound.span(),
313 "lifetime param bounds must be outlives, but found {bound:?}"
314 )
315 }
316 };
317 let pred =
318 ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(r1, r2)).upcast(tcx);
319 (pred, span)
320 }))
321 }
322
323 hir::WherePredicateKind::EqPredicate(..) => {
324 }
326 }
327 }
328
329 if tcx.features().generic_const_exprs() {
330 predicates.extend(const_evaluatable_predicates_of(tcx, def_id, &predicates));
331 }
332
333 let attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(def_id));
334 let allow_unstable_feature_attr =
338 find_attr!(attrs, AttributeKind::UnstableFeatureBound(i) => i)
339 .map(|i| i.as_slice())
340 .unwrap_or_default();
341
342 for (feat_name, span) in allow_unstable_feature_attr {
343 predicates.insert((ty::ClauseKind::UnstableFeature(*feat_name).upcast(tcx), *span));
344 }
345
346 let mut predicates: Vec<_> = predicates.into_iter().collect();
347
348 if let Node::Item(&Item { kind: ItemKind::Impl { .. }, .. }) = node {
354 let self_ty = tcx.type_of(def_id).instantiate_identity();
355 let trait_ref = tcx.impl_trait_ref(def_id).map(ty::EarlyBinder::instantiate_identity);
356 cgp::setup_constraining_predicates(
357 tcx,
358 &mut predicates,
359 trait_ref,
360 &mut cgp::parameters_for_impl(tcx, self_ty, trait_ref),
361 );
362 }
363
364 if let Node::OpaqueTy(..) = node {
368 compute_bidirectional_outlives_predicates(tcx, &generics.own_params, &mut predicates);
369 debug!(?predicates);
370 }
371
372 ty::GenericPredicates {
373 parent: generics.parent,
374 predicates: tcx.arena.alloc_from_iter(predicates),
375 }
376}
377
378fn compute_bidirectional_outlives_predicates<'tcx>(
381 tcx: TyCtxt<'tcx>,
382 opaque_own_params: &[ty::GenericParamDef],
383 predicates: &mut Vec<(ty::Clause<'tcx>, Span)>,
384) {
385 for param in opaque_own_params {
386 let orig_lifetime = tcx.map_opaque_lifetime_to_parent_lifetime(param.def_id.expect_local());
387 if let ty::ReEarlyParam(..) = orig_lifetime.kind() {
388 let dup_lifetime = ty::Region::new_early_param(
389 tcx,
390 ty::EarlyParamRegion { index: param.index, name: param.name },
391 );
392 let span = tcx.def_span(param.def_id);
393 predicates.push((
394 ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(orig_lifetime, dup_lifetime))
395 .upcast(tcx),
396 span,
397 ));
398 predicates.push((
399 ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(dup_lifetime, orig_lifetime))
400 .upcast(tcx),
401 span,
402 ));
403 }
404 }
405}
406
407#[instrument(level = "debug", skip(tcx, predicates), ret)]
408fn const_evaluatable_predicates_of<'tcx>(
409 tcx: TyCtxt<'tcx>,
410 def_id: LocalDefId,
411 predicates: &FxIndexSet<(ty::Clause<'tcx>, Span)>,
412) -> FxIndexSet<(ty::Clause<'tcx>, Span)> {
413 struct ConstCollector<'tcx> {
414 tcx: TyCtxt<'tcx>,
415 preds: FxIndexSet<(ty::Clause<'tcx>, Span)>,
416 }
417
418 fn is_const_param_default(tcx: TyCtxt<'_>, def: LocalDefId) -> bool {
419 let hir_id = tcx.local_def_id_to_hir_id(def);
420 let (_, parent_node) = tcx
421 .hir_parent_iter(hir_id)
422 .skip_while(|(_, n)| matches!(n, Node::ConstArg(..)))
423 .next()
424 .unwrap();
425 matches!(
426 parent_node,
427 Node::GenericParam(hir::GenericParam { kind: hir::GenericParamKind::Const { .. }, .. })
428 )
429 }
430
431 impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ConstCollector<'tcx> {
432 fn visit_const(&mut self, c: ty::Const<'tcx>) {
433 if let ty::ConstKind::Unevaluated(uv) = c.kind() {
434 if let Some(local) = uv.def.as_local()
435 && is_const_param_default(self.tcx, local)
436 {
437 return;
445 }
446
447 let span = self.tcx.def_span(uv.def);
448 self.preds.insert((ty::ClauseKind::ConstEvaluatable(c).upcast(self.tcx), span));
449 }
450 }
451 }
452
453 let hir_id = tcx.local_def_id_to_hir_id(def_id);
454 let node = tcx.hir_node(hir_id);
455
456 let mut collector = ConstCollector { tcx, preds: FxIndexSet::default() };
457
458 for (clause, _sp) in predicates {
459 clause.visit_with(&mut collector);
460 }
461
462 if let hir::Node::Item(item) = node
463 && let hir::ItemKind::Impl(_) = item.kind
464 {
465 if let Some(of_trait) = tcx.impl_trait_ref(def_id) {
466 debug!("visit impl trait_ref");
467 of_trait.instantiate_identity().visit_with(&mut collector);
468 }
469
470 debug!("visit self_ty");
471 let self_ty = tcx.type_of(def_id);
472 self_ty.instantiate_identity().visit_with(&mut collector);
473 }
474
475 if let Some(_) = tcx.hir_fn_sig_by_hir_id(hir_id) {
476 debug!("visit fn sig");
477 let fn_sig = tcx.fn_sig(def_id);
478 let fn_sig = fn_sig.instantiate_identity();
479 debug!(?fn_sig);
480 fn_sig.visit_with(&mut collector);
481 }
482
483 collector.preds
484}
485
486pub(super) fn trait_explicit_predicates_and_bounds(
487 tcx: TyCtxt<'_>,
488 def_id: LocalDefId,
489) -> ty::GenericPredicates<'_> {
490 assert_eq!(tcx.def_kind(def_id), DefKind::Trait);
491 gather_explicit_predicates_of(tcx, def_id)
492}
493
494pub(super) fn explicit_predicates_of<'tcx>(
495 tcx: TyCtxt<'tcx>,
496 def_id: LocalDefId,
497) -> ty::GenericPredicates<'tcx> {
498 let def_kind = tcx.def_kind(def_id);
499 if let DefKind::Trait = def_kind {
500 let predicates_and_bounds = tcx.trait_explicit_predicates_and_bounds(def_id);
503 let trait_identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
504
505 let is_assoc_item_ty = |ty: Ty<'tcx>| {
506 if let ty::Alias(ty::Projection, projection) = ty.kind() {
516 projection.args == trait_identity_args
517 && !tcx.is_impl_trait_in_trait(projection.def_id)
519 && tcx.parent(projection.def_id) == def_id.to_def_id()
520 } else {
521 false
522 }
523 };
524
525 let predicates: Vec<_> = predicates_and_bounds
526 .predicates
527 .iter()
528 .copied()
529 .filter(|(pred, _)| match pred.kind().skip_binder() {
530 ty::ClauseKind::Trait(tr) => !is_assoc_item_ty(tr.self_ty()),
531 ty::ClauseKind::Projection(proj) => {
532 !is_assoc_item_ty(proj.projection_term.self_ty())
533 }
534 ty::ClauseKind::TypeOutlives(outlives) => !is_assoc_item_ty(outlives.0),
535 _ => true,
536 })
537 .collect();
538 if predicates.len() == predicates_and_bounds.predicates.len() {
539 predicates_and_bounds
540 } else {
541 ty::GenericPredicates {
542 parent: predicates_and_bounds.parent,
543 predicates: tcx.arena.alloc_slice(&predicates),
544 }
545 }
546 } else {
547 if matches!(def_kind, DefKind::AnonConst)
548 && tcx.features().generic_const_exprs()
549 && let Some(defaulted_param_def_id) =
550 tcx.hir_opt_const_param_default_param_def_id(tcx.local_def_id_to_hir_id(def_id))
551 {
552 let parent_def_id = tcx.local_parent(def_id);
565 let parent_preds = tcx.explicit_predicates_of(parent_def_id);
566
567 let filtered_predicates = parent_preds
571 .predicates
572 .into_iter()
573 .filter(|(pred, _)| {
574 if let ty::ClauseKind::ConstArgHasType(ct, _) = pred.kind().skip_binder() {
575 match ct.kind() {
576 ty::ConstKind::Param(param_const) => {
577 let defaulted_param_idx = tcx
578 .generics_of(parent_def_id)
579 .param_def_id_to_index[&defaulted_param_def_id.to_def_id()];
580 param_const.index < defaulted_param_idx
581 }
582 _ => bug!(
583 "`ConstArgHasType` in `predicates_of`\
584 that isn't a `Param` const"
585 ),
586 }
587 } else {
588 true
589 }
590 })
591 .cloned();
592 return GenericPredicates {
593 parent: parent_preds.parent,
594 predicates: { tcx.arena.alloc_from_iter(filtered_predicates) },
595 };
596 }
597 gather_explicit_predicates_of(tcx, def_id)
598 }
599}
600
601pub(super) fn explicit_super_predicates_of<'tcx>(
605 tcx: TyCtxt<'tcx>,
606 trait_def_id: LocalDefId,
607) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
608 implied_predicates_with_filter(tcx, trait_def_id.to_def_id(), PredicateFilter::SelfOnly)
609}
610
611pub(super) fn explicit_supertraits_containing_assoc_item<'tcx>(
612 tcx: TyCtxt<'tcx>,
613 (trait_def_id, assoc_ident): (DefId, Ident),
614) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
615 implied_predicates_with_filter(
616 tcx,
617 trait_def_id,
618 PredicateFilter::SelfTraitThatDefines(assoc_ident),
619 )
620}
621
622pub(super) fn explicit_implied_predicates_of<'tcx>(
623 tcx: TyCtxt<'tcx>,
624 trait_def_id: LocalDefId,
625) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
626 implied_predicates_with_filter(
627 tcx,
628 trait_def_id.to_def_id(),
629 if tcx.is_trait_alias(trait_def_id.to_def_id()) {
630 PredicateFilter::All
631 } else {
632 PredicateFilter::SelfAndAssociatedTypeBounds
633 },
634 )
635}
636
637pub(super) fn implied_predicates_with_filter<'tcx>(
641 tcx: TyCtxt<'tcx>,
642 trait_def_id: DefId,
643 filter: PredicateFilter,
644) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
645 let Some(trait_def_id) = trait_def_id.as_local() else {
646 assert_matches!(filter, PredicateFilter::SelfTraitThatDefines(_));
649 return tcx.explicit_super_predicates_of(trait_def_id);
650 };
651
652 let Node::Item(item) = tcx.hir_node_by_def_id(trait_def_id) else {
653 bug!("trait_def_id {trait_def_id:?} is not an item");
654 };
655
656 let (generics, superbounds) = match item.kind {
657 hir::ItemKind::Trait(.., generics, supertraits, _) => (generics, supertraits),
658 hir::ItemKind::TraitAlias(_, generics, supertraits) => (generics, supertraits),
659 _ => span_bug!(item.span, "super_predicates invoked on non-trait"),
660 };
661
662 let icx = ItemCtxt::new(tcx, trait_def_id);
663
664 let self_param_ty = tcx.types.self_param;
665 let mut bounds = Vec::new();
666 icx.lowerer().lower_bounds(
667 self_param_ty,
668 superbounds,
669 &mut bounds,
670 ty::List::empty(),
671 filter,
672 OverlappingAsssocItemConstraints::Allowed,
673 );
674 match filter {
675 PredicateFilter::All
676 | PredicateFilter::SelfOnly
677 | PredicateFilter::SelfTraitThatDefines(_)
678 | PredicateFilter::SelfAndAssociatedTypeBounds => {
679 icx.lowerer().add_default_super_traits(
680 trait_def_id,
681 &mut bounds,
682 superbounds,
683 generics,
684 item.span,
685 );
686 }
687 PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
689 }
690
691 let where_bounds_that_match =
692 icx.probe_ty_param_bounds_in_generics(generics, item.owner_id.def_id, filter);
693
694 let implied_bounds =
696 &*tcx.arena.alloc_from_iter(bounds.into_iter().chain(where_bounds_that_match));
697 debug!(?implied_bounds);
698
699 match filter {
704 PredicateFilter::SelfOnly => {
705 for &(pred, span) in implied_bounds {
706 debug!("superbound: {:?}", pred);
707 if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
708 && bound.polarity == ty::PredicatePolarity::Positive
709 {
710 tcx.at(span).explicit_super_predicates_of(bound.def_id());
711 }
712 }
713 }
714 PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
715 for &(pred, span) in implied_bounds {
716 debug!("superbound: {:?}", pred);
717 if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
718 && bound.polarity == ty::PredicatePolarity::Positive
719 {
720 tcx.at(span).explicit_implied_predicates_of(bound.def_id());
721 }
722 }
723 }
724 _ => {}
725 }
726
727 assert_only_contains_predicates_from(filter, implied_bounds, tcx.types.self_param);
728
729 ty::EarlyBinder::bind(implied_bounds)
730}
731
732pub(super) fn assert_only_contains_predicates_from<'tcx>(
736 filter: PredicateFilter,
737 bounds: &'tcx [(ty::Clause<'tcx>, Span)],
738 ty: Ty<'tcx>,
739) {
740 if !cfg!(debug_assertions) {
741 return;
742 }
743
744 match filter {
745 PredicateFilter::SelfOnly => {
746 for (clause, _) in bounds {
747 match clause.kind().skip_binder() {
748 ty::ClauseKind::Trait(trait_predicate) => {
749 assert_eq!(
750 trait_predicate.self_ty(),
751 ty,
752 "expected `Self` predicate when computing \
753 `{filter:?}` implied bounds: {clause:?}"
754 );
755 }
756 ty::ClauseKind::Projection(projection_predicate) => {
757 assert_eq!(
758 projection_predicate.self_ty(),
759 ty,
760 "expected `Self` predicate when computing \
761 `{filter:?}` implied bounds: {clause:?}"
762 );
763 }
764 ty::ClauseKind::TypeOutlives(outlives_predicate) => {
765 assert_eq!(
766 outlives_predicate.0, ty,
767 "expected `Self` predicate when computing \
768 `{filter:?}` implied bounds: {clause:?}"
769 );
770 }
771 ty::ClauseKind::HostEffect(host_effect_predicate) => {
772 assert_eq!(
773 host_effect_predicate.self_ty(),
774 ty,
775 "expected `Self` predicate when computing \
776 `{filter:?}` implied bounds: {clause:?}"
777 );
778 }
779
780 ty::ClauseKind::RegionOutlives(_)
781 | ty::ClauseKind::ConstArgHasType(_, _)
782 | ty::ClauseKind::WellFormed(_)
783 | ty::ClauseKind::UnstableFeature(_)
784 | ty::ClauseKind::ConstEvaluatable(_) => {
785 bug!(
786 "unexpected non-`Self` predicate when computing \
787 `{filter:?}` implied bounds: {clause:?}"
788 );
789 }
790 }
791 }
792 }
793 PredicateFilter::SelfTraitThatDefines(_) => {
794 for (clause, _) in bounds {
795 match clause.kind().skip_binder() {
796 ty::ClauseKind::Trait(trait_predicate) => {
797 assert_eq!(
798 trait_predicate.self_ty(),
799 ty,
800 "expected `Self` predicate when computing \
801 `{filter:?}` implied bounds: {clause:?}"
802 );
803 }
804
805 ty::ClauseKind::Projection(_)
806 | ty::ClauseKind::TypeOutlives(_)
807 | ty::ClauseKind::RegionOutlives(_)
808 | ty::ClauseKind::ConstArgHasType(_, _)
809 | ty::ClauseKind::WellFormed(_)
810 | ty::ClauseKind::ConstEvaluatable(_)
811 | ty::ClauseKind::UnstableFeature(_)
812 | ty::ClauseKind::HostEffect(..) => {
813 bug!(
814 "unexpected non-`Self` predicate when computing \
815 `{filter:?}` implied bounds: {clause:?}"
816 );
817 }
818 }
819 }
820 }
821 PredicateFilter::ConstIfConst => {
822 for (clause, _) in bounds {
823 match clause.kind().skip_binder() {
824 ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
825 trait_ref: _,
826 constness: ty::BoundConstness::Maybe,
827 }) => {}
828 _ => {
829 bug!(
830 "unexpected non-`HostEffect` predicate when computing \
831 `{filter:?}` implied bounds: {clause:?}"
832 );
833 }
834 }
835 }
836 }
837 PredicateFilter::SelfConstIfConst => {
838 for (clause, _) in bounds {
839 match clause.kind().skip_binder() {
840 ty::ClauseKind::HostEffect(pred) => {
841 assert_eq!(
842 pred.constness,
843 ty::BoundConstness::Maybe,
844 "expected `[const]` predicate when computing `{filter:?}` \
845 implied bounds: {clause:?}",
846 );
847 assert_eq!(
848 pred.trait_ref.self_ty(),
849 ty,
850 "expected `Self` predicate when computing `{filter:?}` \
851 implied bounds: {clause:?}"
852 );
853 }
854 _ => {
855 bug!(
856 "unexpected non-`HostEffect` predicate when computing \
857 `{filter:?}` implied bounds: {clause:?}"
858 );
859 }
860 }
861 }
862 }
863 PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {}
864 }
865}
866
867#[instrument(level = "trace", skip(tcx))]
870pub(super) fn type_param_predicates<'tcx>(
871 tcx: TyCtxt<'tcx>,
872 (item_def_id, def_id, assoc_ident): (LocalDefId, LocalDefId, Ident),
873) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
874 match tcx.opt_rpitit_info(item_def_id.to_def_id()) {
875 Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
876 return tcx.type_param_predicates((opaque_def_id.expect_local(), def_id, assoc_ident));
877 }
878 Some(ty::ImplTraitInTraitData::Impl { .. }) => {
879 unreachable!("should not be lowering bounds on RPITIT in impl")
880 }
881 None => {}
882 }
883
884 let param_id = tcx.local_def_id_to_hir_id(def_id);
889 let param_owner = tcx.hir_ty_param_owner(def_id);
890
891 let parent = if item_def_id == param_owner {
893 None
895 } else {
896 tcx.generics_of(item_def_id).parent.map(|def_id| def_id.expect_local())
897 };
898
899 let result = if let Some(parent) = parent {
900 let icx = ItemCtxt::new(tcx, parent);
901 icx.probe_ty_param_bounds(DUMMY_SP, def_id, assoc_ident)
902 } else {
903 ty::EarlyBinder::bind(&[] as &[_])
904 };
905 let mut extend = None;
906
907 let item_hir_id = tcx.local_def_id_to_hir_id(item_def_id);
908
909 let hir_node = tcx.hir_node(item_hir_id);
910 let Some(hir_generics) = hir_node.generics() else {
911 return result;
912 };
913
914 if let Node::Item(item) = hir_node
915 && let hir::ItemKind::Trait(..) = item.kind
916 && param_id == item_hir_id
918 {
919 let identity_trait_ref = ty::TraitRef::identity(tcx, item_def_id.to_def_id());
920 extend = Some((identity_trait_ref.upcast(tcx), item.span));
921 }
922
923 let icx = ItemCtxt::new(tcx, item_def_id);
924 let extra_predicates = extend.into_iter().chain(icx.probe_ty_param_bounds_in_generics(
925 hir_generics,
926 def_id,
927 PredicateFilter::SelfTraitThatDefines(assoc_ident),
928 ));
929
930 let bounds =
931 &*tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates));
932
933 let self_ty = match tcx.def_kind(def_id) {
935 DefKind::TyParam => Ty::new_param(
936 tcx,
937 tcx.generics_of(item_def_id)
938 .param_def_id_to_index(tcx, def_id.to_def_id())
939 .expect("expected generic param to be owned by item"),
940 tcx.item_name(def_id.to_def_id()),
941 ),
942 DefKind::Trait | DefKind::TraitAlias => tcx.types.self_param,
943 _ => unreachable!(),
944 };
945 assert_only_contains_predicates_from(
946 PredicateFilter::SelfTraitThatDefines(assoc_ident),
947 bounds,
948 self_ty,
949 );
950
951 ty::EarlyBinder::bind(bounds)
952}
953
954impl<'tcx> ItemCtxt<'tcx> {
955 #[instrument(level = "trace", skip(self, hir_generics))]
961 fn probe_ty_param_bounds_in_generics(
962 &self,
963 hir_generics: &'tcx hir::Generics<'tcx>,
964 param_def_id: LocalDefId,
965 filter: PredicateFilter,
966 ) -> Vec<(ty::Clause<'tcx>, Span)> {
967 let mut bounds = Vec::new();
968
969 for predicate in hir_generics.predicates {
970 let hir_id = predicate.hir_id;
971 let hir::WherePredicateKind::BoundPredicate(predicate) = predicate.kind else {
972 continue;
973 };
974
975 match filter {
976 _ if predicate.is_param_bound(param_def_id.to_def_id()) => {
977 }
979 PredicateFilter::All => {
980 }
982 PredicateFilter::SelfOnly
983 | PredicateFilter::SelfTraitThatDefines(_)
984 | PredicateFilter::SelfConstIfConst
985 | PredicateFilter::SelfAndAssociatedTypeBounds => continue,
986 PredicateFilter::ConstIfConst => unreachable!(),
987 }
988
989 let bound_ty = self.lowerer().lower_ty_maybe_return_type_notation(predicate.bounded_ty);
990
991 let bound_vars = self.tcx.late_bound_vars(hir_id);
992 self.lowerer().lower_bounds(
993 bound_ty,
994 predicate.bounds,
995 &mut bounds,
996 bound_vars,
997 filter,
998 OverlappingAsssocItemConstraints::Allowed,
999 );
1000 }
1001
1002 bounds
1003 }
1004}
1005
1006pub(super) fn const_conditions<'tcx>(
1007 tcx: TyCtxt<'tcx>,
1008 def_id: LocalDefId,
1009) -> ty::ConstConditions<'tcx> {
1010 if !tcx.is_conditionally_const(def_id) {
1011 bug!("const_conditions invoked for item that is not conditionally const: {def_id:?}");
1012 }
1013
1014 match tcx.opt_rpitit_info(def_id.to_def_id()) {
1015 Some(
1017 ty::ImplTraitInTraitData::Impl { fn_def_id }
1018 | ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
1019 ) => return tcx.const_conditions(fn_def_id),
1020 None => {}
1021 }
1022
1023 let (generics, trait_def_id_and_supertraits, has_parent) = match tcx.hir_node_by_def_id(def_id)
1024 {
1025 Node::Item(item) => match item.kind {
1026 hir::ItemKind::Impl(impl_) => (impl_.generics, None, false),
1027 hir::ItemKind::Fn { generics, .. } => (generics, None, false),
1028 hir::ItemKind::Trait(_, _, _, _, generics, supertraits, _) => {
1029 (generics, Some((item.owner_id.def_id, supertraits)), false)
1030 }
1031 _ => bug!("const_conditions called on wrong item: {def_id:?}"),
1032 },
1033 Node::TraitItem(item) => match item.kind {
1038 hir::TraitItemKind::Fn(_, _) | hir::TraitItemKind::Type(_, _) => {
1039 (item.generics, None, true)
1040 }
1041 _ => bug!("const_conditions called on wrong item: {def_id:?}"),
1042 },
1043 Node::ImplItem(item) => match item.kind {
1044 hir::ImplItemKind::Fn(_, _) | hir::ImplItemKind::Type(_) => {
1045 (item.generics, None, tcx.is_conditionally_const(tcx.local_parent(def_id)))
1046 }
1047 _ => bug!("const_conditions called on wrong item: {def_id:?}"),
1048 },
1049 Node::ForeignItem(item) => match item.kind {
1050 hir::ForeignItemKind::Fn(_, _, generics) => (generics, None, false),
1051 _ => bug!("const_conditions called on wrong item: {def_id:?}"),
1052 },
1053 Node::OpaqueTy(opaque) => match opaque.origin {
1054 hir::OpaqueTyOrigin::FnReturn { parent, .. } => return tcx.const_conditions(parent),
1055 hir::OpaqueTyOrigin::AsyncFn { .. } | hir::OpaqueTyOrigin::TyAlias { .. } => {
1056 unreachable!()
1057 }
1058 },
1059 Node::Ctor(hir::VariantData::Tuple { .. }) => return Default::default(),
1061 _ => bug!("const_conditions called on wrong item: {def_id:?}"),
1062 };
1063
1064 let icx = ItemCtxt::new(tcx, def_id);
1065 let mut bounds = Vec::new();
1066
1067 for pred in generics.predicates {
1068 match pred.kind {
1069 hir::WherePredicateKind::BoundPredicate(bound_pred) => {
1070 let ty = icx.lowerer().lower_ty_maybe_return_type_notation(bound_pred.bounded_ty);
1071 let bound_vars = tcx.late_bound_vars(pred.hir_id);
1072 icx.lowerer().lower_bounds(
1073 ty,
1074 bound_pred.bounds.iter(),
1075 &mut bounds,
1076 bound_vars,
1077 PredicateFilter::ConstIfConst,
1078 OverlappingAsssocItemConstraints::Allowed,
1079 );
1080 }
1081 _ => {}
1082 }
1083 }
1084
1085 if let Some((def_id, supertraits)) = trait_def_id_and_supertraits {
1086 bounds.push((
1088 ty::Binder::dummy(ty::TraitRef::identity(tcx, def_id.to_def_id()))
1089 .to_host_effect_clause(tcx, ty::BoundConstness::Maybe),
1090 DUMMY_SP,
1091 ));
1092
1093 icx.lowerer().lower_bounds(
1094 tcx.types.self_param,
1095 supertraits,
1096 &mut bounds,
1097 ty::List::empty(),
1098 PredicateFilter::ConstIfConst,
1099 OverlappingAsssocItemConstraints::Allowed,
1100 );
1101 }
1102
1103 ty::ConstConditions {
1104 parent: has_parent.then(|| tcx.local_parent(def_id).to_def_id()),
1105 predicates: tcx.arena.alloc_from_iter(bounds.into_iter().map(|(clause, span)| {
1106 (
1107 clause.kind().map_bound(|clause| match clause {
1108 ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
1109 trait_ref,
1110 constness: ty::BoundConstness::Maybe,
1111 }) => trait_ref,
1112 _ => bug!("converted {clause:?}"),
1113 }),
1114 span,
1115 )
1116 })),
1117 }
1118}
1119
1120pub(super) fn explicit_implied_const_bounds<'tcx>(
1121 tcx: TyCtxt<'tcx>,
1122 def_id: LocalDefId,
1123) -> ty::EarlyBinder<'tcx, &'tcx [(ty::PolyTraitRef<'tcx>, Span)]> {
1124 if !tcx.is_conditionally_const(def_id) {
1125 bug!(
1126 "explicit_implied_const_bounds invoked for item that is not conditionally const: {def_id:?}"
1127 );
1128 }
1129
1130 let bounds = match tcx.opt_rpitit_info(def_id.to_def_id()) {
1131 Some(ty::ImplTraitInTraitData::Trait { .. }) => {
1134 explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
1135 }
1136 Some(ty::ImplTraitInTraitData::Impl { .. }) => {
1137 span_bug!(tcx.def_span(def_id), "RPITIT in impl should not have item bounds")
1138 }
1139 None => match tcx.hir_node_by_def_id(def_id) {
1140 Node::Item(hir::Item { kind: hir::ItemKind::Trait(..), .. }) => {
1141 implied_predicates_with_filter(
1142 tcx,
1143 def_id.to_def_id(),
1144 PredicateFilter::SelfConstIfConst,
1145 )
1146 }
1147 Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. })
1148 | Node::OpaqueTy(_) => {
1149 explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
1150 }
1151 _ => bug!("explicit_implied_const_bounds called on wrong item: {def_id:?}"),
1152 },
1153 };
1154
1155 bounds.map_bound(|bounds| {
1156 &*tcx.arena.alloc_from_iter(bounds.iter().copied().map(|(clause, span)| {
1157 (
1158 clause.kind().map_bound(|clause| match clause {
1159 ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
1160 trait_ref,
1161 constness: ty::BoundConstness::Maybe,
1162 }) => trait_ref,
1163 _ => bug!("converted {clause:?}"),
1164 }),
1165 span,
1166 )
1167 }))
1168 })
1169}