Skip to content

Commit

Permalink
Auto merge of rust-lang#85193 - pnkfelix:readd-support-for-inner-attr…
Browse files Browse the repository at this point in the history
…s-within-match, r=nikomatsakis

Re-add support for parsing (and pretty-printing) inner-attributes in match body

Re-add support for parsing (and pretty-printing) inner-attributes within body of a `match`.

In other words, we can do `match EXPR { #![inner_attr] ARM_1 ARM_2 ... }` again.

I believe this unbreaks the only four crates that crater flagged as broken by PR rust-lang#83312.

(I am putting this up so that the lang-team can check it out and decide whether it changes their mind about what to do regarding PR rust-lang#83312.)
  • Loading branch information
bors committed Jun 22, 2021
2 parents b8be316 + 8ce761d commit 6a758ea
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true)
}

fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) {
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
}

fn print_outer_attributes(&mut self, attrs: &[ast::Attribute]) {
self.print_either_attributes(attrs, ast::AttrStyle::Outer, false, true)
}
Expand Down Expand Up @@ -1960,6 +1964,7 @@ impl<'a> State<'a> {
self.print_expr_as_cond(expr);
self.s.space();
self.bopen();
self.print_inner_attributes_no_trailing_hardbreak(attrs);
for arm in arms {
self.print_arm(arm);
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1945,7 +1945,7 @@ impl<'a> Parser<'a> {
}

/// Parses a `match ... { ... }` expression (`match` token already eaten).
fn parse_match_expr(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> {
fn parse_match_expr(&mut self, mut attrs: AttrVec) -> PResult<'a, P<Expr>> {
let match_span = self.prev_token.span;
let lo = self.prev_token.span;
let scrutinee = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
Expand All @@ -1960,6 +1960,7 @@ impl<'a> Parser<'a> {
}
return Err(e);
}
attrs.extend(self.parse_inner_attributes()?);

let mut arms: Vec<Arm> = Vec::new();
while self.token != token::CloseDelim(token::Brace) {
Expand Down
8 changes: 4 additions & 4 deletions src/test/pretty/ast-stmt-expr-attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ fn syntax() {
#![attr]
};
let _ =
#[attr] match true
{
#[attr]
_ => false,
#[attr] match true {
#![attr]
#[attr]
_ => false,
};
let _ = #[attr] || #[attr] foo;
let _ = #[attr] move || #[attr] foo;
Expand Down
17 changes: 14 additions & 3 deletions src/test/pretty/stmt_expr_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,16 @@ fn _3() {
fn _4() {

#[rustc_dummy]
match () { _ => (), }
match () {
#![rustc_dummy]
_ => (),
}

let _ = #[rustc_dummy] match () { () => (), };
let _ =
#[rustc_dummy] match () {
#![rustc_dummy]
() => (),
};
}

fn _5() {
Expand Down Expand Up @@ -164,7 +171,11 @@ fn _11() {
#[rustc_dummy] loop {
#![rustc_dummy]
};
let _ = #[rustc_dummy] match false { _ => (), };
let _ =
#[rustc_dummy] match false {
#![rustc_dummy]
_ => (),
};
let _ = #[rustc_dummy] || #[rustc_dummy] ();
let _ = #[rustc_dummy] move || #[rustc_dummy] ();
let _ =
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/parser/stmt_expr_attrs_placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() {
//~^ ERROR an inner attribute is not permitted in this context

let g = match true { #![allow(warnings)] _ => {} };
//~^ ERROR an inner attribute is not permitted in this context


struct MyStruct { field: u8 }
let h = MyStruct { #![allow(warnings)] field: 0 };
Expand Down
10 changes: 1 addition & 9 deletions src/test/ui/parser/stmt_expr_attrs_placement.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ LL | let f = [#![allow(warnings)] 1; 0];
|
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files. Outer attributes, like `#[test]`, annotate the item following them.

error: an inner attribute is not permitted in this context
--> $DIR/stmt_expr_attrs_placement.rs:32:26
|
LL | let g = match true { #![allow(warnings)] _ => {} };
| ^^^^^^^^^^^^^^^^^^^
|
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files. Outer attributes, like `#[test]`, annotate the item following them.

error: an inner attribute is not permitted in this context
--> $DIR/stmt_expr_attrs_placement.rs:36:24
|
Expand All @@ -62,5 +54,5 @@ LL | let h = MyStruct { #![allow(warnings)] field: 0 };
|
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files. Outer attributes, like `#[test]`, annotate the item following them.

error: aborting due to 8 previous errors
error: aborting due to 7 previous errors

0 comments on commit 6a758ea

Please sign in to comment.