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

Refactor path segment parameter error #98119

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 26 additions & 19 deletions compiler/rustc_ast_lowering/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,25 +196,32 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
ParenthesizedGenericArgs::Err => {
let mut err = struct_span_err!(self.sess, data.span, E0214, "{}", msg);
err.span_label(data.span, "only `Fn` traits may use parentheses");
if let Ok(snippet) = self.sess.source_map().span_to_snippet(data.span) {
// Do not suggest going from `Trait()` to `Trait<>`
if !data.inputs.is_empty() {
// Suggest replacing `(` and `)` with `<` and `>`
// The snippet may be missing the closing `)`, skip that case
if snippet.ends_with(')') {
if let Some(split) = snippet.find('(') {
let trait_name = &snippet[0..split];
let args = &snippet[split + 1..snippet.len() - 1];
err.span_suggestion(
data.span,
"use angle brackets instead",
format!("{}<{}>", trait_name, args),
Applicability::MaybeIncorrect,
);
}
}
}
};
// Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
if !data.inputs.is_empty() {
// Start of the span to the 1st character of 1st argument
let open_param = data.inputs_span.shrink_to_lo().to(data
.inputs
.first()
.unwrap()
.span
.shrink_to_lo());
// Last character position of last argument to the end of the span
let close_param = data
.inputs
.last()
.unwrap()
.span
.shrink_to_hi()
.to(data.inputs_span.shrink_to_hi());
err.multipart_suggestion(
&format!("use angle brackets instead",),
vec![
(open_param, String::from("<")),
(close_param, String::from(">")),
],
Applicability::MaybeIncorrect,
);
}
err.emit();
(
self.lower_angle_bracketed_parameter_data(
Expand Down
10 changes: 6 additions & 4 deletions src/test/ui/error-codes/E0214.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
--> $DIR/E0214.rs:2:12
|
LL | let v: Vec(&str) = vec!["foo"];
| ^^^^^^^^^
| |
| only `Fn` traits may use parentheses
| help: use angle brackets instead: `Vec<&str>`
| ^^^^^^^^^ only `Fn` traits may use parentheses
|
help: use angle brackets instead
|
LL | let v: Vec<&str> = vec!["foo"];
| ~ ~

error: aborting due to previous error

Expand Down
10 changes: 6 additions & 4 deletions src/test/ui/issues/issue-23589.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
--> $DIR/issue-23589.rs:2:12
|
LL | let v: Vec(&str) = vec!['1', '2'];
| ^^^^^^^^^
| |
| only `Fn` traits may use parentheses
| help: use angle brackets instead: `Vec<&str>`
| ^^^^^^^^^ only `Fn` traits may use parentheses
|
help: use angle brackets instead
|
LL | let v: Vec<&str> = vec!['1', '2'];
| ~ ~

error[E0308]: mismatched types
--> $DIR/issue-23589.rs:2:29
Expand Down
10 changes: 6 additions & 4 deletions src/test/ui/proc-macro/issue-66286.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
--> $DIR/issue-66286.rs:8:22
|
LL | pub extern fn foo(_: Vec(u32)) -> u32 {
| ^^^^^^^^
| |
| only `Fn` traits may use parentheses
| help: use angle brackets instead: `Vec<u32>`
| ^^^^^^^^ only `Fn` traits may use parentheses
|
help: use angle brackets instead
|
LL | pub extern fn foo(_: Vec<u32>) -> u32 {
| ~ ~

error: aborting due to previous error

Expand Down
10 changes: 6 additions & 4 deletions src/test/ui/suggestions/let-binding-init-expr-as-ty.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
--> $DIR/let-binding-init-expr-as-ty.rs:2:19
|
LL | let foo: i32::from_be(num);
| ^^^^^^^^^^^^
| |
| only `Fn` traits may use parentheses
| help: use angle brackets instead: `from_be<num>`
| ^^^^^^^^^^^^ only `Fn` traits may use parentheses
|
help: use angle brackets instead
|
LL | let foo: i32::from_be<num>;
| ~ ~

error[E0223]: ambiguous associated type
--> $DIR/let-binding-init-expr-as-ty.rs:2:14
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/type/issue-91268.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
|
LL | 0: u8(ţ
| ^^^^ only `Fn` traits may use parentheses
|
help: use angle brackets instead
|
LL | 0: u8<ţ>
| ~ +

error[E0109]: type arguments are not allowed on this type
--> $DIR/issue-91268.rs:9:11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
--> $DIR/unboxed-closure-sugar-used-on-struct-3.rs:14:13
|
LL | let b = Bar::(isize, usize)::new(); // OK too (for the parser)
| ^^^^^^^^^^^^^^^^^^^
| |
| only `Fn` traits may use parentheses
| help: use angle brackets instead: `Bar::<isize, usize>`
| ^^^^^^^^^^^^^^^^^^^ only `Fn` traits may use parentheses
|
help: use angle brackets instead
|
LL | let b = Bar::<isize, usize>::new(); // OK too (for the parser)
| ~ ~

error: aborting due to previous error

Expand Down