Skip to content

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

Open
@jruderman

Description

@jruderman

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions