diff options
| author | bors <bors@rust-lang.org> | 2019-09-29 18:35:04 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-09-29 18:35:04 +0000 |
| commit | 8431f261dd160021b6af85916f161a13dd101ca0 (patch) | |
| tree | 73c411884bf616dec9f4efa6ef66ee2275358205 /src/libsyntax/parse/parser.rs | |
| parent | 06c68947ad88f22a8342fe6b54dc0fe54de6e011 (diff) | |
| parent | 50940ae952ffcab9abd39a2bc1dfd3e1605e0ece (diff) | |
| download | rust-8431f261dd160021b6af85916f161a13dd101ca0.tar.gz rust-8431f261dd160021b6af85916f161a13dd101ca0.zip | |
Auto merge of #64902 - Centril:rollup-1i431vs, r=Centril
Rollup of 6 pull requests Successful merges: - #64691 (Point at definition when misusing ADT) - #64735 (Add long error explanation for E0533) - #64825 (Point at enclosing match when expecting `()` in arm) - #64858 (Add support for relating slices in `super_relate_consts`) - #64894 (syntax: fix dropping of attribute on first param of non-method assocated fn) - #64898 (fixed typo) Failed merges: r? @ghost
Diffstat (limited to 'src/libsyntax/parse/parser.rs')
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 73 |
1 files changed, 23 insertions, 50 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index f22fd5ad703..2fb6f197dad 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -974,15 +974,22 @@ impl<'a> Parser<'a> { /// This version of parse param doesn't necessarily require identifier names. fn parse_param_general( &mut self, + is_self_allowed: bool, is_trait_item: bool, allow_c_variadic: bool, is_name_required: impl Fn(&token::Token) -> bool, ) -> PResult<'a, Param> { let lo = self.token.span; let attrs = self.parse_outer_attributes()?; + + // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here. if let Some(mut param) = self.parse_self_param()? { param.attrs = attrs.into(); - return self.recover_bad_self_param(param, is_trait_item); + return if is_self_allowed { + Ok(param) + } else { + self.recover_bad_self_param(param, is_trait_item) + }; } let is_name_required = is_name_required(&self.token); @@ -1208,6 +1215,7 @@ impl<'a> Parser<'a> { }; match p.parse_param_general( false, + false, allow_c_variadic, do_not_enforce_named_arguments_for_c_variadic ) { @@ -1361,60 +1369,25 @@ impl<'a> Parser<'a> { Ok(Some(Param::from_self(ThinVec::default(), eself, eself_ident))) } - /// Returns the parsed optional self parameter with attributes and whether a self - /// shortcut was used. - fn parse_self_parameter_with_attrs(&mut self) -> PResult<'a, Option<Param>> { - let attrs = self.parse_outer_attributes()?; - let param_opt = self.parse_self_param()?; - Ok(param_opt.map(|mut param| { - param.attrs = attrs.into(); - param - })) - } - /// Parses the parameter list and result type of a function that may have a `self` parameter. - fn parse_fn_decl_with_self<F>(&mut self, parse_param_fn: F) -> PResult<'a, P<FnDecl>> - where F: FnMut(&mut Parser<'a>) -> PResult<'a, Param>, - { - self.expect(&token::OpenDelim(token::Paren))?; - - // Parse optional self argument. - let self_param = self.parse_self_parameter_with_attrs()?; - - // Parse the rest of the function parameter list. - let sep = SeqSep::trailing_allowed(token::Comma); - let (mut fn_inputs, recovered) = if let Some(self_param) = self_param { - if self.check(&token::CloseDelim(token::Paren)) { - (vec![self_param], false) - } else if self.eat(&token::Comma) { - let mut fn_inputs = vec![self_param]; - let (mut input, _, recovered) = self.parse_seq_to_before_end( - &token::CloseDelim(token::Paren), sep, parse_param_fn)?; - fn_inputs.append(&mut input); - (fn_inputs, recovered) - } else { - match self.expect_one_of(&[], &[]) { - Err(err) => return Err(err), - Ok(recovered) => (vec![self_param], recovered), - } - } - } else { - let (input, _, recovered) = - self.parse_seq_to_before_end(&token::CloseDelim(token::Paren), - sep, - parse_param_fn)?; - (input, recovered) - }; + fn parse_fn_decl_with_self( + &mut self, + is_name_required: impl Copy + Fn(&token::Token) -> bool, + ) -> PResult<'a, P<FnDecl>> { + // Parse the arguments, starting out with `self` being allowed... + let mut is_self_allowed = true; + let (mut inputs, _): (Vec<_>, _) = self.parse_paren_comma_seq(|p| { + let res = p.parse_param_general(is_self_allowed, true, false, is_name_required); + // ...but now that we've parsed the first argument, `self` is no longer allowed. + is_self_allowed = false; + res + })?; - if !recovered { - // Parse closing paren and return type. - self.expect(&token::CloseDelim(token::Paren))?; - } // Replace duplicated recovered params with `_` pattern to avoid unecessary errors. - self.deduplicate_recovered_params_names(&mut fn_inputs); + self.deduplicate_recovered_params_names(&mut inputs); Ok(P(FnDecl { - inputs: fn_inputs, + inputs, output: self.parse_ret_ty(true)?, })) } |
