Skip to content

Commit dce7e87

Browse files
committed
Reduce arbitrary self type suggestions
1 parent 8c8af6c commit dce7e87

File tree

4 files changed

+15
-32
lines changed

4 files changed

+15
-32
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,7 +2019,7 @@ impl<'a> Parser<'a> {
20192019
{
20202020
let rfc_note = "anonymous parameters are removed in the 2018 edition (see RFC 1685)";
20212021

2022-
let (ident, self_sugg, param_sugg, type_sugg, self_span, param_span, type_span, maybe_name) =
2022+
let (ident, self_sugg, param_sugg, type_sugg, self_span, param_span, type_span) =
20232023
match pat.kind {
20242024
PatKind::Ident(_, ident, _) => (
20252025
ident,
@@ -2029,7 +2029,6 @@ impl<'a> Parser<'a> {
20292029
pat.span.shrink_to_lo(),
20302030
pat.span.shrink_to_hi(),
20312031
pat.span.shrink_to_lo(),
2032-
true,
20332032
),
20342033
// Also catches `fn foo(&a)`.
20352034
PatKind::Ref(ref inner_pat, mutab)
@@ -2046,22 +2045,11 @@ impl<'a> Parser<'a> {
20462045
pat.span.shrink_to_lo(),
20472046
pat.span,
20482047
pat.span.shrink_to_lo(),
2049-
true,
20502048
)
20512049
}
20522050
_ => unreachable!(),
20532051
}
2054-
},
2055-
PatKind::Path(_, ref path) if let Some(segment) = path.segments.last() => (
2056-
segment.ident,
2057-
"self: ",
2058-
": TypeName".to_string(),
2059-
"_: ",
2060-
pat.span.shrink_to_lo(),
2061-
pat.span.shrink_to_hi(),
2062-
pat.span.shrink_to_lo(),
2063-
path.segments.len() == 1, // Avoid suggesting that `fn foo(a::b)` is fixed with a change to `fn foo(a::b: TypeName)`.
2064-
),
2052+
}
20652053
_ => {
20662054
// Otherwise, try to get a type and emit a suggestion.
20672055
if let Some(ty) = pat.to_ty() {
@@ -2089,7 +2077,7 @@ impl<'a> Parser<'a> {
20892077
}
20902078
// Avoid suggesting that `fn foo(HashMap<u32>)` is fixed with a change to
20912079
// `fn foo(HashMap: TypeName<u32>)`.
2092-
if self.token != token::Lt && maybe_name {
2080+
if self.token != token::Lt {
20932081
err.span_suggestion(
20942082
param_span,
20952083
"if this is a parameter name, give it a type",

tests/ui/anon-params/anon-params-denied-2018.stderr

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,10 @@ LL | fn foo_with_qualified_path(<Bar as T>::Baz);
4545
| ^ expected one of 8 possible tokens
4646
|
4747
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
48-
help: if this is a `self` type, give it a parameter name
49-
|
50-
LL | fn foo_with_qualified_path(self: <Bar as T>::Baz);
51-
| +++++
52-
help: if this is a type, explicitly ignore the parameter name
48+
help: explicitly ignore the parameter name
5349
|
5450
LL | fn foo_with_qualified_path(_: <Bar as T>::Baz);
55-
| ++
51+
| ~~~~~~~~~~~~~~~~~~
5652

5753
error: expected one of `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `)`
5854
--> $DIR/anon-params-denied-2018.rs:15:56
@@ -73,14 +69,10 @@ LL | fn foo_with_multiple_qualified_paths(<Bar as T>::Baz, <Bar as T>::Baz);
7369
| ^ expected one of 8 possible tokens
7470
|
7571
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
76-
help: if this is a `self` type, give it a parameter name
77-
|
78-
LL | fn foo_with_multiple_qualified_paths(self: <Bar as T>::Baz, <Bar as T>::Baz);
79-
| +++++
80-
help: if this is a type, explicitly ignore the parameter name
72+
help: explicitly ignore the parameter name
8173
|
8274
LL | fn foo_with_multiple_qualified_paths(_: <Bar as T>::Baz, <Bar as T>::Baz);
83-
| ++
75+
| ~~~~~~~~~~~~~~~~~~
8476

8577
error: expected one of `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `)`
8678
--> $DIR/anon-params-denied-2018.rs:18:74
@@ -89,10 +81,10 @@ LL | fn foo_with_multiple_qualified_paths(<Bar as T>::Baz, <Bar as T>::Baz);
8981
| ^ expected one of 8 possible tokens
9082
|
9183
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
92-
help: if this is a type, explicitly ignore the parameter name
84+
help: explicitly ignore the parameter name
9385
|
9486
LL | fn foo_with_multiple_qualified_paths(<Bar as T>::Baz, _: <Bar as T>::Baz);
95-
| ++
87+
| ~~~~~~~~~~~~~~~~~~
9688

9789
error: expected one of `:`, `@`, or `|`, found `,`
9890
--> $DIR/anon-params-denied-2018.rs:22:36
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// This test checks that a suggestion to add a `self: ` parameter name is provided
22
// to functions where this is applicable.
33

4-
pub fn foo(Box<Self>) { } //~ ERROR expected one of
4+
pub fn foo(Box<Self>) { }
5+
//~^ ERROR expected one of `:`, `@`, or `|`, found `<`
6+
57
struct Bar;
68

79
impl Bar {
8-
fn bar(Box<Self>) { } //~ ERROR expected one of
10+
fn bar(Box<Self>) { }
11+
//~^ ERROR expected one of `:`, `@`, or `|`, found `<`
912
}
1013

1114
fn main() { }

tests/ui/suggestions/issue-64252-self-type.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LL | pub fn foo(_: Box<Self>) { }
1515
| ++
1616

1717
error: expected one of `:`, `@`, or `|`, found `<`
18-
--> $DIR/issue-64252-self-type.rs:8:15
18+
--> $DIR/issue-64252-self-type.rs:10:15
1919
|
2020
LL | fn bar(Box<Self>) { }
2121
| ^ expected one of `:`, `@`, or `|`

0 commit comments

Comments
 (0)