Skip to content

Commit

Permalink
Re-add support for parsing (and pretty-printing) inner-attributes wit…
Browse files Browse the repository at this point in the history
…hin 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 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 83312.)
  • Loading branch information
pnkfelix committed May 11, 2021
1 parent 2bafe96 commit 75d6293
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
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 @@ -366,6 +366,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 @@ -1940,6 +1944,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 @@ -1947,7 +1947,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 @@ -1962,6 +1962,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

0 comments on commit 75d6293

Please sign in to comment.