1
Fork 0

Rollup merge of #94985 - dtolnay:constattr, r=pnkfelix

Parse inner attributes on inline const block

According to https://github.com/rust-lang/rust/pull/84414#issuecomment-826150936, 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 (https://github.com/rust-lang/rust/issues/76001), which contain statements just like an unsafe block or a loop body.

```rust
let _ = const {
    #![allow(...)]

    let x = ();
    x
};
```
This commit is contained in:
Dylan DPC 2022-04-16 19:42:00 +02:00 committed by GitHub
commit 22d554657d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 6 deletions

View file

@ -959,7 +959,7 @@ impl<'a> State<'a> {
self.word_space("="); self.word_space("=");
match term { match term {
Term::Ty(ty) => self.print_type(ty), Term::Ty(ty) => self.print_type(ty),
Term::Const(c) => self.print_expr_anon_const(c), Term::Const(c) => self.print_expr_anon_const(c, &[]),
} }
} }
ast::AssocConstraintKind::Bound { bounds } => self.print_type_bounds(":", &*bounds), ast::AssocConstraintKind::Bound { bounds } => self.print_type_bounds(":", &*bounds),

View file

@ -88,10 +88,21 @@ impl<'a> State<'a> {
self.end(); self.end();
} }
pub(super) fn print_expr_anon_const(&mut self, expr: &ast::AnonConst) { pub(super) fn print_expr_anon_const(
&mut self,
expr: &ast::AnonConst,
attrs: &[ast::Attribute],
) {
self.ibox(INDENT_UNIT); self.ibox(INDENT_UNIT);
self.word("const"); self.word("const");
self.print_expr(&expr.value); self.nbsp();
if let ast::ExprKind::Block(block, None) = &expr.value.kind {
self.cbox(0);
self.ibox(0);
self.print_block_with_attrs(block, attrs);
} else {
self.print_expr(&expr.value);
}
self.end(); self.end();
} }
@ -275,7 +286,7 @@ impl<'a> State<'a> {
self.print_expr_vec(exprs); self.print_expr_vec(exprs);
} }
ast::ExprKind::ConstBlock(ref anon_const) => { ast::ExprKind::ConstBlock(ref anon_const) => {
self.print_expr_anon_const(anon_const); self.print_expr_anon_const(anon_const, attrs);
} }
ast::ExprKind::Repeat(ref element, ref count) => { ast::ExprKind::Repeat(ref element, ref count) => {
self.print_expr_repeat(element, count); self.print_expr_repeat(element, count);

View file

@ -1125,13 +1125,13 @@ impl<'a> Parser<'a> {
self.sess.gated_spans.gate(sym::inline_const, span); self.sess.gated_spans.gate(sym::inline_const, span);
} }
self.eat_keyword(kw::Const); self.eat_keyword(kw::Const);
let blk = self.parse_block()?; let (attrs, blk) = self.parse_inner_attrs_and_block()?;
let anon_const = AnonConst { let anon_const = AnonConst {
id: DUMMY_NODE_ID, id: DUMMY_NODE_ID,
value: self.mk_expr(blk.span, ExprKind::Block(blk, None), AttrVec::new()), value: self.mk_expr(blk.span, ExprKind::Block(blk, None), AttrVec::new()),
}; };
let blk_span = anon_const.value.span; let blk_span = anon_const.value.span;
Ok(self.mk_expr(span.to(blk_span), ExprKind::ConstBlock(anon_const), AttrVec::new())) Ok(self.mk_expr(span.to(blk_span), ExprKind::ConstBlock(anon_const), AttrVec::from(attrs)))
} }
/// Parses mutability (`mut` or nothing). /// Parses mutability (`mut` or nothing).

View file

@ -1,6 +1,8 @@
// pp-exact // pp-exact
#![feature(box_syntax)] #![feature(box_syntax)]
#![feature(inline_const)]
#![feature(inline_const_pat)]
#![feature(rustc_attrs)] #![feature(rustc_attrs)]
#![feature(stmt_expr_attributes)] #![feature(stmt_expr_attributes)]
@ -16,6 +18,7 @@ fn _1() {
#[rustc_dummy] #[rustc_dummy]
unsafe { unsafe {
#![rustc_dummy]
// code // code
} }
} }
@ -206,6 +209,12 @@ fn _11() {
let _ = (); let _ = ();
() ()
}; };
let const {
#![rustc_dummy]
} =
#[rustc_dummy] const {
#![rustc_dummy]
};
let mut x = 0; let mut x = 0;
let _ = #[rustc_dummy] x = 15; let _ = #[rustc_dummy] x = 15;
let _ = #[rustc_dummy] x += 15; let _ = #[rustc_dummy] x += 15;