Skip to content

Commit d144915

Browse files
committed
rustfmt: support const block items
1 parent a315d23 commit d144915

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

‎src/tools/rustfmt/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,
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); }
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+
}

‎tests/ui/parser/const-block-items/inner-attr.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#![feature(const_block_items)]
22

3+
// ATTENTION: if we ever start accepting inner attributes here, make sure `rustfmt` can handle them.
4+
// see: https://github.com/rust-lang/rustfmt/issues/6158
5+
36
const {
47
#![expect(unused)] //~ ERROR: an inner attribute is not permitted in this context
58
let a = 1;

‎tests/ui/parser/const-block-items/inner-attr.stderr‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: an inner attribute is not permitted in this context
2-
--> $DIR/inner-attr.rs:4:5
2+
--> $DIR/inner-attr.rs:7:5
33
|
44
LL | #![expect(unused)]
55
| ^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)