diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 982f601c0d5a2..8247dfa61a2f0 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -2494,11 +2494,19 @@ impl<'a> Parser<'a> { // Parse the arguments, starting out with `self` being allowed... let (mut params, _) = self.parse_paren_comma_seq(|p| { p.recover_diff_marker(); + let meet_left_paren = p.check_noexpect(&token::OpenDelim(Delimiter::Parenthesis)); let param = p.parse_param_general(req_name, first_param).or_else(|mut e| { - e.emit(); + if p.capture_cfg { + e.cancel(); + } else { + e.emit(); + } let lo = p.prev_token.span; // Skip every token until next possible arg or end. p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(Delimiter::Parenthesis)]); + if p.capture_cfg && meet_left_paren { + p.eat_noexpect(&token::CloseDelim(Delimiter::Parenthesis)); + } // Create a placeholder argument for proper arg count (issue #34264). Ok(dummy_arg(Ident::new(kw::Empty, lo.to(p.prev_token.span)))) }); diff --git a/tests/ui/parser/issue-116781.rs b/tests/ui/parser/issue-116781.rs new file mode 100644 index 0000000000000..44d354c3ff269 --- /dev/null +++ b/tests/ui/parser/issue-116781.rs @@ -0,0 +1,8 @@ +#[derive(Debug)] +struct Foo { + #[cfg(all())] + field: fn(($id1), $id2), //~ ERROR expected pattern, found `$` + //~^ ERROR expected identifier, found `$` +} + +fn main() {} diff --git a/tests/ui/parser/issue-116781.stderr b/tests/ui/parser/issue-116781.stderr new file mode 100644 index 0000000000000..42a9a0baa1e0f --- /dev/null +++ b/tests/ui/parser/issue-116781.stderr @@ -0,0 +1,17 @@ +error: expected pattern, found `$` + --> $DIR/issue-116781.rs:4:16 + | +LL | field: fn(($id1), $id2), + | ^ expected pattern + +error: expected identifier, found `$` + --> $DIR/issue-116781.rs:4:23 + | +LL | struct Foo { + | --- while parsing this struct +LL | #[cfg(all())] +LL | field: fn(($id1), $id2), + | ^ expected identifier + +error: aborting due to 2 previous errors +