1use std::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_hir::find_attr;
9use rustc_middle::ty::{
10 self, GenericPredicates, ImplTraitInTraitData, Ty, TyCtxt, TypeVisitable, TypeVisitor, Upcast,
11};
12use rustc_middle::{bug, span_bug};
13use rustc_span::{DUMMY_SP, Ident, Span};
14use tracing::{debug, instrument, trace};
15
16use super::item_bounds::explicit_item_bounds_with_filter;
17use crate::collect::ItemCtxt;
18use crate::constrained_generic_params as cgp;
19use crate::delegation::inherit_predicates_for_delegation_item;
20use crate::hir_ty_lowering::{
21 HirTyLowerer, ImpliedBoundsContext, OverlappingAsssocItemConstraints, PredicateFilter,
22 RegionInferReason,
23};
24
25#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("predicates_of",
"rustc_hir_analysis::collect::predicates_of",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
::tracing_core::__macro_support::Option::Some(28u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
::tracing_core::field::FieldSet::new(&["def_id"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_id)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: ty::GenericPredicates<'_> =
loop {};
return __tracing_attr_fake_return;
}
{
let mut result = tcx.explicit_predicates_of(def_id);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/collect/predicates_of.rs:31",
"rustc_hir_analysis::collect::predicates_of",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
::tracing_core::__macro_support::Option::Some(31u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("predicates_of: explicit_predicates_of({0:?}) = {1:?}",
def_id, result) as &dyn Value))])
});
} else { ; }
};
let inferred_outlives = tcx.inferred_outlives_of(def_id);
if !inferred_outlives.is_empty() {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/collect/predicates_of.rs:35",
"rustc_hir_analysis::collect::predicates_of",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
::tracing_core::__macro_support::Option::Some(35u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("predicates_of: inferred_outlives_of({0:?}) = {1:?}",
def_id, inferred_outlives) as &dyn Value))])
});
} else { ; }
};
let inferred_outlives_iter =
inferred_outlives.iter().map(|(clause, span)|
((*clause).upcast(tcx), *span));
if result.predicates.is_empty() {
result.predicates =
tcx.arena.alloc_from_iter(inferred_outlives_iter);
} else {
result.predicates =
tcx.arena.alloc_from_iter(result.predicates.into_iter().copied().chain(inferred_outlives_iter));
}
}
if tcx.is_trait(def_id) {
let span = DUMMY_SP;
result.predicates =
tcx.arena.alloc_from_iter(result.predicates.iter().copied().chain(std::iter::once((ty::TraitRef::identity(tcx,
def_id).upcast(tcx), span))));
}
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/collect/predicates_of.rs:76",
"rustc_hir_analysis::collect::predicates_of",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
::tracing_core::__macro_support::Option::Some(76u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("predicates_of({0:?}) = {1:?}",
def_id, result) as &dyn Value))])
});
} else { ; }
};
result
}
}
}#[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
80x;#[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.local_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 = tcx.impl_trait_ref(impl_def_id).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 =
165 Some(ty::Binder::dummy(tcx.impl_trait_ref(def_id).instantiate_identity()));
166 }
167 }
168 ItemKind::Trait(_, _, _, _, _, self_bounds, ..)
169 | ItemKind::TraitAlias(_, _, _, self_bounds) => {
170 is_trait = Some((self_bounds, item.span));
171 }
172 _ => {}
173 }
174 };
175
176 let generics = tcx.generics_of(def_id);
177
178 if let Some((self_bounds, span)) = is_trait {
183 let mut bounds = Vec::new();
184 icx.lowerer().lower_bounds(
185 tcx.types.self_param,
186 self_bounds,
187 &mut bounds,
188 ty::List::empty(),
189 PredicateFilter::All,
190 OverlappingAsssocItemConstraints::Allowed,
191 );
192 icx.lowerer().add_implicit_sizedness_bounds(
193 &mut bounds,
194 tcx.types.self_param,
195 self_bounds,
196 ImpliedBoundsContext::TraitDef(def_id),
197 span,
198 );
199 icx.lowerer().add_default_traits(
200 &mut bounds,
201 tcx.types.self_param,
202 self_bounds,
203 ImpliedBoundsContext::TraitDef(def_id),
204 span,
205 );
206 predicates.extend(bounds);
207 }
208
209 if let Some(trait_ref) = is_default_impl_trait {
218 predicates.insert((trait_ref.upcast(tcx), tcx.def_span(def_id)));
219 }
220
221 for param in hir_generics.params {
225 match param.kind {
226 GenericParamKind::Lifetime { .. } => (),
227 GenericParamKind::Type { .. } => {
228 let param_ty = icx.lowerer().lower_ty_param(param.hir_id);
229 let mut bounds = Vec::new();
230 icx.lowerer().add_implicit_sizedness_bounds(
232 &mut bounds,
233 param_ty,
234 &[],
235 ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
236 param.span,
237 );
238 icx.lowerer().add_default_traits(
239 &mut bounds,
240 param_ty,
241 &[],
242 ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
243 param.span,
244 );
245 trace!(?bounds);
246 predicates.extend(bounds);
247 trace!(?predicates);
248 }
249 hir::GenericParamKind::Const { .. } => {
250 let param_def_id = param.def_id.to_def_id();
251 let ct_ty = tcx.type_of(param_def_id).instantiate_identity();
252 let ct = icx.lowerer().lower_const_param(param_def_id, param.hir_id);
253 predicates
254 .insert((ty::ClauseKind::ConstArgHasType(ct, ct_ty).upcast(tcx), param.span));
255 }
256 }
257 }
258
259 trace!(?predicates);
260 for predicate in hir_generics.predicates {
262 match predicate.kind {
263 hir::WherePredicateKind::BoundPredicate(bound_pred) => {
264 let ty = icx.lowerer().lower_ty_maybe_return_type_notation(bound_pred.bounded_ty);
265 let bound_vars = tcx.late_bound_vars(predicate.hir_id);
266
267 if bound_pred.bounds.is_empty() {
269 if let ty::Param(_) = ty.kind() {
270 } else {
272 let span = bound_pred.bounded_ty.span;
276 let predicate = ty::Binder::bind_with_vars(
277 ty::ClauseKind::WellFormed(ty.into()),
278 bound_vars,
279 );
280 predicates.insert((predicate.upcast(tcx), span));
281 }
282 }
283
284 let mut bounds = Vec::new();
285 icx.lowerer().lower_bounds(
286 ty,
287 bound_pred.bounds,
288 &mut bounds,
289 bound_vars,
290 PredicateFilter::All,
291 OverlappingAsssocItemConstraints::Allowed,
292 );
293 predicates.extend(bounds);
294 }
295
296 hir::WherePredicateKind::RegionPredicate(region_pred) => {
297 let r1 = icx
298 .lowerer()
299 .lower_lifetime(region_pred.lifetime, RegionInferReason::RegionPredicate);
300 predicates.extend(region_pred.bounds.iter().map(|bound| {
301 let (r2, span) = match bound {
302 hir::GenericBound::Outlives(lt) => (
303 icx.lowerer().lower_lifetime(lt, RegionInferReason::RegionPredicate),
304 lt.ident.span,
305 ),
306 bound => {
307 span_bug!(
308 bound.span(),
309 "lifetime param bounds must be outlives, but found {bound:?}"
310 )
311 }
312 };
313 let pred =
314 ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(r1, r2)).upcast(tcx);
315 (pred, span)
316 }))
317 }
318
319 hir::WherePredicateKind::EqPredicate(..) => {
320 }
322 }
323 }
324
325 if tcx.features().generic_const_exprs() {
326 predicates.extend(const_evaluatable_predicates_of(tcx, def_id, &predicates));
327 }
328
329 let attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(def_id));
330 let allow_unstable_feature_attr =
334 find_attr!(attrs, UnstableFeatureBound(i) => i).map(|i| i.as_slice()).unwrap_or_default();
335
336 for (feat_name, span) in allow_unstable_feature_attr {
337 predicates.insert((ty::ClauseKind::UnstableFeature(*feat_name).upcast(tcx), *span));
338 }
339
340 let mut predicates: Vec<_> = predicates.into_iter().collect();
341
342 if let Node::Item(&Item { kind: ItemKind::Impl(impl_), .. }) = node {
348 let self_ty = tcx.type_of(def_id).instantiate_identity();
349 let trait_ref =
350 impl_.of_trait.is_some().then(|| tcx.impl_trait_ref(def_id).instantiate_identity());
351 cgp::setup_constraining_predicates(
352 tcx,
353 &mut predicates,
354 trait_ref,
355 &mut cgp::parameters_for_impl(tcx, self_ty, trait_ref),
356 );
357 }
358
359 if let Node::OpaqueTy(..) = node {
363 compute_bidirectional_outlives_predicates(tcx, &generics.own_params, &mut predicates);
364 debug!(?predicates);
365 }
366
367 ty::GenericPredicates {
368 parent: generics.parent,
369 predicates: tcx.arena.alloc_from_iter(predicates),
370 }
371}
372
373fn compute_bidirectional_outlives_predicates<'tcx>(
376 tcx: TyCtxt<'tcx>,
377 opaque_own_params: &[ty::GenericParamDef],
378 predicates: &mut Vec<(ty::Clause<'tcx>, Span)>,
379) {
380 for param in opaque_own_params {
381 let orig_lifetime = tcx.map_opaque_lifetime_to_parent_lifetime(param.def_id.expect_local());
382 if let ty::ReEarlyParam(..) = orig_lifetime.kind() {
383 let dup_lifetime = ty::Region::new_early_param(
384 tcx,
385 ty::EarlyParamRegion { index: param.index, name: param.name },
386 );
387 let span = tcx.def_span(param.def_id);
388 predicates.push((
389 ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(orig_lifetime, dup_lifetime))
390 .upcast(tcx),
391 span,
392 ));
393 predicates.push((
394 ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(dup_lifetime, orig_lifetime))
395 .upcast(tcx),
396 span,
397 ));
398 }
399 }
400}
401
402x;#[instrument(level = "debug", skip(tcx, predicates), ret)]
403fn const_evaluatable_predicates_of<'tcx>(
404 tcx: TyCtxt<'tcx>,
405 def_id: LocalDefId,
406 predicates: &FxIndexSet<(ty::Clause<'tcx>, Span)>,
407) -> FxIndexSet<(ty::Clause<'tcx>, Span)> {
408 struct ConstCollector<'tcx> {
409 tcx: TyCtxt<'tcx>,
410 preds: FxIndexSet<(ty::Clause<'tcx>, Span)>,
411 }
412
413 fn is_const_param_default(tcx: TyCtxt<'_>, def: LocalDefId) -> bool {
414 let hir_id = tcx.local_def_id_to_hir_id(def);
415 let (_, parent_node) = tcx
416 .hir_parent_iter(hir_id)
417 .skip_while(|(_, n)| matches!(n, Node::ConstArg(..)))
418 .next()
419 .unwrap();
420 matches!(
421 parent_node,
422 Node::GenericParam(hir::GenericParam { kind: hir::GenericParamKind::Const { .. }, .. })
423 )
424 }
425
426 impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ConstCollector<'tcx> {
427 fn visit_const(&mut self, c: ty::Const<'tcx>) {
428 if let ty::ConstKind::Unevaluated(uv) = c.kind() {
429 if let Some(local) = uv.def.as_local()
430 && is_const_param_default(self.tcx, local)
431 {
432 return;
440 }
441
442 if self.tcx.is_type_const(uv.def) {
444 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(impl_) = item.kind
464 {
465 if impl_.of_trait.is_some() {
466 debug!("visit impl trait_ref");
467 let trait_ref = tcx.impl_trait_ref(def_id);
468 trait_ref.instantiate_identity().visit_with(&mut collector);
469 }
470
471 debug!("visit self_ty");
472 let self_ty = tcx.type_of(def_id);
473 self_ty.instantiate_identity().visit_with(&mut collector);
474 }
475
476 if let Some(_) = tcx.hir_fn_sig_by_hir_id(hir_id) {
477 debug!("visit fn sig");
478 let fn_sig = tcx.fn_sig(def_id);
479 let fn_sig = fn_sig.instantiate_identity();
480 debug!(?fn_sig);
481 fn_sig.visit_with(&mut collector);
482 }
483
484 collector.preds
485}
486
487pub(super) fn trait_explicit_predicates_and_bounds(
488 tcx: TyCtxt<'_>,
489 def_id: LocalDefId,
490) -> ty::GenericPredicates<'_> {
491 match (&tcx.def_kind(def_id), &DefKind::Trait) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(tcx.def_kind(def_id), DefKind::Trait);
492 gather_explicit_predicates_of(tcx, def_id)
493}
494
495pub(super) fn explicit_predicates_of<'tcx>(
496 tcx: TyCtxt<'tcx>,
497 def_id: LocalDefId,
498) -> ty::GenericPredicates<'tcx> {
499 let def_kind = tcx.def_kind(def_id);
500 if let DefKind::Trait = def_kind {
501 let predicates_and_bounds = tcx.trait_explicit_predicates_and_bounds(def_id);
504 let trait_identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
505
506 let is_assoc_item_ty = |ty: Ty<'tcx>| {
507 if let &ty::Alias(
517 projection @ ty::AliasTy {
518 kind: ty::Projection { def_id: projection_def_id }, ..
519 },
520 ) = ty.kind()
521 {
522 projection.args == trait_identity_args
523 && !tcx.is_impl_trait_in_trait(projection_def_id)
525 && tcx.parent(projection_def_id) == def_id.to_def_id()
526 } else {
527 false
528 }
529 };
530
531 let predicates: Vec<_> = predicates_and_bounds
532 .predicates
533 .iter()
534 .copied()
535 .filter(|(pred, _)| match pred.kind().skip_binder() {
536 ty::ClauseKind::Trait(tr) => !is_assoc_item_ty(tr.self_ty()),
537 ty::ClauseKind::Projection(proj) => {
538 !is_assoc_item_ty(proj.projection_term.self_ty())
539 }
540 ty::ClauseKind::TypeOutlives(outlives) => !is_assoc_item_ty(outlives.0),
541 _ => true,
542 })
543 .collect();
544 if predicates.len() == predicates_and_bounds.predicates.len() {
545 predicates_and_bounds
546 } else {
547 ty::GenericPredicates {
548 parent: predicates_and_bounds.parent,
549 predicates: tcx.arena.alloc_slice(&predicates),
550 }
551 }
552 } else {
553 if def_kind == DefKind::AnonConst
554 && tcx.features().generic_const_exprs()
555 && let Some(defaulted_param_def_id) =
556 tcx.hir_opt_const_param_default_param_def_id(tcx.local_def_id_to_hir_id(def_id))
557 {
558 let parent_def_id = tcx.local_parent(def_id);
571 let parent_preds = tcx.explicit_predicates_of(parent_def_id);
572
573 let filtered_predicates = parent_preds
577 .predicates
578 .into_iter()
579 .filter(|(pred, _)| {
580 if let ty::ClauseKind::ConstArgHasType(ct, _) = pred.kind().skip_binder() {
581 match ct.kind() {
582 ty::ConstKind::Param(param_const) => {
583 let defaulted_param_idx = tcx
584 .generics_of(parent_def_id)
585 .param_def_id_to_index[&defaulted_param_def_id.to_def_id()];
586 param_const.index < defaulted_param_idx
587 }
588 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("`ConstArgHasType` in `predicates_of`that isn\'t a `Param` const"))bug!(
589 "`ConstArgHasType` in `predicates_of`\
590 that isn't a `Param` const"
591 ),
592 }
593 } else {
594 true
595 }
596 })
597 .cloned();
598 return GenericPredicates {
599 parent: parent_preds.parent,
600 predicates: { tcx.arena.alloc_from_iter(filtered_predicates) },
601 };
602 }
603 gather_explicit_predicates_of(tcx, def_id)
604 }
605}
606
607pub(super) fn explicit_super_predicates_of<'tcx>(
611 tcx: TyCtxt<'tcx>,
612 trait_def_id: LocalDefId,
613) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
614 implied_predicates_with_filter(tcx, trait_def_id.to_def_id(), PredicateFilter::SelfOnly)
615}
616
617pub(super) fn explicit_supertraits_containing_assoc_item<'tcx>(
618 tcx: TyCtxt<'tcx>,
619 (trait_def_id, assoc_ident): (DefId, Ident),
620) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
621 implied_predicates_with_filter(
622 tcx,
623 trait_def_id,
624 PredicateFilter::SelfTraitThatDefines(assoc_ident),
625 )
626}
627
628pub(super) fn explicit_implied_predicates_of<'tcx>(
629 tcx: TyCtxt<'tcx>,
630 trait_def_id: LocalDefId,
631) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
632 implied_predicates_with_filter(
633 tcx,
634 trait_def_id.to_def_id(),
635 if tcx.is_trait_alias(trait_def_id.to_def_id()) {
636 PredicateFilter::All
637 } else {
638 PredicateFilter::SelfAndAssociatedTypeBounds
639 },
640 )
641}
642
643pub(super) fn implied_predicates_with_filter<'tcx>(
647 tcx: TyCtxt<'tcx>,
648 trait_def_id: DefId,
649 filter: PredicateFilter,
650) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
651 let Some(trait_def_id) = trait_def_id.as_local() else {
652 match filter {
PredicateFilter::SelfTraitThatDefines(_) => {}
ref left_val => {
::core::panicking::assert_matches_failed(left_val,
"PredicateFilter::SelfTraitThatDefines(_)",
::core::option::Option::None);
}
};assert_matches!(filter, PredicateFilter::SelfTraitThatDefines(_));
655 return tcx.explicit_super_predicates_of(trait_def_id);
656 };
657
658 let Node::Item(item) = tcx.hir_node_by_def_id(trait_def_id) else {
659 ::rustc_middle::util::bug::bug_fmt(format_args!("trait_def_id {0:?} is not an item",
trait_def_id));bug!("trait_def_id {trait_def_id:?} is not an item");
660 };
661
662 let (generics, superbounds) = match item.kind {
663 hir::ItemKind::Trait(.., generics, supertraits, _) => (generics, supertraits),
664 hir::ItemKind::TraitAlias(_, _, generics, supertraits) => (generics, supertraits),
665 _ => ::rustc_middle::util::bug::span_bug_fmt(item.span,
format_args!("super_predicates invoked on non-trait"))span_bug!(item.span, "super_predicates invoked on non-trait"),
666 };
667
668 let icx = ItemCtxt::new(tcx, trait_def_id);
669
670 let self_param_ty = tcx.types.self_param;
671 let mut bounds = Vec::new();
672 icx.lowerer().lower_bounds(
673 self_param_ty,
674 superbounds,
675 &mut bounds,
676 ty::List::empty(),
677 filter,
678 OverlappingAsssocItemConstraints::Allowed,
679 );
680 match filter {
681 PredicateFilter::All
682 | PredicateFilter::SelfOnly
683 | PredicateFilter::SelfTraitThatDefines(_)
684 | PredicateFilter::SelfAndAssociatedTypeBounds => {
685 icx.lowerer().add_implicit_sizedness_bounds(
686 &mut bounds,
687 self_param_ty,
688 superbounds,
689 ImpliedBoundsContext::TraitDef(trait_def_id),
690 item.span,
691 );
692 icx.lowerer().add_default_traits(
693 &mut bounds,
694 self_param_ty,
695 superbounds,
696 ImpliedBoundsContext::TraitDef(trait_def_id),
697 item.span,
698 );
699 }
700 PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
702 }
703
704 let where_bounds_that_match =
705 icx.probe_ty_param_bounds_in_generics(generics, item.owner_id.def_id, filter);
706
707 let implied_bounds =
709 &*tcx.arena.alloc_from_iter(bounds.into_iter().chain(where_bounds_that_match));
710 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/collect/predicates_of.rs:710",
"rustc_hir_analysis::collect::predicates_of",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
::tracing_core::__macro_support::Option::Some(710u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
::tracing_core::field::FieldSet::new(&["implied_bounds"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&implied_bounds)
as &dyn Value))])
});
} else { ; }
};debug!(?implied_bounds);
711
712 match filter {
717 PredicateFilter::SelfOnly => {
718 for &(pred, span) in implied_bounds {
719 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/collect/predicates_of.rs:719",
"rustc_hir_analysis::collect::predicates_of",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
::tracing_core::__macro_support::Option::Some(719u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("superbound: {0:?}",
pred) as &dyn Value))])
});
} else { ; }
};debug!("superbound: {:?}", pred);
720 if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
721 && bound.polarity == ty::PredicatePolarity::Positive
722 {
723 tcx.at(span).explicit_super_predicates_of(bound.def_id());
724 }
725 }
726 }
727 PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
728 for &(pred, span) in implied_bounds {
729 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/collect/predicates_of.rs:729",
"rustc_hir_analysis::collect::predicates_of",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
::tracing_core::__macro_support::Option::Some(729u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("superbound: {0:?}",
pred) as &dyn Value))])
});
} else { ; }
};debug!("superbound: {:?}", pred);
730 if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
731 && bound.polarity == ty::PredicatePolarity::Positive
732 {
733 tcx.at(span).explicit_implied_predicates_of(bound.def_id());
734 }
735 }
736 }
737 _ => {}
738 }
739
740 assert_only_contains_predicates_from(filter, implied_bounds, tcx.types.self_param);
741
742 ty::EarlyBinder::bind(implied_bounds)
743}
744
745pub(super) fn assert_only_contains_predicates_from<'tcx>(
749 filter: PredicateFilter,
750 bounds: &'tcx [(ty::Clause<'tcx>, Span)],
751 ty: Ty<'tcx>,
752) {
753 if !truecfg!(debug_assertions) {
754 return;
755 }
756
757 match filter {
758 PredicateFilter::SelfOnly => {
759 for (clause, _) in bounds {
760 match clause.kind().skip_binder() {
761 ty::ClauseKind::Trait(trait_predicate) => {
762 match (&trait_predicate.self_ty(), &ty) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("expected `Self` predicate when computing `{0:?}` implied bounds: {1:?}",
filter, clause)));
}
}
};assert_eq!(
763 trait_predicate.self_ty(),
764 ty,
765 "expected `Self` predicate when computing \
766 `{filter:?}` implied bounds: {clause:?}"
767 );
768 }
769 ty::ClauseKind::Projection(projection_predicate) => {
770 match (&projection_predicate.self_ty(), &ty) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("expected `Self` predicate when computing `{0:?}` implied bounds: {1:?}",
filter, clause)));
}
}
};assert_eq!(
771 projection_predicate.self_ty(),
772 ty,
773 "expected `Self` predicate when computing \
774 `{filter:?}` implied bounds: {clause:?}"
775 );
776 }
777 ty::ClauseKind::TypeOutlives(outlives_predicate) => {
778 match (&outlives_predicate.0, &ty) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("expected `Self` predicate when computing `{0:?}` implied bounds: {1:?}",
filter, clause)));
}
}
};assert_eq!(
779 outlives_predicate.0, ty,
780 "expected `Self` predicate when computing \
781 `{filter:?}` implied bounds: {clause:?}"
782 );
783 }
784 ty::ClauseKind::HostEffect(host_effect_predicate) => {
785 match (&host_effect_predicate.self_ty(), &ty) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("expected `Self` predicate when computing `{0:?}` implied bounds: {1:?}",
filter, clause)));
}
}
};assert_eq!(
786 host_effect_predicate.self_ty(),
787 ty,
788 "expected `Self` predicate when computing \
789 `{filter:?}` implied bounds: {clause:?}"
790 );
791 }
792
793 ty::ClauseKind::RegionOutlives(_)
794 | ty::ClauseKind::ConstArgHasType(_, _)
795 | ty::ClauseKind::WellFormed(_)
796 | ty::ClauseKind::UnstableFeature(_)
797 | ty::ClauseKind::ConstEvaluatable(_) => {
798 ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected non-`Self` predicate when computing `{0:?}` implied bounds: {1:?}",
filter, clause));bug!(
799 "unexpected non-`Self` predicate when computing \
800 `{filter:?}` implied bounds: {clause:?}"
801 );
802 }
803 }
804 }
805 }
806 PredicateFilter::SelfTraitThatDefines(_) => {
807 for (clause, _) in bounds {
808 match clause.kind().skip_binder() {
809 ty::ClauseKind::Trait(trait_predicate) => {
810 match (&trait_predicate.self_ty(), &ty) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("expected `Self` predicate when computing `{0:?}` implied bounds: {1:?}",
filter, clause)));
}
}
};assert_eq!(
811 trait_predicate.self_ty(),
812 ty,
813 "expected `Self` predicate when computing \
814 `{filter:?}` implied bounds: {clause:?}"
815 );
816 }
817
818 ty::ClauseKind::Projection(_)
819 | ty::ClauseKind::TypeOutlives(_)
820 | ty::ClauseKind::RegionOutlives(_)
821 | ty::ClauseKind::ConstArgHasType(_, _)
822 | ty::ClauseKind::WellFormed(_)
823 | ty::ClauseKind::ConstEvaluatable(_)
824 | ty::ClauseKind::UnstableFeature(_)
825 | ty::ClauseKind::HostEffect(..) => {
826 ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected non-`Self` predicate when computing `{0:?}` implied bounds: {1:?}",
filter, clause));bug!(
827 "unexpected non-`Self` predicate when computing \
828 `{filter:?}` implied bounds: {clause:?}"
829 );
830 }
831 }
832 }
833 }
834 PredicateFilter::ConstIfConst => {
835 for (clause, _) in bounds {
836 match clause.kind().skip_binder() {
837 ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
838 trait_ref: _,
839 constness: ty::BoundConstness::Maybe,
840 }) => {}
841 _ => {
842 ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected non-`HostEffect` predicate when computing `{0:?}` implied bounds: {1:?}",
filter, clause));bug!(
843 "unexpected non-`HostEffect` predicate when computing \
844 `{filter:?}` implied bounds: {clause:?}"
845 );
846 }
847 }
848 }
849 }
850 PredicateFilter::SelfConstIfConst => {
851 for (clause, _) in bounds {
852 match clause.kind().skip_binder() {
853 ty::ClauseKind::HostEffect(pred) => {
854 match (&pred.constness, &ty::BoundConstness::Maybe) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("expected `[const]` predicate when computing `{0:?}` implied bounds: {1:?}",
filter, clause)));
}
}
};assert_eq!(
855 pred.constness,
856 ty::BoundConstness::Maybe,
857 "expected `[const]` predicate when computing `{filter:?}` \
858 implied bounds: {clause:?}",
859 );
860 match (&pred.trait_ref.self_ty(), &ty) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("expected `Self` predicate when computing `{0:?}` implied bounds: {1:?}",
filter, clause)));
}
}
};assert_eq!(
861 pred.trait_ref.self_ty(),
862 ty,
863 "expected `Self` predicate when computing `{filter:?}` \
864 implied bounds: {clause:?}"
865 );
866 }
867 _ => {
868 ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected non-`HostEffect` predicate when computing `{0:?}` implied bounds: {1:?}",
filter, clause));bug!(
869 "unexpected non-`HostEffect` predicate when computing \
870 `{filter:?}` implied bounds: {clause:?}"
871 );
872 }
873 }
874 }
875 }
876 PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {}
877 }
878}
879
880#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("type_param_predicates",
"rustc_hir_analysis::collect::predicates_of",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
::tracing_core::__macro_support::Option::Some(882u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
::tracing_core::field::FieldSet::new(&["item_def_id",
"def_id", "assoc_ident"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&item_def_id)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_id)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&assoc_ident)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return:
ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> =
loop {};
return __tracing_attr_fake_return;
}
{
match tcx.opt_rpitit_info(item_def_id.to_def_id()) {
Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) =>
{
return tcx.type_param_predicates((opaque_def_id.expect_local(),
def_id, assoc_ident));
}
Some(ty::ImplTraitInTraitData::Impl { .. }) => {
{
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("should not be lowering bounds on RPITIT in impl")));
}
}
None => {}
}
let param_id = tcx.local_def_id_to_hir_id(def_id);
let param_owner = tcx.hir_ty_param_owner(def_id);
let parent =
if item_def_id == param_owner {
None
} else {
tcx.generics_of(item_def_id).parent.map(|def_id|
def_id.expect_local())
};
let result =
if let Some(parent) = parent {
let icx = ItemCtxt::new(tcx, parent);
icx.probe_ty_param_bounds(DUMMY_SP, def_id, assoc_ident)
} else { ty::EarlyBinder::bind(&[] as &[_]) };
let mut extend = None;
let item_hir_id = tcx.local_def_id_to_hir_id(item_def_id);
let hir_node = tcx.hir_node(item_hir_id);
let Some(hir_generics) =
hir_node.generics() else { return result; };
if let Node::Item(item) = hir_node &&
let hir::ItemKind::Trait(..) = item.kind &&
param_id == item_hir_id {
let identity_trait_ref =
ty::TraitRef::identity(tcx, item_def_id.to_def_id());
extend = Some((identity_trait_ref.upcast(tcx), item.span));
}
let icx = ItemCtxt::new(tcx, item_def_id);
let extra_predicates =
extend.into_iter().chain(icx.probe_ty_param_bounds_in_generics(hir_generics,
def_id,
PredicateFilter::SelfTraitThatDefines(assoc_ident)));
let bounds =
&*tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates));
let self_ty =
match tcx.def_kind(def_id) {
DefKind::TyParam =>
Ty::new_param(tcx,
tcx.generics_of(item_def_id).param_def_id_to_index(tcx,
def_id.to_def_id()).expect("expected generic param to be owned by item"),
tcx.item_name(def_id.to_def_id())),
DefKind::Trait | DefKind::TraitAlias =>
tcx.types.self_param,
_ =>
::core::panicking::panic("internal error: entered unreachable code"),
};
assert_only_contains_predicates_from(PredicateFilter::SelfTraitThatDefines(assoc_ident),
bounds, self_ty);
ty::EarlyBinder::bind(bounds)
}
}
}#[instrument(level = "trace", skip(tcx))]
883pub(super) fn type_param_predicates<'tcx>(
884 tcx: TyCtxt<'tcx>,
885 (item_def_id, def_id, assoc_ident): (LocalDefId, LocalDefId, Ident),
886) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
887 match tcx.opt_rpitit_info(item_def_id.to_def_id()) {
888 Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
889 return tcx.type_param_predicates((opaque_def_id.expect_local(), def_id, assoc_ident));
890 }
891 Some(ty::ImplTraitInTraitData::Impl { .. }) => {
892 unreachable!("should not be lowering bounds on RPITIT in impl")
893 }
894 None => {}
895 }
896
897 let param_id = tcx.local_def_id_to_hir_id(def_id);
902 let param_owner = tcx.hir_ty_param_owner(def_id);
903
904 let parent = if item_def_id == param_owner {
906 None
908 } else {
909 tcx.generics_of(item_def_id).parent.map(|def_id| def_id.expect_local())
910 };
911
912 let result = if let Some(parent) = parent {
913 let icx = ItemCtxt::new(tcx, parent);
914 icx.probe_ty_param_bounds(DUMMY_SP, def_id, assoc_ident)
915 } else {
916 ty::EarlyBinder::bind(&[] as &[_])
917 };
918 let mut extend = None;
919
920 let item_hir_id = tcx.local_def_id_to_hir_id(item_def_id);
921
922 let hir_node = tcx.hir_node(item_hir_id);
923 let Some(hir_generics) = hir_node.generics() else {
924 return result;
925 };
926
927 if let Node::Item(item) = hir_node
928 && let hir::ItemKind::Trait(..) = item.kind
929 && param_id == item_hir_id
931 {
932 let identity_trait_ref = ty::TraitRef::identity(tcx, item_def_id.to_def_id());
933 extend = Some((identity_trait_ref.upcast(tcx), item.span));
934 }
935
936 let icx = ItemCtxt::new(tcx, item_def_id);
937 let extra_predicates = extend.into_iter().chain(icx.probe_ty_param_bounds_in_generics(
938 hir_generics,
939 def_id,
940 PredicateFilter::SelfTraitThatDefines(assoc_ident),
941 ));
942
943 let bounds =
944 &*tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates));
945
946 let self_ty = match tcx.def_kind(def_id) {
948 DefKind::TyParam => Ty::new_param(
949 tcx,
950 tcx.generics_of(item_def_id)
951 .param_def_id_to_index(tcx, def_id.to_def_id())
952 .expect("expected generic param to be owned by item"),
953 tcx.item_name(def_id.to_def_id()),
954 ),
955 DefKind::Trait | DefKind::TraitAlias => tcx.types.self_param,
956 _ => unreachable!(),
957 };
958 assert_only_contains_predicates_from(
959 PredicateFilter::SelfTraitThatDefines(assoc_ident),
960 bounds,
961 self_ty,
962 );
963
964 ty::EarlyBinder::bind(bounds)
965}
966
967impl<'tcx> ItemCtxt<'tcx> {
968 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("probe_ty_param_bounds_in_generics",
"rustc_hir_analysis::collect::predicates_of",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
::tracing_core::__macro_support::Option::Some(973u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
::tracing_core::field::FieldSet::new(&["param_def_id",
"filter"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(¶m_def_id)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&filter)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Vec<(ty::Clause<'tcx>, Span)> =
loop {};
return __tracing_attr_fake_return;
}
{
let mut bounds = Vec::new();
for predicate in hir_generics.predicates {
let hir_id = predicate.hir_id;
let hir::WherePredicateKind::BoundPredicate(predicate) =
predicate.kind else { continue; };
match filter {
_ if predicate.is_param_bound(param_def_id.to_def_id()) =>
{}
PredicateFilter::All => {}
PredicateFilter::SelfOnly |
PredicateFilter::SelfTraitThatDefines(_) |
PredicateFilter::SelfConstIfConst |
PredicateFilter::SelfAndAssociatedTypeBounds => continue,
PredicateFilter::ConstIfConst =>
::core::panicking::panic("internal error: entered unreachable code"),
}
let bound_ty =
self.lowerer().lower_ty_maybe_return_type_notation(predicate.bounded_ty);
let bound_vars = self.tcx.late_bound_vars(hir_id);
self.lowerer().lower_bounds(bound_ty, predicate.bounds,
&mut bounds, bound_vars, filter,
OverlappingAsssocItemConstraints::Allowed);
}
bounds
}
}
}#[instrument(level = "trace", skip(self, hir_generics))]
974 fn probe_ty_param_bounds_in_generics(
975 &self,
976 hir_generics: &'tcx hir::Generics<'tcx>,
977 param_def_id: LocalDefId,
978 filter: PredicateFilter,
979 ) -> Vec<(ty::Clause<'tcx>, Span)> {
980 let mut bounds = Vec::new();
981
982 for predicate in hir_generics.predicates {
983 let hir_id = predicate.hir_id;
984 let hir::WherePredicateKind::BoundPredicate(predicate) = predicate.kind else {
985 continue;
986 };
987
988 match filter {
989 _ if predicate.is_param_bound(param_def_id.to_def_id()) => {
990 }
992 PredicateFilter::All => {
993 }
995 PredicateFilter::SelfOnly
996 | PredicateFilter::SelfTraitThatDefines(_)
997 | PredicateFilter::SelfConstIfConst
998 | PredicateFilter::SelfAndAssociatedTypeBounds => continue,
999 PredicateFilter::ConstIfConst => unreachable!(),
1000 }
1001
1002 let bound_ty = self.lowerer().lower_ty_maybe_return_type_notation(predicate.bounded_ty);
1003
1004 let bound_vars = self.tcx.late_bound_vars(hir_id);
1005 self.lowerer().lower_bounds(
1006 bound_ty,
1007 predicate.bounds,
1008 &mut bounds,
1009 bound_vars,
1010 filter,
1011 OverlappingAsssocItemConstraints::Allowed,
1012 );
1013 }
1014
1015 bounds
1016 }
1017}
1018
1019pub(super) fn const_conditions<'tcx>(
1020 tcx: TyCtxt<'tcx>,
1021 def_id: LocalDefId,
1022) -> ty::ConstConditions<'tcx> {
1023 if !tcx.is_conditionally_const(def_id) {
1024 ::rustc_middle::util::bug::bug_fmt(format_args!("const_conditions invoked for item that is not conditionally const: {0:?}",
def_id));bug!("const_conditions invoked for item that is not conditionally const: {def_id:?}");
1025 }
1026
1027 match tcx.opt_rpitit_info(def_id.to_def_id()) {
1028 Some(
1030 ty::ImplTraitInTraitData::Impl { fn_def_id }
1031 | ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
1032 ) => return tcx.const_conditions(fn_def_id),
1033 None => {}
1034 }
1035
1036 let (generics, trait_def_id_and_supertraits, has_parent) = match tcx.hir_node_by_def_id(def_id)
1037 {
1038 Node::Item(item) => match item.kind {
1039 hir::ItemKind::Impl(impl_) => (impl_.generics, None, false),
1040 hir::ItemKind::Fn { generics, .. } => (generics, None, false),
1041 hir::ItemKind::Trait(_, _, _, _, generics, supertraits, _) => {
1042 (generics, Some((Some(item.owner_id.def_id), supertraits)), false)
1043 }
1044 hir::ItemKind::TraitAlias(_, _, generics, supertraits) => {
1045 (generics, Some((None, supertraits)), false)
1046 }
1047 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("const_conditions called on wrong item: {0:?}",
def_id))bug!("const_conditions called on wrong item: {def_id:?}"),
1048 },
1049 Node::TraitItem(item) => match item.kind {
1054 hir::TraitItemKind::Fn(_, _) | hir::TraitItemKind::Type(_, _) => {
1055 (item.generics, None, true)
1056 }
1057 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("const_conditions called on wrong item: {0:?}",
def_id))bug!("const_conditions called on wrong item: {def_id:?}"),
1058 },
1059 Node::ImplItem(item) => match item.kind {
1060 hir::ImplItemKind::Fn(_, _) | hir::ImplItemKind::Type(_) => {
1061 (item.generics, None, tcx.is_conditionally_const(tcx.local_parent(def_id)))
1062 }
1063 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("const_conditions called on wrong item: {0:?}",
def_id))bug!("const_conditions called on wrong item: {def_id:?}"),
1064 },
1065 Node::ForeignItem(item) => match item.kind {
1066 hir::ForeignItemKind::Fn(_, _, generics) => (generics, None, false),
1067 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("const_conditions called on wrong item: {0:?}",
def_id))bug!("const_conditions called on wrong item: {def_id:?}"),
1068 },
1069 Node::OpaqueTy(opaque) => match opaque.origin {
1070 hir::OpaqueTyOrigin::FnReturn { parent, .. } => return tcx.const_conditions(parent),
1071 hir::OpaqueTyOrigin::AsyncFn { .. } | hir::OpaqueTyOrigin::TyAlias { .. } => {
1072 ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
1073 }
1074 },
1075 Node::Ctor(hir::VariantData::Tuple { .. }) => return Default::default(),
1077 Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(_), .. }) => {
1078 (hir::Generics::empty(), None, tcx.is_conditionally_const(tcx.local_parent(def_id)))
1079 }
1080 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("const_conditions called on wrong item: {0:?}",
def_id))bug!("const_conditions called on wrong item: {def_id:?}"),
1081 };
1082
1083 let icx = ItemCtxt::new(tcx, def_id);
1084 let mut bounds = Vec::new();
1085
1086 for pred in generics.predicates {
1087 match pred.kind {
1088 hir::WherePredicateKind::BoundPredicate(bound_pred) => {
1089 let ty = icx.lowerer().lower_ty_maybe_return_type_notation(bound_pred.bounded_ty);
1090 let bound_vars = tcx.late_bound_vars(pred.hir_id);
1091 icx.lowerer().lower_bounds(
1092 ty,
1093 bound_pred.bounds.iter(),
1094 &mut bounds,
1095 bound_vars,
1096 PredicateFilter::ConstIfConst,
1097 OverlappingAsssocItemConstraints::Allowed,
1098 );
1099 }
1100 _ => {}
1101 }
1102 }
1103
1104 if let Some((def_id, supertraits)) = trait_def_id_and_supertraits {
1105 if let Some(def_id) = def_id {
1106 bounds.push((
1108 ty::Binder::dummy(ty::TraitRef::identity(tcx, def_id.to_def_id()))
1109 .to_host_effect_clause(tcx, ty::BoundConstness::Maybe),
1110 DUMMY_SP,
1111 ));
1112 }
1113
1114 icx.lowerer().lower_bounds(
1115 tcx.types.self_param,
1116 supertraits,
1117 &mut bounds,
1118 ty::List::empty(),
1119 PredicateFilter::ConstIfConst,
1120 OverlappingAsssocItemConstraints::Allowed,
1121 );
1122 }
1123
1124 ty::ConstConditions {
1125 parent: has_parent.then(|| tcx.local_parent(def_id).to_def_id()),
1126 predicates: tcx.arena.alloc_from_iter(bounds.into_iter().map(|(clause, span)| {
1127 (
1128 clause.kind().map_bound(|clause| match clause {
1129 ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
1130 trait_ref,
1131 constness: ty::BoundConstness::Maybe,
1132 }) => trait_ref,
1133 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("converted {0:?}", clause))bug!("converted {clause:?}"),
1134 }),
1135 span,
1136 )
1137 })),
1138 }
1139}
1140
1141pub(super) fn explicit_implied_const_bounds<'tcx>(
1142 tcx: TyCtxt<'tcx>,
1143 def_id: LocalDefId,
1144) -> ty::EarlyBinder<'tcx, &'tcx [(ty::PolyTraitRef<'tcx>, Span)]> {
1145 if !tcx.is_conditionally_const(def_id) {
1146 ::rustc_middle::util::bug::bug_fmt(format_args!("explicit_implied_const_bounds invoked for item that is not conditionally const: {0:?}",
def_id));bug!(
1147 "explicit_implied_const_bounds invoked for item that is not conditionally const: {def_id:?}"
1148 );
1149 }
1150
1151 let bounds = match tcx.opt_rpitit_info(def_id.to_def_id()) {
1152 Some(ty::ImplTraitInTraitData::Trait { .. }) => {
1155 explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
1156 }
1157 Some(ty::ImplTraitInTraitData::Impl { .. }) => {
1158 ::rustc_middle::util::bug::span_bug_fmt(tcx.def_span(def_id),
format_args!("RPITIT in impl should not have item bounds"))span_bug!(tcx.def_span(def_id), "RPITIT in impl should not have item bounds")
1159 }
1160 None => match tcx.hir_node_by_def_id(def_id) {
1161 Node::Item(hir::Item {
1162 kind: hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..),
1163 ..
1164 }) => implied_predicates_with_filter(
1165 tcx,
1166 def_id.to_def_id(),
1167 PredicateFilter::SelfConstIfConst,
1168 ),
1169 Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. })
1170 | Node::OpaqueTy(_) => {
1171 explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
1172 }
1173 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("explicit_implied_const_bounds called on wrong item: {0:?}",
def_id))bug!("explicit_implied_const_bounds called on wrong item: {def_id:?}"),
1174 },
1175 };
1176
1177 bounds.map_bound(|bounds| {
1178 &*tcx.arena.alloc_from_iter(bounds.iter().copied().map(|(clause, span)| {
1179 (
1180 clause.kind().map_bound(|clause| match clause {
1181 ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
1182 trait_ref,
1183 constness: ty::BoundConstness::Maybe,
1184 }) => trait_ref,
1185 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("converted {0:?}", clause))bug!("converted {clause:?}"),
1186 }),
1187 span,
1188 )
1189 }))
1190 })
1191}