Skip to content

Invalid just add parentheses suggestion for closure doesn't impl trait #145404

@ijackson

Description

@ijackson

Code

use std::fmt::Display;
struct S;

impl S {
    fn call(&self, _: impl Display) {}
}

fn main() {
    S.call(|| "hello");
}

Current output

error[E0277]: `{closure@src/main.rs:9:12: 9:14}` doesn't implement `std::fmt::Display`
 --> src/main.rs:9:12
  |
9 |     S.call(|| "hello");
  |       ---- ^^^^^^^^^^ the trait `std::fmt::Display` is not implemented for closure `{closure@src/main.rs:9:12: 9:14}`
  |       |
  |       required by a bound introduced by this call
  |
note: required by a bound in `S::call`
 --> src/main.rs:5:28
  |
5 |     fn call(&self, _: impl Display) {}
  |                            ^^^^^^^ required by this bound in `S::call`
help: use parentheses to call this closure
  |
9 |     S.call(|| "hello"());
  |                      ++

Desired output

9 |     S.call((|| "hello")());

Rationale and extra context

The suggestion doesn't compile because it fails to take account of precedence.

Other cases

#144401 may be related?

Rust Version

Rust playground, stable, today.

Anything else?

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=36069edd49c45f097c97f7795867a426

Metadata

Metadata

Assignees

Labels

A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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