]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/solve/trait_goals/structural_traits.rs
Replacing bound vars is actually instantiating a binder
[rust.git] / compiler / rustc_trait_selection / src / solve / trait_goals / structural_traits.rs
1 use rustc_hir::{Movability, Mutability};
2 use rustc_infer::{infer::InferCtxt, traits::query::NoSolution};
3 use rustc_middle::ty::{self, Ty, TyCtxt};
4
5 // Calculates the constituent types of a type for `auto trait` purposes.
6 //
7 // For types with an "existential" binder, i.e. generator witnesses, we also
8 // instantiate the binder with placeholders eagerly.
9 pub(super) fn instantiate_constituent_tys_for_auto_trait<'tcx>(
10     infcx: &InferCtxt<'tcx>,
11     ty: Ty<'tcx>,
12 ) -> Result<Vec<Ty<'tcx>>, NoSolution> {
13     let tcx = infcx.tcx;
14     match *ty.kind() {
15         ty::Uint(_)
16         | ty::Int(_)
17         | ty::Bool
18         | ty::Float(_)
19         | ty::FnDef(..)
20         | ty::FnPtr(_)
21         | ty::Str
22         | ty::Error(_)
23         | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
24         | ty::Never
25         | ty::Char => Ok(vec![]),
26
27         ty::Dynamic(..)
28         | ty::Param(..)
29         | ty::Foreign(..)
30         | ty::Alias(ty::Projection, ..)
31         | ty::Placeholder(..) => Err(NoSolution),
32
33         ty::Bound(..)
34         | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
35             bug!("unexpected type `{ty}`")
36         }
37
38         ty::RawPtr(ty::TypeAndMut { ty: element_ty, .. }) | ty::Ref(_, element_ty, _) => {
39             Ok(vec![element_ty])
40         }
41
42         ty::Array(element_ty, _) | ty::Slice(element_ty) => Ok(vec![element_ty]),
43
44         ty::Tuple(ref tys) => {
45             // (T1, ..., Tn) -- meets any bound that all of T1...Tn meet
46             Ok(tys.iter().collect())
47         }
48
49         ty::Closure(_, ref substs) => Ok(vec![substs.as_closure().tupled_upvars_ty()]),
50
51         ty::Generator(_, ref substs, _) => {
52             let generator_substs = substs.as_generator();
53             Ok(vec![generator_substs.tupled_upvars_ty(), generator_substs.witness()])
54         }
55
56         ty::GeneratorWitness(types) => {
57             Ok(infcx.instantiate_binder_with_placeholders(types).to_vec())
58         }
59
60         ty::GeneratorWitnessMIR(..) => todo!(),
61
62         // For `PhantomData<T>`, we pass `T`.
63         ty::Adt(def, substs) if def.is_phantom_data() => Ok(vec![substs.type_at(0)]),
64
65         ty::Adt(def, substs) => Ok(def.all_fields().map(|f| f.ty(tcx, substs)).collect()),
66
67         ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => {
68             // We can resolve the `impl Trait` to its concrete type,
69             // which enforces a DAG between the functions requiring
70             // the auto trait bounds in question.
71             Ok(vec![tcx.bound_type_of(def_id).subst(tcx, substs)])
72         }
73     }
74 }
75
76 pub(super) fn instantiate_constituent_tys_for_sized_trait<'tcx>(
77     infcx: &InferCtxt<'tcx>,
78     ty: Ty<'tcx>,
79 ) -> Result<Vec<Ty<'tcx>>, NoSolution> {
80     match *ty.kind() {
81         ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
82         | ty::Uint(_)
83         | ty::Int(_)
84         | ty::Bool
85         | ty::Float(_)
86         | ty::FnDef(..)
87         | ty::FnPtr(_)
88         | ty::RawPtr(..)
89         | ty::Char
90         | ty::Ref(..)
91         | ty::Generator(..)
92         | ty::GeneratorWitness(..)
93         | ty::GeneratorWitnessMIR(..)
94         | ty::Array(..)
95         | ty::Closure(..)
96         | ty::Never
97         | ty::Dynamic(_, _, ty::DynStar)
98         | ty::Error(_) => Ok(vec![]),
99
100         ty::Str
101         | ty::Slice(_)
102         | ty::Dynamic(..)
103         | ty::Foreign(..)
104         | ty::Alias(..)
105         | ty::Param(_)
106         | ty::Placeholder(..) => Err(NoSolution),
107
108         ty::Bound(..)
109         | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
110             bug!("unexpected type `{ty}`")
111         }
112
113         ty::Tuple(tys) => Ok(tys.to_vec()),
114
115         ty::Adt(def, substs) => {
116             let sized_crit = def.sized_constraint(infcx.tcx);
117             Ok(sized_crit
118                 .0
119                 .iter()
120                 .map(|ty| sized_crit.rebind(*ty).subst(infcx.tcx, substs))
121                 .collect())
122         }
123     }
124 }
125
126 pub(super) fn instantiate_constituent_tys_for_copy_clone_trait<'tcx>(
127     infcx: &InferCtxt<'tcx>,
128     ty: Ty<'tcx>,
129 ) -> Result<Vec<Ty<'tcx>>, NoSolution> {
130     match *ty.kind() {
131         ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
132         | ty::FnDef(..)
133         | ty::FnPtr(_)
134         | ty::Error(_) => Ok(vec![]),
135
136         // Implementations are provided in core
137         ty::Uint(_)
138         | ty::Int(_)
139         | ty::Bool
140         | ty::Float(_)
141         | ty::Char
142         | ty::RawPtr(..)
143         | ty::Never
144         | ty::Ref(_, _, Mutability::Not)
145         | ty::Array(..) => Err(NoSolution),
146
147         ty::Dynamic(..)
148         | ty::Str
149         | ty::Slice(_)
150         | ty::Generator(_, _, Movability::Static)
151         | ty::Foreign(..)
152         | ty::Ref(_, _, Mutability::Mut)
153         | ty::Adt(_, _)
154         | ty::Alias(_, _)
155         | ty::Param(_)
156         | ty::Placeholder(..) => Err(NoSolution),
157
158         ty::Bound(..)
159         | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
160             bug!("unexpected type `{ty}`")
161         }
162
163         ty::Tuple(tys) => Ok(tys.to_vec()),
164
165         ty::Closure(_, substs) => Ok(vec![substs.as_closure().tupled_upvars_ty()]),
166
167         ty::Generator(_, substs, Movability::Movable) => {
168             if infcx.tcx.features().generator_clone {
169                 let generator = substs.as_generator();
170                 Ok(vec![generator.tupled_upvars_ty(), generator.witness()])
171             } else {
172                 Err(NoSolution)
173             }
174         }
175
176         ty::GeneratorWitness(types) => {
177             Ok(infcx.instantiate_binder_with_placeholders(types).to_vec())
178         }
179
180         ty::GeneratorWitnessMIR(..) => todo!(),
181     }
182 }
183
184 // Returns a binder of the tupled inputs types and output type from a builtin callable type.
185 pub(crate) fn extract_tupled_inputs_and_output_from_callable<'tcx>(
186     tcx: TyCtxt<'tcx>,
187     self_ty: Ty<'tcx>,
188     goal_kind: ty::ClosureKind,
189 ) -> Result<Option<ty::Binder<'tcx, (Ty<'tcx>, Ty<'tcx>)>>, NoSolution> {
190     match *self_ty.kind() {
191         ty::FnDef(def_id, substs) => Ok(Some(
192             tcx.fn_sig(def_id)
193                 .subst(tcx, substs)
194                 .map_bound(|sig| (tcx.mk_tup(sig.inputs().iter()), sig.output())),
195         )),
196         ty::FnPtr(sig) => {
197             Ok(Some(sig.map_bound(|sig| (tcx.mk_tup(sig.inputs().iter()), sig.output()))))
198         }
199         ty::Closure(_, substs) => {
200             let closure_substs = substs.as_closure();
201             match closure_substs.kind_ty().to_opt_closure_kind() {
202                 Some(closure_kind) if closure_kind.extends(goal_kind) => {}
203                 None => return Ok(None),
204                 _ => return Err(NoSolution),
205             }
206             Ok(Some(closure_substs.sig().map_bound(|sig| (sig.inputs()[0], sig.output()))))
207         }
208         ty::Bool
209         | ty::Char
210         | ty::Int(_)
211         | ty::Uint(_)
212         | ty::Float(_)
213         | ty::Adt(_, _)
214         | ty::Foreign(_)
215         | ty::Str
216         | ty::Array(_, _)
217         | ty::Slice(_)
218         | ty::RawPtr(_)
219         | ty::Ref(_, _, _)
220         | ty::Dynamic(_, _, _)
221         | ty::Generator(_, _, _)
222         | ty::GeneratorWitness(_)
223         | ty::GeneratorWitnessMIR(..)
224         | ty::Never
225         | ty::Tuple(_)
226         | ty::Alias(_, _)
227         | ty::Param(_)
228         | ty::Placeholder(..)
229         | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
230         | ty::Error(_) => Err(NoSolution),
231
232         ty::Bound(..)
233         | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
234             bug!("unexpected type `{self_ty}`")
235         }
236     }
237 }