Skip to content

Commit 6a758ea

Browse files
committed
Auto merge of rust-lang#85193 - pnkfelix:readd-support-for-inner-attrs-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.)
2 parents b8be316 + 8ce761d commit 6a758ea

File tree

6 files changed

+27
-18
lines changed

6 files changed

+27
-18
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+5
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
369369
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true)
370370
}
371371

372+
fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) {
373+
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
374+
}
375+
372376
fn print_outer_attributes(&mut self, attrs: &[ast::Attribute]) {
373377
self.print_either_attributes(attrs, ast::AttrStyle::Outer, false, true)
374378
}
@@ -1960,6 +1964,7 @@ impl<'a> State<'a> {
19601964
self.print_expr_as_cond(expr);
19611965
self.s.space();
19621966
self.bopen();
1967+
self.print_inner_attributes_no_trailing_hardbreak(attrs);
19631968
for arm in arms {
19641969
self.print_arm(arm);
19651970
}

compiler/rustc_parse/src/parser/expr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1945,7 +1945,7 @@ impl<'a> Parser<'a> {
19451945
}
19461946

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

19641965
let mut arms: Vec<Arm> = Vec::new();
19651966
while self.token != token::CloseDelim(token::Brace) {

src/test/pretty/ast-stmt-expr-attr.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ fn syntax() {
4343
#![attr]
4444
};
4545
let _ =
46-
#[attr] match true
47-
{
48-
#[attr]
49-
_ => false,
46+
#[attr] match true {
47+
#![attr]
48+
#[attr]
49+
_ => false,
5050
};
5151
let _ = #[attr] || #[attr] foo;
5252
let _ = #[attr] move || #[attr] foo;

src/test/pretty/stmt_expr_attributes.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,16 @@ fn _3() {
4141
fn _4() {
4242

4343
#[rustc_dummy]
44-
match () { _ => (), }
44+
match () {
45+
#![rustc_dummy]
46+
_ => (),
47+
}
4548

46-
let _ = #[rustc_dummy] match () { () => (), };
49+
let _ =
50+
#[rustc_dummy] match () {
51+
#![rustc_dummy]
52+
() => (),
53+
};
4754
}
4855

4956
fn _5() {
@@ -164,7 +171,11 @@ fn _11() {
164171
#[rustc_dummy] loop {
165172
#![rustc_dummy]
166173
};
167-
let _ = #[rustc_dummy] match false { _ => (), };
174+
let _ =
175+
#[rustc_dummy] match false {
176+
#![rustc_dummy]
177+
_ => (),
178+
};
168179
let _ = #[rustc_dummy] || #[rustc_dummy] ();
169180
let _ = #[rustc_dummy] move || #[rustc_dummy] ();
170181
let _ =

src/test/ui/parser/stmt_expr_attrs_placement.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn main() {
3030
//~^ ERROR an inner attribute is not permitted in this context
3131

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

3535
struct MyStruct { field: u8 }
3636
let h = MyStruct { #![allow(warnings)] field: 0 };

src/test/ui/parser/stmt_expr_attrs_placement.stderr

+1-9
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,6 @@ LL | let f = [#![allow(warnings)] 1; 0];
4646
|
4747
= 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.
4848

49-
error: an inner attribute is not permitted in this context
50-
--> $DIR/stmt_expr_attrs_placement.rs:32:26
51-
|
52-
LL | let g = match true { #![allow(warnings)] _ => {} };
53-
| ^^^^^^^^^^^^^^^^^^^
54-
|
55-
= 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.
56-
5749
error: an inner attribute is not permitted in this context
5850
--> $DIR/stmt_expr_attrs_placement.rs:36:24
5951
|
@@ -62,5 +54,5 @@ LL | let h = MyStruct { #![allow(warnings)] field: 0 };
6254
|
6355
= 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.
6456

65-
error: aborting due to 8 previous errors
57+
error: aborting due to 7 previous errors
6658

0 commit comments

Comments
 (0)