1use crate::frontend::prelude::*;
2
3use enum_as_inner::EnumAsInner;
4use rustc_middle::ty;
5
6sinto_reexport!(
7 ty::ParamTy,
8 ty::ParamConst,
9 ty::BoundVar,
10 ty::EarlyParamRegion,
11 ty::BoundVarIndexKind,
12 ty::DebruijnIndex,
13 ty::InferTy,
14 ty::Variance,
15 ty::Region<'tcx>,
16 ty::PlaceholderRegion<'tcx>,
17);
18
19#[derive(Debug, Clone, PartialEq, Eq, Hash)]
21#[derive(AdtInto)]
22#[args(<'tcx, S: UnderOwnerState<'tcx>>, from: ty::BoundTyKind<'tcx>, state: S as s)]
23pub enum BoundTyKind {
24 Anon,
25 #[custom_arm(&FROM_TYPE::Param(def_id) => TO_TYPE::Param(def_id.sinto(s), s.base().tcx.item_name(def_id).sinto(s)),)]
26 Param(DefId, Symbol),
27}
28
29#[derive(Debug, Clone, PartialEq, Eq, Hash)]
31#[derive(AdtInto)]
32#[args(<'tcx, S: UnderOwnerState<'tcx>>, from: ty::BoundTy<'tcx>, state: S as s)]
33pub struct BoundTy {
34 pub var: BoundVar,
35 pub kind: BoundTyKind,
36}
37
38#[derive(Debug, Clone, PartialEq, Eq, Hash)]
39pub struct BoundTyRef {
40 pub index: BoundVarIndexKind,
41 pub ty: BoundTy,
42}
43
44#[derive(Debug, Clone, PartialEq, Eq, Hash)]
46#[derive(AdtInto)]
47#[args(<'tcx, S: UnderOwnerState<'tcx>>, from: ty::BoundRegionKind<'tcx>, state: S as s)]
48pub enum BoundRegionKind {
49 Anon,
50 NamedForPrinting(Symbol),
51 #[custom_arm(&FROM_TYPE::Named(def_id) => TO_TYPE::Named(def_id.sinto(s), s.base().tcx.item_name(def_id).sinto(s)),)]
52 Named(DefId, Symbol),
53 ClosureEnv,
54}
55
56pub type PlaceholderConst = Placeholder<BoundVar>;
58pub type PlaceholderTy = Placeholder<BoundTy>;
60
61#[derive(Debug, Clone, PartialEq, Eq, Hash)]
63pub struct Placeholder<T> {
64 pub bound: T,
65}
66
67impl<'tcx, S: UnderOwnerState<'tcx>, T: SInto<S, U>, U> SInto<S, Placeholder<U>>
68 for ty::Placeholder<ty::TyCtxt<'tcx>, T>
69{
70 fn sinto(&self, s: &S) -> Placeholder<U> {
71 Placeholder {
72 bound: self.bound.sinto(s),
73 }
74 }
75}
76
77#[derive(Debug, Clone)]
79pub struct Canonical<T> {
80 pub value: T,
81}
82pub type CanonicalUserType = Canonical<UserType>;
84
85impl<'tcx, S: UnderOwnerState<'tcx>, T: SInto<S, U>, U> SInto<S, Canonical<U>>
86 for rustc_middle::infer::canonical::Canonical<'tcx, T>
87{
88 fn sinto(&self, s: &S) -> Canonical<U> {
89 Canonical {
90 value: self.value.sinto(s),
91 }
92 }
93}
94
95#[derive(Debug, Clone, PartialEq, Eq, Hash)]
97#[derive(EnumAsInner)]
98#[derive(AdtInto)]
99#[args(< S: UnderOwnerState<'tcx>>, from: ty::GenericArgKind<'tcx>, state: S as s)]
100pub enum GenericArg<'tcx> {
101 Lifetime(Region<'tcx>),
102 Type(Ty<'tcx>),
103 Const(ConstantExpr<'tcx>),
104}
105
106impl<'tcx, S: UnderOwnerState<'tcx>> SInto<S, GenericArg<'tcx>> for ty::GenericArg<'tcx> {
107 fn sinto(&self, s: &S) -> GenericArg<'tcx> {
108 self.kind().sinto(s)
109 }
110}
111
112impl<'tcx, S: UnderOwnerState<'tcx>> SInto<S, Vec<GenericArg<'tcx>>> for ty::GenericArgs<'tcx> {
113 fn sinto(&self, s: &S) -> Vec<GenericArg<'tcx>> {
114 self.iter().map(|v| v.kind().sinto(s)).collect()
115 }
116}
117
118#[derive(AdtInto)]
120#[args(< S: UnderOwnerState<'tcx>>, from: ty::GenericParamDef, state: S as s)]
121#[derive(Debug, Clone)]
122pub struct GenericParamDef<'tcx> {
123 pub name: Symbol,
124 pub def_id: DefId,
125 pub index: u32,
126 #[value(
127 match self.kind {
128 ty::GenericParamDefKind::Lifetime => GenericParamDefKind::Lifetime,
129 ty::GenericParamDefKind::Type { has_default, synthetic } => GenericParamDefKind::Type {
130 has_default,
131 synthetic,
132 inference_value: None,
133 },
134 ty::GenericParamDefKind::Const { has_default, .. } => {
135 let tcx = s.base().tcx;
136 let ty = if let Some(item) = s.owner().as_synthetic(s)
137 && let Some(ty) = item.const_param_ty(s, self.index)
138 {
139 ty::Unnormalized::new(ty)
142 } else {
143 tcx.type_of(self.def_id).instantiate_identity()
144 };
145 let ty = normalize(tcx, s.typing_env(), ty).sinto(s);
146 GenericParamDefKind::Const { has_default, ty }
147 },
148 }
149 )]
150 pub kind: GenericParamDefKind<'tcx>,
151 #[value({
153 use SyntheticItem::*;
154 use rustc_hir::def::DefKind::*;
155 let tcx = s.base().tcx;
156 match s.owner().as_synthetic(s) {
157 Some(Array | Slice | Tuple(_) | Str) => Some(Variance::Covariant),
158 Some(FnPtr(_)) => None,
160 None => {
161 let parent = tcx.parent(self.def_id);
162 match tcx.def_kind(parent) {
163 Fn | AssocFn | Enum | Struct | Union | Ctor(..) | OpaqueTy => {
164 tcx.variances_of(parent).get(self.index as usize).sinto(s)
165 }
166 _ => None,
167 }
168 }
169 }
170 })]
171 pub variance: Option<Variance>,
172}
173
174#[derive(Debug, Clone)]
176pub enum GenericParamDefKind<'tcx> {
177 Lifetime,
178 Type {
179 has_default: bool,
180 synthetic: bool,
181 inference_value: Option<Ty<'tcx>>,
184 },
185 Const {
186 has_default: bool,
187 ty: Ty<'tcx>,
188 },
189}
190
191#[derive(AdtInto)]
193#[args(< S: UnderOwnerState<'tcx>>, from: rustc_trait_elaboration::GenericParams<'tcx, DefId>, state: S as state)]
194#[derive(Debug, Clone)]
195pub struct TyGenerics<'tcx> {
196 #[value(self.parent.clone())]
197 pub parent: Option<DefId>,
198 pub parent_count: usize,
199 #[from(own_params)]
200 pub params: Vec<GenericParamDef<'tcx>>,
201}
202
203pub type UnitBinder<'tcx> = ty::Binder<'tcx, ()>;
204
205#[derive(AdtInto)]
207#[args(<'tcx, S: UnderOwnerState<'tcx>>, from: ty::BoundVariableKind<'tcx>, state: S as tcx)]
208#[derive(Debug, Clone, PartialEq, Eq, Hash)]
209pub enum BoundVariableKind {
210 Ty(BoundTyKind),
211 #[custom_arm(
212 &FROM_TYPE::Region(region) => TO_TYPE::Region(region.sinto(tcx), None),
213 )]
214 Region(BoundRegionKind, Option<Variance>),
215 Const,
216}
217
218#[derive(Debug, Clone, PartialEq, Eq, Hash)]
220pub struct Binder<T> {
221 pub value: T,
222 pub bound_vars: Vec<BoundVariableKind>,
223}
224
225#[derive(Debug, Clone, PartialEq, Eq, Hash)]
230pub struct LateBinder<T> {
231 pub def_id: DefId,
232 pub binder: Binder<T>,
233}
234
235impl Binder<()> {
236 pub fn empty() -> Self {
237 Binder {
238 value: (),
239 bound_vars: vec![],
240 }
241 }
242}
243
244impl<T> Binder<T> {
245 pub fn as_ref(&self) -> Binder<&T> {
246 Binder {
247 value: &self.value,
248 bound_vars: self.bound_vars.clone(),
249 }
250 }
251
252 pub fn frontend_skip_binder(self) -> T {
253 self.value
254 }
255
256 pub fn frontend_skip_binder_ref(&self) -> &T {
257 &self.value
258 }
259
260 pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Binder<U> {
261 Binder {
262 value: f(self.value),
263 bound_vars: self.bound_vars,
264 }
265 }
266
267 pub fn inner_mut(&mut self) -> &mut T {
268 &mut self.value
269 }
270
271 pub fn rebind<U>(&self, value: U) -> Binder<U> {
272 self.as_ref().map(|_| value)
273 }
274}
275
276#[derive(Debug, Clone, PartialEq, Eq, Hash)]
280pub struct DynBinder<'tcx, T> {
281 pub existential_ty: ParamTy,
283 pub predicates: GenericPredicates<'tcx>,
286 pub val: T,
288}
289
290impl<'tcx, S, T, U> SInto<S, DynBinder<'tcx, U>>
291 for rustc_trait_elaboration::DynBinder<'tcx, T, DefId>
292where
293 S: UnderOwnerState<'tcx>,
294 T: SInto<S, U>,
295{
296 fn sinto(&self, s: &S) -> DynBinder<'tcx, U> {
297 DynBinder {
298 existential_ty: self.existential_ty.sinto(s),
299 predicates: GenericPredicates {
300 predicates: self.predicates.sinto(s),
301 },
302 val: self.val.sinto(s),
303 }
304 }
305}
306
307trait BinderVariances<'tcx>: Sized {
309 fn variances(
310 _tcx: ty::TyCtxt<'tcx>,
311 _binder: &ty::Binder<'tcx, Self>,
312 ) -> HashMap<ty::BoundVar, ty::Variance> {
313 HashMap::default()
314 }
315}
316
317impl<'tcx> BinderVariances<'tcx> for ty::FnSig<'tcx> {
318 fn variances(
319 tcx: ty::TyCtxt<'tcx>,
320 binder: &ty::Binder<'tcx, Self>,
321 ) -> HashMap<ty::BoundVar, ty::Variance> {
322 fn_sig_bound_region_variances(tcx, *binder)
323 }
324}
325
326impl<'tcx> BinderVariances<'tcx> for ty::Ty<'tcx> {}
327impl<'tcx> BinderVariances<'tcx> for ty::ClauseKind<'tcx> {}
328impl<'tcx> BinderVariances<'tcx> for ty::PredicateKind<'tcx> {}
329impl<'tcx> BinderVariances<'tcx> for ty::TraitRef<'tcx> {}
330impl<'tcx> BinderVariances<'tcx> for ty::TraitClause<'tcx> {}
331impl<'tcx> BinderVariances<'tcx> for UserAnnotationData<'tcx> {}
332
333impl<'tcx, S: UnderOwnerState<'tcx>, T1, T2> SInto<S, Binder<T2>> for ty::Binder<'tcx, T1>
334where
335 T1: SInto<(StateWithOwner<'tcx>, UnitBinder<'tcx>), T2> + BinderVariances<'tcx>,
336{
337 fn sinto(&self, s: &S) -> Binder<T2> {
338 let variances = T1::variances(s.base().tcx, self);
339 let mut bound_vars = self.bound_vars().sinto(s);
340 for (index, var) in bound_vars.iter_mut().enumerate() {
341 if let BoundVariableKind::Region(_, variance) = var {
342 *variance = variances
343 .get(&ty::BoundVar::from_usize(index))
344 .copied()
345 .sinto(s);
346 }
347 }
348 let value = {
349 let under_binder_s = (s.as_state_with_owner(), self.as_ref().map_bound(|_| ()));
353 self.as_ref().skip_binder().sinto(&under_binder_s)
354 };
355 Binder { value, bound_vars }
356 }
357}