Skip to content

Commit f5db411

Browse files
committed
add suggestion for inverted function parameters
Fixes #54065.
1 parent 6ddab3e commit f5db411

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

src/libsyntax/parse/parser.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -1777,7 +1777,26 @@ impl<'a> Parser<'a> {
17771777
require_name);
17781778
let pat = self.parse_pat()?;
17791779

1780-
self.expect(&token::Colon)?;
1780+
if let Err(mut err) = self.expect(&token::Colon) {
1781+
// If we find a pattern followed by an identifier, it could be an (incorrect)
1782+
// C-style parameter declaration.
1783+
if self.check_ident() && self.look_ahead(1, |t| {
1784+
*t == token::Comma || *t == token::CloseDelim(token::Paren)
1785+
}) {
1786+
let ident = self.parse_ident().unwrap();
1787+
let span = pat.span.with_hi(ident.span.hi());
1788+
1789+
err.span_suggestion_with_applicability(
1790+
span,
1791+
"declare the type after the parameter binding",
1792+
String::from("<identifier>: <type>"),
1793+
Applicability::HasPlaceholders,
1794+
);
1795+
}
1796+
1797+
return Err(err);
1798+
}
1799+
17811800
(pat, self.parse_ty()?)
17821801
} else {
17831802
debug!("parse_arg_general ident_to_pat");
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct S;
12+
13+
impl S {
14+
fn foo(&self, &str bar) {}
15+
//~^ ERROR expected one of `:` or `@`
16+
//~| HELP declare the type after the parameter binding
17+
//~| SUGGESTION <identifier>: <type>
18+
}
19+
20+
fn baz(S quux, xyzzy: i32) {}
21+
//~^ ERROR expected one of `:` or `@`
22+
//~| HELP declare the type after the parameter binding
23+
//~| SUGGESTION <identifier>: <type>
24+
25+
fn one(i32 a b) {}
26+
//~^ ERROR expected one of `:` or `@`
27+
28+
fn pattern((i32, i32) (a, b)) {}
29+
//~^ ERROR expected `:`
30+
31+
fn fizz(i32) {}
32+
//~^ ERROR expected one of `:` or `@`
33+
34+
fn missing_colon(quux S) {}
35+
//~^ ERROR expected one of `:` or `@`
36+
//~| HELP declare the type after the parameter binding
37+
//~| SUGGESTION <identifier>: <type>
38+
39+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error: expected one of `:` or `@`, found `bar`
2+
--> $DIR/inverted-parameters.rs:14:24
3+
|
4+
LL | fn foo(&self, &str bar) {}
5+
| -----^^^
6+
| | |
7+
| | expected one of `:` or `@` here
8+
| help: declare the type after the parameter binding: `<identifier>: <type>`
9+
10+
error: expected one of `:` or `@`, found `quux`
11+
--> $DIR/inverted-parameters.rs:20:10
12+
|
13+
LL | fn baz(S quux, xyzzy: i32) {}
14+
| --^^^^
15+
| | |
16+
| | expected one of `:` or `@` here
17+
| help: declare the type after the parameter binding: `<identifier>: <type>`
18+
19+
error: expected one of `:` or `@`, found `a`
20+
--> $DIR/inverted-parameters.rs:25:12
21+
|
22+
LL | fn one(i32 a b) {}
23+
| ^ expected one of `:` or `@` here
24+
25+
error: expected `:`, found `(`
26+
--> $DIR/inverted-parameters.rs:28:23
27+
|
28+
LL | fn pattern((i32, i32) (a, b)) {}
29+
| ^ expected `:`
30+
31+
error: expected one of `:` or `@`, found `)`
32+
--> $DIR/inverted-parameters.rs:31:12
33+
|
34+
LL | fn fizz(i32) {}
35+
| ^ expected one of `:` or `@` here
36+
37+
error: expected one of `:` or `@`, found `S`
38+
--> $DIR/inverted-parameters.rs:34:23
39+
|
40+
LL | fn missing_colon(quux S) {}
41+
| -----^
42+
| | |
43+
| | expected one of `:` or `@` here
44+
| help: declare the type after the parameter binding: `<identifier>: <type>`
45+
46+
error: aborting due to 6 previous errors
47+

0 commit comments

Comments
 (0)