Skip to content

Commit 75d226e

Browse files
Rollup merge of rust-lang#56002 - Axary:master, r=estebank
fix rust-lang#55972: Erroneous self arguments on bare functions emit subpar compilation error rust-lang#55972 r? @estebank
2 parents 636f0a9 + 88d6094 commit 75d226e

File tree

7 files changed

+58
-2
lines changed

7 files changed

+58
-2
lines changed

src/libsyntax/parse/parser.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,14 @@ impl<'a> Parser<'a> {
18241824
fn parse_arg_general(&mut self, require_name: bool) -> PResult<'a, Arg> {
18251825
maybe_whole!(self, NtArg, |x| x);
18261826

1827+
if let Ok(Some(_)) = self.parse_self_arg() {
1828+
let mut err = self.struct_span_err(self.prev_span,
1829+
"unexpected `self` argument in function");
1830+
err.span_label(self.prev_span,
1831+
"`self` is only valid as the first argument of an associated function");
1832+
return Err(err);
1833+
}
1834+
18271835
let (pat, ty) = if require_name || self.is_named_argument() {
18281836
debug!("parse_arg_general parse_pat (require_name:{})",
18291837
require_name);
@@ -5385,11 +5393,12 @@ impl<'a> Parser<'a> {
53855393

53865394
fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool)
53875395
-> PResult<'a, (Vec<Arg> , bool)> {
5396+
self.expect(&token::OpenDelim(token::Paren))?;
5397+
53885398
let sp = self.span;
53895399
let mut variadic = false;
53905400
let args: Vec<Option<Arg>> =
5391-
self.parse_unspanned_seq(
5392-
&token::OpenDelim(token::Paren),
5401+
self.parse_seq_to_before_end(
53935402
&token::CloseDelim(token::Paren),
53945403
SeqSep::trailing_allowed(token::Comma),
53955404
|p| {
@@ -5436,6 +5445,8 @@ impl<'a> Parser<'a> {
54365445
}
54375446
)?;
54385447

5448+
self.eat(&token::CloseDelim(token::Paren));
5449+
54395450
let args: Vec<_> = args.into_iter().filter_map(|x| x).collect();
54405451

54415452
if variadic && args.is_empty() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn a(&self) { }
2+
//~^ ERROR unexpected `self` argument in function
3+
//~| NOTE `self` is only valid as the first argument of an associated function
4+
5+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected `self` argument in function
2+
--> $DIR/bare-fn-start.rs:1:7
3+
|
4+
LL | fn a(&self) { }
5+
| ^^^^ `self` is only valid as the first argument of an associated function
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn b(foo: u32, &mut self) { }
2+
//~^ ERROR unexpected `self` argument in function
3+
//~| NOTE `self` is only valid as the first argument of an associated function
4+
5+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected `self` argument in function
2+
--> $DIR/bare-fn.rs:1:21
3+
|
4+
LL | fn b(foo: u32, &mut self) { }
5+
| ^^^^ `self` is only valid as the first argument of an associated function
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct Foo {}
2+
3+
impl Foo {
4+
fn c(foo: u32, self) {}
5+
//~^ ERROR unexpected `self` argument in function
6+
//~| NOTE `self` is only valid as the first argument of an associated function
7+
8+
fn good(&mut self, foo: u32) {}
9+
}
10+
11+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected `self` argument in function
2+
--> $DIR/trait-fn.rs:4:20
3+
|
4+
LL | fn c(foo: u32, self) {}
5+
| ^^^^ `self` is only valid as the first argument of an associated function
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)