]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_trait_selection/src/solve/trait_goals/structural_traits.rs
New upstream version 1.68.2+dfsg1
[rustc.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::Placeholder(..)
28 | ty::Dynamic(..)
29 | ty::Param(..)
30 | ty::Foreign(..)
31 | ty::Alias(ty::Projection, ..)
32 | ty::Bound(..)
33 | ty::Infer(ty::TyVar(_)) => Err(NoSolution),
34
35 ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => bug!(),
36
37 ty::RawPtr(ty::TypeAndMut { ty: element_ty, .. }) | ty::Ref(_, element_ty, _) => {
38 Ok(vec![element_ty])
39 }
40
41 ty::Array(element_ty, _) | ty::Slice(element_ty) => Ok(vec![element_ty]),
42
43 ty::Tuple(ref tys) => {
44 // (T1, ..., Tn) -- meets any bound that all of T1...Tn meet
45 Ok(tys.iter().collect())
46 }
47
48 ty::Closure(_, ref substs) => Ok(vec![substs.as_closure().tupled_upvars_ty()]),
49
50 ty::Generator(_, ref substs, _) => {
51 let generator_substs = substs.as_generator();
52 Ok(vec![generator_substs.tupled_upvars_ty(), generator_substs.witness()])
53 }
54
55 ty::GeneratorWitness(types) => {
56 Ok(infcx.replace_bound_vars_with_placeholders(types).to_vec())
57 }
58
59 // For `PhantomData<T>`, we pass `T`.
60 ty::Adt(def, substs) if def.is_phantom_data() => Ok(vec![substs.type_at(0)]),
61
62 ty::Adt(def, substs) => Ok(def.all_fields().map(|f| f.ty(tcx, substs)).collect()),
63
64 ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => {
65 // We can resolve the `impl Trait` to its concrete type,
66 // which enforces a DAG between the functions requiring
67 // the auto trait bounds in question.
68 Ok(vec![tcx.bound_type_of(def_id).subst(tcx, substs)])
69 }
70 }
71 }
72
73 pub(super) fn instantiate_constituent_tys_for_sized_trait<'tcx>(
74 infcx: &InferCtxt<'tcx>,
75 ty: Ty<'tcx>,
76 ) -> Result<Vec<Ty<'tcx>>, NoSolution> {
77 match *ty.kind() {
78 ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
79 | ty::Uint(_)
80 | ty::Int(_)
81 | ty::Bool
82 | ty::Float(_)
83 | ty::FnDef(..)
84 | ty::FnPtr(_)
85 | ty::RawPtr(..)
86 | ty::Char
87 | ty::Ref(..)
88 | ty::Generator(..)
89 | ty::GeneratorWitness(..)
90 | ty::Array(..)
91 | ty::Closure(..)
92 | ty::Never
93 | ty::Dynamic(_, _, ty::DynStar)
94 | ty::Error(_) => Ok(vec![]),
95
96 ty::Str
97 | ty::Slice(_)
98 | ty::Dynamic(..)
99 | ty::Foreign(..)
100 | ty::Alias(..)
101 | ty::Param(_)
102 | ty::Infer(ty::TyVar(_)) => Err(NoSolution),
103
104 ty::Placeholder(..)
105 | ty::Bound(..)
106 | ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => bug!(),
107
108 ty::Tuple(tys) => Ok(tys.to_vec()),
109
110 ty::Adt(def, substs) => {
111 let sized_crit = def.sized_constraint(infcx.tcx);
112 Ok(sized_crit
113 .0
114 .iter()
115 .map(|ty| sized_crit.rebind(*ty).subst(infcx.tcx, substs))
116 .collect())
117 }
118 }
119 }
120
121 pub(super) fn instantiate_constituent_tys_for_copy_clone_trait<'tcx>(
122 infcx: &InferCtxt<'tcx>,
123 ty: Ty<'tcx>,
124 ) -> Result<Vec<Ty<'tcx>>, NoSolution> {
125 match *ty.kind() {
126 ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
127 | ty::FnDef(..)
128 | ty::FnPtr(_)
129 | ty::Error(_) => Ok(vec![]),
130
131 // Implementations are provided in core
132 ty::Uint(_)
133 | ty::Int(_)
134 | ty::Bool
135 | ty::Float(_)
136 | ty::Char
137 | ty::RawPtr(..)
138 | ty::Never
139 | ty::Ref(_, _, Mutability::Not)
140 | ty::Array(..) => Err(NoSolution),
141
142 ty::Dynamic(..)
143 | ty::Str
144 | ty::Slice(_)
145 | ty::Generator(_, _, Movability::Static)
146 | ty::Foreign(..)
147 | ty::Ref(_, _, Mutability::Mut)
148 | ty::Adt(_, _)
149 | ty::Alias(_, _)
150 | ty::Param(_)
151 | ty::Infer(ty::TyVar(_)) => Err(NoSolution),
152
153 ty::Placeholder(..)
154 | ty::Bound(..)
155 | ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => bug!(),
156
157 ty::Tuple(tys) => Ok(tys.to_vec()),
158
159 ty::Closure(_, substs) => Ok(vec![substs.as_closure().tupled_upvars_ty()]),
160
161 ty::Generator(_, substs, Movability::Movable) => {
162 if infcx.tcx.features().generator_clone {
163 let generator = substs.as_generator();
164 Ok(vec![generator.tupled_upvars_ty(), generator.witness()])
165 } else {
166 Err(NoSolution)
167 }
168 }
169
170 ty::GeneratorWitness(types) => {
171 Ok(infcx.replace_bound_vars_with_placeholders(types).to_vec())
172 }
173 }
174 }
175
176 pub(crate) fn extract_tupled_inputs_and_output_from_callable<'tcx>(
177 tcx: TyCtxt<'tcx>,
178 self_ty: Ty<'tcx>,
179 goal_kind: ty::ClosureKind,
180 ) -> Result<Option<ty::Binder<'tcx, (Ty<'tcx>, Ty<'tcx>)>>, NoSolution> {
181 match *self_ty.kind() {
182 ty::FnDef(def_id, substs) => Ok(Some(
183 tcx.bound_fn_sig(def_id)
184 .subst(tcx, substs)
185 .map_bound(|sig| (tcx.mk_tup(sig.inputs().iter()), sig.output())),
186 )),
187 ty::FnPtr(sig) => {
188 Ok(Some(sig.map_bound(|sig| (tcx.mk_tup(sig.inputs().iter()), sig.output()))))
189 }
190 ty::Closure(_, substs) => {
191 let closure_substs = substs.as_closure();
192 match closure_substs.kind_ty().to_opt_closure_kind() {
193 Some(closure_kind) if closure_kind.extends(goal_kind) => {}
194 None => return Ok(None),
195 _ => return Err(NoSolution),
196 }
197 Ok(Some(closure_substs.sig().map_bound(|sig| (sig.inputs()[0], sig.output()))))
198 }
199 ty::Bool
200 | ty::Char
201 | ty::Int(_)
202 | ty::Uint(_)
203 | ty::Float(_)
204 | ty::Adt(_, _)
205 | ty::Foreign(_)
206 | ty::Str
207 | ty::Array(_, _)
208 | ty::Slice(_)
209 | ty::RawPtr(_)
210 | ty::Ref(_, _, _)
211 | ty::Dynamic(_, _, _)
212 | ty::Generator(_, _, _)
213 | ty::GeneratorWitness(_)
214 | ty::Never
215 | ty::Tuple(_)
216 | ty::Alias(_, _)
217 | ty::Param(_)
218 | ty::Placeholder(_)
219 | ty::Bound(_, _)
220 | ty::Infer(_)
221 | ty::Error(_) => Err(NoSolution),
222 }
223 }