From 27b866d59a3e5ccbaf9723a37d353db6d2079f16 Mon Sep 17 00:00:00 2001 From: xizheyin Date: Tue, 1 Apr 2025 13:22:45 +0800 Subject: [PATCH 1/2] Add ui test ui/traits/object/suggestion-trait-object-issue-139174.rs Signed-off-by: xizheyin --- .../suggestion-trait-object-issue-139174.rs | 24 ++++++++ ...uggestion-trait-object-issue-139174.stderr | 55 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 tests/ui/traits/object/suggestion-trait-object-issue-139174.rs create mode 100644 tests/ui/traits/object/suggestion-trait-object-issue-139174.stderr diff --git a/tests/ui/traits/object/suggestion-trait-object-issue-139174.rs b/tests/ui/traits/object/suggestion-trait-object-issue-139174.rs new file mode 100644 index 0000000000000..f8fa410b7d495 --- /dev/null +++ b/tests/ui/traits/object/suggestion-trait-object-issue-139174.rs @@ -0,0 +1,24 @@ +//@compile-flags: --edition 2021 + +fn f<'a>(x: Box Option>) -> usize { + //~^ ERROR expected trait, found builtin type `usize` + //~| ERROR expected a type, found a trait [E0782] + 0 +} + +fn create_adder<'a>(x: i32) -> usize + 'a { + //~^ ERROR expected trait, found builtin type `usize` + //~| ERROR expected a type, found a trait [E0782] + move |y| x + y +} + +struct Struct<'a>{ + x: usize + 'a, + //~^ ERROR expected trait, found builtin type `usize` + //~| ERROR expected a type, found a trait [E0782] +} + + +fn main() { + +} diff --git a/tests/ui/traits/object/suggestion-trait-object-issue-139174.stderr b/tests/ui/traits/object/suggestion-trait-object-issue-139174.stderr new file mode 100644 index 0000000000000..ddee0d5586b07 --- /dev/null +++ b/tests/ui/traits/object/suggestion-trait-object-issue-139174.stderr @@ -0,0 +1,55 @@ +error[E0404]: expected trait, found builtin type `usize` + --> $DIR/suggestion-trait-object-issue-139174.rs:3:36 + | +LL | fn f<'a>(x: Box Option>) -> usize { + | ^^^^^ not a trait + +error[E0404]: expected trait, found builtin type `usize` + --> $DIR/suggestion-trait-object-issue-139174.rs:9:32 + | +LL | fn create_adder<'a>(x: i32) -> usize + 'a { + | ^^^^^ not a trait + +error[E0404]: expected trait, found builtin type `usize` + --> $DIR/suggestion-trait-object-issue-139174.rs:16:8 + | +LL | x: usize + 'a, + | ^^^^^ not a trait + +error[E0782]: expected a type, found a trait + --> $DIR/suggestion-trait-object-issue-139174.rs:3:36 + | +LL | fn f<'a>(x: Box Option>) -> usize { + | ^^^^^^^^^^ + | +help: you can add the `dyn` keyword if you want a trait object + | +LL | fn f<'a>(x: Box Option>) -> usize { + | +++ + +error[E0782]: expected a type, found a trait + --> $DIR/suggestion-trait-object-issue-139174.rs:9:32 + | +LL | fn create_adder<'a>(x: i32) -> usize + 'a { + | ^^^^^^^^^^ + | +help: `usize + 'a` is dyn-incompatible, use `impl usize + 'a` to return an opaque type, as long as you return a single underlying type + | +LL | fn create_adder<'a>(x: i32) -> impl usize + 'a { + | ++++ + +error[E0782]: expected a type, found a trait + --> $DIR/suggestion-trait-object-issue-139174.rs:16:8 + | +LL | x: usize + 'a, + | ^^^^^^^^^^ + | +help: you can add the `dyn` keyword if you want a trait object + | +LL | x: dyn usize + 'a, + | +++ + +error: aborting due to 6 previous errors + +Some errors have detailed explanations: E0404, E0782. +For more information about an error, try `rustc --explain E0404`. From 12604fa071b01f7342520224507e43e432294c2c Mon Sep 17 00:00:00 2001 From: xizheyin Date: Tue, 1 Apr 2025 14:19:40 +0800 Subject: [PATCH 2/2] Skip suggest impl or dyn when poly trait is not a real trait Signed-off-by: xizheyin --- .../src/hir_ty_lowering/lint.rs | 1 + .../suggestion-trait-object-issue-139174.stderr | 15 --------------- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs index 5588631228441..8e62dce21913b 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs @@ -86,6 +86,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { "expected a type, found a trait" ); if self_ty.span.can_be_used_for_suggestions() + && poly_trait_ref.trait_ref.trait_def_id().is_some() && !self.maybe_suggest_impl_trait(self_ty, &mut diag) && !self.maybe_suggest_dyn_trait(self_ty, sugg, &mut diag) { diff --git a/tests/ui/traits/object/suggestion-trait-object-issue-139174.stderr b/tests/ui/traits/object/suggestion-trait-object-issue-139174.stderr index ddee0d5586b07..0d1ce85fc2888 100644 --- a/tests/ui/traits/object/suggestion-trait-object-issue-139174.stderr +++ b/tests/ui/traits/object/suggestion-trait-object-issue-139174.stderr @@ -21,33 +21,18 @@ error[E0782]: expected a type, found a trait | LL | fn f<'a>(x: Box Option>) -> usize { | ^^^^^^^^^^ - | -help: you can add the `dyn` keyword if you want a trait object - | -LL | fn f<'a>(x: Box Option>) -> usize { - | +++ error[E0782]: expected a type, found a trait --> $DIR/suggestion-trait-object-issue-139174.rs:9:32 | LL | fn create_adder<'a>(x: i32) -> usize + 'a { | ^^^^^^^^^^ - | -help: `usize + 'a` is dyn-incompatible, use `impl usize + 'a` to return an opaque type, as long as you return a single underlying type - | -LL | fn create_adder<'a>(x: i32) -> impl usize + 'a { - | ++++ error[E0782]: expected a type, found a trait --> $DIR/suggestion-trait-object-issue-139174.rs:16:8 | LL | x: usize + 'a, | ^^^^^^^^^^ - | -help: you can add the `dyn` keyword if you want a trait object - | -LL | x: dyn usize + 'a, - | +++ error: aborting due to 6 previous errors