1use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
2use rustc_hir as hir;
3use rustc_infer::traits::util;
4use rustc_middle::ty::{
5 self, GenericArgs, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
6 Upcast, shift_vars,
7};
8use rustc_middle::{bug, span_bug};
9use rustc_span::Span;
10use rustc_span::def_id::{DefId, LocalDefId};
11use tracing::{debug, instrument};
12
13use super::ItemCtxt;
14use super::predicates_of::assert_only_contains_predicates_from;
15use crate::hir_ty_lowering::{
16 HirTyLowerer, ImpliedBoundsContext, OverlappingAsssocItemConstraints, PredicateFilter,
17};
18
19fn associated_type_bounds<'tcx>(
27 tcx: TyCtxt<'tcx>,
28 assoc_item_def_id: LocalDefId,
29 hir_bounds: &'tcx [hir::GenericBound<'tcx>],
30 span: Span,
31 filter: PredicateFilter,
32) -> &'tcx [(ty::Clause<'tcx>, Span)] {
33 {
let _guard = ReducedQueriesGuard::new();
{
let item_ty =
Ty::new_projection_from_args(tcx, assoc_item_def_id.to_def_id(),
GenericArgs::identity_for_item(tcx, assoc_item_def_id));
let icx = ItemCtxt::new(tcx, assoc_item_def_id);
let mut bounds = Vec::new();
icx.lowerer().lower_bounds(item_ty, hir_bounds, &mut bounds,
ty::List::empty(), filter,
OverlappingAsssocItemConstraints::Allowed);
match filter {
PredicateFilter::All | PredicateFilter::SelfOnly |
PredicateFilter::SelfTraitThatDefines(_) |
PredicateFilter::SelfAndAssociatedTypeBounds => {
icx.lowerer().add_implicit_sizedness_bounds(&mut bounds,
item_ty, hir_bounds, &[],
ImpliedBoundsContext::AssociatedType(assoc_item_def_id),
span);
icx.lowerer().add_default_traits(&mut bounds, item_ty,
hir_bounds, &[],
ImpliedBoundsContext::AssociatedType(assoc_item_def_id),
span);
let trait_def_id = tcx.local_parent(assoc_item_def_id);
let trait_predicates =
tcx.trait_explicit_predicates_and_bounds(trait_def_id);
let item_trait_ref =
ty::TraitRef::identity(tcx,
tcx.parent(assoc_item_def_id.to_def_id()));
bounds.extend(trait_predicates.predicates.iter().copied().filter_map(|(clause,
span)|
{
remap_gat_vars_and_recurse_into_nested_projections(tcx,
filter, item_trait_ref, assoc_item_def_id, span, clause)
}));
}
PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst
=> {}
}
let bounds = tcx.arena.alloc_from_iter(bounds);
{
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/item_bounds.rs:106",
"rustc_hir_analysis::collect::item_bounds",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/item_bounds.rs"),
::tracing_core::__macro_support::Option::Some(106u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::item_bounds"),
::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!("associated_type_bounds({0}) = {1:?}",
tcx.def_path_str(assoc_item_def_id.to_def_id()), bounds) as
&dyn Value))])
});
} else { ; }
};
assert_only_contains_predicates_from(filter, bounds, item_ty);
bounds
}
}ty::print::with_reduced_queries!({
34 let item_ty = Ty::new_projection_from_args(
35 tcx,
36 assoc_item_def_id.to_def_id(),
37 GenericArgs::identity_for_item(tcx, assoc_item_def_id),
38 );
39
40 let icx = ItemCtxt::new(tcx, assoc_item_def_id);
41 let mut bounds = Vec::new();
42 icx.lowerer().lower_bounds(
43 item_ty,
44 hir_bounds,
45 &mut bounds,
46 ty::List::empty(),
47 filter,
48 OverlappingAsssocItemConstraints::Allowed,
49 );
50
51 match filter {
52 PredicateFilter::All
53 | PredicateFilter::SelfOnly
54 | PredicateFilter::SelfTraitThatDefines(_)
55 | PredicateFilter::SelfAndAssociatedTypeBounds => {
56 icx.lowerer().add_implicit_sizedness_bounds(
58 &mut bounds,
59 item_ty,
60 hir_bounds,
61 &[],
62 ImpliedBoundsContext::AssociatedType(assoc_item_def_id),
63 span,
64 );
65 icx.lowerer().add_default_traits(
66 &mut bounds,
67 item_ty,
68 hir_bounds,
69 &[],
70 ImpliedBoundsContext::AssociatedType(assoc_item_def_id),
71 span,
72 );
73
74 let trait_def_id = tcx.local_parent(assoc_item_def_id);
76 let trait_predicates = tcx.trait_explicit_predicates_and_bounds(trait_def_id);
77
78 let item_trait_ref =
79 ty::TraitRef::identity(tcx, tcx.parent(assoc_item_def_id.to_def_id()));
80 bounds.extend(trait_predicates.predicates.iter().copied().filter_map(
81 |(clause, span)| {
82 remap_gat_vars_and_recurse_into_nested_projections(
83 tcx,
84 filter,
85 item_trait_ref,
86 assoc_item_def_id,
87 span,
88 clause,
89 )
90 },
91 ));
92 }
93 PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {
95 }
103 }
104
105 let bounds = tcx.arena.alloc_from_iter(bounds);
106 debug!(
107 "associated_type_bounds({}) = {:?}",
108 tcx.def_path_str(assoc_item_def_id.to_def_id()),
109 bounds
110 );
111
112 assert_only_contains_predicates_from(filter, bounds, item_ty);
113
114 bounds
115 })
116}
117
118fn remap_gat_vars_and_recurse_into_nested_projections<'tcx>(
132 tcx: TyCtxt<'tcx>,
133 filter: PredicateFilter,
134 item_trait_ref: ty::TraitRef<'tcx>,
135 assoc_item_def_id: LocalDefId,
136 span: Span,
137 clause: ty::Clause<'tcx>,
138) -> Option<(ty::Clause<'tcx>, Span)> {
139 let mut clause_ty = match clause.kind().skip_binder() {
140 ty::ClauseKind::Trait(tr) => tr.self_ty(),
141 ty::ClauseKind::Projection(proj) => proj.projection_term.self_ty(),
142 ty::ClauseKind::TypeOutlives(outlives) => outlives.0,
143 ty::ClauseKind::HostEffect(host) => host.self_ty(),
144 _ => return None,
145 };
146
147 let gat_vars = loop {
148 if let ty::Alias(
149 alias_ty @ ty::AliasTy { kind: ty::Projection { def_id: alias_ty_def_id }, .. },
150 ) = *clause_ty.kind()
151 {
152 if alias_ty.trait_ref(tcx) == item_trait_ref
153 && alias_ty_def_id == assoc_item_def_id.to_def_id()
154 {
155 break &alias_ty.args[item_trait_ref.args.len()..];
158 } else {
159 match filter {
161 PredicateFilter::All => {}
162 PredicateFilter::SelfOnly => {
163 return None;
164 }
165 PredicateFilter::SelfTraitThatDefines(_)
166 | PredicateFilter::SelfConstIfConst
167 | PredicateFilter::SelfAndAssociatedTypeBounds
168 | PredicateFilter::ConstIfConst => {
169 {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("invalid predicate filter for `remap_gat_vars_and_recurse_into_nested_projections`")));
}unreachable!(
170 "invalid predicate filter for \
171 `remap_gat_vars_and_recurse_into_nested_projections`"
172 )
173 }
174 }
175
176 clause_ty = alias_ty.self_ty();
177 continue;
178 }
179 }
180
181 return None;
182 };
183
184 if gat_vars.is_empty() {
186 return Some((clause, span));
187 }
188
189 let mut mapping = FxIndexMap::default();
192 let generics = tcx.generics_of(assoc_item_def_id);
193 for (param, var) in std::iter::zip(&generics.own_params, gat_vars) {
194 let existing = match var.kind() {
195 ty::GenericArgKind::Lifetime(re) => {
196 let ty::RegionKind::ReBound(ty::BoundVarIndexKind::Bound(ty::INNERMOST), bv) =
197 re.kind()
198 else {
199 return None;
200 };
201 mapping.insert(bv.var, tcx.mk_param_from_def(param))
202 }
203 ty::GenericArgKind::Type(ty) => {
204 let ty::Bound(ty::BoundVarIndexKind::Bound(ty::INNERMOST), bv) = *ty.kind() else {
205 return None;
206 };
207 mapping.insert(bv.var, tcx.mk_param_from_def(param))
208 }
209 ty::GenericArgKind::Const(ct) => {
210 let ty::ConstKind::Bound(ty::BoundVarIndexKind::Bound(ty::INNERMOST), bv) =
211 ct.kind()
212 else {
213 return None;
214 };
215 mapping.insert(bv.var, tcx.mk_param_from_def(param))
216 }
217 };
218
219 if existing.is_some() {
220 return None;
221 }
222 }
223
224 let mut folder =
227 MapAndCompressBoundVars { tcx, binder: ty::INNERMOST, still_bound_vars: ::alloc::vec::Vec::new()vec![], mapping };
228 let pred = clause.kind().skip_binder().fold_with(&mut folder);
229
230 Some((
231 ty::Binder::bind_with_vars(pred, tcx.mk_bound_variable_kinds(&folder.still_bound_vars))
232 .upcast(tcx),
233 span,
234 ))
235}
236
237struct MapAndCompressBoundVars<'tcx> {
244 tcx: TyCtxt<'tcx>,
245 binder: ty::DebruijnIndex,
247 still_bound_vars: Vec<ty::BoundVariableKind<'tcx>>,
250 mapping: FxIndexMap<ty::BoundVar, ty::GenericArg<'tcx>>,
254}
255
256impl<'tcx> TypeFolder<TyCtxt<'tcx>> for MapAndCompressBoundVars<'tcx> {
257 fn cx(&self) -> TyCtxt<'tcx> {
258 self.tcx
259 }
260
261 fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
262 where
263 ty::Binder<'tcx, T>: TypeSuperFoldable<TyCtxt<'tcx>>,
264 {
265 self.binder.shift_in(1);
266 let out = t.super_fold_with(self);
267 self.binder.shift_out(1);
268 out
269 }
270
271 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
272 if !ty.has_bound_vars() {
273 return ty;
274 }
275
276 if let ty::Bound(ty::BoundVarIndexKind::Bound(binder), old_bound) = *ty.kind()
277 && self.binder == binder
278 {
279 let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
280 mapped.expect_ty()
281 } else {
282 let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
285 self.still_bound_vars.push(ty::BoundVariableKind::Ty(old_bound.kind));
286 let mapped = Ty::new_bound(
287 self.tcx,
288 ty::INNERMOST,
289 ty::BoundTy { var, kind: old_bound.kind },
290 );
291 self.mapping.insert(old_bound.var, mapped.into());
292 mapped
293 };
294
295 shift_vars(self.tcx, mapped, self.binder.as_u32())
296 } else {
297 ty.super_fold_with(self)
298 }
299 }
300
301 fn fold_region(&mut self, re: ty::Region<'tcx>) -> ty::Region<'tcx> {
302 if let ty::ReBound(ty::BoundVarIndexKind::Bound(binder), old_bound) = re.kind()
303 && self.binder == binder
304 {
305 let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
306 mapped.expect_region()
307 } else {
308 let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
309 self.still_bound_vars.push(ty::BoundVariableKind::Region(old_bound.kind));
310 let mapped = ty::Region::new_bound(
311 self.tcx,
312 ty::INNERMOST,
313 ty::BoundRegion { var, kind: old_bound.kind },
314 );
315 self.mapping.insert(old_bound.var, mapped.into());
316 mapped
317 };
318
319 shift_vars(self.tcx, mapped, self.binder.as_u32())
320 } else {
321 re
322 }
323 }
324
325 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
326 if !ct.has_bound_vars() {
327 return ct;
328 }
329
330 if let ty::ConstKind::Bound(ty::BoundVarIndexKind::Bound(binder), old_bound) = ct.kind()
331 && self.binder == binder
332 {
333 let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
334 mapped.expect_const()
335 } else {
336 let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
337 self.still_bound_vars.push(ty::BoundVariableKind::Const);
338 let mapped =
339 ty::Const::new_bound(self.tcx, ty::INNERMOST, ty::BoundConst::new(var));
340 self.mapping.insert(old_bound.var, mapped.into());
341 mapped
342 };
343
344 shift_vars(self.tcx, mapped, self.binder.as_u32())
345 } else {
346 ct.super_fold_with(self)
347 }
348 }
349
350 fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
351 if !p.has_bound_vars() { p } else { p.super_fold_with(self) }
352 }
353}
354
355#[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("opaque_type_bounds",
"rustc_hir_analysis::collect::item_bounds",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/item_bounds.rs"),
::tracing_core::__macro_support::Option::Some(359u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::item_bounds"),
::tracing_core::field::FieldSet::new(&["opaque_def_id",
"hir_bounds", "span", "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(&opaque_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(&hir_bounds)
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(&span)
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: &'tcx [(ty::Clause<'tcx>, Span)] =
loop {};
return __tracing_attr_fake_return;
}
{
{
let _guard = ReducedQueriesGuard::new();
{
let icx = ItemCtxt::new(tcx, opaque_def_id);
let mut bounds = Vec::new();
icx.lowerer().lower_bounds(item_ty, hir_bounds, &mut bounds,
ty::List::empty(), filter,
OverlappingAsssocItemConstraints::Allowed);
match filter {
PredicateFilter::All | PredicateFilter::SelfOnly |
PredicateFilter::SelfTraitThatDefines(_) |
PredicateFilter::SelfAndAssociatedTypeBounds => {
icx.lowerer().add_implicit_sizedness_bounds(&mut bounds,
item_ty, hir_bounds, &[], ImpliedBoundsContext::ImplTrait,
span);
icx.lowerer().add_default_traits(&mut bounds, item_ty,
hir_bounds, &[], ImpliedBoundsContext::ImplTrait, span);
}
PredicateFilter::ConstIfConst |
PredicateFilter::SelfConstIfConst => {}
}
{
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/item_bounds.rs:405",
"rustc_hir_analysis::collect::item_bounds",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/item_bounds.rs"),
::tracing_core::__macro_support::Option::Some(405u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::item_bounds"),
::tracing_core::field::FieldSet::new(&["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(&bounds) as
&dyn Value))])
});
} else { ; }
};
tcx.arena.alloc_slice(&bounds)
}
}
}
}
}#[instrument(level = "trace", skip(tcx, item_ty))]
360fn opaque_type_bounds<'tcx>(
361 tcx: TyCtxt<'tcx>,
362 opaque_def_id: LocalDefId,
363 hir_bounds: &'tcx [hir::GenericBound<'tcx>],
364 item_ty: Ty<'tcx>,
365 span: Span,
366 filter: PredicateFilter,
367) -> &'tcx [(ty::Clause<'tcx>, Span)] {
368 ty::print::with_reduced_queries!({
369 let icx = ItemCtxt::new(tcx, opaque_def_id);
370 let mut bounds = Vec::new();
371 icx.lowerer().lower_bounds(
372 item_ty,
373 hir_bounds,
374 &mut bounds,
375 ty::List::empty(),
376 filter,
377 OverlappingAsssocItemConstraints::Allowed,
378 );
379 match filter {
381 PredicateFilter::All
382 | PredicateFilter::SelfOnly
383 | PredicateFilter::SelfTraitThatDefines(_)
384 | PredicateFilter::SelfAndAssociatedTypeBounds => {
385 icx.lowerer().add_implicit_sizedness_bounds(
386 &mut bounds,
387 item_ty,
388 hir_bounds,
389 &[],
390 ImpliedBoundsContext::ImplTrait,
391 span,
392 );
393 icx.lowerer().add_default_traits(
394 &mut bounds,
395 item_ty,
396 hir_bounds,
397 &[],
398 ImpliedBoundsContext::ImplTrait,
399 span,
400 );
401 }
402 PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
404 }
405 debug!(?bounds);
406
407 tcx.arena.alloc_slice(&bounds)
408 })
409}
410
411pub(super) fn explicit_item_bounds(
412 tcx: TyCtxt<'_>,
413 def_id: LocalDefId,
414) -> ty::EarlyBinder<'_, &'_ [(ty::Clause<'_>, Span)]> {
415 explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::All)
416}
417
418pub(super) fn explicit_item_self_bounds(
419 tcx: TyCtxt<'_>,
420 def_id: LocalDefId,
421) -> ty::EarlyBinder<'_, &'_ [(ty::Clause<'_>, Span)]> {
422 explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::SelfOnly)
423}
424
425pub(super) fn explicit_item_bounds_with_filter(
426 tcx: TyCtxt<'_>,
427 def_id: LocalDefId,
428 filter: PredicateFilter,
429) -> ty::EarlyBinder<'_, &'_ [(ty::Clause<'_>, Span)]> {
430 match tcx.opt_rpitit_info(def_id.to_def_id()) {
431 Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
434 let opaque_ty = tcx.hir_node_by_def_id(opaque_def_id.expect_local()).expect_opaque_ty();
435 let bounds =
436 associated_type_bounds(tcx, def_id, opaque_ty.bounds, opaque_ty.span, filter);
437 return ty::EarlyBinder::bind(bounds);
438 }
439 Some(ty::ImplTraitInTraitData::Impl { .. }) => {
440 ::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")
441 }
442 None => {}
443 }
444
445 let bounds = match tcx.hir_node_by_def_id(def_id) {
446 hir::Node::TraitItem(hir::TraitItem {
447 kind: hir::TraitItemKind::Type(bounds, _),
448 span,
449 ..
450 }) => associated_type_bounds(tcx, def_id, bounds, *span, filter),
451 hir::Node::OpaqueTy(hir::OpaqueTy { bounds, origin, span, .. }) => match origin {
452 rustc_hir::OpaqueTyOrigin::FnReturn {
456 parent,
457 in_trait_or_impl: Some(hir::RpitContext::Trait),
458 }
459 | rustc_hir::OpaqueTyOrigin::AsyncFn {
460 parent,
461 in_trait_or_impl: Some(hir::RpitContext::Trait),
462 } => {
463 let args = GenericArgs::identity_for_item(tcx, def_id);
464 let item_ty = Ty::new_opaque(tcx, def_id.to_def_id(), args);
465 let bounds = &*tcx.arena.alloc_slice(
466 &opaque_type_bounds(tcx, def_id, bounds, item_ty, *span, filter)
467 .to_vec()
468 .fold_with(&mut AssocTyToOpaque { tcx, fn_def_id: parent.to_def_id() }),
469 );
470 assert_only_contains_predicates_from(filter, bounds, item_ty);
471 bounds
472 }
473 rustc_hir::OpaqueTyOrigin::FnReturn {
474 parent: _,
475 in_trait_or_impl: None | Some(hir::RpitContext::TraitImpl),
476 }
477 | rustc_hir::OpaqueTyOrigin::AsyncFn {
478 parent: _,
479 in_trait_or_impl: None | Some(hir::RpitContext::TraitImpl),
480 }
481 | rustc_hir::OpaqueTyOrigin::TyAlias { parent: _, .. } => {
482 let args = GenericArgs::identity_for_item(tcx, def_id);
483 let item_ty = Ty::new_opaque(tcx, def_id.to_def_id(), args);
484 let bounds = opaque_type_bounds(tcx, def_id, bounds, item_ty, *span, filter);
485 assert_only_contains_predicates_from(filter, bounds, item_ty);
486 bounds
487 }
488 },
489 hir::Node::Item(hir::Item { kind: hir::ItemKind::TyAlias(..), .. }) => &[],
490 node => ::rustc_middle::util::bug::bug_fmt(format_args!("item_bounds called on {0:?} => {1:?}",
def_id, node))bug!("item_bounds called on {def_id:?} => {node:?}"),
491 };
492
493 ty::EarlyBinder::bind(bounds)
494}
495
496pub(super) fn item_bounds(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
497 tcx.explicit_item_bounds(def_id).map_bound(|bounds| {
498 tcx.mk_clauses_from_iter(util::elaborate(tcx, bounds.iter().map(|&(bound, _span)| bound)))
499 })
500}
501
502pub(super) fn item_self_bounds(
503 tcx: TyCtxt<'_>,
504 def_id: DefId,
505) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
506 tcx.explicit_item_self_bounds(def_id).map_bound(|bounds| {
507 tcx.mk_clauses_from_iter(
508 util::elaborate(tcx, bounds.iter().map(|&(bound, _span)| bound)).filter_only_self(),
509 )
510 })
511}
512
513pub(super) fn item_non_self_bounds(
516 tcx: TyCtxt<'_>,
517 def_id: DefId,
518) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
519 let all_bounds: FxIndexSet<_> = tcx.item_bounds(def_id).skip_binder().iter().collect();
520 let own_bounds: FxIndexSet<_> = tcx.item_self_bounds(def_id).skip_binder().iter().collect();
521 if all_bounds.len() == own_bounds.len() {
522 ty::EarlyBinder::bind(ty::ListWithCachedTypeInfo::empty())
523 } else {
524 ty::EarlyBinder::bind(tcx.mk_clauses_from_iter(all_bounds.difference(&own_bounds).copied()))
525 }
526}
527
528pub(super) fn impl_super_outlives(
531 tcx: TyCtxt<'_>,
532 def_id: DefId,
533) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
534 tcx.impl_trait_header(def_id).trait_ref.map_bound(|trait_ref| {
535 let clause: ty::Clause<'_> = trait_ref.upcast(tcx);
536 tcx.mk_clauses_from_iter(util::elaborate(tcx, [clause]).filter(|clause| {
537 #[allow(non_exhaustive_omitted_patterns)] match clause.kind().skip_binder() {
ty::ClauseKind::TypeOutlives(_) | ty::ClauseKind::RegionOutlives(_) =>
true,
_ => false,
}matches!(
538 clause.kind().skip_binder(),
539 ty::ClauseKind::TypeOutlives(_) | ty::ClauseKind::RegionOutlives(_)
540 )
541 }))
542 })
543}
544
545struct AssocTyToOpaque<'tcx> {
546 tcx: TyCtxt<'tcx>,
547 fn_def_id: DefId,
548}
549
550impl<'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTyToOpaque<'tcx> {
551 fn cx(&self) -> TyCtxt<'tcx> {
552 self.tcx
553 }
554
555 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
556 if let &ty::Alias(ty::AliasTy {
557 kind: ty::Projection { def_id: projection_ty_def_id },
558 args,
559 ..
560 }) = ty.kind()
561 && let Some(ty::ImplTraitInTraitData::Trait { fn_def_id, .. }) =
562 self.tcx.opt_rpitit_info(projection_ty_def_id)
563 && fn_def_id == self.fn_def_id
564 {
565 self.tcx.type_of(projection_ty_def_id).instantiate(self.tcx, args).skip_norm_wip()
566 } else {
567 ty.super_fold_with(self)
568 }
569 }
570}