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

Invalid suggestion to add dyn to something that is not a trait #139174

Closed
pacak opened this issue Mar 31, 2025 · 1 comment · Fixed by #139200
Closed

Invalid suggestion to add dyn to something that is not a trait #139174

pacak opened this issue Mar 31, 2025 · 1 comment · Fixed by #139200
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-objects Area: trait objects, vtable layout D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pacak
Copy link
Contributor

pacak commented Mar 31, 2025

Code

fn derp<'a>(x: Box<dyn Fn() -> Option<usize + 'a>>) -> usize {
    x()
}

Current output

Checking foo v0.1.0 (/home/pacak/tmp/foo)
error[E0404]: expected trait, found builtin type `usize`
 --> src/lib.rs:1:39
  |
1 | fn derp<'a>(x: Box<dyn Fn() -> Option<usize + 'a>>) -> usize {
  |                                       ^^^^^ not a trait

error[E0782]: expected a type, found a trait
 --> src/lib.rs:1:39
  |
1 | fn derp<'a>(x: Box<dyn Fn() -> Option<usize + 'a>>) -> usize {
  |                                       ^^^^^^^^^^
  |
help: you can add the `dyn` keyword if you want a trait object
  |
1 | fn derp<'a>(x: Box<dyn Fn() -> Option<dyn usize + 'a>>) -> usize {
  |                                       +++

Some errors have detailed explanations: E0404, E0782.
For more information about an error, try `rustc --explain E0404`.
error: could not compile `foo` (lib) due to 2 previous errors

Desired output

Not suggesting dyn would be a good start, suggesting to move > to the left would be even better

Rationale and extra context

Let's start with some valid code (lifetime is redundant here, but makes sense in my full code).

fn derp<'a>(x: Box<dyn Fn() -> usize + 'a>) -> usize {
    x(); todo!();
}

And change it to the value produced is Optional:

This works:

fn derp<'a>(x: Box<dyn Fn() -> Optional<usize> + 'a>) -> usize {
    x(); todo!();
}

But imagine making a typo...

fn derp<'a>(x: Box<dyn Fn() -> Optional<usize + 'a>>) -> usize {
    x(); todo!();
}

Error message is confusing and suggestion is wrong.

Other cases

Nightly suggests to add a parameter for a closure that takes no parameters... In addition to suggesting to adding dyn

error[E0057]: this function takes 1 argument but 0 arguments were supplied
 --> src/lib.rs:2:5
  |
2 |     x()
  |     ^-- argument #1 is missing
  |
note: implementation defined here
 --> /rustc/f8c27dfe1a2e7fb538fd91dad53de06992c7c967/library/alloc/src/boxed.rs:1978:1
help: provide the argument
  |
2 |     x(/* value */)
  |       +++++++++++

Rust Version

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.1.7

Anything else?

No response

@pacak pacak added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 31, 2025
@fmease fmease added D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. A-trait-objects Area: trait objects, vtable layout labels Mar 31, 2025
@xizheyin
Copy link
Contributor

xizheyin commented Apr 1, 2025

@rustbot claim

@bors bors closed this as completed in 2d43a8b Apr 2, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 2, 2025
Rollup merge of rust-lang#139200 - xizheyin:issue-139174, r=compiler-errors

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

Fixes rust-lang#139174
When `poly_trait_ref` is not a real trait, we should stop suggesting `impl` and `dyn` to avoid false positives. 3 cases were added to the ui test.
https://github.com/rust-lang/rust/blob/0b45675cfcec57f30a3794e1a1e18423aa9cf200/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs#L88-L93

In the first commit, I submitted the test and passed it. In the second commit, I modified the code and we can see the changes in the test.

r? compiler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-objects Area: trait objects, vtable layout D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants