Tracking Issue for item-level `const` blocks
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
There has been no RFC, afaict. However, t-lang have expressed enthusiasm about seeing this added.
The feature gate for the issue is #![feature(const_block_items)].
Currently, this feature allows you to write const blocks as items in modules. So, it makes the following code possible:
#![feature(const_block_items)]
const {
let a = 2 + 2;
assert!(a != 5)
}
fn main() { }
Which is equivalent to the current stable's:
const _: () = {
let a = 2 + 2;
assert!(a != 5)
};
fn main() { }
About tracking issues
Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Discussion comments will get marked as off-topic or deleted.
Repeated discussions on the tracking issue may lead to the tracking issue getting locked.
See also
- https://github.com/rust-lang/lang-team/issues/251
- Zulip: #t-lang > item-level const blocks
- Zulip: #t-lang > inline static expressions
Steps
- Implementation (see at the very bottom)
- Adjust documentation (see instructions on rustc-dev-guide)
- Style updates for any new syntax (nightly-style-procedure)
- Style team decision on new formatting
- Formatting for new syntax has been added to the Style Guide
- (non-blocking) Formatting has been implemented in
rustfmt
- Stabilization PR (see instructions on rustc-dev-guide)
Unresolved Questions
- RFC needed?
- it has been decided that module-level
const { ... }item is equivalent toconst _: () =, but what about statement-items (i.e. inside function body) or assoc items? - Making
const { ... }sometimes an item and sometimes an expression complicates our story about that.Other possibilities would be things likenobody was too enthusiastic about this approach: see https://github.com/rust-lang/rust/pull/149738const _ = ...;to clarify that it's an item, but shorten the syntax (like one can omit-> ()on functions)
- The diagnostics for module-level const block items are subpar, and could benefit from
- mentioning that the return type of the block is expected to be
() - actually having the phrase "const block item" in messages instead of "malformed
const" - not leaking the dummy
_identifiers that are inserted during AST->HIR lowering ofconst { ... }toconst _: () = const { ... }; - rejecting visibility qualifiers on module-level const block items, since those have no effect (see also: #147136)
- mentioning that the return type of the block is expected to be
- module-level const block items currently reject inner attributes during parsing. Is this the desired behavior?
- maybe use
static { ... }instead ofconst { ... }?
Implementation history
- Module-level
constblocks: #149174 - Ability to omit
: ()forconstitems: #149738
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the const_block_items feature gate, implementation history in #149174 and #149738, and the unresolved questions listed here. Completion requires resolving the design questions, implementing the feature, updating documentation and syntax style, and completing stabilization work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100