improve error when self is used as not the first argument
This commit is contained in:
parent
646d68f585
commit
fe23ffbda0
5 changed files with 30 additions and 13 deletions
|
@ -1824,6 +1824,14 @@ impl<'a> Parser<'a> {
|
|||
fn parse_arg_general(&mut self, require_name: bool) -> PResult<'a, Arg> {
|
||||
maybe_whole!(self, NtArg, |x| x);
|
||||
|
||||
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 a trait function");
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
let (pat, ty) = if require_name || self.is_named_argument() {
|
||||
debug!("parse_arg_general parse_pat (require_name:{})",
|
||||
require_name);
|
||||
|
@ -5387,13 +5395,6 @@ impl<'a> Parser<'a> {
|
|||
-> PResult<'a, (Vec<Arg> , bool)> {
|
||||
self.expect(&token::OpenDelim(token::Paren))?;
|
||||
|
||||
if let Ok(Some(_)) = self.parse_self_arg() {
|
||||
let mut err = self.struct_span_err(self.prev_span
|
||||
, "unexpected `self` argument in bare function");
|
||||
err.span_label(self.prev_span, "invalid argument in bare function");
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
let sp = self.span;
|
||||
let mut variadic = false;
|
||||
let args: Vec<Option<Arg>> =
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
fn a(&self) { }
|
||||
//~^ ERROR unexpected `self` argument in bare function
|
||||
//~| NOTE invalid argument in bare function
|
||||
|
||||
fn main() { }
|
5
src/test/ui/invalid-self-argument/bare-fn-start.rs
Normal file
5
src/test/ui/invalid-self-argument/bare-fn-start.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
fn a(&self) { }
|
||||
//~^ ERROR unexpected `self` argument in function
|
||||
//~| NOTE `self` is only valid as the first argument of a trait function
|
||||
|
||||
fn main() { }
|
5
src/test/ui/invalid-self-argument/bare-fn.rs
Normal file
5
src/test/ui/invalid-self-argument/bare-fn.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
fn b(foo: u32, &mut self) { }
|
||||
//~^ ERROR unexpected `self` argument in function
|
||||
//~| NOTE `self` is only valid as the first argument of a trait function
|
||||
|
||||
fn main() { }
|
11
src/test/ui/invalid-self-argument/trait-fn.rs
Normal file
11
src/test/ui/invalid-self-argument/trait-fn.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
struct Foo {}
|
||||
|
||||
impl Foo {
|
||||
fn c(foo: u32, self) {}
|
||||
//~^ ERROR unexpected `self` argument in function
|
||||
//~| NOTE `self` is only valid as the first argument of a trait function
|
||||
|
||||
fn good(&mut self, foo: u32) {}
|
||||
}
|
||||
|
||||
fn main() { }
|
Loading…
Add table
Add a link
Reference in a new issue