rust icon indicating copy to clipboard operation
rust copied to clipboard

Divergent suggestion "expressions must be enclosed in braces to be used as const generic arguments"

Open jruderman opened this issue 3 years ago • 0 comments

I noticed this while trying to understand #103143.

Given the following code (playground):

fn main() {
    x::< #[a] (1 + 2) >
}

The current output starts with:

error: invalid const generic expression
 --> src/main.rs:2:15
  |
2 |     x::< #[a] (1 + 2) >
  |               ^^^^^^^
  |
help: expressions must be enclosed in braces to be used as const generic arguments
  |
2 |     x::< #[a] { (1 + 2) } >
  |               +         +

Why this doesn't work

Adding the braces as suggested:

fn main() {
    x::< #[a] { (1 + 2) } >
}

doesn't fix the problem, and the compiler continues suggesting to add more braces indefinitely:

error: invalid const generic expression
 --> src/main.rs:2:15
  |
2 |     x::< #[a] { (1 + 2) } >
  |               ^^^^^^^^^^^
  |
help: expressions must be enclosed in braces to be used as const generic arguments
  |
2 |     x::< #[a] { { (1 + 2) } } >
  |               +             +

Possible alternative

The suggestion could place the opening brace before the #[a], i.e.:

fn main() {
    x::< { #[a] (1 + 2) } >
}

This at least leads to a different error message:

error[E0658]: attributes on expressions are experimental
 --> src/main.rs:2:12
  |
2 |     x::< { #[a] (1 + 2) } >
  |            ^^^^
  |
  = note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
  = help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable

jruderman avatar Oct 17 '22 10:10 jruderman