Skip to content

E0277 shows invalid help in closures #111932

Closed
@MariaSolOs

Description

@MariaSolOs

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)

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions