Skip to content

Confusing diagnostic when argument type in impl FnOnce() is not found #113566

Closed
@GoldsteinE

Description

@GoldsteinE

Code

type Foo = Bar;

fn check(f: impl FnOnce(Foo), val: Foo) {
    f(val);
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0412]: cannot find type `Bar` in this scope
 --> src/lib.rs:1:12
  |
1 | type Foo = Bar;
  |            ^^^ not found in this scope

error[E0618]: expected function, found `impl FnOnce(Foo)`
 --> src/lib.rs:4:5
  |
3 | fn check(f: impl FnOnce(Foo), val: Foo) {
  |          - `f` has type `impl FnOnce(Foo)`
4 |     f(val);
  |     ^-----
  |     |
  |     call expression requires function

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

Desired output

Compiling playground v0.0.1 (/playground)
error[E0412]: cannot find type `Bar` in this scope
 --> src/lib.rs:1:12
  |
1 | type Foo = Bar;
  |            ^^^ not found in this scope


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

Rationale and extra context

Saying that impl FnOnce() is not a function is confusing and distracts from the real issue. I think compiler should either report nothing on the line with the call or say something about unknown types in function signature.

Other cases

type Foo = Bar;

// Doesn’t exhibit this issue
fn check(f: impl FnOnce() -> Foo) {
    f();
}

Anything else?

No response

Metadata

Metadata

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