rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.33k stars 12.59k forks source link

Improve error for missing braces in const block #78168

Open camelid opened 3 years ago

camelid commented 3 years ago

Someone may try to write code like this (playground):

#![allow(incomplete_features)]
#![feature(inline_const)]

fn foo() {
    let x = 2;
    match x {
        const 2 => {}
    }
}

Yet this produces these misleading errors:

   Compiling playground v0.0.1 (/playground)
error: expected identifier, found keyword `const`
 --> src/lib.rs:7:9
  |
7 |         const 2 => {}
  |         ^^^^^ expected identifier, found keyword

error: expected one of `=>`, `@`, `if`, or `|`, found `2`
 --> src/lib.rs:7:15
  |
7 |         const 2 => {}
  |               ^ expected one of `=>`, `@`, `if`, or `|`

error: aborting due to 2 previous errors

error: could not compile `playground`

To learn more, run the command again with --verbose.

Another example (playground):

#![allow(incomplete_features)]
#![feature(inline_const)]

fn foo() {
    let x = const 2;
}

Error:

   Compiling playground v0.0.1 (/playground)
error: expected expression, found keyword `const`
 --> src/lib.rs:5:13
  |
5 |     let x = const 2;
  |             ^^^^^ expected expression

error: aborting due to previous error

error: could not compile `playground`

To learn more, run the command again with --verbose.
camelid commented 3 years ago

I would like to work on this.

spastorino commented 3 years ago

@camelid :+1:, would be happy to review

GrigorenkoPV commented 1 week ago

@rustbot claim