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.instantiate_binder_with_placeholders(types).to_vec())
60 ty::GeneratorWitnessMIR(..) => todo!(),
62 // For `PhantomData<T>`, we pass `T`.
63 ty::Adt(def, substs) if def.is_phantom_data() => Ok(vec![substs.type_at(0)]),
65 ty::Adt(def, substs) => Ok(def.all_fields().map(|f| f.ty(tcx, substs)).collect()),
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)])
76 pub(super) fn instantiate_constituent_tys_for_sized_trait<'tcx>(
77 infcx: &InferCtxt<'tcx>,
79 ) -> Result<Vec<Ty<'tcx>>, NoSolution> {
81 ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
92 | ty::GeneratorWitness(..)
93 | ty::GeneratorWitnessMIR(..)
97 | ty::Dynamic(_, _, ty::DynStar)
98 | ty::Error(_) => Ok(vec![]),
106 | ty::Placeholder(..) => Err(NoSolution),
109 | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
110 bug!("unexpected type `{ty}`")
113 ty::Tuple(tys) => Ok(tys.to_vec()),
115 ty::Adt(def, substs) => {
116 let sized_crit = def.sized_constraint(infcx.tcx);
120 .map(|ty| sized_crit.rebind(*ty).subst(infcx.tcx, substs))
126 pub(super) fn instantiate_constituent_tys_for_copy_clone_trait<'tcx>(
127 infcx: &InferCtxt<'tcx>,
129 ) -> Result<Vec<Ty<'tcx>>, NoSolution> {
131 ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
134 | ty::Error(_) => Ok(vec![]),
136 // Implementations are provided in core
144 | ty::Ref(_, _, Mutability::Not)
145 | ty::Array(..) => Err(NoSolution),
150 | ty::Generator(_, _, Movability::Static)
152 | ty::Ref(_, _, Mutability::Mut)
156 | ty::Placeholder(..) => Err(NoSolution),
159 | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
160 bug!("unexpected type `{ty}`")
163 ty::Tuple(tys) => Ok(tys.to_vec()),
165 ty::Closure(_, substs) => Ok(vec![substs.as_closure().tupled_upvars_ty()]),
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()])
176 ty::GeneratorWitness(types) => {
177 Ok(infcx.instantiate_binder_with_placeholders(types).to_vec())
180 ty::GeneratorWitnessMIR(..) => todo!(),
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>(
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(
194 .map_bound(|sig| (tcx.mk_tup(sig.inputs().iter()), sig.output())),
197 Ok(Some(sig.map_bound(|sig| (tcx.mk_tup(sig.inputs().iter()), sig.output()))))
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),
206 Ok(Some(closure_substs.sig().map_bound(|sig| (sig.inputs()[0], sig.output()))))
220 | ty::Dynamic(_, _, _)
221 | ty::Generator(_, _, _)
222 | ty::GeneratorWitness(_)
223 | ty::GeneratorWitnessMIR(..)
228 | ty::Placeholder(..)
229 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
230 | ty::Error(_) => Err(NoSolution),
233 | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
234 bug!("unexpected type `{self_ty}`")