Check for substs compatibility for RPITITs

This commit is contained in:
Michael Goulet 2022-11-01 01:34:59 +00:00
parent 95a3a7277b
commit a2037e3012
3 changed files with 44 additions and 3 deletions

View File

@ -2192,7 +2192,7 @@ fn confirm_impl_candidate<'cx, 'tcx>(
// Verify that the trait item and its implementation have compatible substs lists
fn check_substs_compatible<'tcx>(
tcx: TyCtxt<'tcx>,
assoc_ty: &ty::AssocItem,
assoc_item: &ty::AssocItem,
substs: ty::SubstsRef<'tcx>,
) -> bool {
fn check_substs_compatible_inner<'tcx>(
@ -2224,7 +2224,10 @@ fn check_substs_compatible<'tcx>(
true
}
check_substs_compatible_inner(tcx, tcx.generics_of(assoc_ty.def_id), substs.as_slice())
let generics = tcx.generics_of(assoc_item.def_id);
// Chop off any additional substs (RPITIT) substs
let substs = &substs[0..generics.count().min(substs.len())];
check_substs_compatible_inner(tcx, generics, substs)
}
fn confirm_impl_trait_in_trait_candidate<'tcx>(
@ -2253,12 +2256,21 @@ fn confirm_impl_trait_in_trait_candidate<'tcx>(
};
}
let impl_fn_def_id = leaf_def.item.def_id;
// Rebase from {trait}::{fn}::{opaque} to {impl}::{fn}::{opaque},
// since `data.substs` are the impl substs.
let impl_fn_substs =
obligation.predicate.substs.rebase_onto(tcx, tcx.parent(trait_fn_def_id), data.substs);
if !check_substs_compatible(tcx, &leaf_def.item, impl_fn_substs) {
let err = tcx.ty_error_with_message(
obligation.cause.span,
"impl method and trait method have different parameters",
);
return Progress { term: err.into(), obligations };
}
let impl_fn_def_id = leaf_def.item.def_id;
let cause = ObligationCause::new(
obligation.cause.span,
obligation.cause.body_id,

View File

@ -0,0 +1,17 @@
#![feature(return_position_impl_trait_in_trait)]
#![allow(incomplete_features)]
struct U;
trait Foo {
fn bar(&self) -> impl Sized;
}
impl Foo for U {
fn bar<T>(&self) {}
//~^ ERROR method `bar` has 1 type parameter but its trait declaration has 0 type parameters
}
fn main() {
U.bar();
}

View File

@ -0,0 +1,12 @@
error[E0049]: method `bar` has 1 type parameter but its trait declaration has 0 type parameters
--> $DIR/generics-mismatch.rs:11:12
|
LL | fn bar(&self) -> impl Sized;
| - expected 0 type parameters
...
LL | fn bar<T>(&self) {}
| ^ found 1 type parameter
error: aborting due to previous error
For more information about this error, try `rustc --explain E0049`.