diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs index 2b2f9a07890fb..9f0f7981efc8e 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs @@ -301,6 +301,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { let rebased_args = alias.args.rebase_onto(tcx, trait_def_id, impl_substs); let impl_item_def_id = leaf_def.item.def_id; + if !tcx.check_args_compatible(impl_item_def_id, rebased_args) { + return false; + } let impl_assoc_ty = tcx.type_of(impl_item_def_id).instantiate(tcx, rebased_args); self.infcx.can_eq(param_env, impl_assoc_ty, concrete) diff --git a/tests/ui/associated-types/suggest-param-env-shadowing-incompatible-args.rs b/tests/ui/associated-types/suggest-param-env-shadowing-incompatible-args.rs new file mode 100644 index 0000000000000..4a3ccc287f7f0 --- /dev/null +++ b/tests/ui/associated-types/suggest-param-env-shadowing-incompatible-args.rs @@ -0,0 +1,18 @@ +//@ compile-flags: -Znext-solver=globally + +// Regression test for https://github.com/rust-lang/rust/issues/152684. + +#![feature(associated_type_defaults)] + +trait Foo { + type Assoc = T; + //~^ ERROR defaults for generic parameters are not allowed here + fn foo() -> Self::Assoc; +} +impl Foo for () { + fn foo() -> Self::Assoc { + [] //~ ERROR mismatched types + } +} + +fn main() {} diff --git a/tests/ui/associated-types/suggest-param-env-shadowing-incompatible-args.stderr b/tests/ui/associated-types/suggest-param-env-shadowing-incompatible-args.stderr new file mode 100644 index 0000000000000..c401233242489 --- /dev/null +++ b/tests/ui/associated-types/suggest-param-env-shadowing-incompatible-args.stderr @@ -0,0 +1,20 @@ +error: defaults for generic parameters are not allowed here + --> $DIR/suggest-param-env-shadowing-incompatible-args.rs:8:16 + | +LL | type Assoc = T; + | ^^^^^^ + +error[E0308]: mismatched types + --> $DIR/suggest-param-env-shadowing-incompatible-args.rs:14:9 + | +LL | fn foo() -> Self::Assoc { + | ----------- expected `<() as Foo>::Assoc` because of return type +LL | [] + | ^^ expected `u8`, found `[_; 0]` + | + = note: expected type `u8` + found array `[_; 0]` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`.