Skip to content

Commit

Permalink
Rollup merge of rust-lang#56002 - Axary:master, r=estebank
Browse files Browse the repository at this point in the history
fix rust-lang#55972: Erroneous self arguments on bare functions emit subpar compilation error

rust-lang#55972

r? @estebank
  • Loading branch information
GuillaumeGomez committed Nov 22, 2018
2 parents 636f0a9 + 88d6094 commit 75d226e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 an associated function");
return Err(err);
}

let (pat, ty) = if require_name || self.is_named_argument() {
debug!("parse_arg_general parse_pat (require_name:{})",
require_name);
Expand Down Expand Up @@ -5385,11 +5393,12 @@ 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))?;

let sp = self.span;
let mut variadic = false;
let args: Vec<Option<Arg>> =
self.parse_unspanned_seq(
&token::OpenDelim(token::Paren),
self.parse_seq_to_before_end(
&token::CloseDelim(token::Paren),
SeqSep::trailing_allowed(token::Comma),
|p| {
Expand Down Expand Up @@ -5436,6 +5445,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() {
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/invalid-self-argument/bare-fn-start.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn a(&self) { }
//~^ ERROR unexpected `self` argument in function
//~| NOTE `self` is only valid as the first argument of an associated function

fn main() { }
8 changes: 8 additions & 0 deletions src/test/ui/invalid-self-argument/bare-fn-start.stderr
Original file line number Diff line number Diff line change
@@ -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 an associated function

error: aborting due to previous error

5 changes: 5 additions & 0 deletions src/test/ui/invalid-self-argument/bare-fn.rs
Original file line number Diff line number Diff line change
@@ -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 an associated function

fn main() { }
8 changes: 8 additions & 0 deletions src/test/ui/invalid-self-argument/bare-fn.stderr
Original file line number Diff line number Diff line change
@@ -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 an associated function

error: aborting due to previous error

11 changes: 11 additions & 0 deletions src/test/ui/invalid-self-argument/trait-fn.rs
Original file line number Diff line number Diff line change
@@ -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 an associated function

fn good(&mut self, foo: u32) {}
}

fn main() { }
8 changes: 8 additions & 0 deletions src/test/ui/invalid-self-argument/trait-fn.stderr
Original file line number Diff line number Diff line change
@@ -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 an associated function

error: aborting due to previous error

0 comments on commit 75d226e

Please sign in to comment.