diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2020-02-02 11:10:27 +0100 |
|---|---|---|
| committer | Mazdak Farrokhzad <twingoow@gmail.com> | 2020-02-02 13:32:37 +0100 |
| commit | 71a6f58229c00720b35579856bdb64e2a19af521 (patch) | |
| tree | c4af395650e4903a37a93c6de89f390852616bf0 /src/librustc_parse/parser | |
| parent | 8674efdb7c9d263caef8d282086a4243eae8df20 (diff) | |
| download | rust-71a6f58229c00720b35579856bdb64e2a19af521.tar.gz rust-71a6f58229c00720b35579856bdb64e2a19af521.zip | |
parser: address review comments re. `self`.
Diffstat (limited to 'src/librustc_parse/parser')
| -rw-r--r-- | src/librustc_parse/parser/diagnostics.rs | 25 | ||||
| -rw-r--r-- | src/librustc_parse/parser/item.rs | 26 | ||||
| -rw-r--r-- | src/librustc_parse/parser/ty.rs | 3 |
3 files changed, 15 insertions, 39 deletions
diff --git a/src/librustc_parse/parser/diagnostics.rs b/src/librustc_parse/parser/diagnostics.rs index 09f393a81ab..4f259d314fb 100644 --- a/src/librustc_parse/parser/diagnostics.rs +++ b/src/librustc_parse/parser/diagnostics.rs @@ -1336,8 +1336,7 @@ impl<'a> Parser<'a> { err: &mut DiagnosticBuilder<'_>, pat: P<ast::Pat>, require_name: bool, - is_self_semantic: bool, - in_assoc_item: bool, + first_param: bool, ) -> Option<Ident> { // If we find a pattern followed by an identifier, it could be an (incorrect) // C-style parameter declaration. @@ -1357,13 +1356,12 @@ impl<'a> Parser<'a> { return Some(ident); } else if let PatKind::Ident(_, ident, _) = pat.kind { if require_name - && (in_assoc_item - || self.token == token::Comma + && (self.token == token::Comma || self.token == token::Lt || self.token == token::CloseDelim(token::Paren)) { // `fn foo(a, b) {}`, `fn foo(a<x>, b<y>) {}` or `fn foo(usize, usize) {}` - if is_self_semantic { + if first_param { err.span_suggestion( pat.span, "if this is a `self` type, give it a parameter name", @@ -1420,21 +1418,12 @@ impl<'a> Parser<'a> { Ok((pat, ty)) } - pub(super) fn recover_bad_self_param( - &mut self, - mut param: ast::Param, - in_assoc_item: bool, - ) -> PResult<'a, ast::Param> { + pub(super) fn recover_bad_self_param(&mut self, mut param: Param) -> PResult<'a, Param> { let sp = param.pat.span; param.ty.kind = TyKind::Err; - let mut err = self.struct_span_err(sp, "unexpected `self` parameter in function"); - if in_assoc_item { - err.span_label(sp, "must be the first associated function parameter"); - } else { - err.span_label(sp, "not valid as function parameter"); - err.note("`self` is only valid as the first parameter of an associated function"); - } - err.emit(); + self.struct_span_err(sp, "unexpected `self` parameter in function") + .span_label(sp, "must be the first parameter of an associated function") + .emit(); Ok(param) } diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index 0ff595f4444..da5cc0bb83e 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -1715,9 +1715,6 @@ impl<'a> Parser<'a> { /// The parsing configuration used to parse a parameter list (see `parse_fn_params`). pub(super) struct ParamCfg { - /// Is `self` is *semantically* allowed as the first parameter? - /// This is only used for diagnostics. - pub in_assoc_item: bool, /// `is_name_required` decides if, per-parameter, /// the parameter must have a pattern or just a type. pub is_name_required: fn(&token::Token) -> bool, @@ -1733,7 +1730,7 @@ impl<'a> Parser<'a> { attrs: Vec<Attribute>, header: FnHeader, ) -> PResult<'a, Option<P<Item>>> { - let cfg = ParamCfg { in_assoc_item: false, is_name_required: |_| true }; + let cfg = ParamCfg { is_name_required: |_| true }; let (ident, decl, generics) = self.parse_fn_sig(&cfg)?; let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; let kind = ItemKind::Fn(FnSig { decl, header }, generics, body); @@ -1748,7 +1745,7 @@ impl<'a> Parser<'a> { attrs: Vec<Attribute>, extern_sp: Span, ) -> PResult<'a, P<ForeignItem>> { - let cfg = ParamCfg { in_assoc_item: false, is_name_required: |_| true }; + let cfg = ParamCfg { is_name_required: |_| true }; self.expect_keyword(kw::Fn)?; let (ident, decl, generics) = self.parse_fn_sig(&cfg)?; let span = lo.to(self.token.span); @@ -1763,9 +1760,8 @@ impl<'a> Parser<'a> { attrs: &mut Vec<Attribute>, is_name_required: fn(&token::Token) -> bool, ) -> PResult<'a, (Ident, AssocItemKind, Generics)> { - let cfg = ParamCfg { in_assoc_item: true, is_name_required }; let header = self.parse_fn_front_matter()?; - let (ident, decl, generics) = self.parse_fn_sig(&cfg)?; + let (ident, decl, generics) = self.parse_fn_sig(&ParamCfg { is_name_required })?; let sig = FnSig { header, decl }; let body = self.parse_assoc_fn_body(at_end, attrs)?; Ok((ident, AssocItemKind::Fn(sig, body), generics)) @@ -1893,11 +1889,7 @@ impl<'a> Parser<'a> { // 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 if first_param { - Ok(param) - } else { - self.recover_bad_self_param(param, cfg.in_assoc_item) - }; + return if first_param { Ok(param) } else { self.recover_bad_self_param(param) }; } let is_name_required = match self.token.kind { @@ -1909,13 +1901,9 @@ impl<'a> Parser<'a> { let pat = self.parse_fn_param_pat()?; if let Err(mut err) = self.expect(&token::Colon) { - return if let Some(ident) = self.parameter_without_type( - &mut err, - pat, - is_name_required, - first_param && cfg.in_assoc_item, - cfg.in_assoc_item, - ) { + return if let Some(ident) = + self.parameter_without_type(&mut err, pat, is_name_required, first_param) + { err.emit(); Ok(dummy_arg(ident)) } else { diff --git a/src/librustc_parse/parser/ty.rs b/src/librustc_parse/parser/ty.rs index 51367a37ad7..c9c2cbb98ca 100644 --- a/src/librustc_parse/parser/ty.rs +++ b/src/librustc_parse/parser/ty.rs @@ -288,8 +288,7 @@ impl<'a> Parser<'a> { let unsafety = self.parse_unsafety(); let ext = self.parse_extern()?; self.expect_keyword(kw::Fn)?; - let cfg = ParamCfg { in_assoc_item: false, is_name_required: |_| false }; - let decl = self.parse_fn_decl(&cfg, false)?; + let decl = self.parse_fn_decl(&ParamCfg { is_name_required: |_| false }, false)?; Ok(TyKind::BareFn(P(BareFnTy { ext, unsafety, generic_params, decl }))) } |
