From 218e35efa1500be4ebc1ee5d84a4e6971352c500 Mon Sep 17 00:00:00 2001 From: Axary Date: Fri, 16 Nov 2018 13:54:49 +0100 Subject: eat CloseDelim --- src/libsyntax/parse/parser.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/libsyntax/parse/parser.rs') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index d90ec4ea081..dd1864ce124 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -5385,11 +5385,16 @@ impl<'a> Parser<'a> { fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool) -> PResult<'a, (Vec , bool)> { + self.expect(&token::OpenDelim(token::Paren))?; + + if let Ok(Some(_)) = self.parse_self_arg() { + return Err(self.fatal("unexpected `self` argument in bare function")) + } + let sp = self.span; let mut variadic = false; let args: Vec> = - self.parse_unspanned_seq( - &token::OpenDelim(token::Paren), + self.parse_seq_to_before_end( &token::CloseDelim(token::Paren), SeqSep::trailing_allowed(token::Comma), |p| { @@ -5436,6 +5441,8 @@ impl<'a> Parser<'a> { } )?; + self.eat(&token::CloseDelim(token::Paren)); + let args: Vec<_> = args.into_iter().filter_map(|x| x).collect(); if variadic && args.is_empty() { -- cgit 1.4.1-3-g733a5 From 646d68f585e6cbabba5b7a67665af9a1f83ea6ea Mon Sep 17 00:00:00 2001 From: Axary Date: Fri, 16 Nov 2018 18:43:06 +0100 Subject: add a note to the error message --- src/libsyntax/parse/parser.rs | 5 ++++- src/test/ui/bare-function-self.rs | 1 + src/test/ui/bare-function-self.stderr | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) (limited to 'src/libsyntax/parse/parser.rs') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index dd1864ce124..7ddb4099e0e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -5388,7 +5388,10 @@ impl<'a> Parser<'a> { self.expect(&token::OpenDelim(token::Paren))?; if let Ok(Some(_)) = self.parse_self_arg() { - return Err(self.fatal("unexpected `self` argument in bare function")) + 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; diff --git a/src/test/ui/bare-function-self.rs b/src/test/ui/bare-function-self.rs index 0a430aee973..f906b176d92 100644 --- a/src/test/ui/bare-function-self.rs +++ b/src/test/ui/bare-function-self.rs @@ -1,4 +1,5 @@ fn a(&self) { } //~^ ERROR unexpected `self` argument in bare function +//~| NOTE invalid argument in bare function fn main() { } diff --git a/src/test/ui/bare-function-self.stderr b/src/test/ui/bare-function-self.stderr index 51db0ddd70d..002d71b1103 100644 --- a/src/test/ui/bare-function-self.stderr +++ b/src/test/ui/bare-function-self.stderr @@ -1,8 +1,8 @@ error: unexpected `self` argument in bare function - --> $DIR/bare-function-self.rs:12:11 + --> $DIR/bare-function-self.rs:1:7 | LL | fn a(&self) { } - | ^ + | ^^^^ invalid argument in bare function error: aborting due to previous error -- cgit 1.4.1-3-g733a5 From fe23ffbda01d2033c98ec6cec7f51cb08f625ec9 Mon Sep 17 00:00:00 2001 From: Axary Date: Fri, 16 Nov 2018 19:27:27 +0100 Subject: improve error when self is used as not the first argument --- src/libsyntax/parse/parser.rs | 17 +++++++++-------- src/test/ui/bare-function-self.rs | 5 ----- src/test/ui/invalid-self-argument/bare-fn-start.rs | 5 +++++ src/test/ui/invalid-self-argument/bare-fn.rs | 5 +++++ src/test/ui/invalid-self-argument/trait-fn.rs | 11 +++++++++++ 5 files changed, 30 insertions(+), 13 deletions(-) delete mode 100644 src/test/ui/bare-function-self.rs create mode 100644 src/test/ui/invalid-self-argument/bare-fn-start.rs create mode 100644 src/test/ui/invalid-self-argument/bare-fn.rs create mode 100644 src/test/ui/invalid-self-argument/trait-fn.rs (limited to 'src/libsyntax/parse/parser.rs') 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 , 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> = 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() { } -- cgit 1.4.1-3-g733a5 From 2be930bd03d3c9b9230ae3b9cc8fc30b83378900 Mon Sep 17 00:00:00 2001 From: Axary Date: Fri, 16 Nov 2018 19:35:13 +0100 Subject: fix tidy (remove whitespace) --- src/libsyntax/parse/parser.rs | 2 +- src/test/ui/invalid-self-argument/bare-fn-start.stderr | 8 ++++++++ src/test/ui/invalid-self-argument/bare-fn.stderr | 8 ++++++++ src/test/ui/invalid-self-argument/trait-fn.stderr | 8 ++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/invalid-self-argument/bare-fn-start.stderr create mode 100644 src/test/ui/invalid-self-argument/bare-fn.stderr create mode 100644 src/test/ui/invalid-self-argument/trait-fn.stderr (limited to 'src/libsyntax/parse/parser.rs') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index a4b01f485d3..18929af4718 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -5394,7 +5394,7 @@ impl<'a> Parser<'a> { fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool) -> PResult<'a, (Vec , bool)> { self.expect(&token::OpenDelim(token::Paren))?; - + let sp = self.span; let mut variadic = false; let args: Vec> = diff --git a/src/test/ui/invalid-self-argument/bare-fn-start.stderr b/src/test/ui/invalid-self-argument/bare-fn-start.stderr new file mode 100644 index 00000000000..d0eca1a9e5c --- /dev/null +++ b/src/test/ui/invalid-self-argument/bare-fn-start.stderr @@ -0,0 +1,8 @@ +error: unexpected `self` argument in function + --> $DIR/bare-fn-start.rs:1:7 + | +LL | fn a(&self) { } + | ^^^^ `self` is only valid as the first argument of a trait function + +error: aborting due to previous error + diff --git a/src/test/ui/invalid-self-argument/bare-fn.stderr b/src/test/ui/invalid-self-argument/bare-fn.stderr new file mode 100644 index 00000000000..bd6c598c88a --- /dev/null +++ b/src/test/ui/invalid-self-argument/bare-fn.stderr @@ -0,0 +1,8 @@ +error: unexpected `self` argument in function + --> $DIR/bare-fn.rs:1:21 + | +LL | fn b(foo: u32, &mut self) { } + | ^^^^ `self` is only valid as the first argument of a trait function + +error: aborting due to previous error + diff --git a/src/test/ui/invalid-self-argument/trait-fn.stderr b/src/test/ui/invalid-self-argument/trait-fn.stderr new file mode 100644 index 00000000000..d056e53b95c --- /dev/null +++ b/src/test/ui/invalid-self-argument/trait-fn.stderr @@ -0,0 +1,8 @@ +error: unexpected `self` argument in function + --> $DIR/trait-fn.rs:4:20 + | +LL | fn c(foo: u32, self) {} + | ^^^^ `self` is only valid as the first argument of a trait function + +error: aborting due to previous error + -- cgit 1.4.1-3-g733a5 From 88d60941da317d8e9deee34d2ed5e8dbb54f928c Mon Sep 17 00:00:00 2001 From: Axary Date: Tue, 20 Nov 2018 14:43:16 +0100 Subject: improve error note --- src/libsyntax/parse/parser.rs | 2 +- src/test/ui/invalid-self-argument/bare-fn-start.rs | 2 +- src/test/ui/invalid-self-argument/bare-fn-start.stderr | 2 +- src/test/ui/invalid-self-argument/bare-fn.rs | 2 +- src/test/ui/invalid-self-argument/bare-fn.stderr | 2 +- src/test/ui/invalid-self-argument/trait-fn.rs | 2 +- src/test/ui/invalid-self-argument/trait-fn.stderr | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) (limited to 'src/libsyntax/parse/parser.rs') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 18929af4718..e4a4c1f5a7c 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1828,7 +1828,7 @@ impl<'a> Parser<'a> { 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"); + "`self` is only valid as the first argument of an associated function"); return Err(err); } diff --git a/src/test/ui/invalid-self-argument/bare-fn-start.rs b/src/test/ui/invalid-self-argument/bare-fn-start.rs index a84fe55502d..741ba5f41ce 100644 --- a/src/test/ui/invalid-self-argument/bare-fn-start.rs +++ b/src/test/ui/invalid-self-argument/bare-fn-start.rs @@ -1,5 +1,5 @@ fn a(&self) { } //~^ ERROR unexpected `self` argument in function -//~| NOTE `self` is only valid as the first argument of a trait function +//~| NOTE `self` is only valid as the first argument of an associated function fn main() { } diff --git a/src/test/ui/invalid-self-argument/bare-fn-start.stderr b/src/test/ui/invalid-self-argument/bare-fn-start.stderr index d0eca1a9e5c..6a878b619d8 100644 --- a/src/test/ui/invalid-self-argument/bare-fn-start.stderr +++ b/src/test/ui/invalid-self-argument/bare-fn-start.stderr @@ -2,7 +2,7 @@ error: unexpected `self` argument in function --> $DIR/bare-fn-start.rs:1:7 | LL | fn a(&self) { } - | ^^^^ `self` is only valid as the first argument of a trait function + | ^^^^ `self` is only valid as the first argument of an associated function error: aborting due to previous error diff --git a/src/test/ui/invalid-self-argument/bare-fn.rs b/src/test/ui/invalid-self-argument/bare-fn.rs index 27e56a53713..704fa996ca6 100644 --- a/src/test/ui/invalid-self-argument/bare-fn.rs +++ b/src/test/ui/invalid-self-argument/bare-fn.rs @@ -1,5 +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 +//~| NOTE `self` is only valid as the first argument of an associated function fn main() { } diff --git a/src/test/ui/invalid-self-argument/bare-fn.stderr b/src/test/ui/invalid-self-argument/bare-fn.stderr index bd6c598c88a..b13f746a4ec 100644 --- a/src/test/ui/invalid-self-argument/bare-fn.stderr +++ b/src/test/ui/invalid-self-argument/bare-fn.stderr @@ -2,7 +2,7 @@ error: unexpected `self` argument in function --> $DIR/bare-fn.rs:1:21 | LL | fn b(foo: u32, &mut self) { } - | ^^^^ `self` is only valid as the first argument of a trait function + | ^^^^ `self` is only valid as the first argument of an associated function error: aborting due to previous error diff --git a/src/test/ui/invalid-self-argument/trait-fn.rs b/src/test/ui/invalid-self-argument/trait-fn.rs index e2107e4d867..31e867bc764 100644 --- a/src/test/ui/invalid-self-argument/trait-fn.rs +++ b/src/test/ui/invalid-self-argument/trait-fn.rs @@ -3,7 +3,7 @@ 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 + //~| NOTE `self` is only valid as the first argument of an associated function fn good(&mut self, foo: u32) {} } diff --git a/src/test/ui/invalid-self-argument/trait-fn.stderr b/src/test/ui/invalid-self-argument/trait-fn.stderr index d056e53b95c..b3c2cc5b5eb 100644 --- a/src/test/ui/invalid-self-argument/trait-fn.stderr +++ b/src/test/ui/invalid-self-argument/trait-fn.stderr @@ -2,7 +2,7 @@ error: unexpected `self` argument in function --> $DIR/trait-fn.rs:4:20 | LL | fn c(foo: u32, self) {} - | ^^^^ `self` is only valid as the first argument of a trait function + | ^^^^ `self` is only valid as the first argument of an associated function error: aborting due to previous error -- cgit 1.4.1-3-g733a5