Skip to main content

charon_lib/ast/
types_utils.rs

1//! This file groups everything which is linked to implementations about [crate::types]
2use crate::ast::*;
3use crate::ids::IndexVec;
4use derive_generic_visitor::*;
5use index_vec::Idx;
6use itertools::Itertools;
7use std::borrow::Cow;
8use std::collections::HashSet;
9use std::convert::Infallible;
10use std::fmt::Debug;
11use std::iter::Iterator;
12use std::mem;
13
14impl TraitParam {
15    /// Constructs the trait ref that refers to this clause.
16    pub fn identity_tref(&self) -> TraitRef {
17        self.identity_tref_at_depth(DeBruijnId::zero())
18    }
19
20    /// Like `identity_tref` but uses variables bound at the given depth.
21    pub fn identity_tref_at_depth(&self, depth: DeBruijnId) -> TraitRef {
22        TraitRef::new(
23            TraitRefKind::Clause(DeBruijnVar::bound(depth, self.clause_id)),
24            self.trait_.clone().move_under_binders(depth),
25        )
26    }
27}
28
29impl GenericParams {
30    pub fn empty() -> Self {
31        Self::default()
32    }
33
34    pub fn is_empty(&self) -> bool {
35        self.len() == 0
36    }
37    /// Whether this has any explicit arguments (types, regions or const generics).
38    pub fn has_explicits(&self) -> bool {
39        !self.regions.is_empty() || !self.types.is_empty() || !self.const_generics.is_empty()
40    }
41    /// Whether this has any implicit arguments (trait clauses, outlives relations, associated type
42    /// equality constraints).
43    pub fn has_predicates(&self) -> bool {
44        !self.trait_clauses.is_empty()
45            || !self.types_outlive.is_empty()
46            || !self.regions_outlive.is_empty()
47            || !self.trait_type_constraints.is_empty()
48    }
49
50    /// Run some sanity checks.
51    pub fn check_consistency(&self) {
52        // Sanity check: check the clause ids are consistent.
53        assert!(
54            self.trait_clauses
55                .iter()
56                .enumerate()
57                .all(|(i, c)| c.clause_id.index() == i)
58        );
59
60        // Sanity check: region names are pairwise distinct (this caused trouble when generating
61        // names for the backward functions in Aeneas): at some point, Rustc introduced names equal
62        // to `Some("'_")` for the anonymous regions, instead of using `None` (we now check in
63        // [translate_region_name] and ignore names equal to "'_").
64        let mut s = HashSet::new();
65        for r in &self.regions {
66            if let Some(name) = &r.name {
67                assert!(
68                    !s.contains(name),
69                    "Name \"{}\" reused for two different lifetimes",
70                    name
71                );
72                s.insert(name);
73            }
74        }
75    }
76
77    pub fn len(&self) -> usize {
78        let GenericParams {
79            regions,
80            types,
81            const_generics,
82            trait_clauses,
83            regions_outlive,
84            types_outlive,
85            trait_type_constraints,
86        } = self;
87        regions.len()
88            + types.len()
89            + const_generics.len()
90            + trait_clauses.len()
91            + regions_outlive.len()
92            + types_outlive.len()
93            + trait_type_constraints.len()
94    }
95
96    /// Construct a set of generic arguments in the scope of `self` that matches `self` and feeds
97    /// each required parameter with itself. E.g. given parameters for `<T, U> where U:
98    /// PartialEq<T>`, the arguments would be `<T, U>[@TraitClause0]`.
99    pub fn identity_args(&self) -> GenericArgs {
100        self.identity_args_at_depth(DeBruijnId::zero())
101    }
102
103    /// Like `identity_args` but uses variables bound at the given depth.
104    pub fn identity_args_at_depth(&self, depth: DeBruijnId) -> GenericArgs {
105        GenericArgs {
106            regions: self
107                .regions
108                .map_ref_indexed(|id, _| Region::Var(DeBruijnVar::bound(depth, id))),
109            types: self
110                .types
111                .map_ref_indexed(|id, _| TyKind::TypeVar(DeBruijnVar::bound(depth, id)).into_ty()),
112            const_generics: self.const_generics.map_ref_indexed(|id, c| ConstantExpr {
113                ty: c.ty.clone(),
114                kind: ConstantExprKind::Var(DeBruijnVar::bound(depth, id)),
115            }),
116            trait_refs: self
117                .trait_clauses
118                .map_ref(|clause| clause.identity_tref_at_depth(depth)),
119        }
120    }
121
122    /// Take the predicates from the another `GenericParams`. This assumes the clause ids etc are
123    /// already consistent.
124    pub fn take_predicates_from(&mut self, other: GenericParams) {
125        assert!(!other.has_explicits());
126        let num_clauses = self.trait_clauses.len();
127        let GenericParams {
128            regions: _,
129            types: _,
130            const_generics: _,
131            trait_clauses,
132            regions_outlive,
133            types_outlive,
134            trait_type_constraints,
135        } = other;
136        self.trait_clauses
137            .extend(trait_clauses.into_iter().update(|clause| {
138                clause.clause_id += num_clauses;
139            }));
140        self.regions_outlive.extend(regions_outlive);
141        self.types_outlive.extend(types_outlive);
142        self.trait_type_constraints.extend(trait_type_constraints);
143    }
144
145    /// Take the predicates from the another `GenericParams`. This assumes that the two
146    /// `GenericParams` are independent, hence will shift clause ids if `other` has any
147    /// trait refs that reference its own clauses.
148    pub fn merge_predicates_from(&mut self, mut other: GenericParams) {
149        // Drop the explicits params.
150        other.types.clear();
151        other.regions.clear();
152        other.const_generics.clear();
153        // The contents of `other` may refer to its own trait clauses, so we must shift clause ids.
154        struct ShiftClausesVisitor(usize);
155        impl VarsVisitor for ShiftClausesVisitor {
156            fn visit_clause_var(&mut self, v: ClauseDbVar) -> Option<TraitRefKind> {
157                if let DeBruijnVar::Bound(DeBruijnId::ZERO, clause_id) = v {
158                    // Replace clause 0 and decrement the others.
159                    Some(TraitRefKind::Clause(DeBruijnVar::Bound(
160                        DeBruijnId::ZERO,
161                        clause_id + self.0,
162                    )))
163                } else {
164                    None
165                }
166            }
167        }
168        let num_clauses = self.trait_clauses.len();
169        other.visit_vars(&mut ShiftClausesVisitor(num_clauses));
170        self.take_predicates_from(other);
171    }
172}
173
174impl<T> Binder<T> {
175    /// Wrap the value in an empty binder, shifting variables appropriately.
176    pub fn empty(kind: BinderKind, x: T) -> Self
177    where
178        T: TyVisitable,
179    {
180        Binder {
181            params: Default::default(),
182            skip_binder: x.move_under_binder(),
183            kind,
184        }
185    }
186    pub fn new(kind: BinderKind, params: GenericParams, skip_binder: T) -> Self {
187        Self {
188            params,
189            skip_binder,
190            kind,
191        }
192    }
193
194    /// Whether this binder binds any variables.
195    pub fn binds_anything(&self) -> bool {
196        !self.params.is_empty()
197    }
198
199    /// Retreive the contents of this binder if the binder binds no variables. This is the invers
200    /// of `Binder::empty`.
201    pub fn get_if_binds_nothing(&self) -> Option<T>
202    where
203        T: TyVisitable + Clone,
204    {
205        self.params
206            .is_empty()
207            .then(|| self.skip_binder.clone().move_from_under_binder().unwrap())
208    }
209
210    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Binder<U> {
211        Binder {
212            params: self.params,
213            skip_binder: f(self.skip_binder),
214            kind: self.kind.clone(),
215        }
216    }
217
218    pub fn map_ref<U>(&self, f: impl FnOnce(&T) -> U) -> Binder<U> {
219        Binder {
220            params: self.params.clone(),
221            skip_binder: f(&self.skip_binder),
222            kind: self.kind.clone(),
223        }
224    }
225
226    /// Substitute the provided arguments for the variables bound in this binder and return the
227    /// substituted inner value.
228    pub fn apply(self, args: &GenericArgs) -> T
229    where
230        T: TyVisitable,
231    {
232        self.skip_binder.substitute(args)
233    }
234}
235
236impl<T: AstVisitable> Binder<Binder<T>> {
237    /// Flatten two levels of binders into a single one.
238    pub fn flatten(self) -> Binder<T> {
239        #[derive(Visitor)]
240        struct FlattenVisitor<'a> {
241            shift_by: &'a GenericParams,
242            binder_depth: DeBruijnId,
243        }
244        impl VisitorWithBinderDepth for FlattenVisitor<'_> {
245            fn binder_depth_mut(&mut self) -> &mut DeBruijnId {
246                &mut self.binder_depth
247            }
248        }
249        impl VisitAstMut for FlattenVisitor<'_> {
250            fn visit<T: AstVisitable>(&mut self, x: &mut T) -> ControlFlow<Self::Break> {
251                VisitWithBinderDepth::new(self).visit(x)
252            }
253
254            fn enter_de_bruijn_id(&mut self, db_id: &mut DeBruijnId) {
255                if *db_id > self.binder_depth {
256                    // We started visiting at the inner binder, so in this branch we're either
257                    // mentioning the outer binder or a binder further beyond. Either way we
258                    // decrease the depth; variables that point to the outer binder don't have to
259                    // be shifted.
260                    *db_id = db_id.decr();
261                }
262            }
263            fn enter_region(&mut self, x: &mut Region) {
264                if let Region::Var(var) = x
265                    && let Some(id) = var.bound_at_depth_mut(self.binder_depth)
266                {
267                    *id += self.shift_by.regions.len();
268                }
269            }
270            fn enter_ty_kind(&mut self, x: &mut TyKind) {
271                if let TyKind::TypeVar(var) = x
272                    && let Some(id) = var.bound_at_depth_mut(self.binder_depth)
273                {
274                    *id += self.shift_by.types.len();
275                }
276            }
277            fn enter_constant_expr(&mut self, x: &mut ConstantExpr) {
278                if let ConstantExprKind::Var(ref mut var) = x.kind
279                    && let Some(id) = var.bound_at_depth_mut(self.binder_depth)
280                {
281                    *id += self.shift_by.const_generics.len();
282                }
283            }
284            fn enter_trait_ref_kind(&mut self, x: &mut TraitRefKind) {
285                if let TraitRefKind::Clause(var) = x
286                    && let Some(id) = var.bound_at_depth_mut(self.binder_depth)
287                {
288                    *id += self.shift_by.trait_clauses.len();
289                }
290            }
291        }
292
293        // We will concatenate both sets of params.
294        let mut outer_params = self.params;
295
296        // The inner value needs to change:
297        // - at binder level 0 we shift all variable ids to match the concatenated params;
298        // - at binder level > 0 we decrease binding level because there's one fewer binder.
299        let mut bound_value = self.skip_binder.skip_binder;
300        let _ = bound_value.drive_mut(&mut FlattenVisitor {
301            shift_by: &outer_params,
302            binder_depth: Default::default(),
303        });
304
305        // The inner params must also be updated, as they can refer to themselves and the outer
306        // one.
307        let mut inner_params = self.skip_binder.params;
308        let _ = inner_params.drive_mut(&mut FlattenVisitor {
309            shift_by: &outer_params,
310            binder_depth: Default::default(),
311        });
312        inner_params
313            .regions
314            .iter_mut()
315            .for_each(|v| v.index += outer_params.regions.len());
316        inner_params
317            .types
318            .iter_mut()
319            .for_each(|v| v.index += outer_params.types.len());
320        inner_params
321            .const_generics
322            .iter_mut()
323            .for_each(|v| v.index += outer_params.const_generics.len());
324        inner_params
325            .trait_clauses
326            .iter_mut()
327            .for_each(|v| v.clause_id += outer_params.trait_clauses.len());
328
329        let GenericParams {
330            regions,
331            types,
332            const_generics,
333            trait_clauses,
334            regions_outlive,
335            types_outlive,
336            trait_type_constraints,
337        } = &inner_params;
338        outer_params.regions.clone_extend_from_other(regions);
339        outer_params.types.clone_extend_from_other(types);
340        outer_params
341            .const_generics
342            .clone_extend_from_other(const_generics);
343        outer_params
344            .trait_clauses
345            .clone_extend_from_other(trait_clauses);
346        outer_params
347            .regions_outlive
348            .extend_from_slice(regions_outlive);
349        outer_params.types_outlive.extend_from_slice(types_outlive);
350        outer_params
351            .trait_type_constraints
352            .clone_extend_from_other(trait_type_constraints);
353
354        Binder {
355            params: outer_params,
356            skip_binder: bound_value,
357            kind: BinderKind::Other,
358        }
359    }
360}
361
362impl<T> RegionBinder<T> {
363    /// Wrap the value in an empty region binder, shifting variables appropriately.
364    pub fn empty(x: T) -> Self
365    where
366        T: TyVisitable,
367    {
368        RegionBinder {
369            regions: Default::default(),
370            skip_binder: x.move_under_binder(),
371        }
372    }
373
374    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> RegionBinder<U> {
375        RegionBinder {
376            regions: self.regions,
377            skip_binder: f(self.skip_binder),
378        }
379    }
380
381    pub fn map_ref<U>(&self, f: impl FnOnce(&T) -> U) -> RegionBinder<U> {
382        RegionBinder {
383            regions: self.regions.clone(),
384            skip_binder: f(&self.skip_binder),
385        }
386    }
387
388    /// Substitute the bound variables with the given lifetimes.
389    pub fn apply(self, regions: IndexVec<RegionId, Region>) -> T
390    where
391        T: TyVisitable,
392    {
393        assert_eq!(regions.len(), self.regions.len());
394        let args = GenericArgs {
395            regions,
396            ..GenericArgs::empty()
397        };
398        self.skip_binder.substitute_inner_binder(&args)
399    }
400
401    /// Substitute the bound variables with erased lifetimes.
402    pub fn erase(self) -> T
403    where
404        T: TyVisitable,
405    {
406        let regions = self.regions.map_ref_indexed(|_, _| Region::Erased);
407        self.apply(regions)
408    }
409}
410
411impl GenericArgs {
412    pub fn len(&self) -> usize {
413        let GenericArgs {
414            regions,
415            types,
416            const_generics,
417            trait_refs,
418        } = self;
419        regions.len() + types.len() + const_generics.len() + trait_refs.len()
420    }
421
422    pub fn is_empty(&self) -> bool {
423        self.len() == 0
424    }
425    /// Whether this has any explicit arguments (types, regions or const generics).
426    pub fn has_explicits(&self) -> bool {
427        !self.regions.is_empty() || !self.types.is_empty() || !self.const_generics.is_empty()
428    }
429    /// Whether this has any implicit arguments (trait refs).
430    pub fn has_implicits(&self) -> bool {
431        !self.trait_refs.is_empty()
432    }
433
434    pub fn empty() -> Self {
435        GenericArgs {
436            regions: Default::default(),
437            types: Default::default(),
438            const_generics: Default::default(),
439            trait_refs: Default::default(),
440        }
441    }
442
443    pub fn new(
444        regions: IndexVec<RegionId, Region>,
445        types: IndexVec<TypeVarId, Ty>,
446        const_generics: IndexVec<ConstGenericVarId, ConstantExpr>,
447        trait_refs: IndexVec<TraitClauseId, TraitRef>,
448    ) -> Self {
449        Self {
450            regions,
451            types,
452            const_generics,
453            trait_refs,
454        }
455    }
456    pub fn new_types(types: IndexVec<TypeVarId, Ty>) -> Self {
457        Self {
458            types,
459            ..Self::empty()
460        }
461    }
462    pub fn new_lifetimes(regions: IndexVec<RegionId, Region>) -> Self {
463        Self {
464            regions,
465            ..Self::empty()
466        }
467    }
468
469    /// Check whether this matches the given `GenericParams`.
470    /// TODO: check more things, e.g. that the trait refs use the correct trait and generics.
471    pub fn matches(&self, params: &GenericParams) -> bool {
472        params.regions.len() == self.regions.len()
473            && params.types.len() == self.types.len()
474            && params.const_generics.len() == self.const_generics.len()
475            && params.trait_clauses.len() == self.trait_refs.len()
476    }
477
478    /// Return the same generics, but where we pop the first type arguments.
479    /// This is useful for trait references (for pretty printing for instance),
480    /// because the first type argument is the type for which the trait is
481    /// implemented.
482    pub fn pop_first_type_arg(&self) -> (Ty, Self) {
483        let mut generics = self.clone();
484        let mut it = mem::take(&mut generics.types).into_iter();
485        let ty = it.next().unwrap();
486        generics.types = it.collect();
487        (ty, generics)
488    }
489
490    /// Concatenate this set of arguments with another one. Use with care, you must manage the
491    /// order of arguments correctly.
492    pub fn concat(mut self, other: &Self) -> Self {
493        let Self {
494            regions,
495            types,
496            const_generics,
497            trait_refs,
498        } = other;
499        self.regions.clone_extend_from_other(regions);
500        self.types.clone_extend_from_other(types);
501        self.const_generics.clone_extend_from_other(const_generics);
502        self.trait_refs.clone_extend_from_other(trait_refs);
503        self
504    }
505}
506
507impl IntTy {
508    /// Important: this returns the target byte count for the types.
509    /// Must not be used for host types from rustc.
510    pub fn target_size(&self, ptr_size: ByteCount) -> usize {
511        match self {
512            IntTy::Isize => ptr_size as usize,
513            IntTy::I8 => size_of::<i8>(),
514            IntTy::I16 => size_of::<i16>(),
515            IntTy::I32 => size_of::<i32>(),
516            IntTy::I64 => size_of::<i64>(),
517            IntTy::I128 => size_of::<i128>(),
518        }
519    }
520}
521impl UIntTy {
522    /// Important: this returns the target byte count for the types.
523    /// Must not be used for host types from rustc.
524    pub fn target_size(&self, ptr_size: ByteCount) -> usize {
525        match self {
526            UIntTy::Usize => ptr_size as usize,
527            UIntTy::U8 => size_of::<u8>(),
528            UIntTy::U16 => size_of::<u16>(),
529            UIntTy::U32 => size_of::<u32>(),
530            UIntTy::U64 => size_of::<u64>(),
531            UIntTy::U128 => size_of::<u128>(),
532        }
533    }
534}
535impl FloatTy {
536    /// Important: this returns the target byte count for the types.
537    /// Must not be used for host types from rustc.
538    pub fn target_size(&self) -> usize {
539        match self {
540            FloatTy::F16 => size_of::<u16>(),
541            FloatTy::F32 => size_of::<u32>(),
542            FloatTy::F64 => size_of::<u64>(),
543            FloatTy::F128 => size_of::<u128>(),
544        }
545    }
546}
547
548impl IntegerTy {
549    pub fn to_unsigned(&self) -> Self {
550        match self {
551            IntegerTy::Signed(IntTy::Isize) => IntegerTy::Unsigned(UIntTy::Usize),
552            IntegerTy::Signed(IntTy::I8) => IntegerTy::Unsigned(UIntTy::U8),
553            IntegerTy::Signed(IntTy::I16) => IntegerTy::Unsigned(UIntTy::U16),
554            IntegerTy::Signed(IntTy::I32) => IntegerTy::Unsigned(UIntTy::U32),
555            IntegerTy::Signed(IntTy::I64) => IntegerTy::Unsigned(UIntTy::U64),
556            IntegerTy::Signed(IntTy::I128) => IntegerTy::Unsigned(UIntTy::U128),
557            _ => *self,
558        }
559    }
560
561    /// Important: this returns the target byte count for the types.
562    /// Must not be used for host types from rustc.
563    pub fn target_size(&self, ptr_size: ByteCount) -> usize {
564        match self {
565            IntegerTy::Signed(ty) => ty.target_size(ptr_size),
566            IntegerTy::Unsigned(ty) => ty.target_size(ptr_size),
567        }
568    }
569}
570
571impl LiteralTy {
572    pub fn to_integer_ty(&self) -> Option<IntegerTy> {
573        match self {
574            Self::Int(int_ty) => Some(IntegerTy::Signed(*int_ty)),
575            Self::UInt(uint_ty) => Some(IntegerTy::Unsigned(*uint_ty)),
576            _ => None,
577        }
578    }
579
580    /// Important: this returns the target byte count for the types.
581    /// Must not be used for host types from rustc.
582    pub fn target_size(&self, ptr_size: ByteCount) -> usize {
583        match self {
584            LiteralTy::Int(int_ty) => int_ty.target_size(ptr_size),
585            LiteralTy::UInt(uint_ty) => uint_ty.target_size(ptr_size),
586            LiteralTy::Float(float_ty) => float_ty.target_size(),
587            LiteralTy::Char => 4,
588            LiteralTy::Bool => 1,
589        }
590    }
591}
592
593impl From<LiteralTy> for Ty {
594    fn from(value: LiteralTy) -> Self {
595        TyKind::Literal(value).into_ty()
596    }
597}
598
599/// A value of type `T` bound by the generic parameters of item
600/// `item`. Used when dealing with multiple items at a time, to
601/// ensure we don't mix up generics.
602///
603/// To get the value, use `under_binder_of` or `subst_for`.
604#[derive(Debug, Clone, Copy)]
605pub struct ItemBinder<ItemId, T> {
606    pub item_id: ItemId,
607    val: T,
608}
609
610impl<ItemId, T> ItemBinder<ItemId, T>
611where
612    ItemId: Debug + Copy + PartialEq,
613{
614    pub fn new(item_id: ItemId, val: T) -> Self {
615        Self { item_id, val }
616    }
617
618    pub fn as_ref(&self) -> ItemBinder<ItemId, &T> {
619        ItemBinder {
620            item_id: self.item_id,
621            val: &self.val,
622        }
623    }
624
625    pub fn map_bound<U>(self, f: impl FnOnce(T) -> U) -> ItemBinder<ItemId, U> {
626        ItemBinder {
627            item_id: self.item_id,
628            val: f(self.val),
629        }
630    }
631
632    fn assert_item_id(&self, item_id: ItemId) {
633        assert_eq!(
634            self.item_id, item_id,
635            "Trying to use item bound for {:?} as if it belonged to {:?}",
636            self.item_id, item_id
637        );
638    }
639
640    /// Assert that the value is bound for item `item_id`, and returns it. This is used when we
641    /// plan to store the returned value inside that item.
642    pub fn under_binder_of(self, item_id: ItemId) -> T {
643        self.assert_item_id(item_id);
644        self.val
645    }
646
647    /// Given generic args for `item_id`, assert that the value is bound for `item_id` and
648    /// substitute it with the provided generic arguments. Because the arguments are bound in the
649    /// context of another item, so it the resulting substituted value.
650    pub fn substitute<OtherItem: Debug + Copy + PartialEq>(
651        self,
652        args: ItemBinder<OtherItem, &GenericArgs>,
653    ) -> ItemBinder<OtherItem, T>
654    where
655        ItemId: Into<ItemId>,
656        T: TyVisitable,
657    {
658        args.map_bound(|args| self.val.substitute(args))
659    }
660}
661
662/// Dummy item identifier that represents the current item when not ambiguous.
663#[derive(Debug, Clone, Copy, PartialEq, Eq)]
664pub struct CurrentItem;
665
666impl<T> ItemBinder<CurrentItem, T> {
667    pub fn under_current_binder(self) -> T {
668        self.val
669    }
670}
671
672impl Ty {
673    pub fn new(kind: TyKind) -> Self {
674        Ty(HashConsed::new(kind))
675    }
676
677    pub fn kind(&self) -> &TyKind {
678        self.0.inner()
679    }
680
681    pub fn with_kind_mut<R>(&mut self, f: impl FnOnce(&mut TyKind) -> R) -> R {
682        self.0.with_inner_mut(f)
683    }
684
685    /// Return the unit type
686    pub fn mk_unit() -> Ty {
687        Self::mk_tuple(vec![])
688    }
689
690    pub fn mk_bool() -> Ty {
691        TyKind::Literal(LiteralTy::Bool).into()
692    }
693
694    pub fn mk_usize() -> Ty {
695        TyKind::Literal(LiteralTy::UInt(UIntTy::Usize)).into()
696    }
697
698    pub fn mk_tuple(tys: Vec<Ty>) -> Ty {
699        TyKind::Adt(TypeDeclRef {
700            id: TypeId::Tuple,
701            generics: Box::new(GenericArgs::new_types(tys.into())),
702        })
703        .into_ty()
704    }
705
706    pub fn mk_array(ty: Ty, len: ConstantExpr) -> Ty {
707        TyKind::Array(ty, Box::new(len)).into_ty()
708    }
709
710    pub fn mk_slice(ty: Ty) -> Ty {
711        TyKind::Slice(ty).into_ty()
712    }
713    /// Return true if it is actually unit (i.e.: 0-tuple)
714    pub fn is_unit(&self) -> bool {
715        match self.as_tuple() {
716            Some(tys) => tys.is_empty(),
717            None => false,
718        }
719    }
720
721    /// Return true if this is a scalar type
722    pub fn is_scalar(&self) -> bool {
723        match self.kind() {
724            TyKind::Literal(kind) => kind.is_int() || kind.is_uint(),
725            _ => false,
726        }
727    }
728
729    pub fn is_unsigned_scalar(&self) -> bool {
730        matches!(self.kind(), TyKind::Literal(LiteralTy::UInt(_)))
731    }
732
733    pub fn is_signed_scalar(&self) -> bool {
734        matches!(self.kind(), TyKind::Literal(LiteralTy::Int(_)))
735    }
736
737    pub fn is_str(&self) -> bool {
738        match self.kind() {
739            TyKind::Adt(ty_ref) if let TypeId::Builtin(BuiltinTy::Str) = ty_ref.id => true,
740            _ => false,
741        }
742    }
743
744    /// Return true if the type is Box
745    pub fn is_box(&self) -> bool {
746        match self.kind() {
747            TyKind::Adt(ty_ref) if let TypeId::Builtin(BuiltinTy::Box) = ty_ref.id => true,
748            _ => false,
749        }
750    }
751
752    pub fn as_box(&self) -> Option<&Ty> {
753        match self.kind() {
754            TyKind::Adt(ty_ref) if let TypeId::Builtin(BuiltinTy::Box) = ty_ref.id => {
755                Some(&ty_ref.generics.types[0])
756            }
757            _ => None,
758        }
759    }
760
761    pub fn get_ptr_metadata(&self, translated: &TranslatedCrate) -> PtrMetadata {
762        let ty_decls = &translated.type_decls;
763        match self.kind() {
764            TyKind::Adt(ty_ref) => {
765                // there are two cases:
766                // 1. if the declared type has a fixed metadata, just returns it
767                // 2. if it depends on some other types or the generic itself
768                match ty_ref.id {
769                    TypeId::Adt(type_decl_id) => {
770                        let Some(decl) = ty_decls.get(type_decl_id) else {
771                            return PtrMetadata::InheritFrom(self.clone());
772                        };
773                        match decl.ptr_metadata.clone().substitute(&ty_ref.generics) {
774                            // if it depends on some type, recursion with the binding env
775                            PtrMetadata::InheritFrom(ty) => ty.get_ptr_metadata(translated),
776                            // otherwise, simply return it
777                            meta => meta,
778                        }
779                    }
780                    // the metadata of a tuple is simply the last field
781                    TypeId::Tuple => {
782                        match ty_ref.generics.types.iter().last() {
783                            // `None` refers to the unit type `()`
784                            None => PtrMetadata::None,
785                            // Otherwise, simply recurse
786                            Some(ty) => ty.get_ptr_metadata(translated),
787                        }
788                    }
789                    // Box is a pointer like ref & raw ptr, hence no metadata
790                    TypeId::Builtin(BuiltinTy::Box) => PtrMetadata::None,
791                    // `str` has metadata length
792                    TypeId::Builtin(BuiltinTy::Str) => PtrMetadata::Length,
793                }
794            }
795            TyKind::DynTrait(pred) => match pred.vtable_ref(translated) {
796                Some(vtable) => PtrMetadata::VTable(vtable),
797                None => PtrMetadata::InheritFrom(self.clone()),
798            },
799            // `[T]` has metadata length
800            TyKind::Slice(..) => PtrMetadata::Length,
801            TyKind::TraitType(..) | TyKind::TypeVar(_) => PtrMetadata::InheritFrom(self.clone()),
802            TyKind::Literal(_)
803            | TyKind::Never
804            | TyKind::Ref(..)
805            | TyKind::RawPtr(..)
806            | TyKind::FnPtr(..)
807            | TyKind::FnDef(..)
808            | TyKind::Array(..)
809            | TyKind::Error(_) => PtrMetadata::None,
810            // The metadata itself must be Sized, hence must with `PtrMetadata::None`
811            TyKind::PtrMetadata(_) => PtrMetadata::None,
812        }
813    }
814
815    pub fn as_ref_or_ptr(&self) -> Option<&Ty> {
816        match self.kind() {
817            TyKind::RawPtr(ty, _) | TyKind::Ref(_, ty, _) => Some(ty),
818            _ => None,
819        }
820    }
821
822    pub fn as_array_or_slice(&self) -> Option<&Ty> {
823        match self.kind() {
824            TyKind::Slice(ty) | TyKind::Array(ty, _) => Some(ty),
825            _ => None,
826        }
827    }
828
829    pub fn as_tuple(&self) -> Option<&IndexVec<TypeVarId, Ty>> {
830        match self.kind() {
831            TyKind::Adt(ty_ref) if let TypeId::Tuple = ty_ref.id => Some(&ty_ref.generics.types),
832            _ => None,
833        }
834    }
835
836    pub fn as_adt(&self) -> Option<&TypeDeclRef> {
837        self.kind().as_adt()
838    }
839}
840
841impl TyKind {
842    pub fn into_ty(self) -> Ty {
843        Ty::new(self)
844    }
845}
846
847impl From<TyKind> for Ty {
848    fn from(kind: TyKind) -> Ty {
849        kind.into_ty()
850    }
851}
852
853/// Convenience for migration purposes.
854impl std::ops::Deref for Ty {
855    type Target = TyKind;
856
857    fn deref(&self) -> &Self::Target {
858        self.kind()
859    }
860}
861/// For deref patterns.
862unsafe impl std::ops::DerefPure for Ty {}
863
864impl TypeDeclRef {
865    pub fn new(id: TypeId, generics: GenericArgs) -> Self {
866        Self {
867            id,
868            generics: Box::new(generics),
869        }
870    }
871}
872
873impl TraitDeclRef {
874    pub fn self_ty<'a>(&'a self, krate: &'a TranslatedCrate) -> Option<&'a Ty> {
875        match self.generics.types.iter().next() {
876            Some(ty) => Some(ty),
877            // TODO(mono): A monomorphized trait takes no arguments.
878            None => {
879                let name = krate.item_name(self.id)?;
880                let args = name.name.last()?.as_monomorphized()?;
881                args.types.iter().next()
882            }
883        }
884    }
885}
886
887impl TraitRef {
888    pub fn new(kind: TraitRefKind, trait_decl_ref: PolyTraitDeclRef) -> Self {
889        TraitRefContents {
890            kind,
891            trait_decl_ref,
892        }
893        .intern()
894    }
895
896    pub fn new_builtin(
897        trait_id: TraitDeclId,
898        ty: Ty,
899        parents: IndexVec<TraitClauseId, TraitRef>,
900        builtin_data: BuiltinImplData,
901    ) -> Self {
902        let trait_decl_ref = RegionBinder::empty(TraitDeclRef {
903            id: trait_id,
904            generics: Box::new(GenericArgs::new_types([ty].into())),
905        });
906        Self::new(
907            TraitRefKind::BuiltinOrAuto {
908                builtin_data,
909                parent_trait_refs: parents,
910                types: Default::default(),
911            },
912            trait_decl_ref,
913        )
914    }
915
916    pub fn trait_id(&self) -> TraitDeclId {
917        self.trait_decl_ref.skip_binder.id
918    }
919
920    /// Get mutable access to the contents. This cloned the value and will re-intern the modified
921    /// value at the end of the function.
922    pub fn with_contents_mut<R>(&mut self, f: impl FnOnce(&mut TraitRefContents) -> R) -> R {
923        self.0.with_inner_mut(f)
924    }
925}
926impl TraitRefContents {
927    pub fn intern(self) -> TraitRef {
928        TraitRef(HashConsed::new(self))
929    }
930}
931
932impl std::ops::Deref for TraitRef {
933    type Target = TraitRefContents;
934    fn deref(&self) -> &Self::Target {
935        &self.0
936    }
937}
938
939impl BuiltinImplData {
940    pub fn as_closure_kind(&self) -> Option<ClosureKind> {
941        match self {
942            BuiltinImplData::FnOnce => Some(ClosureKind::FnOnce),
943            BuiltinImplData::FnMut => Some(ClosureKind::FnMut),
944            BuiltinImplData::Fn => Some(ClosureKind::Fn),
945            _ => None,
946        }
947    }
948}
949
950impl PtrMetadata {
951    pub fn into_type(self) -> Ty {
952        match self {
953            PtrMetadata::None => Ty::mk_unit(),
954            PtrMetadata::Length => Ty::mk_usize(),
955            PtrMetadata::VTable(type_decl_ref) => Ty::new(TyKind::Ref(
956                Region::Static,
957                Ty::new(TyKind::Adt(type_decl_ref)),
958                RefKind::Shared,
959            )),
960            PtrMetadata::InheritFrom(ty) => Ty::new(TyKind::PtrMetadata(ty)),
961        }
962    }
963}
964
965impl Field {
966    /// The new name for this field, as suggested by the `#[charon::rename]` attribute.
967    pub fn renamed_name(&self) -> Option<&str> {
968        self.attr_info.rename.as_deref().or(self.name.as_deref())
969    }
970
971    /// Whether this field has a `#[charon::opaque]` annotation.
972    pub fn is_opaque(&self) -> bool {
973        self.attr_info
974            .attributes
975            .iter()
976            .any(|attr| attr.is_opaque())
977    }
978}
979
980impl Variant {
981    /// The new name for this variant, as suggested by the `#[charon::rename]` and
982    /// `#[charon::variants_prefix]` attributes.
983    pub fn renamed_name(&self) -> &str {
984        self.attr_info
985            .rename
986            .as_deref()
987            .unwrap_or(self.name.as_ref())
988    }
989
990    /// Whether this variant has a `#[charon::opaque]` annotation.
991    pub fn is_opaque(&self) -> bool {
992        self.attr_info
993            .attributes
994            .iter()
995            .any(|attr| attr.is_opaque())
996    }
997}
998
999impl DynPredicate {
1000    /// Get a reference to the vtable type that corresponds to this predicate.
1001    pub fn vtable_ref(&self, translated: &TranslatedCrate) -> Option<TypeDeclRef> {
1002        let dyn_ty = TyKind::DynTrait(self.clone()).into_ty();
1003        // The first clause is the one relevant for the vtable. We're extracting it from our binder
1004        // so must give a value for the `Self` type.
1005        let relevant_tref = self.binder.params.trait_clauses[0]
1006            .trait_
1007            .clone()
1008            .erase()
1009            .substitute(&GenericArgs::new_types([dyn_ty].into_iter().collect()));
1010
1011        // Get the vtable ref from the trait decl
1012        let trait_decl = translated.trait_decls.get(relevant_tref.id)?;
1013        let vtable_ref = trait_decl
1014            .vtable
1015            .clone()?
1016            .substitute_with_self(&relevant_tref.generics, &TraitRefKind::Dyn);
1017        Some(vtable_ref)
1018    }
1019}
1020
1021impl RefKind {
1022    pub fn mutable(x: bool) -> Self {
1023        if x { Self::Mut } else { Self::Shared }
1024    }
1025}
1026
1027/// Visitor for type-level variables. Used to visit the variables contained in a value, as seen
1028/// from the outside of the value. This means that any variable bound inside the value will be
1029/// skipped, and all the seen De Bruijn indices will count from the outside of the value. The
1030/// returned value, if any, will be put in place of the variable.
1031pub trait VarsVisitor {
1032    fn visit_erased_region(&mut self) -> Option<Region> {
1033        None
1034    }
1035    fn visit_region_var(&mut self, _v: RegionDbVar) -> Option<Region> {
1036        None
1037    }
1038    fn visit_type_var(&mut self, _v: TypeDbVar) -> Option<Ty> {
1039        None
1040    }
1041    fn visit_const_generic_var(&mut self, _v: ConstGenericDbVar) -> Option<ConstantExprKind> {
1042        None
1043    }
1044    fn visit_clause_var(&mut self, _v: ClauseDbVar) -> Option<TraitRefKind> {
1045        None
1046    }
1047    fn visit_self_clause(&mut self) -> Option<TraitRefKind> {
1048        None
1049    }
1050}
1051
1052/// Visitor for the [TyVisitable::substitute] function.
1053/// This substitutes variables bound at the level where we start to substitute (level 0).
1054#[derive(Visitor)]
1055pub(crate) struct SubstVisitor<'a> {
1056    generics: &'a GenericArgs,
1057    self_ref: Option<&'a TraitRefKind>,
1058    /// Whether to substitute explicit variables only (types, regions, const generics).
1059    explicits_only: bool,
1060    had_error: bool,
1061}
1062impl<'a> SubstVisitor<'a> {
1063    pub(crate) fn new(
1064        generics: &'a GenericArgs,
1065        self_ref: Option<&'a TraitRefKind>,
1066        explicits_only: bool,
1067    ) -> Self {
1068        Self {
1069            generics,
1070            self_ref,
1071            explicits_only,
1072            had_error: false,
1073        }
1074    }
1075
1076    pub fn visit<T: TyVisitable>(mut self, mut x: T) -> Result<T, GenericsMismatch> {
1077        x.visit_vars(&mut self);
1078        if self.had_error {
1079            Err(GenericsMismatch)
1080        } else {
1081            Ok(x)
1082        }
1083    }
1084
1085    /// Returns the value for this variable, if any.
1086    fn process_var<Id, T>(
1087        &mut self,
1088        var: DeBruijnVar<Id>,
1089        get: impl Fn(Id) -> Option<&'a T>,
1090    ) -> Option<T>
1091    where
1092        Id: Copy,
1093        T: Clone + TyVisitable,
1094        DeBruijnVar<Id>: Into<T>,
1095    {
1096        match var {
1097            DeBruijnVar::Bound(dbid, varid) => {
1098                Some(if let Some(dbid) = dbid.sub(DeBruijnId::one()) {
1099                    // This is bound outside the binder we're substituting for.
1100                    DeBruijnVar::Bound(dbid, varid).into()
1101                } else {
1102                    match get(varid) {
1103                        Some(v) => v.clone(),
1104                        None => {
1105                            self.had_error = true;
1106                            return None;
1107                        }
1108                    }
1109                })
1110            }
1111            DeBruijnVar::Free(..) => None,
1112        }
1113    }
1114}
1115impl VarsVisitor for SubstVisitor<'_> {
1116    fn visit_region_var(&mut self, v: RegionDbVar) -> Option<Region> {
1117        self.process_var(v, |id| self.generics.regions.get(id))
1118    }
1119    fn visit_type_var(&mut self, v: TypeDbVar) -> Option<Ty> {
1120        self.process_var(v, |id| self.generics.types.get(id))
1121    }
1122    fn visit_const_generic_var(&mut self, v: ConstGenericDbVar) -> Option<ConstantExprKind> {
1123        self.process_var(v, |id| {
1124            self.generics.const_generics.get(id).map(|c| &c.kind)
1125        })
1126    }
1127    fn visit_clause_var(&mut self, v: ClauseDbVar) -> Option<TraitRefKind> {
1128        if self.explicits_only {
1129            None
1130        } else {
1131            self.process_var(v, |id| Some(&self.generics.trait_refs.get(id)?.kind))
1132        }
1133    }
1134    fn visit_self_clause(&mut self) -> Option<TraitRefKind> {
1135        Some(self.self_ref.cloned().expect(
1136            "used `substitute` on an item coming from a trait; \
1137            use `substitute_with_self` or `substitute_inner_binder` instead.",
1138        ))
1139    }
1140}
1141
1142#[derive(Debug)]
1143pub struct GenericsMismatch;
1144
1145/// Types that are involved at the type-level and may be substituted around.
1146pub trait TyVisitable: Sized + AstVisitable {
1147    /// Visit the variables contained in `self`, as seen from the outside of `self`. This means
1148    /// that any variable bound inside `self` will be skipped, and all the seen De Bruijn indices
1149    /// will count from the outside of `self`.
1150    fn visit_vars(&mut self, v: &mut impl VarsVisitor) {
1151        #[derive(Visitor)]
1152        struct Wrap<'v, V> {
1153            v: &'v mut V,
1154            depth: DeBruijnId,
1155        }
1156        impl<V> VisitorWithBinderDepth for Wrap<'_, V> {
1157            fn binder_depth_mut(&mut self) -> &mut DeBruijnId {
1158                &mut self.depth
1159            }
1160        }
1161        impl<V: VarsVisitor> VisitAstMut for Wrap<'_, V> {
1162            fn visit<T: AstVisitable>(&mut self, x: &mut T) -> ControlFlow<Self::Break> {
1163                VisitWithBinderDepth::new(self).visit(x)
1164            }
1165
1166            fn exit_region(&mut self, r: &mut Region) {
1167                match r {
1168                    Region::Var(var)
1169                        if let Some(var) = var.move_out_from_depth(self.depth)
1170                            && let Some(new_r) = self.v.visit_region_var(var) =>
1171                    {
1172                        *r = new_r.move_under_binders(self.depth);
1173                    }
1174                    Region::Erased | Region::Body(..)
1175                        if let Some(new_r) = self.v.visit_erased_region() =>
1176                    {
1177                        *r = new_r.move_under_binders(self.depth);
1178                    }
1179                    _ => (),
1180                }
1181            }
1182            fn exit_ty(&mut self, ty: &mut Ty) {
1183                if let TyKind::TypeVar(var) = ty.kind()
1184                    && let Some(var) = var.move_out_from_depth(self.depth)
1185                    && let Some(new_ty) = self.v.visit_type_var(var)
1186                {
1187                    *ty = new_ty.move_under_binders(self.depth);
1188                }
1189            }
1190            fn exit_constant_expr(&mut self, ce: &mut ConstantExpr) {
1191                if let ConstantExprKind::Var(var) = &mut ce.kind
1192                    && let Some(var) = var.move_out_from_depth(self.depth)
1193                    && let Some(new_cg) = self.v.visit_const_generic_var(var)
1194                {
1195                    ce.kind = new_cg.move_under_binders(self.depth);
1196                }
1197            }
1198            fn exit_trait_ref_kind(&mut self, kind: &mut TraitRefKind) {
1199                match kind {
1200                    TraitRefKind::SelfId => {
1201                        if let Some(new_kind) = self.v.visit_self_clause() {
1202                            *kind = new_kind.move_under_binders(self.depth);
1203                        }
1204                    }
1205                    TraitRefKind::Clause(var) => {
1206                        if let Some(var) = var.move_out_from_depth(self.depth)
1207                            && let Some(new_kind) = self.v.visit_clause_var(var)
1208                        {
1209                            *kind = new_kind.move_under_binders(self.depth);
1210                        }
1211                    }
1212                    _ => {}
1213                }
1214            }
1215        }
1216        Wrap {
1217            v,
1218            depth: DeBruijnId::zero(),
1219        }
1220        .visit(self);
1221    }
1222
1223    /// Substitute the generic variables inside `self` by replacing them with the provided values.
1224    /// Note: if `self` is an item that comes from a `TraitDecl`, you must use
1225    /// `substitute_with_self` or `substitute_inner_binder`, otherwise you'll get panics.
1226    fn substitute(self, generics: &GenericArgs) -> Self {
1227        SubstVisitor::new(generics, None, false)
1228            .visit(self)
1229            .unwrap()
1230    }
1231    /// Substitute the generic variables inside `self` by replacing them with the provided values.
1232    /// This is appropriate when substituting an inner binder.
1233    fn substitute_inner_binder(self, generics: &GenericArgs) -> Self {
1234        self.substitute_with_self(generics, &TraitRefKind::SelfId)
1235    }
1236    /// Substitute only the type, region and const generic args.
1237    fn substitute_explicits(self, generics: &GenericArgs) -> Self {
1238        SubstVisitor::new(generics, None, true).visit(self).unwrap()
1239    }
1240    /// Substitute the generic variables as well as the `TraitRefKind::SelfId` trait ref.
1241    fn substitute_with_self(self, generics: &GenericArgs, self_ref: &TraitRefKind) -> Self {
1242        self.try_substitute_with_self(generics, self_ref).unwrap()
1243    }
1244    /// Substitute the generic variables as well as the `TraitRefKind::SelfId` trait ref.
1245    fn substitute_with_tref(self, tref: &TraitRef) -> Self {
1246        let pred = tref.trait_decl_ref.clone().erase();
1247        self.substitute_with_self(&pred.generics, &tref.kind)
1248    }
1249
1250    fn try_substitute(self, generics: &GenericArgs) -> Result<Self, GenericsMismatch> {
1251        SubstVisitor::new(generics, None, false).visit(self)
1252    }
1253    fn try_substitute_with_self(
1254        self,
1255        generics: &GenericArgs,
1256        self_ref: &TraitRefKind,
1257    ) -> Result<Self, GenericsMismatch> {
1258        SubstVisitor::new(generics, Some(self_ref), false).visit(self)
1259    }
1260
1261    /// Move under one binder.
1262    fn move_under_binder(self) -> Self {
1263        self.move_under_binders(DeBruijnId::one())
1264    }
1265
1266    /// Move under `depth` binders.
1267    fn move_under_binders(mut self, depth: DeBruijnId) -> Self {
1268        if !depth.is_zero() {
1269            let Continue(()) = self.visit_db_id::<Infallible>(|id| {
1270                *id = id.plus(depth);
1271                Continue(())
1272            });
1273        }
1274        self
1275    }
1276
1277    /// Move from under one binder.
1278    fn move_from_under_binder(self) -> Option<Self> {
1279        self.move_from_under_binders(DeBruijnId::one())
1280    }
1281
1282    /// Move the value out of `depth` binders. Returns `None` if it contains a variable bound in
1283    /// one of these `depth` binders.
1284    fn move_from_under_binders(mut self, depth: DeBruijnId) -> Option<Self> {
1285        self.visit_db_id::<()>(|id| match id.sub(depth) {
1286            Some(sub) => {
1287                *id = sub;
1288                Continue(())
1289            }
1290            None => Break(()),
1291        })
1292        .is_continue()
1293        .then_some(self)
1294    }
1295
1296    /// Visit the de Bruijn ids contained in `self`, as seen from the outside of `self`. This means
1297    /// that any variable bound inside `self` will be skipped, and all the seen indices will count
1298    /// from the outside of self.
1299    fn visit_db_id<B>(
1300        &mut self,
1301        f: impl FnMut(&mut DeBruijnId) -> ControlFlow<B>,
1302    ) -> ControlFlow<B> {
1303        struct Wrap<F> {
1304            f: F,
1305            depth: DeBruijnId,
1306        }
1307        impl<B, F> Visitor for Wrap<F>
1308        where
1309            F: FnMut(&mut DeBruijnId) -> ControlFlow<B>,
1310        {
1311            type Break = B;
1312        }
1313        impl<B, F> VisitAstMut for Wrap<F>
1314        where
1315            F: FnMut(&mut DeBruijnId) -> ControlFlow<B>,
1316        {
1317            fn enter_region_binder<T: AstVisitable>(&mut self, _: &mut RegionBinder<T>) {
1318                self.depth = self.depth.incr()
1319            }
1320            fn exit_region_binder<T: AstVisitable>(&mut self, _: &mut RegionBinder<T>) {
1321                self.depth = self.depth.decr()
1322            }
1323            fn enter_binder<T: AstVisitable>(&mut self, _: &mut Binder<T>) {
1324                self.depth = self.depth.incr()
1325            }
1326            fn exit_binder<T: AstVisitable>(&mut self, _: &mut Binder<T>) {
1327                self.depth = self.depth.decr()
1328            }
1329
1330            fn visit_de_bruijn_id(&mut self, x: &mut DeBruijnId) -> ControlFlow<Self::Break> {
1331                if let Some(mut shifted) = x.sub(self.depth) {
1332                    (self.f)(&mut shifted)?;
1333                    *x = shifted.plus(self.depth)
1334                }
1335                Continue(())
1336            }
1337        }
1338        self.drive_mut(&mut Wrap {
1339            f,
1340            depth: DeBruijnId::zero(),
1341        })
1342    }
1343
1344    /// Replace all the erased regions by the output of the provided function. Binders levels are
1345    /// handled automatically.
1346    fn replace_erased_regions(mut self, f: impl FnMut() -> Region) -> Self {
1347        #[derive(Visitor)]
1348        struct RefreshErasedRegions<F>(F);
1349        impl<F: FnMut() -> Region> VarsVisitor for RefreshErasedRegions<F> {
1350            fn visit_erased_region(&mut self) -> Option<Region> {
1351                Some((self.0)())
1352            }
1353        }
1354        self.visit_vars(&mut RefreshErasedRegions(f));
1355        self
1356    }
1357}
1358
1359/// A value of type `T` applied to some `GenericArgs`, except we havent applied them yet to avoid a
1360/// deep clone.
1361#[derive(Debug, Clone)]
1362pub struct Substituted<'a, T> {
1363    pub val: &'a T,
1364    pub generics: Cow<'a, GenericArgs>,
1365    pub trait_self: Option<&'a TraitRefKind>,
1366}
1367
1368impl<'a, T> Substituted<'a, T> {
1369    pub fn new(val: &'a T, generics: &'a GenericArgs) -> Self {
1370        Self {
1371            val,
1372            generics: Cow::Borrowed(generics),
1373            trait_self: None,
1374        }
1375    }
1376    pub fn new_for_trait(
1377        val: &'a T,
1378        generics: &'a GenericArgs,
1379        trait_self: &'a TraitRefKind,
1380    ) -> Self {
1381        Self {
1382            val,
1383            generics: Cow::Borrowed(generics),
1384            trait_self: Some(trait_self),
1385        }
1386    }
1387    pub fn new_for_trait_ref(val: &'a T, tref: &'a TraitRef) -> Self {
1388        Self {
1389            val,
1390            generics: Cow::Owned(*tref.trait_decl_ref.clone().erase().generics),
1391            trait_self: Some(&tref.kind),
1392        }
1393    }
1394
1395    pub fn rebind<U>(&self, val: &'a U) -> Substituted<'a, U> {
1396        Substituted {
1397            val,
1398            generics: self.generics.clone(),
1399            trait_self: self.trait_self,
1400        }
1401    }
1402
1403    pub fn substitute(&self) -> T
1404    where
1405        T: TyVisitable + Clone,
1406    {
1407        self.try_substitute().unwrap()
1408    }
1409    pub fn try_substitute(&self) -> Result<T, GenericsMismatch>
1410    where
1411        T: TyVisitable + Clone,
1412    {
1413        match self.trait_self {
1414            None => self.val.clone().try_substitute(&self.generics),
1415            Some(trait_self) => self
1416                .val
1417                .clone()
1418                .try_substitute_with_self(&self.generics, trait_self),
1419        }
1420    }
1421
1422    pub fn iter<Item: 'a>(&self) -> impl Iterator<Item = Substituted<'a, Item>>
1423    where
1424        &'a T: IntoIterator<Item = &'a Item>,
1425    {
1426        self.val.into_iter().map(move |x| self.rebind(x))
1427    }
1428}
1429
1430impl TypeDecl {
1431    pub fn is_c_repr(&self) -> bool {
1432        self.repr
1433            .as_ref()
1434            .is_some_and(|repr| repr.repr_algo == ReprAlgorithm::C)
1435    }
1436
1437    pub fn get_field(&self, variant: Option<VariantId>, field: FieldId) -> Option<&Field> {
1438        let fields = match &self.kind {
1439            TypeDeclKind::Struct(fields) | TypeDeclKind::Union(fields) => fields,
1440            TypeDeclKind::Enum(variants) => &variants[variant.unwrap()].fields,
1441            _ => return None,
1442        };
1443        fields.get(field)
1444    }
1445
1446    pub fn get_field_by_name(
1447        &self,
1448        variant: Option<VariantId>,
1449        field_name: &str,
1450    ) -> Option<(FieldId, &Field)> {
1451        let fields = match &self.kind {
1452            TypeDeclKind::Struct(fields) | TypeDeclKind::Union(fields) => fields,
1453            TypeDeclKind::Enum(variants) => &variants[variant.unwrap()].fields,
1454            _ => return None,
1455        };
1456        fields
1457            .iter_enumerated()
1458            .find(|(_, field)| field.name.as_deref() == Some(field_name))
1459    }
1460}
1461
1462#[derive(Debug, PartialEq, Eq)]
1463pub enum DiscriminantReadError {
1464    /// We read an uninitialized byte.
1465    UninitByte,
1466    /// We reached an invalid discriminant state.
1467    InvalidDiscriminant,
1468}
1469
1470impl Discriminator {
1471    /// Make a trivial discriminator that always returns the given variant id.
1472    pub fn trivial(variant_id: VariantId) -> Self {
1473        Self::Known(variant_id)
1474    }
1475
1476    /// Read a discriminant from memory. The `read` function simulates reading an integer of the
1477    /// given type at the given byte offset from memory and can return `UninitByte` if the byte
1478    /// could not be read.
1479    pub fn read_discriminant(
1480        &self,
1481        read: impl Fn(ByteCount, IntegerTy) -> Result<ScalarValue, DiscriminantReadError> + Copy,
1482    ) -> Result<VariantId, DiscriminantReadError> {
1483        match self {
1484            Discriminator::Known(id) => Ok(*id),
1485            Discriminator::Invalid => Err(DiscriminantReadError::InvalidDiscriminant),
1486            Discriminator::Branch {
1487                offset,
1488                int_ty,
1489                fallback,
1490                children,
1491            } => {
1492                let val = read(*offset, *int_ty)?;
1493                for (range, child) in children {
1494                    if range.contains(&val) {
1495                        return child.read_discriminant(read);
1496                    }
1497                }
1498                fallback.read_discriminant(read)
1499            }
1500        }
1501    }
1502}
1503
1504impl Layout {
1505    pub fn is_variant_uninhabited(&self, variant_id: VariantId) -> bool {
1506        if let Some(v) = self.variant_layouts.get(variant_id) {
1507            v.uninhabited
1508        } else {
1509            false
1510        }
1511    }
1512}
1513
1514impl ReprOptions {
1515    /// Whether this representation options guarantee a fixed
1516    /// field ordering for the type.
1517    ///
1518    /// Since we don't support `repr(simd)` or `repr(linear)` yet, this is
1519    /// the case if it's either `repr(C)` or an explicit discriminant type for
1520    /// an enum with fields (if it doesn't have fields, this obviously doesn't matter anyway).
1521    ///
1522    /// Cf. <https://doc.rust-lang.org/reference/type-layout.html#r-layout.repr.c.struct>
1523    /// and <https://doc.rust-lang.org/reference/type-layout.html#r-layout.repr.primitive.adt>.
1524    pub fn guarantees_fixed_field_order(&self) -> bool {
1525        self.repr_algo == ReprAlgorithm::C || self.explicit_discr_type
1526    }
1527}
1528
1529impl<T: AstVisitable> TyVisitable for T {}
1530
1531impl Eq for TraitParam {}
1532
1533pub trait HasIdxVecOf<Id: Idx>: std::ops::Index<Id, Output: Sized> {
1534    fn get_idx_vec(&self) -> &IndexVec<Id, Self::Output>;
1535    fn get_idx_vec_mut(&mut self) -> &mut IndexVec<Id, Self::Output>;
1536}
1537
1538/// Delegate `Index` implementations to subfields.
1539macro_rules! mk_index_impls {
1540    ($ty:ident.$field:ident[$idx:ty]: $output:ty) => {
1541        impl std::ops::Index<$idx> for $ty {
1542            type Output = $output;
1543            fn index(&self, index: $idx) -> &Self::Output {
1544                &self.$field[index]
1545            }
1546        }
1547        impl std::ops::IndexMut<$idx> for $ty {
1548            fn index_mut(&mut self, index: $idx) -> &mut Self::Output {
1549                &mut self.$field[index]
1550            }
1551        }
1552        impl HasIdxVecOf<$idx> for $ty {
1553            fn get_idx_vec(&self) -> &IndexVec<$idx, Self::Output> {
1554                &self.$field
1555            }
1556            fn get_idx_vec_mut(&mut self) -> &mut IndexVec<$idx, Self::Output> {
1557                &mut self.$field
1558            }
1559        }
1560    };
1561}
1562mk_index_impls!(GenericArgs.regions[RegionId]: Region);
1563mk_index_impls!(GenericArgs.types[TypeVarId]: Ty);
1564mk_index_impls!(GenericArgs.const_generics[ConstGenericVarId]: ConstantExpr);
1565mk_index_impls!(GenericArgs.trait_refs[TraitClauseId]: TraitRef);
1566mk_index_impls!(GenericParams.regions[RegionId]: RegionParam);
1567mk_index_impls!(GenericParams.types[TypeVarId]: TypeParam);
1568mk_index_impls!(GenericParams.const_generics[ConstGenericVarId]: ConstGenericParam);
1569mk_index_impls!(GenericParams.trait_clauses[TraitClauseId]: TraitParam);