flux_rustc_bridge/ty/
subst.rs1use flux_arc_interner::{Internable, List};
2
3use super::{
4 Binder, Const, ConstKind, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef,
5 FnSig, GenericArg, Region, Ty, TyKind,
6};
7use crate::ty::TraitRef;
8
9pub(super) trait Subst {
10 fn subst(&self, args: &[GenericArg]) -> Self;
11}
12
13impl<T> Subst for Binder<T>
14where
15 T: Subst,
16{
17 fn subst(&self, args: &[GenericArg]) -> Self {
18 Binder(self.0.subst(args), self.1.clone())
19 }
20}
21
22impl Subst for FnSig {
23 fn subst(&self, args: &[GenericArg]) -> Self {
24 FnSig {
25 safety: self.safety,
26 abi: self.abi,
27 inputs_and_output: self.inputs_and_output.subst(args),
28 }
29 }
30}
31
32impl Subst for Ty {
33 fn subst(&self, args: &[GenericArg]) -> Ty {
34 match self.kind() {
35 TyKind::Adt(adt_def, args2) => Ty::mk_adt(adt_def.clone(), args2.subst(args)),
36 TyKind::FnDef(def_id, args2) => Ty::mk_fn_def(*def_id, args2.subst(args)),
37 TyKind::Array(ty, len) => Ty::mk_array(ty.subst(args), len.clone()),
38 TyKind::Ref(re, ty, mutbl) => Ty::mk_ref(*re, ty.subst(args), *mutbl),
39 TyKind::Tuple(tys) => Ty::mk_tuple(tys.subst(args)),
40 TyKind::Slice(ty) => Ty::mk_slice(ty.subst(args)),
41 TyKind::Closure(def_id, args2) => Ty::mk_closure(*def_id, args2.subst(args)),
42 TyKind::Coroutine(def_id, args2) => Ty::mk_coroutine(*def_id, args2.subst(args)),
43 TyKind::CoroutineWitness(def_id, args2) => {
44 Ty::mk_generator_witness(*def_id, args2.subst(args))
45 }
46 TyKind::Alias(alias_ty) => Ty::mk_alias(alias_ty.kind, alias_ty.args.subst(args)),
47 TyKind::RawPtr(ty, mutbl) => Ty::mk_raw_ptr(ty.subst(args), *mutbl),
48 TyKind::Param(param_ty) => args[param_ty.index as usize].expect_type().clone(),
49 TyKind::FnPtr(fn_sig) => Ty::mk_fn_ptr(fn_sig.subst(args)),
50 TyKind::Dynamic(exi_preds, re) => Ty::mk_dynamic(exi_preds.subst(args), *re),
51 TyKind::Foreign(def_id) => Ty::mk_foreign(*def_id),
52 TyKind::Pat => todo!(),
53 TyKind::Bool
54 | TyKind::Uint(_)
55 | TyKind::Str
56 | TyKind::Char
57 | TyKind::Float(_)
58 | TyKind::Int(_)
59 | TyKind::Never => self.clone(),
60 }
61 }
62}
63
64impl Subst for TraitRef {
65 fn subst(&self, args: &[GenericArg]) -> Self {
66 let def_id = self.def_id;
67 TraitRef { def_id, args: self.args.subst(args) }
68 }
69}
70
71impl Subst for ExistentialTraitRef {
72 fn subst(&self, args: &[GenericArg]) -> Self {
73 ExistentialTraitRef { def_id: self.def_id, args: self.args.subst(args) }
74 }
75}
76
77impl Subst for ExistentialProjection {
78 fn subst(&self, args: &[GenericArg]) -> Self {
79 ExistentialProjection {
80 def_id: self.def_id,
81 args: self.args.subst(args),
82 term: self.term.subst(args),
83 }
84 }
85}
86
87impl Subst for ExistentialPredicate {
88 fn subst(&self, args: &[GenericArg]) -> Self {
89 match self {
90 ExistentialPredicate::Trait(exi_trait_ref) => {
91 ExistentialPredicate::Trait(exi_trait_ref.subst(args))
92 }
93 ExistentialPredicate::Projection(exi_proj_pred) => {
94 ExistentialPredicate::Projection(exi_proj_pred.subst(args))
95 }
96 ExistentialPredicate::AutoTrait(def_id) => ExistentialPredicate::AutoTrait(*def_id),
97 }
98 }
99}
100
101impl Subst for GenericArg {
102 fn subst(&self, args: &[GenericArg]) -> Self {
103 match self {
104 GenericArg::Ty(ty) => GenericArg::Ty(ty.subst(args)),
105 GenericArg::Lifetime(re) => GenericArg::Lifetime(re.subst(args)),
106 GenericArg::Const(c) => GenericArg::Const(c.subst(args)),
107 }
108 }
109}
110
111impl Subst for Const {
112 fn subst(&self, args: &[GenericArg]) -> Self {
113 if let ConstKind::Param(param_const) = &self.kind {
114 args[param_const.index as usize].expect_const().clone()
115 } else {
116 self.clone()
117 }
118 }
119}
120
121impl Subst for Region {
122 fn subst(&self, args: &[GenericArg]) -> Self {
123 match self {
124 Region::ReEarlyParam(ebr) => args[ebr.index as usize].expect_lifetime(),
125 Region::ReLateParam(..)
126 | Region::ReBound(_, _)
127 | Region::ReStatic
128 | Region::ReErased
129 | Region::ReVar(_) => *self,
130 }
131 }
132}
133
134impl<T> Subst for List<T>
135where
136 T: Subst,
137 [T]: Internable,
138{
139 fn subst(&self, args: &[GenericArg]) -> Self {
140 self.iter().map(|t| t.subst(args)).collect()
141 }
142}