Skip to content

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

Closed

Description

@Rageking8

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.

Activity

  1. added
    A-diagnostics
    T-compiler
    on Aug 4, 2022
  2. compiler-errors commented on Aug 4, 2022

    @compiler-errors
    Contributor

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

  3. WaffleLapkin commented on Aug 4, 2022

    @WaffleLapkin
    Member

    @rustbot claim

  4. WaffleLapkin commented on Aug 5, 2022

    @WaffleLapkin
    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?

  5. Rageking8 commented on Aug 5, 2022

    @Rageking8
    ContributorAuthor

    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)

  6. chenyukang commented on Aug 5, 2022

    @chenyukang
    Member

    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.

  7. chenyukang commented on Aug 5, 2022

    @chenyukang
    Member

    @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?

  8. WaffleLapkin commented on Aug 5, 2022

    @WaffleLapkin
    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.

  9. chenyukang commented on Aug 5, 2022

    @chenyukang
    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

  10. added a commit that references this issue on Aug 5, 2022
    e7ed844
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsT-compiler

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions