Clean up substs building

This commit is contained in:
Michael Goulet 2023-03-22 04:33:20 +00:00
parent 08284449a2
commit 5456eecab0

View File

@ -1876,14 +1876,17 @@ pub(super) fn check_type_bounds<'tcx>(
impl_ty: ty::AssocItem, impl_ty: ty::AssocItem,
impl_trait_ref: ty::TraitRef<'tcx>, impl_trait_ref: ty::TraitRef<'tcx>,
) -> Result<(), ErrorGuaranteed> { ) -> Result<(), ErrorGuaranteed> {
let param_env = tcx.param_env(impl_ty.def_id);
let container_id = impl_ty.container_id(tcx);
// Given // Given
// //
// impl<A, B> Foo<u32> for (A, B) { // impl<A, B> Foo<u32> for (A, B) {
// type Bar<C> =... // type Bar<C> = Wrapper<A, B, C>
// } // }
// //
// - `impl_trait_ref` would be `<(A, B) as Foo<u32>>` // - `impl_trait_ref` would be `<(A, B) as Foo<u32>>`
// - `impl_ty_substs` would be `[A, B, ^0.0]` (`^0.0` here is the bound var with db 0 and index 0) // - `normalize_impl_ty_substs` would be `[A, B, ^0.0]` (`^0.0` here is the bound var with db 0 and index 0)
// - `normalize_impl_ty` would be `Wrapper<A, B, ^0.0>`
// - `rebased_substs` would be `[(A, B), u32, ^0.0]`, combining the substs from // - `rebased_substs` would be `[(A, B), u32, ^0.0]`, combining the substs from
// the *trait* with the generic associated type parameters (as bound vars). // the *trait* with the generic associated type parameters (as bound vars).
// //
@ -1912,56 +1915,46 @@ pub(super) fn check_type_bounds<'tcx>(
// Member<C: Eq> = .... That type would fail a well-formedness check that we ought to be doing // Member<C: Eq> = .... That type would fail a well-formedness check that we ought to be doing
// elsewhere, which would check that any <T as Family>::Member<X> meets the bounds declared in // elsewhere, which would check that any <T as Family>::Member<X> meets the bounds declared in
// the trait (notably, that X: Eq and T: Family). // the trait (notably, that X: Eq and T: Family).
let defs: &ty::Generics = tcx.generics_of(impl_ty.def_id);
let mut substs = smallvec::SmallVec::with_capacity(defs.count());
if let Some(def_id) = defs.parent {
let parent_defs = tcx.generics_of(def_id);
InternalSubsts::fill_item(&mut substs, tcx, parent_defs, &mut |param, _| {
tcx.mk_param_from_def(param)
});
}
let mut bound_vars: smallvec::SmallVec<[ty::BoundVariableKind; 8]> = let mut bound_vars: smallvec::SmallVec<[ty::BoundVariableKind; 8]> =
smallvec::SmallVec::with_capacity(defs.count()); smallvec::SmallVec::with_capacity(tcx.generics_of(impl_ty.def_id).params.len());
InternalSubsts::fill_single(&mut substs, defs, &mut |param, _| match param.kind { // Extend the impl's identity substs with late-bound GAT vars
GenericParamDefKind::Type { .. } => { let normalize_impl_ty_substs = ty::InternalSubsts::identity_for_item(tcx, container_id)
let kind = ty::BoundTyKind::Param(param.def_id, param.name); .extend_to(tcx, impl_ty.def_id, |param, _| match param.kind {
let bound_var = ty::BoundVariableKind::Ty(kind); GenericParamDefKind::Type { .. } => {
bound_vars.push(bound_var); let kind = ty::BoundTyKind::Param(param.def_id, param.name);
tcx.mk_bound( let bound_var = ty::BoundVariableKind::Ty(kind);
ty::INNERMOST, bound_vars.push(bound_var);
ty::BoundTy { var: ty::BoundVar::from_usize(bound_vars.len() - 1), kind }, tcx.mk_bound(
) ty::INNERMOST,
.into() ty::BoundTy { var: ty::BoundVar::from_usize(bound_vars.len() - 1), kind },
} )
GenericParamDefKind::Lifetime => { .into()
let kind = ty::BoundRegionKind::BrNamed(param.def_id, param.name); }
let bound_var = ty::BoundVariableKind::Region(kind); GenericParamDefKind::Lifetime => {
bound_vars.push(bound_var); let kind = ty::BoundRegionKind::BrNamed(param.def_id, param.name);
tcx.mk_re_late_bound( let bound_var = ty::BoundVariableKind::Region(kind);
ty::INNERMOST, bound_vars.push(bound_var);
ty::BoundRegion { var: ty::BoundVar::from_usize(bound_vars.len() - 1), kind }, tcx.mk_re_late_bound(
) ty::INNERMOST,
.into() ty::BoundRegion { var: ty::BoundVar::from_usize(bound_vars.len() - 1), kind },
} )
GenericParamDefKind::Const { .. } => { .into()
let bound_var = ty::BoundVariableKind::Const; }
bound_vars.push(bound_var); GenericParamDefKind::Const { .. } => {
tcx.mk_const( let bound_var = ty::BoundVariableKind::Const;
ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_usize(bound_vars.len() - 1)), bound_vars.push(bound_var);
tcx.type_of(param.def_id).subst_identity(), tcx.mk_const(
) ty::ConstKind::Bound(
.into() ty::INNERMOST,
} ty::BoundVar::from_usize(bound_vars.len() - 1),
}); ),
let bound_vars = tcx.mk_bound_variable_kinds(&bound_vars); tcx.type_of(param.def_id)
let impl_ty_substs = tcx.mk_substs(&substs); .no_bound_vars()
let container_id = impl_ty.container_id(tcx); .expect("const parameter types cannot be generic"),
)
let rebased_substs = impl_ty_substs.rebase_onto(tcx, container_id, impl_trait_ref.substs); .into()
let impl_ty_value = tcx.type_of(impl_ty.def_id).subst(tcx, impl_ty_substs); }
});
let param_env = tcx.param_env(impl_ty.def_id);
// When checking something like // When checking something like
// //
// trait X { type Y: PartialEq<<Self as X>::Y> } // trait X { type Y: PartialEq<<Self as X>::Y> }
@ -1971,9 +1964,13 @@ pub(super) fn check_type_bounds<'tcx>(
// we want <T as X>::Y to normalize to S. This is valid because we are // we want <T as X>::Y to normalize to S. This is valid because we are
// checking the default value specifically here. Add this equality to the // checking the default value specifically here. Add this equality to the
// ParamEnv for normalization specifically. // ParamEnv for normalization specifically.
let normalize_impl_ty = tcx.type_of(impl_ty.def_id).subst(tcx, normalize_impl_ty_substs);
let rebased_substs =
normalize_impl_ty_substs.rebase_onto(tcx, container_id, impl_trait_ref.substs);
let bound_vars = tcx.mk_bound_variable_kinds(&bound_vars);
let normalize_param_env = { let normalize_param_env = {
let mut predicates = param_env.caller_bounds().iter().collect::<Vec<_>>(); let mut predicates = param_env.caller_bounds().iter().collect::<Vec<_>>();
match impl_ty_value.kind() { match normalize_impl_ty.kind() {
ty::Alias(ty::Projection, proj) ty::Alias(ty::Projection, proj)
if proj.def_id == trait_ty.def_id && proj.substs == rebased_substs => if proj.def_id == trait_ty.def_id && proj.substs == rebased_substs =>
{ {
@ -1987,7 +1984,7 @@ pub(super) fn check_type_bounds<'tcx>(
ty::Binder::bind_with_vars( ty::Binder::bind_with_vars(
ty::ProjectionPredicate { ty::ProjectionPredicate {
projection_ty: tcx.mk_alias_ty(trait_ty.def_id, rebased_substs), projection_ty: tcx.mk_alias_ty(trait_ty.def_id, rebased_substs),
term: impl_ty_value.into(), term: normalize_impl_ty.into(),
}, },
bound_vars, bound_vars,
) )