rustc_hir_analysis/collect/
item_bounds.rs

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
19/// For associated types we include both bounds written on the type
20/// (`type X: Trait`) and predicates from the trait: `where Self::X: Trait`.
21///
22/// Note that this filtering is done with the items identity args to
23/// simplify checking that these bounds are met in impls. This means that
24/// a bound such as `for<'b> <Self as X<'b>>::U: Clone` can't be used, as in
25/// `hr-associated-type-bound-1.rs`.
26fn 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    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                // Implicit bounds are added to associated types unless a `?Trait` bound is found.
57                icx.lowerer().add_implicit_sizedness_bounds(
58                    &mut bounds,
59                    item_ty,
60                    hir_bounds,
61                    ImpliedBoundsContext::AssociatedTypeOrImplTrait,
62                    span,
63                );
64                icx.lowerer().add_default_traits(
65                    &mut bounds,
66                    item_ty,
67                    hir_bounds,
68                    ImpliedBoundsContext::AssociatedTypeOrImplTrait,
69                    span,
70                );
71
72                // Also collect `where Self::Assoc: Trait` from the parent trait's where clauses.
73                let trait_def_id = tcx.local_parent(assoc_item_def_id);
74                let trait_predicates = tcx.trait_explicit_predicates_and_bounds(trait_def_id);
75
76                let item_trait_ref =
77                    ty::TraitRef::identity(tcx, tcx.parent(assoc_item_def_id.to_def_id()));
78                bounds.extend(trait_predicates.predicates.iter().copied().filter_map(
79                    |(clause, span)| {
80                        remap_gat_vars_and_recurse_into_nested_projections(
81                            tcx,
82                            filter,
83                            item_trait_ref,
84                            assoc_item_def_id,
85                            span,
86                            clause,
87                        )
88                    },
89                ));
90            }
91            // `ConstIfConst` is only interested in `[const]` bounds.
92            PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {
93                // FIXME(const_trait_impl): We *could* uplift the
94                // `where Self::Assoc: [const] Trait` bounds from the parent trait
95                // here too, but we'd need to split `const_conditions` into two
96                // queries (like we do for `trait_explicit_predicates_and_bounds`)
97                // since we need to also filter the predicates *out* of the const
98                // conditions or they lead to cycles in the trait solver when
99                // utilizing these bounds. For now, let's do nothing.
100            }
101        }
102
103        let bounds = tcx.arena.alloc_from_iter(bounds);
104        debug!(
105            "associated_type_bounds({}) = {:?}",
106            tcx.def_path_str(assoc_item_def_id.to_def_id()),
107            bounds
108        );
109
110        assert_only_contains_predicates_from(filter, bounds, item_ty);
111
112        bounds
113    })
114}
115
116/// The code below is quite involved, so let me explain.
117///
118/// We loop here, because we also want to collect vars for nested associated items as
119/// well. For example, given a clause like `Self::A::B`, we want to add that to the
120/// item bounds for `A`, so that we may use that bound in the case that `Self::A::B` is
121/// rigid.
122///
123/// Secondly, regarding bound vars, when we see a where clause that mentions a GAT
124/// like `for<'a, ...> Self::Assoc<'a, ...>: Bound<'b, ...>`, we want to turn that into
125/// an item bound on the GAT, where all of the GAT args are substituted with the GAT's
126/// param regions, and then keep all of the other late-bound vars in the bound around.
127/// We need to "compress" the binder so that it doesn't mention any of those vars that
128/// were mapped to params.
129fn remap_gat_vars_and_recurse_into_nested_projections<'tcx>(
130    tcx: TyCtxt<'tcx>,
131    filter: PredicateFilter,
132    item_trait_ref: ty::TraitRef<'tcx>,
133    assoc_item_def_id: LocalDefId,
134    span: Span,
135    clause: ty::Clause<'tcx>,
136) -> Option<(ty::Clause<'tcx>, Span)> {
137    let mut clause_ty = match clause.kind().skip_binder() {
138        ty::ClauseKind::Trait(tr) => tr.self_ty(),
139        ty::ClauseKind::Projection(proj) => proj.projection_term.self_ty(),
140        ty::ClauseKind::TypeOutlives(outlives) => outlives.0,
141        ty::ClauseKind::HostEffect(host) => host.self_ty(),
142        _ => return None,
143    };
144
145    let gat_vars = loop {
146        if let ty::Alias(ty::Projection, alias_ty) = *clause_ty.kind() {
147            if alias_ty.trait_ref(tcx) == item_trait_ref
148                && alias_ty.def_id == assoc_item_def_id.to_def_id()
149            {
150                // We have found the GAT in question...
151                // Return the vars, since we may need to remap them.
152                break &alias_ty.args[item_trait_ref.args.len()..];
153            } else {
154                // Only collect *self* type bounds if the filter is for self.
155                match filter {
156                    PredicateFilter::All => {}
157                    PredicateFilter::SelfOnly => {
158                        return None;
159                    }
160                    PredicateFilter::SelfTraitThatDefines(_)
161                    | PredicateFilter::SelfConstIfConst
162                    | PredicateFilter::SelfAndAssociatedTypeBounds
163                    | PredicateFilter::ConstIfConst => {
164                        unreachable!(
165                            "invalid predicate filter for \
166                            `remap_gat_vars_and_recurse_into_nested_projections`"
167                        )
168                    }
169                }
170
171                clause_ty = alias_ty.self_ty();
172                continue;
173            }
174        }
175
176        return None;
177    };
178
179    // Special-case: No GAT vars, no mapping needed.
180    if gat_vars.is_empty() {
181        return Some((clause, span));
182    }
183
184    // First, check that all of the GAT args are substituted with a unique late-bound arg.
185    // If we find a duplicate, then it can't be mapped to the definition's params.
186    let mut mapping = FxIndexMap::default();
187    let generics = tcx.generics_of(assoc_item_def_id);
188    for (param, var) in std::iter::zip(&generics.own_params, gat_vars) {
189        let existing = match var.kind() {
190            ty::GenericArgKind::Lifetime(re) => {
191                let ty::RegionKind::ReBound(ty::BoundVarIndexKind::Bound(ty::INNERMOST), bv) =
192                    re.kind()
193                else {
194                    return None;
195                };
196                mapping.insert(bv.var, tcx.mk_param_from_def(param))
197            }
198            ty::GenericArgKind::Type(ty) => {
199                let ty::Bound(ty::BoundVarIndexKind::Bound(ty::INNERMOST), bv) = *ty.kind() else {
200                    return None;
201                };
202                mapping.insert(bv.var, tcx.mk_param_from_def(param))
203            }
204            ty::GenericArgKind::Const(ct) => {
205                let ty::ConstKind::Bound(ty::BoundVarIndexKind::Bound(ty::INNERMOST), bv) =
206                    ct.kind()
207                else {
208                    return None;
209                };
210                mapping.insert(bv.var, tcx.mk_param_from_def(param))
211            }
212        };
213
214        if existing.is_some() {
215            return None;
216        }
217    }
218
219    // Finally, map all of the args in the GAT to the params we expect, and compress
220    // the remaining late-bound vars so that they count up from var 0.
221    let mut folder =
222        MapAndCompressBoundVars { tcx, binder: ty::INNERMOST, still_bound_vars: vec![], mapping };
223    let pred = clause.kind().skip_binder().fold_with(&mut folder);
224
225    Some((
226        ty::Binder::bind_with_vars(pred, tcx.mk_bound_variable_kinds(&folder.still_bound_vars))
227            .upcast(tcx),
228        span,
229    ))
230}
231
232/// Given some where clause like `for<'b, 'c> <Self as Trait<'a_identity>>::Gat<'b>: Bound<'c>`,
233/// the mapping will map `'b` back to the GAT's `'b_identity`. Then we need to compress the
234/// remaining bound var `'c` to index 0.
235///
236/// This folder gives us: `for<'c> <Self as Trait<'a_identity>>::Gat<'b_identity>: Bound<'c>`,
237/// which is sufficient for an item bound for `Gat`, since all of the GAT's args are identity.
238struct MapAndCompressBoundVars<'tcx> {
239    tcx: TyCtxt<'tcx>,
240    /// How deep are we? Makes sure we don't touch the vars of nested binders.
241    binder: ty::DebruijnIndex,
242    /// List of bound vars that remain unsubstituted because they were not
243    /// mentioned in the GAT's args.
244    still_bound_vars: Vec<ty::BoundVariableKind>,
245    /// Subtle invariant: If the `GenericArg` is bound, then it should be
246    /// stored with the debruijn index of `INNERMOST` so it can be shifted
247    /// correctly during substitution.
248    mapping: FxIndexMap<ty::BoundVar, ty::GenericArg<'tcx>>,
249}
250
251impl<'tcx> TypeFolder<TyCtxt<'tcx>> for MapAndCompressBoundVars<'tcx> {
252    fn cx(&self) -> TyCtxt<'tcx> {
253        self.tcx
254    }
255
256    fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
257    where
258        ty::Binder<'tcx, T>: TypeSuperFoldable<TyCtxt<'tcx>>,
259    {
260        self.binder.shift_in(1);
261        let out = t.super_fold_with(self);
262        self.binder.shift_out(1);
263        out
264    }
265
266    fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
267        if !ty.has_bound_vars() {
268            return ty;
269        }
270
271        if let ty::Bound(ty::BoundVarIndexKind::Bound(binder), old_bound) = *ty.kind()
272            && self.binder == binder
273        {
274            let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
275                mapped.expect_ty()
276            } else {
277                // If we didn't find a mapped generic, then make a new one.
278                // Allocate a new var idx, and insert a new bound ty.
279                let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
280                self.still_bound_vars.push(ty::BoundVariableKind::Ty(old_bound.kind));
281                let mapped = Ty::new_bound(
282                    self.tcx,
283                    ty::INNERMOST,
284                    ty::BoundTy { var, kind: old_bound.kind },
285                );
286                self.mapping.insert(old_bound.var, mapped.into());
287                mapped
288            };
289
290            shift_vars(self.tcx, mapped, self.binder.as_u32())
291        } else {
292            ty.super_fold_with(self)
293        }
294    }
295
296    fn fold_region(&mut self, re: ty::Region<'tcx>) -> ty::Region<'tcx> {
297        if let ty::ReBound(ty::BoundVarIndexKind::Bound(binder), old_bound) = re.kind()
298            && self.binder == binder
299        {
300            let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
301                mapped.expect_region()
302            } else {
303                let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
304                self.still_bound_vars.push(ty::BoundVariableKind::Region(old_bound.kind));
305                let mapped = ty::Region::new_bound(
306                    self.tcx,
307                    ty::INNERMOST,
308                    ty::BoundRegion { var, kind: old_bound.kind },
309                );
310                self.mapping.insert(old_bound.var, mapped.into());
311                mapped
312            };
313
314            shift_vars(self.tcx, mapped, self.binder.as_u32())
315        } else {
316            re
317        }
318    }
319
320    fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
321        if !ct.has_bound_vars() {
322            return ct;
323        }
324
325        if let ty::ConstKind::Bound(ty::BoundVarIndexKind::Bound(binder), old_bound) = ct.kind()
326            && self.binder == binder
327        {
328            let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
329                mapped.expect_const()
330            } else {
331                let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
332                self.still_bound_vars.push(ty::BoundVariableKind::Const);
333                let mapped = ty::Const::new_bound(self.tcx, ty::INNERMOST, ty::BoundConst { var });
334                self.mapping.insert(old_bound.var, mapped.into());
335                mapped
336            };
337
338            shift_vars(self.tcx, mapped, self.binder.as_u32())
339        } else {
340            ct.super_fold_with(self)
341        }
342    }
343
344    fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
345        if !p.has_bound_vars() { p } else { p.super_fold_with(self) }
346    }
347}
348
349/// Opaque types don't inherit bounds from their parent: for return position
350/// impl trait it isn't possible to write a suitable predicate on the
351/// containing function and for type-alias impl trait we don't have a backwards
352/// compatibility issue.
353#[instrument(level = "trace", skip(tcx, item_ty))]
354fn opaque_type_bounds<'tcx>(
355    tcx: TyCtxt<'tcx>,
356    opaque_def_id: LocalDefId,
357    hir_bounds: &'tcx [hir::GenericBound<'tcx>],
358    item_ty: Ty<'tcx>,
359    span: Span,
360    filter: PredicateFilter,
361) -> &'tcx [(ty::Clause<'tcx>, Span)] {
362    ty::print::with_reduced_queries!({
363        let icx = ItemCtxt::new(tcx, opaque_def_id);
364        let mut bounds = Vec::new();
365        icx.lowerer().lower_bounds(
366            item_ty,
367            hir_bounds,
368            &mut bounds,
369            ty::List::empty(),
370            filter,
371            OverlappingAsssocItemConstraints::Allowed,
372        );
373        // Implicit bounds are added to opaque types unless a `?Trait` bound is found
374        match filter {
375            PredicateFilter::All
376            | PredicateFilter::SelfOnly
377            | PredicateFilter::SelfTraitThatDefines(_)
378            | PredicateFilter::SelfAndAssociatedTypeBounds => {
379                icx.lowerer().add_implicit_sizedness_bounds(
380                    &mut bounds,
381                    item_ty,
382                    hir_bounds,
383                    ImpliedBoundsContext::AssociatedTypeOrImplTrait,
384                    span,
385                );
386                icx.lowerer().add_default_traits(
387                    &mut bounds,
388                    item_ty,
389                    hir_bounds,
390                    ImpliedBoundsContext::AssociatedTypeOrImplTrait,
391                    span,
392                );
393            }
394            //`ConstIfConst` is only interested in `[const]` bounds.
395            PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
396        }
397        debug!(?bounds);
398
399        tcx.arena.alloc_slice(&bounds)
400    })
401}
402
403pub(super) fn explicit_item_bounds(
404    tcx: TyCtxt<'_>,
405    def_id: LocalDefId,
406) -> ty::EarlyBinder<'_, &'_ [(ty::Clause<'_>, Span)]> {
407    explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::All)
408}
409
410pub(super) fn explicit_item_self_bounds(
411    tcx: TyCtxt<'_>,
412    def_id: LocalDefId,
413) -> ty::EarlyBinder<'_, &'_ [(ty::Clause<'_>, Span)]> {
414    explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::SelfOnly)
415}
416
417pub(super) fn explicit_item_bounds_with_filter(
418    tcx: TyCtxt<'_>,
419    def_id: LocalDefId,
420    filter: PredicateFilter,
421) -> ty::EarlyBinder<'_, &'_ [(ty::Clause<'_>, Span)]> {
422    match tcx.opt_rpitit_info(def_id.to_def_id()) {
423        // RPITIT's bounds are the same as opaque type bounds, but with
424        // a projection self type.
425        Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
426            let opaque_ty = tcx.hir_node_by_def_id(opaque_def_id.expect_local()).expect_opaque_ty();
427            let bounds =
428                associated_type_bounds(tcx, def_id, opaque_ty.bounds, opaque_ty.span, filter);
429            return ty::EarlyBinder::bind(bounds);
430        }
431        Some(ty::ImplTraitInTraitData::Impl { .. }) => {
432            span_bug!(tcx.def_span(def_id), "RPITIT in impl should not have item bounds")
433        }
434        None => {}
435    }
436
437    let bounds = match tcx.hir_node_by_def_id(def_id) {
438        hir::Node::TraitItem(hir::TraitItem {
439            kind: hir::TraitItemKind::Type(bounds, _),
440            span,
441            ..
442        }) => associated_type_bounds(tcx, def_id, bounds, *span, filter),
443        hir::Node::OpaqueTy(hir::OpaqueTy { bounds, origin, span, .. }) => match origin {
444            // Since RPITITs are lowered as projections in `<dyn HirTyLowerer>::lower_ty`,
445            // when we're asking for the item bounds of the *opaques* in a trait's default
446            // method signature, we need to map these projections back to opaques.
447            rustc_hir::OpaqueTyOrigin::FnReturn {
448                parent,
449                in_trait_or_impl: Some(hir::RpitContext::Trait),
450            }
451            | rustc_hir::OpaqueTyOrigin::AsyncFn {
452                parent,
453                in_trait_or_impl: Some(hir::RpitContext::Trait),
454            } => {
455                let args = GenericArgs::identity_for_item(tcx, def_id);
456                let item_ty = Ty::new_opaque(tcx, def_id.to_def_id(), args);
457                let bounds = &*tcx.arena.alloc_slice(
458                    &opaque_type_bounds(tcx, def_id, bounds, item_ty, *span, filter)
459                        .to_vec()
460                        .fold_with(&mut AssocTyToOpaque { tcx, fn_def_id: parent.to_def_id() }),
461                );
462                assert_only_contains_predicates_from(filter, bounds, item_ty);
463                bounds
464            }
465            rustc_hir::OpaqueTyOrigin::FnReturn {
466                parent: _,
467                in_trait_or_impl: None | Some(hir::RpitContext::TraitImpl),
468            }
469            | rustc_hir::OpaqueTyOrigin::AsyncFn {
470                parent: _,
471                in_trait_or_impl: None | Some(hir::RpitContext::TraitImpl),
472            }
473            | rustc_hir::OpaqueTyOrigin::TyAlias { parent: _, .. } => {
474                let args = GenericArgs::identity_for_item(tcx, def_id);
475                let item_ty = Ty::new_opaque(tcx, def_id.to_def_id(), args);
476                let bounds = opaque_type_bounds(tcx, def_id, bounds, item_ty, *span, filter);
477                assert_only_contains_predicates_from(filter, bounds, item_ty);
478                bounds
479            }
480        },
481        hir::Node::Item(hir::Item { kind: hir::ItemKind::TyAlias(..), .. }) => &[],
482        node => bug!("item_bounds called on {def_id:?} => {node:?}"),
483    };
484
485    ty::EarlyBinder::bind(bounds)
486}
487
488pub(super) fn item_bounds(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
489    tcx.explicit_item_bounds(def_id).map_bound(|bounds| {
490        tcx.mk_clauses_from_iter(util::elaborate(tcx, bounds.iter().map(|&(bound, _span)| bound)))
491    })
492}
493
494pub(super) fn item_self_bounds(
495    tcx: TyCtxt<'_>,
496    def_id: DefId,
497) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
498    tcx.explicit_item_self_bounds(def_id).map_bound(|bounds| {
499        tcx.mk_clauses_from_iter(
500            util::elaborate(tcx, bounds.iter().map(|&(bound, _span)| bound)).filter_only_self(),
501        )
502    })
503}
504
505/// This exists as an optimization to compute only the item bounds of the item
506/// that are not `Self` bounds.
507pub(super) fn item_non_self_bounds(
508    tcx: TyCtxt<'_>,
509    def_id: DefId,
510) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
511    let all_bounds: FxIndexSet<_> = tcx.item_bounds(def_id).skip_binder().iter().collect();
512    let own_bounds: FxIndexSet<_> = tcx.item_self_bounds(def_id).skip_binder().iter().collect();
513    if all_bounds.len() == own_bounds.len() {
514        ty::EarlyBinder::bind(ty::ListWithCachedTypeInfo::empty())
515    } else {
516        ty::EarlyBinder::bind(tcx.mk_clauses_from_iter(all_bounds.difference(&own_bounds).copied()))
517    }
518}
519
520/// This exists as an optimization to compute only the supertraits of this impl's
521/// trait that are outlives bounds.
522pub(super) fn impl_super_outlives(
523    tcx: TyCtxt<'_>,
524    def_id: DefId,
525) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
526    tcx.impl_trait_header(def_id).trait_ref.map_bound(|trait_ref| {
527        let clause: ty::Clause<'_> = trait_ref.upcast(tcx);
528        tcx.mk_clauses_from_iter(util::elaborate(tcx, [clause]).filter(|clause| {
529            matches!(
530                clause.kind().skip_binder(),
531                ty::ClauseKind::TypeOutlives(_) | ty::ClauseKind::RegionOutlives(_)
532            )
533        }))
534    })
535}
536
537struct AssocTyToOpaque<'tcx> {
538    tcx: TyCtxt<'tcx>,
539    fn_def_id: DefId,
540}
541
542impl<'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTyToOpaque<'tcx> {
543    fn cx(&self) -> TyCtxt<'tcx> {
544        self.tcx
545    }
546
547    fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
548        if let ty::Alias(ty::Projection, projection_ty) = ty.kind()
549            && let Some(ty::ImplTraitInTraitData::Trait { fn_def_id, .. }) =
550                self.tcx.opt_rpitit_info(projection_ty.def_id)
551            && fn_def_id == self.fn_def_id
552        {
553            self.tcx.type_of(projection_ty.def_id).instantiate(self.tcx, projection_ty.args)
554        } else {
555            ty.super_fold_with(self)
556        }
557    }
558}