1use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
2use rustc_hir as hir;
3use rustc_infer::traits::util;
4use rustc_middle::ty::{
5 self, GenericArgs, IncludeSizedImplicitBounds, PredicateProxy, Ty, TyCtxt, TypeFoldable,
6 TypeFolder, TypeSuperFoldable, TypeVisitableExt, Upcast, shift_vars,
7};
8use rustc_span::def_id::{DefId, LocalDefId};
9use rustc_span::{Span, bug, span_bug};
10use tracing::{debug, instrument};
11
12use super::ItemCtxt;
13use super::clauses_of::assert_only_contains_clauses_from;
14use crate::hir_ty_lowering::{
15 HirTyLowerer, ImpliedBoundsContext, OverlappingAsssocItemConstraints, PredicateFilter,
16};
17
18fn associated_type_bounds<'tcx>(
26 tcx: TyCtxt<'tcx>,
27 assoc_item_def_id: LocalDefId,
28 hir_bounds: &'tcx [hir::GenericBound<'tcx>],
29 span: Span,
30 filter: PredicateFilter,
31) -> &'tcx [(ty::Clause<'tcx>, Span)] {
32 {
let _guard = ReducedQueriesGuard::new();
{
let item_ty =
Ty::new_projection_from_args(tcx, ty::IsRigid::No,
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_bounds(&mut bounds, item_ty,
hir_bounds, ImpliedBoundsContext::AssociatedTypeOrImplTrait,
span, IncludeSizedImplicitBounds::Yes);
let trait_def_id = tcx.local_parent(assoc_item_def_id);
let trait_clauses =
tcx.trait_explicit_clauses_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_clauses.clauses.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 /rustc-dev/3ef0c22cdb86ac42dc6b8f0f1ec36857daa6f2a1/compiler/rustc_hir_analysis/src/collect/item_bounds.rs:98",
"rustc_hir_analysis::collect::item_bounds",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/3ef0c22cdb86ac42dc6b8f0f1ec36857daa6f2a1/compiler/rustc_hir_analysis/src/collect/item_bounds.rs"),
::tracing_core::__macro_support::Option::Some(98u32),
::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};
__CALLSITE.metadata().fields().value_set_all(&[(::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 ::tracing::field::Value))])
});
} else { ; }
};
assert_only_contains_clauses_from(filter, bounds, item_ty);
bounds
}
}ty::print::with_reduced_queries!({
33 let item_ty = Ty::new_projection_from_args(
34 tcx,
35 ty::IsRigid::No,
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_bounds(
58 &mut bounds,
59 item_ty,
60 hir_bounds,
61 ImpliedBoundsContext::AssociatedTypeOrImplTrait,
62 span,
63 IncludeSizedImplicitBounds::Yes,
64 );
65
66 let trait_def_id = tcx.local_parent(assoc_item_def_id);
68 let trait_clauses = tcx.trait_explicit_clauses_and_bounds(trait_def_id);
69
70 let item_trait_ref =
71 ty::TraitRef::identity(tcx, tcx.parent(assoc_item_def_id.to_def_id()));
72 bounds.extend(trait_clauses.clauses.iter().copied().filter_map(
73 |(clause, span)| {
74 remap_gat_vars_and_recurse_into_nested_projections(
75 tcx,
76 filter,
77 item_trait_ref,
78 assoc_item_def_id,
79 span,
80 clause,
81 )
82 },
83 ));
84 }
85 PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {
87 }
95 }
96
97 let bounds = tcx.arena.alloc_from_iter(bounds);
98 debug!(
99 "associated_type_bounds({}) = {:?}",
100 tcx.def_path_str(assoc_item_def_id.to_def_id()),
101 bounds
102 );
103
104 assert_only_contains_clauses_from(filter, bounds, item_ty);
105
106 bounds
107 })
108}
109
110fn remap_gat_vars_and_recurse_into_nested_projections<'tcx>(
124 tcx: TyCtxt<'tcx>,
125 filter: PredicateFilter,
126 item_trait_ref: ty::TraitRef<'tcx>,
127 assoc_item_def_id: LocalDefId,
128 span: Span,
129 clause: ty::Clause<'tcx>,
130) -> Option<(ty::Clause<'tcx>, Span)> {
131 let mut clause_ty = match clause.kind().skip_binder() {
132 ty::ClauseKind::Trait(tr) => tr.self_ty(),
133 ty::ClauseKind::Projection(proj) => proj.projection_term.self_ty(),
134 ty::ClauseKind::TypeOutlives(outlives) => outlives.0,
135 ty::ClauseKind::HostEffect(host) => host.self_ty(),
136 _ => return None,
137 };
138
139 let gat_vars = loop {
140 if let ty::Alias(
141 _,
142 alias_ty @ ty::AliasTy { kind: ty::Projection { def_id: alias_ty_def_id }, .. },
143 ) = *clause_ty.kind()
144 {
145 if alias_ty.trait_ref(tcx) == item_trait_ref
146 && alias_ty_def_id == assoc_item_def_id.to_def_id()
147 {
148 break tcx.mk_args(&alias_ty.args.as_slice()[item_trait_ref.args.len()..]);
151 } else {
152 match filter {
154 PredicateFilter::All => {}
155 PredicateFilter::SelfOnly => {
156 return None;
157 }
158 PredicateFilter::SelfTraitThatDefines(_)
159 | PredicateFilter::SelfConstIfConst
160 | PredicateFilter::SelfAndAssociatedTypeBounds
161 | PredicateFilter::ConstIfConst => {
162 {
::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!(
163 "invalid predicate filter for \
164 `remap_gat_vars_and_recurse_into_nested_projections`"
165 )
166 }
167 }
168
169 clause_ty = alias_ty.self_ty();
170 continue;
171 }
172 }
173
174 return None;
175 };
176
177 if gat_vars.is_empty() {
179 return Some((clause, span));
180 }
181
182 let mut mapping = FxIndexMap::default();
185 let generics = tcx.generics_of(assoc_item_def_id);
186 for (param, var) in std::iter::zip(&generics.own_params, gat_vars) {
187 let existing = match var.kind() {
188 ty::GenericArgKind::Lifetime(re) => {
189 let ty::RegionKind::ReBound(ty::BoundVarIndexKind::Bound(ty::INNERMOST), bv) =
190 re.kind()
191 else {
192 return None;
193 };
194 mapping.insert(bv.var, tcx.mk_param_from_def(param))
195 }
196 ty::GenericArgKind::Type(ty) => {
197 let ty::Bound(ty::BoundVarIndexKind::Bound(ty::INNERMOST), bv) = *ty.kind() else {
198 return None;
199 };
200 mapping.insert(bv.var, tcx.mk_param_from_def(param))
201 }
202 ty::GenericArgKind::Const(ct) => {
203 let ty::ConstKind::Bound(ty::BoundVarIndexKind::Bound(ty::INNERMOST), bv) =
204 ct.kind()
205 else {
206 return None;
207 };
208 mapping.insert(bv.var, tcx.mk_param_from_def(param))
209 }
210 };
211
212 if existing.is_some() {
213 return None;
214 }
215 }
216
217 let mut folder =
220 MapAndCompressBoundVars { tcx, binder: ty::INNERMOST, still_bound_vars: ::alloc::vec::Vec::new()vec![], mapping };
221 let pred = clause.kind().skip_binder().fold_with(&mut folder);
222
223 Some((
224 ty::Binder::bind_with_vars(pred, tcx.mk_bound_variable_kinds(&folder.still_bound_vars))
225 .upcast(tcx),
226 span,
227 ))
228}
229
230struct MapAndCompressBoundVars<'tcx> {
237 tcx: TyCtxt<'tcx>,
238 binder: ty::DebruijnIndex,
240 still_bound_vars: Vec<ty::BoundVariableKind<'tcx>>,
243 mapping: FxIndexMap<ty::BoundVar, ty::GenericArg<'tcx>>,
247}
248
249impl<'tcx> TypeFolder<TyCtxt<'tcx>> for MapAndCompressBoundVars<'tcx> {
250 fn cx(&self) -> TyCtxt<'tcx> {
251 self.tcx
252 }
253
254 fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
255 where
256 ty::Binder<'tcx, T>: TypeSuperFoldable<TyCtxt<'tcx>>,
257 {
258 self.binder.shift_in(1);
259 let out = t.super_fold_with(self);
260 self.binder.shift_out(1);
261 out
262 }
263
264 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
265 if !ty.has_bound_vars() {
266 return ty;
267 }
268
269 if let ty::Bound(ty::BoundVarIndexKind::Bound(binder), old_bound) = *ty.kind()
270 && self.binder == binder
271 {
272 let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
273 mapped.expect_ty()
274 } else {
275 let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
278 self.still_bound_vars.push(ty::BoundVariableKind::Ty(old_bound.kind));
279 let mapped = Ty::new_bound(
280 self.tcx,
281 ty::INNERMOST,
282 ty::BoundTy { var, kind: old_bound.kind },
283 );
284 self.mapping.insert(old_bound.var, mapped.into());
285 mapped
286 };
287
288 shift_vars(self.tcx, mapped, self.binder.as_u32())
289 } else {
290 ty.super_fold_with(self)
291 }
292 }
293
294 fn fold_region(&mut self, re: ty::Region<'tcx>) -> ty::Region<'tcx> {
295 if let ty::ReBound(ty::BoundVarIndexKind::Bound(binder), old_bound) = re.kind()
296 && self.binder == binder
297 {
298 let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
299 mapped.expect_region()
300 } else {
301 let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
302 self.still_bound_vars.push(ty::BoundVariableKind::Region(old_bound.kind));
303 let mapped = ty::Region::new_bound(
304 self.tcx,
305 ty::INNERMOST,
306 ty::BoundRegion { var, kind: old_bound.kind },
307 );
308 self.mapping.insert(old_bound.var, mapped.into());
309 mapped
310 };
311
312 shift_vars(self.tcx, mapped, self.binder.as_u32())
313 } else {
314 re
315 }
316 }
317
318 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
319 if !ct.has_bound_vars() {
320 return ct;
321 }
322
323 if let ty::ConstKind::Bound(ty::BoundVarIndexKind::Bound(binder), old_bound) = ct.kind()
324 && self.binder == binder
325 {
326 let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
327 mapped.expect_const()
328 } else {
329 let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
330 self.still_bound_vars.push(ty::BoundVariableKind::Const);
331 let mapped =
332 ty::Const::new_bound(self.tcx, ty::INNERMOST, ty::BoundConst::new(var));
333 self.mapping.insert(old_bound.var, mapped.into());
334 mapped
335 };
336
337 shift_vars(self.tcx, mapped, self.binder.as_u32())
338 } else {
339 ct.super_fold_with(self)
340 }
341 }
342
343 fn fold_predicate<P: PredicateProxy<TyCtxt<'tcx>>>(&mut self, p: P) -> P {
344 if !p.has_bound_vars() { p } else { p.super_fold_with(self) }
345 }
346}
347
348{}
#[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("/rustc-dev/3ef0c22cdb86ac42dc6b8f0f1ec36857daa6f2a1/compiler/rustc_hir_analysis/src/collect/item_bounds.rs"),
::tracing_core::__macro_support::Option::Some(352u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::item_bounds"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("opaque_def_id")
}> =
::tracing::__macro_support::FieldName::new("opaque_def_id");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("hir_bounds")
}> =
::tracing::__macro_support::FieldName::new("hir_bounds");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("span")
}> =
::tracing::__macro_support::FieldName::new("span");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("filter")
}> =
::tracing::__macro_support::FieldName::new("filter");
NAME.as_str()
}], ::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};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&opaque_def_id)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&hir_bounds)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&filter)
as &dyn ::tracing::field::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_bounds(&mut bounds, item_ty,
hir_bounds, ImpliedBoundsContext::AssociatedTypeOrImplTrait,
span, IncludeSizedImplicitBounds::Yes);
}
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 /rustc-dev/3ef0c22cdb86ac42dc6b8f0f1ec36857daa6f2a1/compiler/rustc_hir_analysis/src/collect/item_bounds.rs:390",
"rustc_hir_analysis::collect::item_bounds",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/3ef0c22cdb86ac42dc6b8f0f1ec36857daa6f2a1/compiler/rustc_hir_analysis/src/collect/item_bounds.rs"),
::tracing_core::__macro_support::Option::Some(390u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::item_bounds"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("bounds")
}> =
::tracing::__macro_support::FieldName::new("bounds");
NAME.as_str()
}], ::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};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&bounds)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};
tcx.arena.alloc_slice(&bounds)
}
}
}
}
}#[instrument(level = "trace", skip(tcx, item_ty))]
353fn opaque_type_bounds<'tcx>(
354 tcx: TyCtxt<'tcx>,
355 opaque_def_id: LocalDefId,
356 hir_bounds: &'tcx [hir::GenericBound<'tcx>],
357 item_ty: Ty<'tcx>,
358 span: Span,
359 filter: PredicateFilter,
360) -> &'tcx [(ty::Clause<'tcx>, Span)] {
361 ty::print::with_reduced_queries!({
362 let icx = ItemCtxt::new(tcx, opaque_def_id);
363 let mut bounds = Vec::new();
364 icx.lowerer().lower_bounds(
365 item_ty,
366 hir_bounds,
367 &mut bounds,
368 ty::List::empty(),
369 filter,
370 OverlappingAsssocItemConstraints::Allowed,
371 );
372 match filter {
374 PredicateFilter::All
375 | PredicateFilter::SelfOnly
376 | PredicateFilter::SelfTraitThatDefines(_)
377 | PredicateFilter::SelfAndAssociatedTypeBounds => {
378 icx.lowerer().add_implicit_bounds(
379 &mut bounds,
380 item_ty,
381 hir_bounds,
382 ImpliedBoundsContext::AssociatedTypeOrImplTrait,
383 span,
384 IncludeSizedImplicitBounds::Yes,
385 );
386 }
387 PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
389 }
390 debug!(?bounds);
391
392 tcx.arena.alloc_slice(&bounds)
393 })
394}
395
396pub(super) fn explicit_item_bounds(
397 tcx: TyCtxt<'_>,
398 def_id: LocalDefId,
399) -> ty::EarlyBinder<'_, &'_ [(ty::Clause<'_>, Span)]> {
400 explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::All)
401}
402
403pub(super) fn explicit_item_self_bounds(
404 tcx: TyCtxt<'_>,
405 def_id: LocalDefId,
406) -> ty::EarlyBinder<'_, &'_ [(ty::Clause<'_>, Span)]> {
407 explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::SelfOnly)
408}
409
410pub(super) fn explicit_item_bounds_with_filter(
411 tcx: TyCtxt<'_>,
412 def_id: LocalDefId,
413 filter: PredicateFilter,
414) -> ty::EarlyBinder<'_, &'_ [(ty::Clause<'_>, Span)]> {
415 match tcx.opt_rpitit_info(def_id.to_def_id()) {
416 Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
419 let opaque_ty = tcx.hir_node_by_def_id(opaque_def_id.expect_local()).expect_opaque_ty();
420 let bounds =
421 associated_type_bounds(tcx, def_id, opaque_ty.bounds, opaque_ty.span, filter);
422 return ty::EarlyBinder::bind_iter(bounds);
423 }
424 Some(ty::ImplTraitInTraitData::Impl { .. }) => {
425 ::rustc_span::macros::bug_impl(Some(tcx.def_span(def_id)),
format_args!("RPITIT in impl should not have item bounds"),
Location::caller())span_bug!(tcx.def_span(def_id), "RPITIT in impl should not have item bounds")
426 }
427 None => {}
428 }
429
430 let bounds = match tcx.hir_node_by_def_id(def_id) {
431 hir::Node::TraitItem(hir::TraitItem {
432 kind: hir::TraitItemKind::Type(bounds, _),
433 span,
434 ..
435 }) => associated_type_bounds(tcx, def_id, bounds, *span, filter),
436 hir::Node::OpaqueTy(hir::OpaqueTy { bounds, origin, span, .. }) => match origin {
437 rustc_hir::OpaqueTyOrigin::FnReturn {
441 parent,
442 in_trait_or_impl: Some(hir::RpitContext::Trait),
443 }
444 | rustc_hir::OpaqueTyOrigin::AsyncFn {
445 parent,
446 in_trait_or_impl: Some(hir::RpitContext::Trait),
447 } => {
448 let args = GenericArgs::identity_for_item(tcx, def_id);
449 let item_ty = Ty::new_opaque(tcx, ty::IsRigid::No, def_id.to_def_id(), args);
450 let bounds = &*tcx.arena.alloc_slice(
451 &opaque_type_bounds(tcx, def_id, bounds, item_ty, *span, filter)
452 .to_vec()
453 .fold_with(&mut AssocTyToOpaque { tcx, fn_def_id: parent.to_def_id() }),
454 );
455 assert_only_contains_clauses_from(filter, bounds, item_ty);
456 bounds
457 }
458 rustc_hir::OpaqueTyOrigin::FnReturn {
459 parent: _,
460 in_trait_or_impl: None | Some(hir::RpitContext::TraitImpl),
461 }
462 | rustc_hir::OpaqueTyOrigin::AsyncFn {
463 parent: _,
464 in_trait_or_impl: None | Some(hir::RpitContext::TraitImpl),
465 }
466 | rustc_hir::OpaqueTyOrigin::TyAlias { parent: _, .. } => {
467 let args = GenericArgs::identity_for_item(tcx, def_id);
468 let item_ty = Ty::new_opaque(tcx, ty::IsRigid::No, def_id.to_def_id(), args);
469 let bounds = opaque_type_bounds(tcx, def_id, bounds, item_ty, *span, filter);
470 assert_only_contains_clauses_from(filter, bounds, item_ty);
471 bounds
472 }
473 },
474 hir::Node::Item(hir::Item { kind: hir::ItemKind::TyAlias(..), .. }) => &[],
475 node => ::rustc_span::macros::bug_impl(None,
format_args!("item_bounds called on {0:?} => {1:?}", def_id, node),
Location::caller())bug!("item_bounds called on {def_id:?} => {node:?}"),
476 };
477
478 ty::EarlyBinder::bind_iter(bounds)
479}
480
481pub(super) fn item_bounds(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
482 tcx.explicit_item_bounds(def_id).map_bound(|bounds| {
483 tcx.mk_clauses_from_iter(util::elaborate(tcx, bounds.iter().map(|&(bound, _span)| bound)))
484 })
485}
486
487pub(super) fn item_self_bounds(
488 tcx: TyCtxt<'_>,
489 def_id: DefId,
490) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
491 tcx.explicit_item_self_bounds(def_id).map_bound(|bounds| {
492 tcx.mk_clauses_from_iter(
493 util::elaborate(tcx, bounds.iter().map(|&(bound, _span)| bound)).filter_only_self(),
494 )
495 })
496}
497
498pub(super) fn item_non_self_bounds(
501 tcx: TyCtxt<'_>,
502 def_id: DefId,
503) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
504 let all_bounds: FxIndexSet<_> = tcx.item_bounds(def_id).skip_binder().iter().collect();
505 let own_bounds: FxIndexSet<_> = tcx.item_self_bounds(def_id).skip_binder().iter().collect();
506 if all_bounds.len() == own_bounds.len() {
507 ty::EarlyBinder::bind(tcx, ty::ListWithCachedTypeInfo::empty())
508 } else {
509 ty::EarlyBinder::bind(
510 tcx,
511 tcx.mk_clauses_from_iter(all_bounds.difference(&own_bounds).copied()),
512 )
513 }
514}
515
516pub(super) fn impl_super_outlives(
519 tcx: TyCtxt<'_>,
520 def_id: DefId,
521) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
522 tcx.impl_trait_header(def_id).trait_ref.map_bound(|trait_ref| {
523 let clause: ty::Clause<'_> = trait_ref.upcast(tcx);
524 tcx.mk_clauses_from_iter(util::elaborate(tcx, [clause]).filter(|clause| {
525 #[allow(non_exhaustive_omitted_patterns)] match clause.kind().skip_binder() {
ty::ClauseKind::TypeOutlives(_) | ty::ClauseKind::RegionOutlives(_) =>
true,
_ => false,
}matches!(
526 clause.kind().skip_binder(),
527 ty::ClauseKind::TypeOutlives(_) | ty::ClauseKind::RegionOutlives(_)
528 )
529 }))
530 })
531}
532
533struct AssocTyToOpaque<'tcx> {
534 tcx: TyCtxt<'tcx>,
535 fn_def_id: DefId,
536}
537
538impl<'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTyToOpaque<'tcx> {
539 fn cx(&self) -> TyCtxt<'tcx> {
540 self.tcx
541 }
542
543 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
544 if let &ty::Alias(
545 _,
546 ty::AliasTy { kind: ty::Projection { def_id: projection_ty_def_id }, args, .. },
547 ) = ty.kind()
548 && let Some(ty::ImplTraitInTraitData::Trait { fn_def_id, .. }) =
549 self.tcx.opt_rpitit_info(projection_ty_def_id)
550 && fn_def_id == self.fn_def_id
551 {
552 self.tcx.type_of(projection_ty_def_id).instantiate(self.tcx, args).skip_norm_wip()
553 } else {
554 ty.super_fold_with(self)
555 }
556 }
557}