Skip to main content

charon_driver/frontend/types/
traits.rs

1use crate::frontend::prelude::*;
2
3use rustc_middle::ty;
4
5sinto_reexport!(
6    ty::AssocKind,
7    ty::AssocConstData,
8    ty::AssocTypeData,
9    ty::ImplTraitInTraitData
10);
11
12/// Reflects [`ty::TraitRef`]
13/// Contains the def_id and arguments passed to the trait. The first type argument is the `Self`
14/// type. The trait proofs are the _required_ predicate for this trait; currently they are always
15/// empty because we consider all trait predicates as implied.
16/// `self.in_trait` is always `None` because a trait can't be associated to another one.
17pub type TraitRef<'tcx> = ItemRef<'tcx>;
18
19impl<'tcx, S: UnderOwnerState<'tcx>> SInto<S, TraitRef<'tcx>> for ty::TraitRef<'tcx> {
20    fn sinto(&self, s: &S) -> TraitRef<'tcx> {
21        translate_item_ref(s, self.def_id, self.args)
22    }
23}
24
25/// Reflects [`ty::AssocItem`]
26#[derive(Debug, Clone, PartialEq, Eq, Hash)]
27pub struct AssocItem<'tcx> {
28    pub def_id: DefId,
29    /// This is `None` for RPTITs.
30    pub name: Option<Symbol>,
31    pub kind: AssocKind,
32    pub container: AssocItemContainer<'tcx>,
33    /// Whether this item has a value (e.g. this is `false` for trait methods without default
34    /// implementations).
35    pub has_value: bool,
36}
37
38impl<'tcx> AssocItem<'tcx> {
39    pub fn sfrom<S: BaseState<'tcx>>(s: &S, item: &ty::AssocItem) -> AssocItem<'tcx> {
40        Self::sfrom_instantiated(s, item, None)
41    }
42
43    /// Translate an `AssocItem` and optionally instantiate it with the provided arguments.
44    pub fn sfrom_instantiated<S: BaseState<'tcx>>(
45        s: &S,
46        item: &ty::AssocItem,
47        item_args: Option<ty::GenericArgs<'tcx>>,
48    ) -> AssocItem<'tcx> {
49        let tcx = s.base().tcx;
50        // We want to solve traits in the context of this item.
51        let item_def_id = item.def_id.sinto(s);
52        let s = &s.with_frontend_owner(&item_def_id);
53        let item_args = item_args.unwrap_or_else(|| item_def_id.identity_args(s));
54        let container_id = item.container_id(tcx);
55        let container_args = item_args.truncate_to(tcx, tcx.generics_of(container_id));
56        let container = match item.container {
57            ty::AssocContainer::Trait => {
58                let trait_ref =
59                    ty::TraitRef::new_from_args(tcx, container_id, container_args).sinto(s);
60                AssocItemContainer::TraitContainer { trait_ref }
61            }
62            ty::AssocContainer::TraitImpl(implemented_item_id) => {
63                let implemented_item_id = implemented_item_id.unwrap();
64                let item = translate_item_ref(s, container_id, container_args);
65                let implemented_trait_ref = tcx
66                    .impl_trait_ref(container_id)
67                    .instantiate(tcx, container_args);
68                let implemented_trait_ref = normalize(tcx, s.typing_env(), implemented_trait_ref);
69                let implemented_trait_item = {
70                    let implemented_item_id = implemented_item_id.sinto(s);
71                    let generics =
72                        item_args.rebase_onto(tcx, container_id, implemented_trait_ref.args);
73                    // Don't resolve, otherwise we'll always get the impl item id back.
74                    ItemRef::translate_from_frontend_def_id_maybe_resolve(
75                        s,
76                        implemented_item_id,
77                        generics,
78                        AssocItemResolution::None,
79                    )
80                };
81                AssocItemContainer::TraitImplContainer {
82                    impl_: item,
83                    implemented_trait_ref: implemented_trait_ref.sinto(s),
84                    implemented_trait_item,
85                    overrides_default: tcx.defaultness(implemented_item_id).has_value(),
86                }
87            }
88            ty::AssocContainer::InherentImpl => AssocItemContainer::InherentImplContainer {
89                impl_id: container_id.sinto(s),
90                self_ty: inst_binder(
91                    tcx,
92                    s.typing_env(),
93                    Some(container_args),
94                    tcx.type_of(container_id),
95                )
96                .sinto(s),
97            },
98        };
99        let name = match item.opt_name() {
100            None if let ty::AssocKind::Type { data } = item.kind
101                && let ty::AssocTypeData::Rpitit(rpitit) = data =>
102            {
103                let (ty::ImplTraitInTraitData::Trait { fn_def_id, .. }
104                | ty::ImplTraitInTraitData::Impl { fn_def_id, .. }) = rpitit;
105                let fn_name = tcx.item_name(fn_def_id);
106                let name = Symbol::intern(&format!("{fn_name}_ty"));
107                Some(name)
108            }
109            opt_name => opt_name,
110        };
111        AssocItem {
112            def_id: item.def_id.sinto(s),
113            name,
114            kind: item.kind.sinto(s),
115            container,
116            has_value: item.defaultness(tcx).has_value(),
117        }
118    }
119
120    /// The trait this item belongs to or implements. `None` for inherent items.
121    pub fn trait_ref(&self) -> Option<&TraitRef<'tcx>> {
122        match &self.container {
123            AssocItemContainer::TraitContainer { trait_ref } => Some(trait_ref),
124            AssocItemContainer::TraitImplContainer {
125                implemented_trait_ref,
126                ..
127            } => Some(implemented_trait_ref),
128            AssocItemContainer::InherentImplContainer { .. } => None,
129        }
130    }
131
132    /// The `DefId` of the trait being implemented.
133    pub fn trait_def_id(&self) -> &DefId {
134        &self.trait_ref().unwrap().def_id
135    }
136
137    /// The `DefId` of the trait or impl containing this item.
138    pub fn container_id(&self) -> &DefId {
139        match &self.container {
140            AssocItemContainer::TraitContainer { trait_ref } => &trait_ref.def_id,
141            AssocItemContainer::TraitImplContainer { impl_, .. } => &impl_.def_id,
142            AssocItemContainer::InherentImplContainer { impl_id, .. } => impl_id,
143        }
144    }
145
146    /// The `DefId` of the item being implemented.
147    pub fn implemented_trait_item_id(&self) -> &DefId {
148        match &self.container {
149            AssocItemContainer::TraitContainer { .. } => &self.def_id,
150            AssocItemContainer::TraitImplContainer {
151                implemented_trait_item,
152                ..
153            } => &implemented_trait_item.def_id,
154            AssocItemContainer::InherentImplContainer { .. } => panic!(),
155        }
156    }
157}
158
159#[derive(Debug, Clone, PartialEq, Eq, Hash)]
160pub enum AssocItemContainer<'tcx> {
161    TraitContainer {
162        trait_ref: TraitRef<'tcx>,
163    },
164    TraitImplContainer {
165        /// Reference to the def_id of the impl block.
166        impl_: ItemRef<'tcx>,
167        /// The trait ref implemented by the impl block.
168        implemented_trait_ref: TraitRef<'tcx>,
169        /// The the associated item (in the trait declaration) that is being implemented.
170        implemented_trait_item: ItemRef<'tcx>,
171        /// Whether the corresponding trait item had a default (and therefore this one overrides
172        /// it).
173        overrides_default: bool,
174    },
175    InherentImplContainer {
176        impl_id: DefId,
177        /// The self type instantiated with this associated item's arguments.
178        self_ty: Ty<'tcx>,
179    },
180}