Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #55972: Erroneous self arguments on bare functions emit subpar compilation error #56002

Merged
merged 9 commits into from
Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 a trait function");
lcnr marked this conversation as resolved.
Show resolved Hide resolved
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 a trait 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 a trait 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 a trait 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 a trait 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 a trait function
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a function in an inherent impl, it would be nice if the error could emphasize here that the problem is that it must be the first argument.

Copy link
Contributor Author

@lcnr lcnr Nov 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion it is not worth to handle invalid uses of self differently. To implement this I would probably change parse_arg_general(&mut self, require_name: bool) to parse_arg_general(&mut self, require_name: bool, is_bare_fn: bool), which is somewhat ugly.

I did think about simple ways to check if we currently parse a associated function but did not find one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a big deal, but still would be nice.

But I am not knowledgeable in the relevant APIs either.^^


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 a trait function

error: aborting due to previous error