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

For const variable declaration, if type is left dangling, also suggest type #100146

Closed
Rageking8 opened this issue Aug 4, 2022 · 8 comments · Fixed by #100168
Closed

For const variable declaration, if type is left dangling, also suggest type #100146

Rageking8 opened this issue Aug 4, 2022 · 8 comments · Fixed by #100168
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

@Rageking8
Copy link
Contributor

Rageking8 commented Aug 4, 2022

Given the following code:

fn main() {
    const b: = 123;
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error: expected type, found `=`
 --> src/main.rs:2:14
  |
2 |     const b: = 123;
  |              ^ expected type

error: could not compile `playground` due to previous error

Since the compiler suggests the type needed for the following:

fn main() {
    const b = 123;
}
   Compiling playground v0.0.1 (/playground)
error: missing type for `const` item
 --> src/main.rs:2:11
  |
2 |     const b = 123;
  |           ^ help: provide a type for the constant: `b: i32`

error: could not compile `playground` due to previous error

The output ideally should also suggest the type if its left dangling.

@Rageking8 Rageking8 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 Aug 4, 2022
@compiler-errors
Copy link
Member

cc @WaffleLapkin who knows how to do this since he worked on that typeof PR i think

@WaffleLapkin
Copy link
Member

@rustbot claim

@WaffleLapkin
Copy link
Member

The output ideally should also suggest the type if its left dangling.

@Rageking8 what you've meant by a type that's left dangling?

@Rageking8
Copy link
Contributor Author

Rageking8 commented Aug 5, 2022

Basically just that the colon is included but no type is specified.
Just like in the first example (where the type is left empty, with a colon), it should also suggest the type to include to make the program compile. (Like the second example which suggests the type to include)

@chenyukang
Copy link
Member

chenyukang commented Aug 5, 2022

This is because if we have colon, the parser will try to parse a type, then error will trigger.
If we don't have colon, parser will try to insert a place hold:

const a = 123
=>
const a: _ = 123

Then type checker will enrich the type messages.

https://github.com/rust-lang/rust/blob/master/compiler/rustc_parse/src/parser/item.rs#L1183

I'm thinking on the solution.

@chenyukang
Copy link
Member

chenyukang commented Aug 5, 2022

@WaffleLapkin
I have a branch in local could fix this issue.
But it also break another testcase with wrong syntax, but I think it's OK since this testcase is far from normal syntax.
Do you mind I send a PR for this?

@WaffleLapkin
Copy link
Member

@chenyukang oh, I've had a fix too, oops 😅

I've opened a PR already: #100168, but feel free to open a PR as well, if you think your approach might be better.

@chenyukang
Copy link
Member

@chenyukang oh, I've had a fix too, oops 😅

I've opened a PR already: #100168, but feel free to open a PR as well, if you think your approach might be better.

My fix is shorter, but seems a little bit hacky 😁
FYI: chenyukang@3440429

@bors bors closed this as completed in e7ed844 Aug 5, 2022
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

Successfully merging a pull request may close this issue.

4 participants