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

E0277 shows invalid help in closures #111932

Closed
MariaSolOs opened this issue May 25, 2023 · 1 comment · Fixed by #112325
Closed

E0277 shows invalid help in closures #111932

MariaSolOs opened this issue May 25, 2023 · 1 comment · Fixed by #112325
Assignees
Labels
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.

Comments

@MariaSolOs
Copy link
Contributor

MariaSolOs commented May 25, 2023

Code

trait Foo {}

fn print_foos(foos: impl Iterator<Item = dyn Foo>) {
    foos.for_each(|foo| {
        println("{:?}", foo);
    });
}

Current output

error[E0277]: the size for values of type `(dyn Foo + 'static)` cannot be known at compilation time
 --> crates/shebling-lexer/src/tests.rs:4:17
  |
4 |     v.for_each(|foo| {
  |                 ^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn Foo + 'static)`
  = help: unsized fn params are gated as an unstable feature
help: you can use `impl Trait` as the argument type
  |
4 |     v.for_each(|impl foo| {
  |                 ++++
help: function arguments must have a statically known size, borrowed types always have a known size
  |
4 |     v.for_each(|&foo| {
  |                 +

Notice how the last 2 suggestions aren't valid in such a closure:

error[E0593]: closure is expected to take 1 argument, but it takes 2 arguments
 --> crates/shebling-lexer/src/tests.rs:4:7
  |
4 |     v.for_each(|impl foo| {
  |       ^^^^^^^^ ---------- takes 2 arguments
  |       |
  |       expected closure that takes 1 argument
error[E0308]: mismatched types
 --> crates/shebling-lexer/src/tests.rs:4:17
  |
4 |     v.for_each(|&foo| {
  |                 ^---
  |                 ||
  |                 |expected due to this
  |                 expected `dyn Foo`, found `&_`
  |
  = note: expected trait object `(dyn Foo + 'static)`
                found reference `&_`
help: consider removing `&` from the pattern
  |
4 -     v.for_each(|&foo| {
4 +     v.for_each(|foo| {
  |

Desired output

The suggestions given in help should help me get closer to fixing the error, or at least not have invalid syntax.

Rationale and extra context

No response

Other cases

No response

Anything else?

Output of rustc -V: rustc 1.71.0-nightly (2c41369ac 2023-05-13)

@MariaSolOs MariaSolOs 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 May 25, 2023
@jieyouxu
Copy link
Member

(if println! is used in the snippet, #112007 comes up again with leaking implementation details)

note: required by a bound in `core::fmt::ArgumentV1::<'a>::new_debug`
   --> /home/jieyouxu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:353:5
    |
353 |     arg_new!(new_debug, Debug);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `ArgumentV1::<'a>::new_debug`
    = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `arg_new` (in Nightly builds, run with -Z macro-backtrace for more info)

@notriddle notriddle self-assigned this Jun 5, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 6, 2023
…r=compiler-errors

diagnostics: do not suggest type name tweaks on type-inferred closure args

Fixes rust-lang#111932
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 6, 2023
…r=compiler-errors

diagnostics: do not suggest type name tweaks on type-inferred closure args

Fixes rust-lang#111932
@bors bors closed this as completed in 467bc9f Jun 6, 2023
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 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