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