Skip to content

Commit 71a6f58

Browse files
committed
parser: address review comments re. self.
1 parent 8674efd commit 71a6f58

19 files changed

+124
-128
lines changed

src/librustc_ast_passes/ast_validation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,10 @@ impl<'a> AstValidator<'a> {
372372
self.err_handler()
373373
.struct_span_err(
374374
param.span,
375-
"`self` parameter only allowed in associated `fn`s",
375+
"`self` parameter is only allowed in associated functions",
376376
)
377377
.span_label(param.span, "not semantically valid as function parameter")
378-
.note("associated `fn`s are those in `impl` or `trait` definitions")
378+
.note("associated functions are those in `impl` or `trait` definitions")
379379
.emit();
380380
}
381381
}

src/librustc_parse/parser/diagnostics.rs

+7-18
Original file line numberDiff line numberDiff line change
@@ -1336,8 +1336,7 @@ impl<'a> Parser<'a> {
13361336
err: &mut DiagnosticBuilder<'_>,
13371337
pat: P<ast::Pat>,
13381338
require_name: bool,
1339-
is_self_semantic: bool,
1340-
in_assoc_item: bool,
1339+
first_param: bool,
13411340
) -> Option<Ident> {
13421341
// If we find a pattern followed by an identifier, it could be an (incorrect)
13431342
// C-style parameter declaration.
@@ -1357,13 +1356,12 @@ impl<'a> Parser<'a> {
13571356
return Some(ident);
13581357
} else if let PatKind::Ident(_, ident, _) = pat.kind {
13591358
if require_name
1360-
&& (in_assoc_item
1361-
|| self.token == token::Comma
1359+
&& (self.token == token::Comma
13621360
|| self.token == token::Lt
13631361
|| self.token == token::CloseDelim(token::Paren))
13641362
{
13651363
// `fn foo(a, b) {}`, `fn foo(a<x>, b<y>) {}` or `fn foo(usize, usize) {}`
1366-
if is_self_semantic {
1364+
if first_param {
13671365
err.span_suggestion(
13681366
pat.span,
13691367
"if this is a `self` type, give it a parameter name",
@@ -1420,21 +1418,12 @@ impl<'a> Parser<'a> {
14201418
Ok((pat, ty))
14211419
}
14221420

1423-
pub(super) fn recover_bad_self_param(
1424-
&mut self,
1425-
mut param: ast::Param,
1426-
in_assoc_item: bool,
1427-
) -> PResult<'a, ast::Param> {
1421+
pub(super) fn recover_bad_self_param(&mut self, mut param: Param) -> PResult<'a, Param> {
14281422
let sp = param.pat.span;
14291423
param.ty.kind = TyKind::Err;
1430-
let mut err = self.struct_span_err(sp, "unexpected `self` parameter in function");
1431-
if in_assoc_item {
1432-
err.span_label(sp, "must be the first associated function parameter");
1433-
} else {
1434-
err.span_label(sp, "not valid as function parameter");
1435-
err.note("`self` is only valid as the first parameter of an associated function");
1436-
}
1437-
err.emit();
1424+
self.struct_span_err(sp, "unexpected `self` parameter in function")
1425+
.span_label(sp, "must be the first parameter of an associated function")
1426+
.emit();
14381427
Ok(param)
14391428
}
14401429

src/librustc_parse/parser/item.rs

+7-19
Original file line numberDiff line numberDiff line change
@@ -1715,9 +1715,6 @@ impl<'a> Parser<'a> {
17151715

17161716
/// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
17171717
pub(super) struct ParamCfg {
1718-
/// Is `self` is *semantically* allowed as the first parameter?
1719-
/// This is only used for diagnostics.
1720-
pub in_assoc_item: bool,
17211718
/// `is_name_required` decides if, per-parameter,
17221719
/// the parameter must have a pattern or just a type.
17231720
pub is_name_required: fn(&token::Token) -> bool,
@@ -1733,7 +1730,7 @@ impl<'a> Parser<'a> {
17331730
attrs: Vec<Attribute>,
17341731
header: FnHeader,
17351732
) -> PResult<'a, Option<P<Item>>> {
1736-
let cfg = ParamCfg { in_assoc_item: false, is_name_required: |_| true };
1733+
let cfg = ParamCfg { is_name_required: |_| true };
17371734
let (ident, decl, generics) = self.parse_fn_sig(&cfg)?;
17381735
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
17391736
let kind = ItemKind::Fn(FnSig { decl, header }, generics, body);
@@ -1748,7 +1745,7 @@ impl<'a> Parser<'a> {
17481745
attrs: Vec<Attribute>,
17491746
extern_sp: Span,
17501747
) -> PResult<'a, P<ForeignItem>> {
1751-
let cfg = ParamCfg { in_assoc_item: false, is_name_required: |_| true };
1748+
let cfg = ParamCfg { is_name_required: |_| true };
17521749
self.expect_keyword(kw::Fn)?;
17531750
let (ident, decl, generics) = self.parse_fn_sig(&cfg)?;
17541751
let span = lo.to(self.token.span);
@@ -1763,9 +1760,8 @@ impl<'a> Parser<'a> {
17631760
attrs: &mut Vec<Attribute>,
17641761
is_name_required: fn(&token::Token) -> bool,
17651762
) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
1766-
let cfg = ParamCfg { in_assoc_item: true, is_name_required };
17671763
let header = self.parse_fn_front_matter()?;
1768-
let (ident, decl, generics) = self.parse_fn_sig(&cfg)?;
1764+
let (ident, decl, generics) = self.parse_fn_sig(&ParamCfg { is_name_required })?;
17691765
let sig = FnSig { header, decl };
17701766
let body = self.parse_assoc_fn_body(at_end, attrs)?;
17711767
Ok((ident, AssocItemKind::Fn(sig, body), generics))
@@ -1893,11 +1889,7 @@ impl<'a> Parser<'a> {
18931889
// Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
18941890
if let Some(mut param) = self.parse_self_param()? {
18951891
param.attrs = attrs.into();
1896-
return if first_param {
1897-
Ok(param)
1898-
} else {
1899-
self.recover_bad_self_param(param, cfg.in_assoc_item)
1900-
};
1892+
return if first_param { Ok(param) } else { self.recover_bad_self_param(param) };
19011893
}
19021894

19031895
let is_name_required = match self.token.kind {
@@ -1909,13 +1901,9 @@ impl<'a> Parser<'a> {
19091901

19101902
let pat = self.parse_fn_param_pat()?;
19111903
if let Err(mut err) = self.expect(&token::Colon) {
1912-
return if let Some(ident) = self.parameter_without_type(
1913-
&mut err,
1914-
pat,
1915-
is_name_required,
1916-
first_param && cfg.in_assoc_item,
1917-
cfg.in_assoc_item,
1918-
) {
1904+
return if let Some(ident) =
1905+
self.parameter_without_type(&mut err, pat, is_name_required, first_param)
1906+
{
19191907
err.emit();
19201908
Ok(dummy_arg(ident))
19211909
} else {

src/librustc_parse/parser/ty.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ impl<'a> Parser<'a> {
288288
let unsafety = self.parse_unsafety();
289289
let ext = self.parse_extern()?;
290290
self.expect_keyword(kw::Fn)?;
291-
let cfg = ParamCfg { in_assoc_item: false, is_name_required: |_| false };
292-
let decl = self.parse_fn_decl(&cfg, false)?;
291+
let decl = self.parse_fn_decl(&ParamCfg { is_name_required: |_| false }, false)?;
293292
Ok(TyKind::BareFn(P(BareFnTy { ext, unsafety, generic_params, decl })))
294293
}
295294

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn a(&self) { }
2-
//~^ ERROR `self` parameter only allowed in associated `fn`s
2+
//~^ ERROR `self` parameter is only allowed in associated functions
33
//~| NOTE not semantically valid as function parameter
4-
//~| NOTE associated `fn`s are those in `impl` or `trait` definitions
4+
//~| NOTE associated functions are those in `impl` or `trait` definitions
55

66
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: `self` parameter only allowed in associated `fn`s
1+
error: `self` parameter is only allowed in associated functions
22
--> $DIR/bare-fn-start.rs:1:6
33
|
44
LL | fn a(&self) { }
55
| ^^^^^ not semantically valid as function parameter
66
|
7-
= note: associated `fn`s are those in `impl` or `trait` definitions
7+
= note: associated functions are those in `impl` or `trait` definitions
88

99
error: aborting due to previous error
1010

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
fn b(foo: u32, &mut self) { }
22
//~^ ERROR unexpected `self` parameter in function
3-
//~| NOTE not valid as function parameter
4-
//~| NOTE `self` is only valid as the first parameter of an associated function
3+
//~| NOTE must be the first parameter of an associated function
54

65
fn main() { }

src/test/ui/invalid-self-argument/bare-fn.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error: unexpected `self` parameter in function
22
--> $DIR/bare-fn.rs:1:16
33
|
44
LL | fn b(foo: u32, &mut self) { }
5-
| ^^^^^^^^^ not valid as function parameter
6-
|
7-
= note: `self` is only valid as the first parameter of an associated function
5+
| ^^^^^^^^^ must be the first parameter of an associated function
86

97
error: aborting due to previous error
108

src/test/ui/invalid-self-argument/trait-fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ struct Foo {}
33
impl Foo {
44
fn c(foo: u32, self) {}
55
//~^ ERROR unexpected `self` parameter in function
6-
//~| NOTE must be the first associated function parameter
6+
//~| NOTE must be the first parameter of an associated function
77

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

src/test/ui/invalid-self-argument/trait-fn.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: unexpected `self` parameter in function
22
--> $DIR/trait-fn.rs:4:20
33
|
44
LL | fn c(foo: u32, self) {}
5-
| ^^^^ must be the first associated function parameter
5+
| ^^^^ must be the first parameter of an associated function
66

77
error: aborting due to previous error
88

src/test/ui/parser/inverted-parameters.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fn pattern((i32, i32) (a, b)) {}
2121
fn fizz(i32) {}
2222
//~^ ERROR expected one of `:`, `@`
2323
//~| HELP if this was a parameter name, give it a type
24+
//~| HELP if this is a `self` type, give it a parameter name
2425
//~| HELP if this is a type, explicitly ignore the parameter name
2526

2627
fn missing_colon(quux S) {}

src/test/ui/parser/inverted-parameters.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ LL | fn fizz(i32) {}
3535
| ^ expected one of `:`, `@`, or `|`
3636
|
3737
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
38+
help: if this is a `self` type, give it a parameter name
39+
|
40+
LL | fn fizz(self: i32) {}
41+
| ^^^^^^^^^
3842
help: if this was a parameter name, give it a type
3943
|
4044
LL | fn fizz(i32: TypeName) {}
@@ -45,7 +49,7 @@ LL | fn fizz(_: i32) {}
4549
| ^^^^^^
4650

4751
error: expected one of `:`, `@`, or `|`, found `S`
48-
--> $DIR/inverted-parameters.rs:26:23
52+
--> $DIR/inverted-parameters.rs:27:23
4953
|
5054
LL | fn missing_colon(quux S) {}
5155
| -----^

src/test/ui/parser/omitted-arg-in-item-fn.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ LL | fn foo(x) {
55
| ^ expected one of `:`, `@`, or `|`
66
|
77
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
8+
help: if this is a `self` type, give it a parameter name
9+
|
10+
LL | fn foo(self: x) {
11+
| ^^^^^^^
812
help: if this was a parameter name, give it a type
913
|
1014
LL | fn foo(x: TypeName) {

src/test/ui/parser/pat-lt-bracket-2.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ LL | fn a(B<) {}
55
| ^ expected one of `:`, `@`, or `|`
66
|
77
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
8+
help: if this is a `self` type, give it a parameter name
9+
|
10+
LL | fn a(self: B<) {}
11+
| ^^^^^^^
812
help: if this is a type, explicitly ignore the parameter name
913
|
1014
LL | fn a(_: B<) {}

src/test/ui/parser/self-in-function-arg.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error: unexpected `self` parameter in function
22
--> $DIR/self-in-function-arg.rs:1:15
33
|
44
LL | fn foo(x:i32, self: i32) -> i32 { self }
5-
| ^^^^ not valid as function parameter
6-
|
7-
= note: `self` is only valid as the first parameter of an associated function
5+
| ^^^^ must be the first parameter of an associated function
86

97
error: aborting due to previous error
108

src/test/ui/parser/self-param-semantic-fail.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,59 @@ fn main() {}
66

77
fn free() {
88
fn f1(self) {}
9-
//~^ ERROR `self` parameter only allowed in associated `fn`s
9+
//~^ ERROR `self` parameter is only allowed in associated functions
1010
fn f2(mut self) {}
11-
//~^ ERROR `self` parameter only allowed in associated `fn`s
11+
//~^ ERROR `self` parameter is only allowed in associated functions
1212
fn f3(&self) {}
13-
//~^ ERROR `self` parameter only allowed in associated `fn`s
13+
//~^ ERROR `self` parameter is only allowed in associated functions
1414
fn f4(&mut self) {}
15-
//~^ ERROR `self` parameter only allowed in associated `fn`s
15+
//~^ ERROR `self` parameter is only allowed in associated functions
1616
fn f5<'a>(&'a self) {}
17-
//~^ ERROR `self` parameter only allowed in associated `fn`s
17+
//~^ ERROR `self` parameter is only allowed in associated functions
1818
fn f6<'a>(&'a mut self) {}
19-
//~^ ERROR `self` parameter only allowed in associated `fn`s
19+
//~^ ERROR `self` parameter is only allowed in associated functions
2020
fn f7(self: u8) {}
21-
//~^ ERROR `self` parameter only allowed in associated `fn`s
21+
//~^ ERROR `self` parameter is only allowed in associated functions
2222
fn f8(mut self: u8) {}
23-
//~^ ERROR `self` parameter only allowed in associated `fn`s
23+
//~^ ERROR `self` parameter is only allowed in associated functions
2424
}
2525

2626
extern {
2727
fn f1(self);
28-
//~^ ERROR `self` parameter only allowed in associated `fn`s
28+
//~^ ERROR `self` parameter is only allowed in associated functions
2929
fn f2(mut self);
30-
//~^ ERROR `self` parameter only allowed in associated `fn`s
30+
//~^ ERROR `self` parameter is only allowed in associated functions
3131
//~| ERROR patterns aren't allowed in
3232
fn f3(&self);
33-
//~^ ERROR `self` parameter only allowed in associated `fn`s
33+
//~^ ERROR `self` parameter is only allowed in associated functions
3434
fn f4(&mut self);
35-
//~^ ERROR `self` parameter only allowed in associated `fn`s
35+
//~^ ERROR `self` parameter is only allowed in associated functions
3636
fn f5<'a>(&'a self);
37-
//~^ ERROR `self` parameter only allowed in associated `fn`s
37+
//~^ ERROR `self` parameter is only allowed in associated functions
3838
fn f6<'a>(&'a mut self);
39-
//~^ ERROR `self` parameter only allowed in associated `fn`s
39+
//~^ ERROR `self` parameter is only allowed in associated functions
4040
fn f7(self: u8);
41-
//~^ ERROR `self` parameter only allowed in associated `fn`s
41+
//~^ ERROR `self` parameter is only allowed in associated functions
4242
fn f8(mut self: u8);
43-
//~^ ERROR `self` parameter only allowed in associated `fn`s
43+
//~^ ERROR `self` parameter is only allowed in associated functions
4444
//~| ERROR patterns aren't allowed in
4545
}
4646

4747
type X1 = fn(self);
48-
//~^ ERROR `self` parameter only allowed in associated `fn`s
48+
//~^ ERROR `self` parameter is only allowed in associated functions
4949
type X2 = fn(mut self);
50-
//~^ ERROR `self` parameter only allowed in associated `fn`s
50+
//~^ ERROR `self` parameter is only allowed in associated functions
5151
//~| ERROR patterns aren't allowed in
5252
type X3 = fn(&self);
53-
//~^ ERROR `self` parameter only allowed in associated `fn`s
53+
//~^ ERROR `self` parameter is only allowed in associated functions
5454
type X4 = fn(&mut self);
55-
//~^ ERROR `self` parameter only allowed in associated `fn`s
55+
//~^ ERROR `self` parameter is only allowed in associated functions
5656
type X5 = for<'a> fn(&'a self);
57-
//~^ ERROR `self` parameter only allowed in associated `fn`s
57+
//~^ ERROR `self` parameter is only allowed in associated functions
5858
type X6 = for<'a> fn(&'a mut self);
59-
//~^ ERROR `self` parameter only allowed in associated `fn`s
59+
//~^ ERROR `self` parameter is only allowed in associated functions
6060
type X7 = fn(self: u8);
61-
//~^ ERROR `self` parameter only allowed in associated `fn`s
61+
//~^ ERROR `self` parameter is only allowed in associated functions
6262
type X8 = fn(mut self: u8);
63-
//~^ ERROR `self` parameter only allowed in associated `fn`s
63+
//~^ ERROR `self` parameter is only allowed in associated functions
6464
//~| ERROR patterns aren't allowed in

0 commit comments

Comments
 (0)