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

Arity errors suggest incorrect fix when there are name errors #126246

Closed
Wilfred opened this issue Jun 10, 2024 · 0 comments · Fixed by #127253
Closed

Arity errors suggest incorrect fix when there are name errors #126246

Wilfred opened this issue Jun 10, 2024 · 0 comments · Fixed by #127253
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Wilfred
Copy link
Contributor

Wilfred commented Jun 10, 2024

Code

fn add_one(x: i32) -> i32 {
    x + 1
}

fn main() {
    add_one(no_such_local, 10);
}

Current output

error[E0425]: cannot find value `no_such_local` in this scope
 --> src/main.rs:6:13
  |
6 |     add_one(no_such_local, 10);
  |             ^^^^^^^^^^^^^ not found in this scope

error[E0061]: this function takes 1 argument but 2 arguments were supplied
 --> src/main.rs:6:5
  |
6 |     add_one(no_such_local, 10);
  |     ^^^^^^^              ----
  |                          | |
  |                          | unexpected argument of type `{integer}`
  |                          help: remove the extra argument
  |
note: function defined here
 --> src/main.rs:1:4
  |
1 | fn add_one(x: i32) -> i32 {
  |    ^^^^^^^ ------

Some errors have detailed explanations: E0061, E0425.
For more information about an error, try `rustc --explain E0061`.

Desired output

error[E0425]: cannot find value `no_such_local` in this scope
 --> src/main.rs:6:13
  |
6 |     add_one(no_such_local, 10);
  |             ^^^^^^^^^^^^^ not found in this scope

error[E0061]: this function takes 1 argument but 2 arguments were supplied
 --> src/main.rs:6:5
  |
6 |     add_one(no_such_local, 10);
  |     ^^^^^^^ ---------------
  |             | |
  |             | unexpected argument
  |             help: remove the extra argument
  |
note: function defined here
 --> src/main.rs:1:4
  |
1 | fn add_one(x: i32) -> i32 {
  |    ^^^^^^^ ------

Some errors have detailed explanations: E0061, E0425.
For more information about an error, try `rustc --explain E0061`.

Rationale and extra context

It's surprising that rustc suggests removing an argument whose type matches the expected type, in favour of a value that doesn't exist.

I encountered this when editing a struct and its methods to remove a field, where self.foo(self.old_field, argument) incorrectly suggested removing argument when foo no longer took two arguments.

Other cases

No response

Rust Version

$ rustc --version --verbose
rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: aarch64-apple-darwin
release: 1.78.0
LLVM version: 18.1.2

Anything else?

No response

@Wilfred Wilfred 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 Jun 10, 2024
@jieyouxu jieyouxu added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Jun 11, 2024
@chenyukang chenyukang self-assigned this Jul 1, 2024
@bors bors closed this as completed in 6031309 Jul 4, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jul 4, 2024
Rollup merge of rust-lang#127253 - chenyukang:yukang-fix-126246-fn-parameters-check, r=estebank

Fix incorrect suggestion for extra argument with a type error

Fixes rust-lang#126246

I tried to fix it in the `find_errors` of ArgMatrix, but seems it's hard to avoid breaking some other test cases.

The root cause is we eliminate the first argument even with a type error at here:
https://github.com/rust-lang/rust/blob/6292b2af620dbd771ebb687c3a93c69ba8f97268/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs#L664

So the left argument is always treated as extra one.

But if there is already a type error, an error message will be generated firstly, which make this issue a trivial one.
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants