Skip to content

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

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

Open
jruderman opened this issue Oct 17, 2022 · 0 comments
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

@jruderman
Copy link
Contributor

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 jruderman 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 Oct 17, 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

No branches or pull requests

1 participant