1 use rustc_hir::{Movability, Mutability};
2 use rustc_infer::{infer::InferCtxt, traits::query::NoSolution};
3 use rustc_middle::ty::{self, Ty, TyCtxt};
5 // Calculates the constituent types of a type for `auto trait` purposes.
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>,
12 ) -> Result<Vec<Ty<'tcx>>, NoSolution> {
23 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
25 | ty::Char => Ok(vec![]),
30 | ty::Alias(ty::Projection, ..)
31 | ty::Placeholder(..) => Err(NoSolution),
34 | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
35 bug!("unexpected type `{ty}`")
38 ty::RawPtr(ty::TypeAndMut { ty: element_ty, .. }) | ty::Ref(_, element_ty, _) => {
42 ty::Array(element_ty, _) | ty::Slice(element_ty) => Ok(vec![element_ty]),
44 ty::Tuple(ref tys) => {
45 // (T1, ..., Tn) -- meets any bound that all of T1...Tn meet
46 Ok(tys.iter().collect())
49 ty::Closure(_, ref substs) => Ok(vec![substs.as_closure().tupled_upvars_ty()]),
51 ty::Generator(_, ref substs, _) => {
52 let generator_substs = substs.as_generator();
53 Ok(vec![generator_substs.tupled_upvars_ty(), generator_substs.witness()])
56 ty::GeneratorWitness(types) => {
57 Ok(infcx.replace_bound_vars_with_placeholders(types).to_vec())
60 // For `PhantomData<T>`, we pass `T`.
61 ty::Adt(def, substs) if def.is_phantom_data() => Ok(vec![substs.type_at(0)]),
63 ty::Adt(def, substs) => Ok(def.all_fields().map(|f| f.ty(tcx, substs)).collect()),
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)])
74 pub(super) fn instantiate_constituent_tys_for_sized_trait<'tcx>(
75 infcx: &InferCtxt<'tcx>,
77 ) -> Result<Vec<Ty<'tcx>>, NoSolution> {
79 ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
90 | ty::GeneratorWitness(..)
94 | ty::Dynamic(_, _, ty::DynStar)
95 | ty::Error(_) => Ok(vec![]),
103 | ty::Placeholder(..) => Err(NoSolution),
106 | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
107 bug!("unexpected type `{ty}`")
110 ty::Tuple(tys) => Ok(tys.to_vec()),
112 ty::Adt(def, substs) => {
113 let sized_crit = def.sized_constraint(infcx.tcx);
117 .map(|ty| sized_crit.rebind(*ty).subst(infcx.tcx, substs))
123 pub(super) fn instantiate_constituent_tys_for_copy_clone_trait<'tcx>(
124 infcx: &InferCtxt<'tcx>,
126 ) -> Result<Vec<Ty<'tcx>>, NoSolution> {
128 ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
131 | ty::Error(_) => Ok(vec![]),
133 // Implementations are provided in core
141 | ty::Ref(_, _, Mutability::Not)
142 | ty::Array(..) => Err(NoSolution),
147 | ty::Generator(_, _, Movability::Static)
149 | ty::Ref(_, _, Mutability::Mut)
153 | ty::Placeholder(..) => Err(NoSolution),
156 | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
157 bug!("unexpected type `{ty}`")
160 ty::Tuple(tys) => Ok(tys.to_vec()),
162 ty::Closure(_, substs) => Ok(vec![substs.as_closure().tupled_upvars_ty()]),
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()])
173 ty::GeneratorWitness(types) => {
174 Ok(infcx.replace_bound_vars_with_placeholders(types).to_vec())
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>(
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)
189 .map_bound(|sig| (tcx.mk_tup(sig.inputs().iter()), sig.output())),
192 Ok(Some(sig.map_bound(|sig| (tcx.mk_tup(sig.inputs().iter()), sig.output()))))
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),
201 Ok(Some(closure_substs.sig().map_bound(|sig| (sig.inputs()[0], sig.output()))))
215 | ty::Dynamic(_, _, _)
216 | ty::Generator(_, _, _)
217 | ty::GeneratorWitness(_)
222 | ty::Placeholder(..)
223 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
224 | ty::Error(_) => Err(NoSolution),
227 | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
228 bug!("unexpected type `{self_ty}`")