Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip suggest impl or dyn when poly trait is not a real trait #139200

Merged
merged 2 commits into from
Apr 2, 2025
Merged
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
1 change: 1 addition & 0 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
24 changes: 24 additions & 0 deletions tests/ui/traits/object/suggestion-trait-object-issue-139174.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//@compile-flags: --edition 2021

fn f<'a>(x: Box<dyn Fn() -> Option<usize + 'a>>) -> 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() {

}
40 changes: 40 additions & 0 deletions tests/ui/traits/object/suggestion-trait-object-issue-139174.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
error[E0404]: expected trait, found builtin type `usize`
--> $DIR/suggestion-trait-object-issue-139174.rs:3:36
|
LL | fn f<'a>(x: Box<dyn Fn() -> Option<usize + 'a>>) -> 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<dyn Fn() -> Option<usize + 'a>>) -> 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 {
| ^^^^^^^^^^

error[E0782]: expected a type, found a trait
--> $DIR/suggestion-trait-object-issue-139174.rs:16:8
|
LL | x: 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`.
Loading