From c7cc1c7442bc09e3c1a37f737790994c1084ad2a Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 16 Mar 2023 19:35:27 -0300 Subject: [PATCH 1/2] Fix generics mismatch errors for RPITITs on -Zlower-impl-trait-in-trait-to-assoc-ty --- .../src/check/compare_impl_item.rs | 11 +++++++++++ ...match.stderr => generics-mismatch.current.stderr} | 2 +- .../in-trait/generics-mismatch.next.stderr | 12 ++++++++++++ tests/ui/impl-trait/in-trait/generics-mismatch.rs | 3 +++ 4 files changed, 27 insertions(+), 1 deletion(-) rename tests/ui/impl-trait/in-trait/{generics-mismatch.stderr => generics-mismatch.current.stderr} (90%) create mode 100644 tests/ui/impl-trait/in-trait/generics-mismatch.next.stderr diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index 6e6f8c1533b..32b6aeed5f8 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -1205,6 +1205,17 @@ fn compare_number_of_generics<'tcx>( return Ok(()); } + // We never need to emit a separate error for RPITITs, since if an RPITIT + // has mismatched type or const generic arguments, then the method that it's + // inheriting the generics from will also have mismatched arguments, and + // we'll report an error for that instead. Delay a bug for safety, though. + if tcx.opt_rpitit_info(trait_.def_id).is_some() { + return Err(tcx.sess.delay_span_bug( + rustc_span::DUMMY_SP, + "errors comparing numbers of generics of trait/impl functions were not emitted", + )); + } + let matchings = [ ("type", trait_own_counts.types, impl_own_counts.types), ("const", trait_own_counts.consts, impl_own_counts.consts), diff --git a/tests/ui/impl-trait/in-trait/generics-mismatch.stderr b/tests/ui/impl-trait/in-trait/generics-mismatch.current.stderr similarity index 90% rename from tests/ui/impl-trait/in-trait/generics-mismatch.stderr rename to tests/ui/impl-trait/in-trait/generics-mismatch.current.stderr index cd42683e022..310edbcb6cd 100644 --- a/tests/ui/impl-trait/in-trait/generics-mismatch.stderr +++ b/tests/ui/impl-trait/in-trait/generics-mismatch.current.stderr @@ -1,5 +1,5 @@ error[E0049]: method `bar` has 1 type parameter but its trait declaration has 0 type parameters - --> $DIR/generics-mismatch.rs:11:12 + --> $DIR/generics-mismatch.rs:14:12 | LL | fn bar(&self) -> impl Sized; | - expected 0 type parameters diff --git a/tests/ui/impl-trait/in-trait/generics-mismatch.next.stderr b/tests/ui/impl-trait/in-trait/generics-mismatch.next.stderr new file mode 100644 index 00000000000..310edbcb6cd --- /dev/null +++ b/tests/ui/impl-trait/in-trait/generics-mismatch.next.stderr @@ -0,0 +1,12 @@ +error[E0049]: method `bar` has 1 type parameter but its trait declaration has 0 type parameters + --> $DIR/generics-mismatch.rs:14:12 + | +LL | fn bar(&self) -> impl Sized; + | - expected 0 type parameters +... +LL | fn bar(&self) {} + | ^ found 1 type parameter + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0049`. diff --git a/tests/ui/impl-trait/in-trait/generics-mismatch.rs b/tests/ui/impl-trait/in-trait/generics-mismatch.rs index cc0fc720ebb..9259ca193d1 100644 --- a/tests/ui/impl-trait/in-trait/generics-mismatch.rs +++ b/tests/ui/impl-trait/in-trait/generics-mismatch.rs @@ -1,3 +1,6 @@ +// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty +// revisions: current next + #![feature(return_position_impl_trait_in_trait)] #![allow(incomplete_features)] From ae7fa1d269a3346372562570da3c51625027097b Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 16 Mar 2023 20:41:24 -0300 Subject: [PATCH 2/2] Add generic parameters mismatch test for async in traits --- .../ui/async-await/in-trait/generics-mismatch.rs | 15 +++++++++++++++ .../in-trait/generics-mismatch.stderr | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tests/ui/async-await/in-trait/generics-mismatch.rs create mode 100644 tests/ui/async-await/in-trait/generics-mismatch.stderr diff --git a/tests/ui/async-await/in-trait/generics-mismatch.rs b/tests/ui/async-await/in-trait/generics-mismatch.rs new file mode 100644 index 00000000000..fc29783c0e3 --- /dev/null +++ b/tests/ui/async-await/in-trait/generics-mismatch.rs @@ -0,0 +1,15 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +trait Foo { + async fn foo(); +} + +impl Foo for () { + async fn foo() {} + //~^ ERROR: method `foo` has an incompatible generic parameter for trait `Foo` [E0053] +} + +fn main() {} diff --git a/tests/ui/async-await/in-trait/generics-mismatch.stderr b/tests/ui/async-await/in-trait/generics-mismatch.stderr new file mode 100644 index 00000000000..3518aa05cec --- /dev/null +++ b/tests/ui/async-await/in-trait/generics-mismatch.stderr @@ -0,0 +1,16 @@ +error[E0053]: method `foo` has an incompatible generic parameter for trait `Foo` + --> $DIR/generics-mismatch.rs:11:18 + | +LL | trait Foo { + | --- +LL | async fn foo(); + | - expected type parameter +... +LL | impl Foo for () { + | --------------- +LL | async fn foo() {} + | ^^^^^^^^^^^^^^ found const parameter of type `usize` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0053`.