Skip to content

Commit 794c7fb

Browse files
authored
Rollup merge of #149174 - GrigorenkoPV:const_block_item, r=me,ytmimi
`const` blocks as a `mod` item Tracking issue: rust-lang/rust#149226 This adds support for writing `const { ... }` as an item in a module. In the current implementation, this is a unique AST item that gets lowered to `const _: () = const { ... };` in HIR. rustfmt support included. TODO: - `pub const { ... }` does not make sense (see rust-lang/rust#147136). Reject it. Should this be rejected by the parser or smth? - Improve diagnostics (preferably they should not mention the fake `_` ident).
2 parents 566ae58 + d7923b1 commit 794c7fb

5 files changed

Lines changed: 73 additions & 5 deletions

File tree

src/parse/macros/cfg_if.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::panic::{AssertUnwindSafe, catch_unwind};
33
use rustc_ast::ast;
44
use rustc_ast::token::TokenKind;
55
use rustc_parse::exp;
6-
use rustc_parse::parser::ForceCollect;
6+
use rustc_parse::parser::{AllowConstBlockItems, ForceCollect};
77
use rustc_span::symbol::kw;
88

99
use crate::parse::macros::build_stream_parser;
@@ -61,7 +61,7 @@ fn parse_cfg_if_inner<'a>(
6161
}
6262

6363
while parser.token != TokenKind::CloseBrace && parser.token.kind != TokenKind::Eof {
64-
let item = match parser.parse_item(ForceCollect::No) {
64+
let item = match parser.parse_item(ForceCollect::No, AllowConstBlockItems::Yes) {
6565
Ok(Some(item_ptr)) => *item_ptr,
6666
Ok(None) => continue,
6767
Err(err) => {

src/parse/macros/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_ast::ast;
22
use rustc_ast::token::{Delimiter, NonterminalKind, NtExprKind::*, NtPatKind::*, TokenKind};
33
use rustc_ast::tokenstream::TokenStream;
44
use rustc_parse::MACRO_ARGUMENTS;
5-
use rustc_parse::parser::{ForceCollect, Parser, Recovery};
5+
use rustc_parse::parser::{AllowConstBlockItems, ForceCollect, Parser, Recovery};
66
use rustc_session::parse::ParseSess;
77
use rustc_span::symbol;
88

@@ -67,7 +67,7 @@ fn parse_macro_arg<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
6767
parse_macro_arg!(
6868
Item,
6969
NonterminalKind::Item,
70-
|parser: &mut Parser<'b>| parser.parse_item(ForceCollect::No),
70+
|parser: &mut Parser<'b>| parser.parse_item(ForceCollect::No, AllowConstBlockItems::Yes),
7171
|x: Option<Box<ast::Item>>| x
7272
);
7373

src/visitor.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use rustc_span::{BytePos, Ident, Pos, Span, symbol};
77
use tracing::debug;
88

99
use crate::attr::*;
10-
use crate::comment::{CodeCharKind, CommentCodeSlices, contains_comment, rewrite_comment};
10+
use crate::comment::{
11+
CodeCharKind, CommentCodeSlices, contains_comment, recover_comment_removed, rewrite_comment,
12+
};
1113
use crate::config::{BraceStyle, Config, MacroSelector, StyleEdition};
1214
use crate::coverage::transform_missing_snippet;
1315
use crate::items::{
@@ -533,6 +535,28 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
533535
ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => {
534536
self.visit_static(&StaticParts::from_item(item));
535537
}
538+
ast::ItemKind::ConstBlock(ast::ConstBlockItem {
539+
id: _,
540+
span,
541+
ref block,
542+
}) => {
543+
let context = &self.get_context();
544+
let offset = self.block_indent;
545+
self.push_rewrite(
546+
item.span,
547+
block
548+
.rewrite(
549+
context,
550+
Shape::legacy(
551+
context.budget(offset.block_indent),
552+
offset.block_only(),
553+
),
554+
)
555+
.map(|rhs| {
556+
recover_comment_removed(format!("const {rhs}"), span, context)
557+
}),
558+
);
559+
}
536560
ast::ItemKind::Fn(ref fn_kind) => {
537561
let ast::Fn {
538562
defaultness,

tests/source/const-block-items.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![feature(const_block_items)]
2+
3+
const {
4+
5+
6+
assert!(true)
7+
}
8+
9+
#[cfg(false)] const { assert!(false) }
10+
11+
12+
#[cfg(false)]
13+
// foo
14+
const
15+
16+
{
17+
// bar
18+
assert!(false)
19+
// baz
20+
} // 123
21+
22+
23+
#[expect(unused)]
24+
pub const { let a = 1; assert!(true); }

tests/target/const-block-items.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(const_block_items)]
2+
3+
const { assert!(true) }
4+
5+
#[cfg(false)]
6+
const { assert!(false) }
7+
8+
#[cfg(false)]
9+
// foo
10+
const {
11+
// bar
12+
assert!(false)
13+
// baz
14+
} // 123
15+
16+
#[expect(unused)]
17+
const {
18+
let a = 1;
19+
assert!(true);
20+
}

0 commit comments

Comments
 (0)