Skip to content

Don't point to Fn::call when the arguments passed to a closure are incorrect #98308

Closed
@jyn514

Description

@jyn514

Given the following code:

fn takes_callback(callback: impl Fn(i32)) {
    callback(&0);
}

The current output is dependent on whether you have the rust-src component installed. With it installed:

error[E0308]: mismatched types
  --> src/main.rs:2:14
   |
2  |     callback(&0);
   |     -------- ^^ expected `i32`, found `&{integer}`
   |     |
   |     arguments to this function are incorrect
   |
note: associated function defined here
  --> /home/jnelson/.local/lib/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:77:27
   |
77 |     extern "rust-call" fn call(&self, args: Args)...
   |                           ^^^^
help: consider removing the borrow
   |
2  -     callback(&0);
2  +     callback(0);

without it (e.g. on playground), the same except the note looks very strange because it doesn't actually point anywhere:

note: associated function defined here
help: consider removing the borrow

Ideally the output should look like:

error[E0308]: mismatched types
  --> src/main.rs:2:14
   |
2  |     callback(&0);
   |     -------- ^^ expected `i32`, found `&{integer}`
   |     |
   |     arguments to this function are incorrect
   |
note: function defined here:
  --> src/main.rs:1:x
   |
1 |     fn takes_callback(callback: impl Fn(i32)) {
   |                                ^^^^^^^^^^^^
help: consider removing the borrow
   |
2  -     callback(&0);
2  +     callback(0);

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsP-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions