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

Suggest the correct name if _ is used in a struct literal #98282

Open
scottmcm opened this issue Jun 20, 2022 · 3 comments · May be fixed by #122288
Open

Suggest the correct name if _ is used in a struct literal #98282

scottmcm opened this issue Jun 20, 2022 · 3 comments · May be fixed by #122288
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference A-parser Area: The parsing of Rust source code to an AST A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
Member

scottmcm commented Jun 20, 2022

Given the following code:

mod blah {
    pub struct Stuff { x: i32 }
    pub fn do_stuff(_: Stuff) {}
}

fn main() {
    blah::do_stuff(_ { x: 10 });
}

The current output is:

error: struct literal body without path
 --> src/main.rs:7:22
  |
7 |     blah::do_stuff(_ { x: 10 });
  |                      ^^^^^^^^^
  |
help: you might have forgotten to add the struct literal inside the block
  |
7 |     blah::do_stuff(_ { SomeStruct { x: 10 } });
  |                      ++++++++++++           +

error: expected one of `)`, `,`, `.`, `?`, or an operator, found `{`
 --> src/main.rs:7:22
  |
7 |     blah::do_stuff(_ { x: 10 });
  |                     -^ expected one of `)`, `,`, `.`, `?`, or an operator
  |                     |
  |                     help: missing `,`

warning: unnecessary braces around function argument
 --> src/main.rs:7:22
  |
7 |     blah::do_stuff(_ { x: 10 });
  |                      ^        ^
  |
  = note: `#[warn(unused_braces)]` on by default
help: remove these braces
  |
7 -     blah::do_stuff(_ { x: 10 });
7 +     blah::do_stuff(_ { x: 10 });
  |

error: in expressions, `_` can only be used on the left-hand side of an assignment
 --> src/main.rs:7:20
  |
7 |     blah::do_stuff(_ { x: 10 });
  |                    ^ `_` not allowed here

error[[E0061]](https://doc.rust-lang.org/nightly/error-index.html#E0061): this function takes 1 argument but 2 arguments were supplied
 --> src/main.rs:7:5
  |
7 |     blah::do_stuff(_ { x: 10 });
  |     ^^^^^^^^^^^^^^   --------- argument unexpected
  |
note: function defined here
 --> src/main.rs:3:12
  |
3 |     pub fn do_stuff(_: Stuff) {}
  |            ^^^^^^^^ --------
help: remove the extra argument
  |
7 |     blah::do_stuff(_);
  |     ~~~~~~~~~~~~~~~~~

Ideally the output should look like:

error[E0121]: the placeholder `_` is not allowed in struct literals
 --> src/main.rs:5:22
  |
9 |     blah::do_stuff(_ { x: 10 });
  |                    ^
  |                    |
  |                    not allowed in struct literals
  |                    help: replace with the correct struct type: `blah::Stuff`

Like how rustc will suggest the correct type if you use -> _ in a function definition.

(Opened after this thread: https://internals.rust-lang.org/t/enum-path-inference-with-variant-syntax/16851/4?u=scottmcm )

@scottmcm scottmcm 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 20, 2022
@estebank estebank added A-parser Area: The parsing of Rust source code to an AST A-inference Area: Type inference A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. labels Jun 20, 2022
@TaKO8Ki TaKO8Ki self-assigned this Jun 21, 2022
@abs0luty
Copy link

abs0luty commented Dec 4, 2023

@rustbot claim

@rustbot rustbot assigned abs0luty and unassigned TaKO8Ki Dec 4, 2023
@fmease
Copy link
Member

fmease commented Feb 29, 2024

We'd somehow need to stash the parse error without accidentally accepting this malformed input syntactically (relevant for #[cfg]'ed-out code) and steal it during type checking where we should have the necessary information available in theory. However, this looks non-trivial to fix from an architectural standpoint.

@estebank
Copy link
Contributor

estebank commented Dec 4, 2024

Current output is at least less verbose:

error: expected one of `)`, `,`, `.`, `?`, or an operator, found `{`
 --> src/main.rs:7:22
  |
7 |     blah::do_stuff(_ { x: 10 });
  |                      ^ expected one of `)`, `,`, `.`, `?`, or an operator

@estebank estebank removed the D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. label Dec 4, 2024
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-inference Area: Type inference A-parser Area: The parsing of Rust source code to an AST A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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.

5 participants