-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
const { ... }
blocks cannot be written outside of function
#128338
Comments
Thanks for the report! However, there is no bug here. This is expected. If you want to just declare a constant outside a function for the purpose of checking an assertion, you can declare an anonymous const item as follows:
|
The diagnostic should say that, then. @rustbot label: -C-discussion +C-bug +A-diagnostics +D-papercut +D-terse |
It would be nice if this was allowed if the const block evaluates to |
while a nice wishlist idea, given that we don't even have a finished story for note that it also would be somewhat unusual for us to start accepting any expression in item context, as normally Rust is quite the hardass about misplacing expressions. for instance, not only is {
const NAME: T = expr;
NAME
} but this is rejected in item context, because blocks are expressions, not items: {
} and part of the reason rustc is persnickety about this sort of thing is that it allows us to be really certain what is supposed to go where, and from there easily infer what was likely intended, and then issue diagnostics. |
I tried this code:
I expected to see this happen: it compiles
Instead, this happened: it doesn't compile
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: