Skip to content

Commit 22d5546

Browse files
authoredApr 16, 2022
Rollup merge of rust-lang#94985 - dtolnay:constattr, r=pnkfelix
Parse inner attributes on inline const block According to rust-lang#84414 (comment), inner attributes are intended to be supported *"in all containers for statements (or some subset of statements)"*. This PR adds inner attribute parsing and pretty-printing for inline const blocks (rust-lang#76001), which contain statements just like an unsafe block or a loop body. ```rust let _ = const { #![allow(...)] let x = (); x }; ```
2 parents d9b3ff7 + f427698 commit 22d5546

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed
 

‎compiler/rustc_ast_pretty/src/pprust/state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ impl<'a> State<'a> {
959959
self.word_space("=");
960960
match term {
961961
Term::Ty(ty) => self.print_type(ty),
962-
Term::Const(c) => self.print_expr_anon_const(c),
962+
Term::Const(c) => self.print_expr_anon_const(c, &[]),
963963
}
964964
}
965965
ast::AssocConstraintKind::Bound { bounds } => self.print_type_bounds(":", &*bounds),

‎compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,21 @@ impl<'a> State<'a> {
8888
self.end();
8989
}
9090

91-
pub(super) fn print_expr_anon_const(&mut self, expr: &ast::AnonConst) {
91+
pub(super) fn print_expr_anon_const(
92+
&mut self,
93+
expr: &ast::AnonConst,
94+
attrs: &[ast::Attribute],
95+
) {
9296
self.ibox(INDENT_UNIT);
9397
self.word("const");
94-
self.print_expr(&expr.value);
98+
self.nbsp();
99+
if let ast::ExprKind::Block(block, None) = &expr.value.kind {
100+
self.cbox(0);
101+
self.ibox(0);
102+
self.print_block_with_attrs(block, attrs);
103+
} else {
104+
self.print_expr(&expr.value);
105+
}
95106
self.end();
96107
}
97108

@@ -275,7 +286,7 @@ impl<'a> State<'a> {
275286
self.print_expr_vec(exprs);
276287
}
277288
ast::ExprKind::ConstBlock(ref anon_const) => {
278-
self.print_expr_anon_const(anon_const);
289+
self.print_expr_anon_const(anon_const, attrs);
279290
}
280291
ast::ExprKind::Repeat(ref element, ref count) => {
281292
self.print_expr_repeat(element, count);

‎compiler/rustc_parse/src/parser/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1125,13 +1125,13 @@ impl<'a> Parser<'a> {
11251125
self.sess.gated_spans.gate(sym::inline_const, span);
11261126
}
11271127
self.eat_keyword(kw::Const);
1128-
let blk = self.parse_block()?;
1128+
let (attrs, blk) = self.parse_inner_attrs_and_block()?;
11291129
let anon_const = AnonConst {
11301130
id: DUMMY_NODE_ID,
11311131
value: self.mk_expr(blk.span, ExprKind::Block(blk, None), AttrVec::new()),
11321132
};
11331133
let blk_span = anon_const.value.span;
1134-
Ok(self.mk_expr(span.to(blk_span), ExprKind::ConstBlock(anon_const), AttrVec::new()))
1134+
Ok(self.mk_expr(span.to(blk_span), ExprKind::ConstBlock(anon_const), AttrVec::from(attrs)))
11351135
}
11361136

11371137
/// Parses mutability (`mut` or nothing).

‎src/test/pretty/stmt_expr_attributes.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// pp-exact
22

33
#![feature(box_syntax)]
4+
#![feature(inline_const)]
5+
#![feature(inline_const_pat)]
46
#![feature(rustc_attrs)]
57
#![feature(stmt_expr_attributes)]
68

@@ -16,6 +18,7 @@ fn _1() {
1618

1719
#[rustc_dummy]
1820
unsafe {
21+
#![rustc_dummy]
1922
// code
2023
}
2124
}
@@ -206,6 +209,12 @@ fn _11() {
206209
let _ = ();
207210
()
208211
};
212+
let const {
213+
#![rustc_dummy]
214+
} =
215+
#[rustc_dummy] const {
216+
#![rustc_dummy]
217+
};
209218
let mut x = 0;
210219
let _ = #[rustc_dummy] x = 15;
211220
let _ = #[rustc_dummy] x += 15;

0 commit comments

Comments
 (0)
Please sign in to comment.