Skip to content

Commit fdf4b8b

Browse files
authored
move self from parse_param_general to parse_fn_params
1 parent 2886b36 commit fdf4b8b

File tree

1 file changed

+14
-16
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+14
-16
lines changed

compiler/rustc_parse/src/parser/item.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,8 +2948,6 @@ impl<'a> Parser<'a> {
29482948

29492949
/// Parses the parameter list of a function, including the `(` and `)` delimiters.
29502950
pub(super) fn parse_fn_params(&mut self, req_name: ReqName) -> PResult<'a, ThinVec<Param>> {
2951-
let mut first_param = true;
2952-
// Parse the arguments, starting out with `self` being allowed...
29532951
if self.token != TokenKind::OpenParen
29542952
// might be typo'd trait impl, handled elsewhere
29552953
&& !self.token.is_keyword(kw::For)
@@ -2959,11 +2957,20 @@ impl<'a> Parser<'a> {
29592957
.emit_err(errors::MissingFnParams { span: self.prev_token.span.shrink_to_hi() });
29602958
return Ok(ThinVec::new());
29612959
}
2960+
2961+
let mut params = ThinVec::new();
29622962

2963-
let (mut params, _) = self.parse_paren_comma_seq(|p| {
2963+
//Parse the self parameter as first parameter
2964+
if let Some(mut self_param) = self.parse_self_param()?{
2965+
let self_attrs = self.parse_outer_attributes()?;
2966+
self_param.attrs = self.attrs;
2967+
params.push(self_param);
2968+
}
2969+
2970+
let (mut reamining_params, _) = self.parse_paren_comma_seq(|p| {
29642971
p.recover_vcs_conflict_marker();
29652972
let snapshot = p.create_snapshot_for_diagnostic();
2966-
let param = p.parse_param_general(req_name, first_param, true).or_else(|e| {
2973+
let param = p.parse_param_general(req_name, true).or_else(|e| {
29672974
let guar = e.emit();
29682975
// When parsing a param failed, we should check to make the span of the param
29692976
// not contain '(' before it.
@@ -2979,35 +2986,26 @@ impl<'a> Parser<'a> {
29792986
// Create a placeholder argument for proper arg count (issue #34264).
29802987
Ok(dummy_arg(Ident::new(sym::dummy, lo.to(p.prev_token.span)), guar))
29812988
});
2982-
// ...now that we've parsed the first argument, `self` is no longer allowed.
2983-
first_param = false;
29842989
param
29852990
})?;
2991+
// Combine self parameter (if any) with remaining parameters
2992+
params.extend(remaining_params);
29862993
// Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
29872994
self.deduplicate_recovered_params_names(&mut params);
29882995
Ok(params)
29892996
}
29902997

29912998
/// Parses a single function parameter.
29922999
///
2993-
/// - `self` is syntactically allowed when `first_param` holds.
29943000
/// - `recover_arg_parse` is used to recover from a failed argument parse.
29953001
pub(super) fn parse_param_general(
29963002
&mut self,
29973003
req_name: ReqName,
2998-
first_param: bool,
29993004
recover_arg_parse: bool,
30003005
) -> PResult<'a, Param> {
30013006
let lo = self.token.span;
30023007
let attrs = self.parse_outer_attributes()?;
30033008
self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
3004-
// Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
3005-
if let Some(mut param) = this.parse_self_param()? {
3006-
param.attrs = attrs;
3007-
let res = if first_param { Ok(param) } else { this.recover_bad_self_param(param) };
3008-
return Ok((res?, Trailing::No, UsePreAttrPos::No));
3009-
}
3010-
30113009
let is_name_required = match this.token.kind {
30123010
token::DotDotDot => false,
30133011
_ => req_name(this.token.span.with_neighbor(this.prev_token.span).edition()),
@@ -3018,7 +3016,7 @@ impl<'a> Parser<'a> {
30183016
if !colon {
30193017
let mut err = this.unexpected().unwrap_err();
30203018
return if let Some(ident) =
3021-
this.parameter_without_type(&mut err, pat, is_name_required, first_param)
3019+
this.parameter_without_type(&mut err, pat, is_name_required, false)
30223020
{
30233021
let guar = err.emit();
30243022
Ok((dummy_arg(ident, guar), Trailing::No, UsePreAttrPos::No))

0 commit comments

Comments
 (0)