]> git.lizzy.rs Git - rust.git/commitdiff
improve error when self is used as not the first argument
authorAxary <bastian_kauschke@hotmail.de>
Fri, 16 Nov 2018 18:27:27 +0000 (19:27 +0100)
committerAxary <bastian_kauschke@hotmail.de>
Fri, 16 Nov 2018 18:27:27 +0000 (19:27 +0100)
src/libsyntax/parse/parser.rs
src/test/ui/bare-function-self.rs [deleted file]
src/test/ui/invalid-self-argument/bare-fn-start.rs [new file with mode: 0644]
src/test/ui/invalid-self-argument/bare-fn.rs [new file with mode: 0644]
src/test/ui/invalid-self-argument/trait-fn.rs [new file with mode: 0644]

index 7ddb4099e0e9079c3445cf1b19b36b466636d3e7..a4b01f485d38b8235546f1cc56b03f9ea062d8cb 100644 (file)
@@ -1824,6 +1824,14 @@ fn eat_incorrect_doc_comment(&mut self, applied_to: &str) {
     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 @@ fn parse_where_clause(&mut self) -> PResult<'a, WhereClause> {
     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 (file)
index f906b17..0000000
+++ /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 (file)
index 0000000..a84fe55
--- /dev/null
@@ -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 (file)
index 0000000..27e56a5
--- /dev/null
@@ -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 (file)
index 0000000..e2107e4
--- /dev/null
@@ -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() { }