Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn check_method_is_structurally_compatible<'tcx>(
) -> Result<(), ErrorGuaranteed> {
compare_self_type(tcx, impl_m, trait_m, impl_trait_ref, delay)?;
compare_number_of_generics(tcx, impl_m, trait_m, delay)?;
compare_generic_param_kinds(tcx, impl_m, trait_m, delay)?;
compare_generic_param_kinds(tcx, impl_m, trait_m, impl_trait_ref, delay)?;
compare_number_of_method_arguments(tcx, impl_m, trait_m, delay)?;
compare_synthetic_generics(tcx, impl_m, trait_m, delay)?;
check_region_bounds_on_impl_item(tcx, impl_m, trait_m, delay)?;
Expand Down Expand Up @@ -2063,6 +2063,7 @@ fn compare_generic_param_kinds<'tcx>(
tcx: TyCtxt<'tcx>,
impl_item: ty::AssocItem,
trait_item: ty::AssocItem,
impl_trait_ref: ty::TraitRef<'tcx>,
delay: bool,
) -> Result<(), ErrorGuaranteed> {
assert_eq!(impl_item.tag(), trait_item.tag());
Expand All @@ -2076,6 +2077,18 @@ fn compare_generic_param_kinds<'tcx>(
})
};

// Map the trait item's generic parameters into the impl item's generic context.
let trait_to_impl_args = ty::GenericArgs::identity_for_item(tcx, impl_item.def_id).rebase_onto(
tcx,
impl_item.container_id(tcx),
impl_trait_ref.args,
);

let const_param_tys_match = |param_impl_def_id, param_trait_def_id| {
tcx.type_of(param_impl_def_id).instantiate_identity().skip_norm_wip()
== tcx.type_of(param_trait_def_id).instantiate(tcx, trait_to_impl_args).skip_norm_wip()
};

for (param_impl, param_trait) in
iter::zip(ty_const_params_of(impl_item.def_id), ty_const_params_of(trait_item.def_id))
{
Expand All @@ -2084,7 +2097,8 @@ fn compare_generic_param_kinds<'tcx>(
(Const { .. }, Const { .. })
if tcx.type_of(param_impl.def_id) != tcx.type_of(param_trait.def_id) =>
{
true
// Do a more thorough check
!const_param_tys_match(param_impl.def_id, param_trait.def_id)
}
(Const { .. }, Type { .. }) | (Type { .. }, Const { .. }) => true,
// this is exhaustive so that anyone adding new generic param kinds knows
Expand Down Expand Up @@ -2146,7 +2160,7 @@ fn compare_impl_const<'tcx>(
) -> Result<(), ErrorGuaranteed> {
compare_const_directness(tcx, impl_const_item, trait_const_item)?;
compare_number_of_generics(tcx, impl_const_item, trait_const_item, false)?;
compare_generic_param_kinds(tcx, impl_const_item, trait_const_item, false)?;
compare_generic_param_kinds(tcx, impl_const_item, trait_const_item, impl_trait_ref, false)?;
check_region_bounds_on_impl_item(tcx, impl_const_item, trait_const_item, false)?;
compare_const_clause_entailment(tcx, impl_const_item, trait_const_item, impl_trait_ref)
}
Expand Down Expand Up @@ -2324,7 +2338,7 @@ fn compare_impl_ty<'tcx>(
impl_trait_ref: ty::TraitRef<'tcx>,
) -> Result<(), ErrorGuaranteed> {
compare_number_of_generics(tcx, impl_ty, trait_ty, false)?;
compare_generic_param_kinds(tcx, impl_ty, trait_ty, false)?;
compare_generic_param_kinds(tcx, impl_ty, trait_ty, impl_trait_ref, false)?;
check_region_bounds_on_impl_item(tcx, impl_ty, trait_ty, false)?;
compare_type_clause_entailment(tcx, impl_ty, trait_ty, impl_trait_ref)?;
check_type_bounds(tcx, trait_ty, impl_ty, impl_trait_ref)
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/const-generics/method-instantiation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//@ compile-flags: --crate-type lib
//@ check-pass

#![feature(min_adt_const_params)]
#![feature(generic_const_parameter_types)]
#![allow(incomplete_features)]

pub trait Trait {
fn method<const A: usize, const B: [usize; A]>();
}

pub struct Foo;

impl Trait for Foo {
fn method<const A: usize, const B: [usize; A]>() {}
}
Loading