Guard AliasTy
creation against passing the wrong number of substs
This commit is contained in:
parent
7bdda8f801
commit
fef872a875
@ -1146,7 +1146,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
||||
|
||||
debug!(?substs_trait_ref_and_assoc_item);
|
||||
|
||||
ty::AliasTy { def_id: assoc_item.def_id, substs: substs_trait_ref_and_assoc_item }
|
||||
self.tcx().mk_alias_ty(assoc_item.def_id, substs_trait_ref_and_assoc_item)
|
||||
});
|
||||
|
||||
if !speculative {
|
||||
|
@ -1746,10 +1746,7 @@ pub fn check_type_bounds<'tcx>(
|
||||
_ => predicates.push(
|
||||
ty::Binder::bind_with_vars(
|
||||
ty::ProjectionPredicate {
|
||||
projection_ty: ty::AliasTy {
|
||||
def_id: trait_ty.def_id,
|
||||
substs: rebased_substs,
|
||||
},
|
||||
projection_ty: tcx.mk_alias_ty(trait_ty.def_id, rebased_substs),
|
||||
term: impl_ty_value.into(),
|
||||
},
|
||||
bound_vars,
|
||||
|
@ -557,10 +557,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
.chain(projection_ty.substs.iter().skip(1)),
|
||||
);
|
||||
|
||||
let quiet_projection_ty = ty::AliasTy {
|
||||
substs: substs_with_infer_self,
|
||||
def_id: projection_ty.def_id,
|
||||
};
|
||||
let quiet_projection_ty =
|
||||
tcx.mk_alias_ty(projection_ty.def_id, substs_with_infer_self);
|
||||
|
||||
let term = pred.skip_binder().term;
|
||||
|
||||
|
@ -18,7 +18,7 @@ use crate::thir::Thir;
|
||||
use crate::traits;
|
||||
use crate::ty::query::{self, TyCtxtAt};
|
||||
use crate::ty::{
|
||||
self, AdtDef, AdtDefData, AdtKind, AliasTy, Binder, BindingMode, BoundVar, CanonicalPolyFnSig,
|
||||
self, AdtDef, AdtDefData, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig,
|
||||
ClosureSizeProfileData, Const, ConstS, DefIdTree, FloatTy, FloatVar, FloatVid,
|
||||
GenericParamDefKind, InferTy, IntTy, IntVar, IntVid, List, ParamConst, ParamTy,
|
||||
PolyExistentialPredicate, PolyFnSig, Predicate, PredicateKind, Region, RegionKind, ReprOptions,
|
||||
@ -2591,12 +2591,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||
|
||||
#[inline]
|
||||
pub fn mk_projection(self, item_def_id: DefId, substs: SubstsRef<'tcx>) -> Ty<'tcx> {
|
||||
debug_assert_eq!(
|
||||
self.generics_of(item_def_id).count(),
|
||||
substs.len(),
|
||||
"wrong number of generic parameters for {item_def_id:?}: {substs:?}",
|
||||
);
|
||||
self.mk_ty(Alias(ty::Projection, AliasTy { def_id: item_def_id, substs }))
|
||||
self.mk_ty(Alias(ty::Projection, self.mk_alias_ty(item_def_id, substs)))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -2867,6 +2862,23 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||
ty::TraitRef::new(trait_def_id, substs)
|
||||
}
|
||||
|
||||
pub fn mk_alias_ty(
|
||||
self,
|
||||
def_id: DefId,
|
||||
substs: impl IntoIterator<Item = impl Into<GenericArg<'tcx>>>,
|
||||
) -> ty::AliasTy<'tcx> {
|
||||
let substs = substs.into_iter().map(Into::into);
|
||||
let n = self.generics_of(def_id).count();
|
||||
debug_assert_eq!(
|
||||
(n, Some(n)),
|
||||
substs.size_hint(),
|
||||
"wrong number of generic parameters for {def_id:?}: {:?} \nDid you accidentally include the self-type in the params list?",
|
||||
substs.collect::<Vec<_>>(),
|
||||
);
|
||||
let substs = self.mk_substs(substs);
|
||||
ty::AliasTy { def_id, substs }
|
||||
}
|
||||
|
||||
pub fn mk_bound_variable_kinds<
|
||||
I: InternAs<ty::BoundVariableKind, &'tcx List<ty::BoundVariableKind>>,
|
||||
>(
|
||||
|
@ -280,7 +280,7 @@ impl<'tcx> Relate<'tcx> for ty::AliasTy<'tcx> {
|
||||
Err(TypeError::ProjectionMismatched(expected_found(relation, a.def_id, b.def_id)))
|
||||
} else {
|
||||
let substs = relation.relate(a.substs, b.substs)?;
|
||||
Ok(ty::AliasTy { def_id: a.def_id, substs: &substs })
|
||||
Ok(relation.tcx().mk_alias_ty(a.def_id, substs))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1449,10 +1449,8 @@ impl<'tcx> ExistentialProjection<'tcx> {
|
||||
debug_assert!(!self_ty.has_escaping_bound_vars());
|
||||
|
||||
ty::ProjectionPredicate {
|
||||
projection_ty: ty::AliasTy {
|
||||
def_id: self.def_id,
|
||||
substs: tcx.mk_substs_trait(self_ty, self.substs),
|
||||
},
|
||||
projection_ty: tcx
|
||||
.mk_alias_ty(self.def_id, [self_ty.into()].into_iter().chain(self.substs)),
|
||||
term: self.term,
|
||||
}
|
||||
}
|
||||
|
@ -3265,9 +3265,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
||||
// in. For example, this would be what `Iterator::Item` is here.
|
||||
let ty_var = self.infcx.next_ty_var(origin);
|
||||
// This corresponds to `<ExprTy as Iterator>::Item = _`.
|
||||
let trait_ref = ty::Binder::dummy(ty::PredicateKind::Clause(
|
||||
let projection = ty::Binder::dummy(ty::PredicateKind::Clause(
|
||||
ty::Clause::Projection(ty::ProjectionPredicate {
|
||||
projection_ty: ty::AliasTy { substs, def_id: proj.def_id },
|
||||
projection_ty: tcx.mk_alias_ty(proj.def_id, substs),
|
||||
term: ty_var.into(),
|
||||
}),
|
||||
));
|
||||
@ -3277,7 +3277,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
||||
span,
|
||||
expr.hir_id,
|
||||
param_env,
|
||||
trait_ref,
|
||||
projection,
|
||||
));
|
||||
if ocx.select_where_possible().is_empty() {
|
||||
// `ty_var` now holds the type that `Item` is for `ExprTy`.
|
||||
|
@ -1867,10 +1867,7 @@ fn confirm_generator_candidate<'cx, 'tcx>(
|
||||
};
|
||||
|
||||
ty::ProjectionPredicate {
|
||||
projection_ty: ty::AliasTy {
|
||||
substs: trait_ref.substs,
|
||||
def_id: obligation.predicate.def_id,
|
||||
},
|
||||
projection_ty: tcx.mk_alias_ty(obligation.predicate.def_id, trait_ref.substs),
|
||||
term: ty.into(),
|
||||
}
|
||||
});
|
||||
@ -1909,10 +1906,7 @@ fn confirm_future_candidate<'cx, 'tcx>(
|
||||
debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name, sym::Output);
|
||||
|
||||
ty::ProjectionPredicate {
|
||||
projection_ty: ty::AliasTy {
|
||||
substs: trait_ref.substs,
|
||||
def_id: obligation.predicate.def_id,
|
||||
},
|
||||
projection_ty: tcx.mk_alias_ty(obligation.predicate.def_id, trait_ref.substs),
|
||||
term: return_ty.into(),
|
||||
}
|
||||
});
|
||||
@ -1965,10 +1959,8 @@ fn confirm_builtin_candidate<'cx, 'tcx>(
|
||||
bug!("unexpected builtin trait with associated type: {:?}", obligation.predicate);
|
||||
};
|
||||
|
||||
let predicate = ty::ProjectionPredicate {
|
||||
projection_ty: ty::AliasTy { substs, def_id: item_def_id },
|
||||
term,
|
||||
};
|
||||
let predicate =
|
||||
ty::ProjectionPredicate { projection_ty: tcx.mk_alias_ty(item_def_id, substs), term };
|
||||
|
||||
confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
|
||||
.with_addl_obligations(obligations)
|
||||
@ -2037,7 +2029,7 @@ fn confirm_callable_candidate<'cx, 'tcx>(
|
||||
flag,
|
||||
)
|
||||
.map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate {
|
||||
projection_ty: ty::AliasTy { substs: trait_ref.substs, def_id: fn_once_output_def_id },
|
||||
projection_ty: tcx.mk_alias_ty(fn_once_output_def_id, trait_ref.substs),
|
||||
term: ret_type.into(),
|
||||
});
|
||||
|
||||
|
@ -536,7 +536,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
||||
let ty = traits::normalize_projection_type(
|
||||
self,
|
||||
param_env,
|
||||
ty::AliasTy { def_id: tcx.lang_items().deref_target()?, substs: trait_ref.substs },
|
||||
tcx.mk_alias_ty(tcx.lang_items().deref_target()?, trait_ref.substs),
|
||||
cause.clone(),
|
||||
0,
|
||||
// We're *intentionally* throwing these away,
|
||||
|
Loading…
x
Reference in New Issue
Block a user