rustc_ty_utils/
implied_bounds.rs

1use std::iter;
2
3use rustc_data_structures::fx::FxHashMap;
4use rustc_hir as hir;
5use rustc_hir::def::DefKind;
6use rustc_hir::def_id::LocalDefId;
7use rustc_middle::query::Providers;
8use rustc_middle::ty::{self, Ty, TyCtxt, fold_regions};
9use rustc_middle::{bug, span_bug};
10use rustc_span::Span;
11
12pub(crate) fn provide(providers: &mut Providers) {
13    *providers = Providers {
14        assumed_wf_types,
15        assumed_wf_types_for_rpitit: |tcx, def_id| {
16            assert!(tcx.is_impl_trait_in_trait(def_id.to_def_id()));
17            tcx.assumed_wf_types(def_id)
18        },
19        ..*providers
20    };
21}
22
23fn assumed_wf_types<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx [(Ty<'tcx>, Span)] {
24    let kind = tcx.def_kind(def_id);
25    match kind {
26        DefKind::Fn => {
27            let sig = tcx.fn_sig(def_id).instantiate_identity();
28            let liberated_sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), sig);
29            tcx.arena.alloc_from_iter(itertools::zip_eq(
30                liberated_sig.inputs_and_output,
31                fn_sig_spans(tcx, def_id),
32            ))
33        }
34        DefKind::AssocFn => {
35            let sig = tcx.fn_sig(def_id).instantiate_identity();
36            let liberated_sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), sig);
37            let mut assumed_wf_types: Vec<_> =
38                tcx.assumed_wf_types(tcx.local_parent(def_id)).into();
39            assumed_wf_types.extend(itertools::zip_eq(
40                liberated_sig.inputs_and_output,
41                fn_sig_spans(tcx, def_id),
42            ));
43            tcx.arena.alloc_slice(&assumed_wf_types)
44        }
45        DefKind::Impl { of_trait } => {
46            // Trait arguments and the self type for trait impls or only the self type for
47            // inherent impls.
48            let tys = if of_trait {
49                let trait_ref = tcx.impl_trait_ref(def_id);
50                trait_ref.skip_binder().args.types().collect()
51            } else {
52                vec![tcx.type_of(def_id).instantiate_identity()]
53            };
54
55            let mut impl_spans = impl_spans(tcx, def_id);
56            tcx.arena.alloc_from_iter(tys.into_iter().map(|ty| (ty, impl_spans.next().unwrap())))
57        }
58        DefKind::AssocTy if let Some(data) = tcx.opt_rpitit_info(def_id.to_def_id()) => {
59            match data {
60                ty::ImplTraitInTraitData::Trait { fn_def_id, .. } => {
61                    // We need to remap all of the late-bound lifetimes in the assumed wf types
62                    // of the fn (which are represented as ReLateParam) to the early-bound lifetimes
63                    // of the RPITIT (which are represented by ReEarlyParam owned by the opaque).
64                    // Luckily, this is very easy to do because we already have that mapping
65                    // stored in the HIR of this RPITIT.
66                    //
67                    // Side-note: We don't really need to do this remapping for early-bound
68                    // lifetimes because they're already "linked" by the bidirectional outlives
69                    // predicates we insert in the `explicit_predicates_of` query for RPITITs.
70                    let mut mapping = FxHashMap::default();
71                    let generics = tcx.generics_of(def_id);
72
73                    // For each captured opaque lifetime, if it's late-bound (`ReLateParam` in this
74                    // case, since it has been liberated), map it back to the early-bound lifetime of
75                    // the GAT. Since RPITITs also have all of the fn's generics, we slice only
76                    // the end of the list corresponding to the opaque's generics.
77                    for param in &generics.own_params[tcx.generics_of(fn_def_id).own_params.len()..]
78                    {
79                        let orig_lt =
80                            tcx.map_opaque_lifetime_to_parent_lifetime(param.def_id.expect_local());
81                        if matches!(orig_lt.kind(), ty::ReLateParam(..)) {
82                            mapping.insert(
83                                orig_lt,
84                                ty::Region::new_early_param(
85                                    tcx,
86                                    ty::EarlyParamRegion { index: param.index, name: param.name },
87                                ),
88                            );
89                        }
90                    }
91                    // FIXME: This could use a real folder, I guess.
92                    let remapped_wf_tys = fold_regions(
93                        tcx,
94                        tcx.assumed_wf_types(fn_def_id.expect_local()).to_vec(),
95                        |region, _| {
96                            // If `region` is a `ReLateParam` that is captured by the
97                            // opaque, remap it to its corresponding the early-
98                            // bound region.
99                            if let Some(remapped_region) = mapping.get(&region) {
100                                *remapped_region
101                            } else {
102                                region
103                            }
104                        },
105                    );
106                    tcx.arena.alloc_from_iter(remapped_wf_tys)
107                }
108                // Assumed wf types for RPITITs in an impl just inherit (and instantiate)
109                // the assumed wf types of the trait's RPITIT GAT.
110                ty::ImplTraitInTraitData::Impl { .. } => {
111                    let impl_def_id = tcx.local_parent(def_id);
112                    let rpitit_def_id = tcx.trait_item_of(def_id).unwrap();
113                    let args = ty::GenericArgs::identity_for_item(tcx, def_id).rebase_onto(
114                        tcx,
115                        impl_def_id.to_def_id(),
116                        tcx.impl_trait_ref(impl_def_id).instantiate_identity().args,
117                    );
118                    tcx.arena.alloc_from_iter(
119                        ty::EarlyBinder::bind(tcx.assumed_wf_types_for_rpitit(rpitit_def_id))
120                            .iter_instantiated_copied(tcx, args)
121                            .chain(tcx.assumed_wf_types(impl_def_id).into_iter().copied()),
122                    )
123                }
124            }
125        }
126        DefKind::AssocConst | DefKind::AssocTy => tcx.assumed_wf_types(tcx.local_parent(def_id)),
127        DefKind::Static { .. }
128        | DefKind::Const
129        | DefKind::AnonConst
130        | DefKind::InlineConst
131        | DefKind::Struct
132        | DefKind::Union
133        | DefKind::Enum
134        | DefKind::Trait
135        | DefKind::TraitAlias
136        | DefKind::TyAlias => ty::List::empty(),
137        DefKind::OpaqueTy
138        | DefKind::Mod
139        | DefKind::Variant
140        | DefKind::ForeignTy
141        | DefKind::TyParam
142        | DefKind::ConstParam
143        | DefKind::Ctor(_, _)
144        | DefKind::Macro(_)
145        | DefKind::ExternCrate
146        | DefKind::Use
147        | DefKind::ForeignMod
148        | DefKind::Field
149        | DefKind::LifetimeParam
150        | DefKind::GlobalAsm
151        | DefKind::Closure
152        | DefKind::SyntheticCoroutineBody => {
153            span_bug!(
154                tcx.def_span(def_id),
155                "`assumed_wf_types` not defined for {} `{def_id:?}`",
156                kind.descr(def_id.to_def_id())
157            );
158        }
159    }
160}
161
162fn fn_sig_spans(tcx: TyCtxt<'_>, def_id: LocalDefId) -> impl Iterator<Item = Span> {
163    let node = tcx.hir_node_by_def_id(def_id);
164    if let Some(decl) = node.fn_decl() {
165        decl.inputs.iter().map(|ty| ty.span).chain(iter::once(decl.output.span()))
166    } else {
167        bug!("unexpected item for fn {def_id:?}: {node:?}")
168    }
169}
170
171fn impl_spans(tcx: TyCtxt<'_>, def_id: LocalDefId) -> impl Iterator<Item = Span> {
172    let item = tcx.hir_expect_item(def_id);
173    if let hir::ItemKind::Impl(impl_) = item.kind {
174        let trait_args = impl_
175            .of_trait
176            .into_iter()
177            .flat_map(|of_trait| of_trait.trait_ref.path.segments.last().unwrap().args().args)
178            .map(|arg| arg.span());
179        let dummy_spans_for_default_args = impl_
180            .of_trait
181            .into_iter()
182            .flat_map(|of_trait| iter::repeat(of_trait.trait_ref.path.span));
183        iter::once(impl_.self_ty.span).chain(trait_args).chain(dummy_spans_for_default_args)
184    } else {
185        bug!("unexpected item for impl {def_id:?}: {item:?}")
186    }
187}