about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorAxary <bastian_kauschke@hotmail.de>2018-11-16 19:27:27 +0100
committerAxary <bastian_kauschke@hotmail.de>2018-11-16 19:27:27 +0100
commitfe23ffbda01d2033c98ec6cec7f51cb08f625ec9 (patch)
treecc2a6830cdafdab958767b4ae39a7597e3a213dc /src
parent646d68f585e6cbabba5b7a67665af9a1f83ea6ea (diff)
downloadrust-fe23ffbda01d2033c98ec6cec7f51cb08f625ec9.tar.gz
rust-fe23ffbda01d2033c98ec6cec7f51cb08f625ec9.zip
improve error when self is used as not the first argument
Diffstat (limited to 'src')
-rw-r--r--src/libsyntax/parse/parser.rs17
-rw-r--r--src/test/ui/bare-function-self.rs5
-rw-r--r--src/test/ui/invalid-self-argument/bare-fn-start.rs5
-rw-r--r--src/test/ui/invalid-self-argument/bare-fn.rs5
-rw-r--r--src/test/ui/invalid-self-argument/trait-fn.rs11
5 files changed, 30 insertions, 13 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 7ddb4099e0e..a4b01f485d3 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -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);
@@ -5386,14 +5394,7 @@ impl<'a> Parser<'a> {
     fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool)
                      -> 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>> =
diff --git a/src/test/ui/bare-function-self.rs b/src/test/ui/bare-function-self.rs
deleted file mode 100644
index f906b176d92..00000000000
--- a/src/test/ui/bare-function-self.rs
+++ /dev/null
@@ -1,5 +0,0 @@
-fn a(&self) { }
-//~^ ERROR unexpected `self` argument in bare function
-//~| NOTE invalid argument in bare function
-
-fn main() { }
diff --git a/src/test/ui/invalid-self-argument/bare-fn-start.rs b/src/test/ui/invalid-self-argument/bare-fn-start.rs
new file mode 100644
index 00000000000..a84fe55502d
--- /dev/null
+++ b/src/test/ui/invalid-self-argument/bare-fn-start.rs
@@ -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() { }
diff --git a/src/test/ui/invalid-self-argument/bare-fn.rs b/src/test/ui/invalid-self-argument/bare-fn.rs
new file mode 100644
index 00000000000..27e56a53713
--- /dev/null
+++ b/src/test/ui/invalid-self-argument/bare-fn.rs
@@ -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() { }
diff --git a/src/test/ui/invalid-self-argument/trait-fn.rs b/src/test/ui/invalid-self-argument/trait-fn.rs
new file mode 100644
index 00000000000..e2107e4d867
--- /dev/null
+++ b/src/test/ui/invalid-self-argument/trait-fn.rs
@@ -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() { }