Tweak self arg not as first argument of a method diagnostic

Mention that `self` is only valid on "associated functions"
```
error: unexpected `self` argument in function
  --> $DIR/self-in-function-arg.rs:1:15
   |
LL | fn foo(x:i32, self: i32) -> i32 { self }
   |               ^^^^ not valid as function argument
   |
   = note: `self` is only valid as the first argument of an associated function
```

When it is a method, mention it must be first
```
error: unexpected `self` argument in function
  --> $DIR/trait-fn.rs:4:20
   |
LL |     fn c(foo: u32, self) {}
   |                    ^^^^ must be the first associated function argument
```
This commit is contained in:
Esteban Küber
2019-05-23 12:54:27 -07:00
parent 02f5786a32
commit 976541884f
10 changed files with 80 additions and 43 deletions

View File

@@ -1805,50 +1805,23 @@ impl<'a> Parser<'a> {
}
/// This version of parse arg doesn't necessarily require identifier names.
fn parse_arg_general(&mut self, require_name: bool, is_trait_item: bool,
allow_c_variadic: bool) -> PResult<'a, Arg> {
if let Ok(Some(_)) = self.parse_self_arg() {
let mut err = self.struct_span_err(self.prev_span,
"unexpected `self` argument in function");
err.span_label(self.prev_span,
"`self` is only valid as the first argument of an associated function");
return Err(err);
fn parse_arg_general(
&mut self,
require_name: bool,
is_trait_item: bool,
allow_c_variadic: bool,
) -> PResult<'a, Arg> {
if let Ok(Some(arg)) = self.parse_self_arg() {
return self.recover_bad_self_arg(arg, is_trait_item);
}
let (pat, ty) = if require_name || self.is_named_argument() {
debug!("parse_arg_general parse_pat (require_name:{})",
require_name);
debug!("parse_arg_general parse_pat (require_name:{})", require_name);
self.eat_incorrect_doc_comment("method arguments");
let pat = self.parse_pat(Some("argument name"))?;
if let Err(mut err) = self.expect(&token::Colon) {
// If we find a pattern followed by an identifier, it could be an (incorrect)
// C-style parameter declaration.
if self.check_ident() && self.look_ahead(1, |t| {
*t == token::Comma || *t == token::CloseDelim(token::Paren)
}) {
let ident = self.parse_ident().unwrap();
let span = pat.span.with_hi(ident.span.hi());
err.span_suggestion(
span,
"declare the type after the parameter binding",
String::from("<identifier>: <type>"),
Applicability::HasPlaceholders,
);
} else if require_name && is_trait_item {
if let PatKind::Ident(_, ident, _) = pat.node {
err.span_suggestion(
pat.span,
"explicitly ignore parameter",
format!("_: {}", ident),
Applicability::MachineApplicable,
);
}
err.note("anonymous parameters are removed in the 2018 edition (see RFC 1685)");
}
self.argument_without_type(&mut err, pat, require_name, is_trait_item);
return Err(err);
}