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

Nested argument mismatch could suggest moving extra parameter outside parenthesis #124691

Open
fee1-dead opened this issue May 4, 2024 · 3 comments
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

@fee1-dead
Copy link
Member

fee1-dead commented May 4, 2024

Code

playground

fn mul(a: i32, b: i32) -> i32 {
    a * b
}

fn main() {
    let x = mul(4.min(3, 14));
    println!("{x}");
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0061]: this method takes 1 argument but 2 arguments were supplied
   --> src/main.rs:6:19
    |
6   |     let x = mul(4.min(3, 14));
    |                   ^^^  ----
    |                        | |
    |                        | unexpected argument of type `{integer}`
    |                        help: remove the extra argument
    |
note: method defined here
   --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cmp.rs:877:8
    |
877 |     fn min(self, other: Self) -> Self
    |        ^^^

error[E0061]: this function takes 2 arguments but 1 argument was supplied
 --> src/main.rs:6:13
  |
6 |     let x = mul(4.min(3, 14));
  |             ^^^-------------- an argument of type `i32` is missing
  |
note: function defined here
 --> src/main.rs:1:4
  |
1 | fn mul(a: i32, b: i32) -> i32 {
  |    ^^^ ------  ------
help: provide the argument
  |
6 |     let x = mul(4.min(3, 14), /* i32 */);
  |                ~~~~~~~~~~~~~~~~~~~~~~~~~

For more information about this error, try `rustc --explain E0061`.
error: could not compile `playground` (bin "playground") due to 2 previous errors

Desired output

Something like

6   |     let x = mul(4.min(3), 14);
    |                   ^^^  ~~~~~
    |                        |
    |                        |
    |                        help: move the argument outside the parenthesis

Rationale and extra context

This happened while trying to pass a missing parameter to an outer function, but I had placed the extra parameter to a call inside instead. When both of the conditions are met, we can be mostly sure that the user intended to pass the parameter to the outer function instead:

  • The inner function/method expects at least one argument and the user passed in an extra one
  • The function call is the last passed in argument to the outer function, but outer function expects one additional argument.
  • The type matches (not sure if possible to check that, but that would make it 100% sure that this is what the user intended)

Other cases

No response

Rust Version

Latest nightly

Anything else?

@fee1-dead fee1-dead 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 4, 2024
@long-long-float
Copy link
Contributor

@rustbot claim

@long-long-float
Copy link
Contributor

I will work on this issue for only one argument first.

@long-long-float
Copy link
Contributor

After investigation the codebase, I think it is hard to achieve this in current structure because I couldn't find the way to get the argument information (expected and actual arguments and their types) without any subeffects.

A suggestion help: provide the argument is emitted in the method report_arg_errors. I planned to emit the subject suggestion in this method, however there are one problem. In the method report_arg_errors, I wanted to get the expected and actual arguments in the inner call if an argument is method call (or function). However I couldn't find the method to get the information without any subeffects. For example, check_argument_types seems to be good method to get, but this method call self.set_tainted_by_errors that has subeffects.

Another method is to copy the related code from check_argument_types method, but it's dangerous to me without detailed knowledge of the method.

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

No branches or pull requests

2 participants