diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-09-29 20:34:18 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-29 20:34:18 +0200 |
| commit | 64afa262530dec633bfcd6b87470ba2a5d6ad170 (patch) | |
| tree | 48adc0fa50662e38ba37f0ce6cf4a9cd177a8184 /src/libsyntax/parse/parser | |
| parent | 4ada68e3251a8cd777a8381a716bacf5af4c01d1 (diff) | |
| parent | 8fd03b1e47147794ff955a13a474cd52a5f78359 (diff) | |
| download | rust-64afa262530dec633bfcd6b87470ba2a5d6ad170.tar.gz rust-64afa262530dec633bfcd6b87470ba2a5d6ad170.zip | |
Rollup merge of #64894 - Centril:fix-64682, r=petrochenkov
syntax: fix dropping of attribute on first param of non-method assocated fn Fixes #64682. The general idea is that we bake parsing of `self` into `parse_param_general` and then we just use standard list parsing. Overall, this simplifies the parsing and makes it more consistent. r? @petrochenkov cc @c410-f3r
Diffstat (limited to 'src/libsyntax/parse/parser')
| -rw-r--r-- | src/libsyntax/parse/parser/item.rs | 44 |
1 files changed, 18 insertions, 26 deletions
diff --git a/src/libsyntax/parse/parser/item.rs b/src/libsyntax/parse/parser/item.rs index 92b19b73e57..64c494416ff 100644 --- a/src/libsyntax/parse/parser/item.rs +++ b/src/libsyntax/parse/parser/item.rs @@ -424,13 +424,7 @@ impl<'a> Parser<'a> { } else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) { let ident = self.parse_ident().unwrap(); self.bump(); // `(` - let kw_name = if let Ok(Some(_)) = self.parse_self_parameter_with_attrs() - .map_err(|mut e| e.cancel()) - { - "method" - } else { - "function" - }; + let kw_name = self.recover_first_param(); self.consume_block(token::Paren); let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) { self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]); @@ -477,13 +471,7 @@ impl<'a> Parser<'a> { self.eat_to_tokens(&[&token::Gt]); self.bump(); // `>` let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) { - if let Ok(Some(_)) = self.parse_self_parameter_with_attrs() - .map_err(|mut e| e.cancel()) - { - ("fn", "method", false) - } else { - ("fn", "function", false) - } + ("fn", self.recover_first_param(), false) } else if self.check(&token::OpenDelim(token::Brace)) { ("struct", "struct", false) } else { @@ -505,6 +493,16 @@ impl<'a> Parser<'a> { self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, visibility) } + fn recover_first_param(&mut self) -> &'static str { + match self.parse_outer_attributes() + .and_then(|_| self.parse_self_param()) + .map_err(|mut e| e.cancel()) + { + Ok(Some(_)) => "method", + _ => "function", + } + } + /// This is the fall-through for parsing items. fn parse_macro_use_or_failure( &mut self, @@ -861,9 +859,7 @@ impl<'a> Parser<'a> { let (constness, unsafety, asyncness, abi) = self.parse_fn_front_matter()?; let ident = self.parse_ident()?; let mut generics = self.parse_generics()?; - let decl = self.parse_fn_decl_with_self(|p| { - p.parse_param_general(true, false, |_| true) - })?; + let decl = self.parse_fn_decl_with_self(|_| true)?; generics.where_clause = self.parse_where_clause()?; *at_end = true; let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; @@ -1034,15 +1030,11 @@ impl<'a> Parser<'a> { let ident = self.parse_ident()?; let mut generics = self.parse_generics()?; - let decl = self.parse_fn_decl_with_self(|p: &mut Parser<'a>| { - // This is somewhat dubious; We don't want to allow - // argument names to be left off if there is a - // definition... - - // We don't allow argument names to be left off in edition 2018. - let is_name_required = p.token.span.rust_2018(); - p.parse_param_general(true, false, |_| is_name_required) - })?; + // This is somewhat dubious; We don't want to allow + // argument names to be left off if there is a definition... + // + // We don't allow argument names to be left off in edition 2018. + let decl = self.parse_fn_decl_with_self(|t| t.span.rust_2018())?; generics.where_clause = self.parse_where_clause()?; let sig = ast::MethodSig { |
