charon_driver/frontend/types/
traits.rs1use 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
12pub 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#[derive(Debug, Clone, PartialEq, Eq, Hash)]
27pub struct AssocItem<'tcx> {
28 pub def_id: DefId,
29 pub name: Option<Symbol>,
31 pub kind: AssocKind,
32 pub container: AssocItemContainer<'tcx>,
33 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 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 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 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 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 pub fn trait_def_id(&self) -> &DefId {
134 &self.trait_ref().unwrap().def_id
135 }
136
137 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 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 impl_: ItemRef<'tcx>,
167 implemented_trait_ref: TraitRef<'tcx>,
169 implemented_trait_item: ItemRef<'tcx>,
171 overrides_default: bool,
174 },
175 InherentImplContainer {
176 impl_id: DefId,
177 self_ty: Ty<'tcx>,
179 },
180}