Skip to content

Commit 2ab6cef

Browse files
committed
Do not suggest angle brackets when there are no type arguments
1 parent d37a6d8 commit 2ab6cef

14 files changed

+45
-48
lines changed

src/librustc/hir/lowering.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ impl<'a> LoweringContext<'a> {
18071807
explicit_owner: Option<NodeId>,
18081808
) -> hir::PathSegment {
18091809
let (mut generic_args, infer_types) = if let Some(ref generic_args) = segment.args {
1810-
let msg = "parenthesized parameters may only be used with a trait";
1810+
let msg = "parenthesized type parameters may only be used with a `Fn` trait";
18111811
match **generic_args {
18121812
GenericArgs::AngleBracketed(ref data) => {
18131813
self.lower_angle_bracketed_parameter_data(data, param_mode, itctx)
@@ -1825,14 +1825,17 @@ impl<'a> LoweringContext<'a> {
18251825
}
18261826
ParenthesizedGenericArgs::Err => {
18271827
let mut err = struct_span_err!(self.sess, data.span, E0214, "{}", msg);
1828-
err.span_label(data.span, "only traits may use parentheses");
1828+
err.span_label(data.span, "only `Fn` traits may use parentheses");
18291829
if let Ok(snippet) = self.sess.source_map().span_to_snippet(data.span) {
1830-
err.span_suggestion_with_applicability(
1831-
data.span,
1832-
"use angle brackets instead",
1833-
format!("<{}>", &snippet[1..snippet.len() - 1]),
1834-
Applicability::MaybeIncorrect,
1835-
);
1830+
// Do not suggest going from `Trait()` to `Trait<>`
1831+
if data.inputs.len() > 0 {
1832+
err.span_suggestion_with_applicability(
1833+
data.span,
1834+
"use angle brackets instead",
1835+
format!("<{}>", &snippet[1..snippet.len() - 1]),
1836+
Applicability::MaybeIncorrect,
1837+
);
1838+
}
18361839
};
18371840
err.emit();
18381841
(self.lower_angle_bracketed_parameter_data(

src/test/ui/error-codes/E0214.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0214]: parenthesized parameters may only be used with a trait
1+
error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
22
--> $DIR/E0214.rs:2:15
33
|
44
LL | let v: Vec(&str) = vec!["foo"];
55
| ^^^^^^
66
| |
7-
| only traits may use parentheses
7+
| only `Fn` traits may use parentheses
88
| help: use angle brackets instead: `<&str>`
99

1010
error: aborting due to previous error

src/test/ui/issues/issue-23589.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
22
let v: Vec(&str) = vec!['1', '2'];
3-
//~^ ERROR parenthesized parameters may only be used with a trait
3+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
44
//~| ERROR mismatched types
55
}

src/test/ui/issues/issue-23589.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0214]: parenthesized parameters may only be used with a trait
1+
error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
22
--> $DIR/issue-23589.rs:2:15
33
|
44
LL | let v: Vec(&str) = vec!['1', '2'];
55
| ^^^^^^
66
| |
7-
| only traits may use parentheses
7+
| only `Fn` traits may use parentheses
88
| help: use angle brackets instead: `<&str>`
99

1010
error[E0308]: mismatched types

src/test/ui/issues/issue-32995-2.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
fn main() {
44
{ fn f<X: ::std::marker()::Send>() {} }
5-
//~^ ERROR parenthesized parameters may only be used with a trait
5+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
66
//~| WARN previously accepted
77

88
{ fn f() -> impl ::std::marker()::Send { } }
9-
//~^ ERROR parenthesized parameters may only be used with a trait
9+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
1010
//~| WARN previously accepted
1111
}
1212

1313
#[derive(Clone)]
1414
struct X;
1515

1616
impl ::std::marker()::Copy for X {}
17-
//~^ ERROR parenthesized parameters may only be used with a trait
17+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
1818
//~| WARN previously accepted

src/test/ui/issues/issue-32995-2.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: parenthesized parameters may only be used with a trait
1+
error: parenthesized type parameters may only be used with a `Fn` trait
22
--> $DIR/issue-32995-2.rs:4:28
33
|
44
LL | { fn f<X: ::std::marker()::Send>() {} }
@@ -8,7 +8,7 @@ LL | { fn f<X: ::std::marker()::Send>() {} }
88
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
99
= note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>
1010

11-
error: parenthesized parameters may only be used with a trait
11+
error: parenthesized type parameters may only be used with a `Fn` trait
1212
--> $DIR/issue-32995-2.rs:8:35
1313
|
1414
LL | { fn f() -> impl ::std::marker()::Send { } }
@@ -17,7 +17,7 @@ LL | { fn f() -> impl ::std::marker()::Send { } }
1717
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1818
= note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>
1919

20-
error: parenthesized parameters may only be used with a trait
20+
error: parenthesized type parameters may only be used with a `Fn` trait
2121
--> $DIR/issue-32995-2.rs:16:19
2222
|
2323
LL | impl ::std::marker()::Copy for X {}

src/test/ui/issues/issue-32995.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@
22

33
fn main() {
44
let x: usize() = 1;
5-
//~^ ERROR parenthesized parameters may only be used with a trait
5+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
66
//~| WARN previously accepted
77

88
let b: ::std::boxed()::Box<_> = Box::new(1);
9-
//~^ ERROR parenthesized parameters may only be used with a trait
9+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
1010
//~| WARN previously accepted
1111

1212
let p = ::std::str::()::from_utf8(b"foo").unwrap();
13-
//~^ ERROR parenthesized parameters may only be used with a trait
13+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
1414
//~| WARN previously accepted
1515

1616
let p = ::std::str::from_utf8::()(b"foo").unwrap();
17-
//~^ ERROR parenthesized parameters may only be used with a trait
17+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
1818
//~| WARN previously accepted
1919

2020
let o : Box<::std::marker()::Send> = Box::new(1);
21-
//~^ ERROR parenthesized parameters may only be used with a trait
21+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
2222
//~| WARN previously accepted
2323

2424
let o : Box<Send + ::std::marker()::Sync> = Box::new(1);
25-
//~^ ERROR parenthesized parameters may only be used with a trait
25+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
2626
//~| WARN previously accepted
2727
}
2828

2929
fn foo<X:Default>() {
3030
let d : X() = Default::default();
31-
//~^ ERROR parenthesized parameters may only be used with a trait
31+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
3232
//~| WARN previously accepted
3333
}

src/test/ui/issues/issue-32995.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: parenthesized parameters may only be used with a trait
1+
error: parenthesized type parameters may only be used with a `Fn` trait
22
--> $DIR/issue-32995.rs:4:17
33
|
44
LL | let x: usize() = 1;
@@ -8,7 +8,7 @@ LL | let x: usize() = 1;
88
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
99
= note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>
1010

11-
error: parenthesized parameters may only be used with a trait
11+
error: parenthesized type parameters may only be used with a `Fn` trait
1212
--> $DIR/issue-32995.rs:8:24
1313
|
1414
LL | let b: ::std::boxed()::Box<_> = Box::new(1);
@@ -17,7 +17,7 @@ LL | let b: ::std::boxed()::Box<_> = Box::new(1);
1717
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1818
= note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>
1919

20-
error: parenthesized parameters may only be used with a trait
20+
error: parenthesized type parameters may only be used with a `Fn` trait
2121
--> $DIR/issue-32995.rs:12:25
2222
|
2323
LL | let p = ::std::str::()::from_utf8(b"foo").unwrap();
@@ -26,7 +26,7 @@ LL | let p = ::std::str::()::from_utf8(b"foo").unwrap();
2626
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2727
= note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>
2828

29-
error: parenthesized parameters may only be used with a trait
29+
error: parenthesized type parameters may only be used with a `Fn` trait
3030
--> $DIR/issue-32995.rs:16:36
3131
|
3232
LL | let p = ::std::str::from_utf8::()(b"foo").unwrap();
@@ -35,7 +35,7 @@ LL | let p = ::std::str::from_utf8::()(b"foo").unwrap();
3535
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3636
= note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>
3737

38-
error: parenthesized parameters may only be used with a trait
38+
error: parenthesized type parameters may only be used with a `Fn` trait
3939
--> $DIR/issue-32995.rs:20:30
4040
|
4141
LL | let o : Box<::std::marker()::Send> = Box::new(1);
@@ -44,7 +44,7 @@ LL | let o : Box<::std::marker()::Send> = Box::new(1);
4444
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4545
= note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>
4646

47-
error: parenthesized parameters may only be used with a trait
47+
error: parenthesized type parameters may only be used with a `Fn` trait
4848
--> $DIR/issue-32995.rs:24:37
4949
|
5050
LL | let o : Box<Send + ::std::marker()::Sync> = Box::new(1);
@@ -53,7 +53,7 @@ LL | let o : Box<Send + ::std::marker()::Sync> = Box::new(1);
5353
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
5454
= note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>
5555

56-
error: parenthesized parameters may only be used with a trait
56+
error: parenthesized type parameters may only be used with a `Fn` trait
5757
--> $DIR/issue-32995.rs:30:14
5858
|
5959
LL | let d : X() = Default::default();

src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ struct Bar<A> {
66

77
fn bar() {
88
let x: Box<Bar()> = panic!();
9-
//~^ ERROR parenthesized parameters may only be used with a trait
9+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
1010
//~| ERROR wrong number of type arguments: expected 1, found 0
1111
}
1212

src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct-1.stderr

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0214]: parenthesized parameters may only be used with a trait
1+
error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
22
--> $DIR/unboxed-closure-sugar-used-on-struct-1.rs:8:19
33
|
44
LL | let x: Box<Bar()> = panic!();
5-
| ^^
6-
| |
7-
| only traits may use parentheses
8-
| help: use angle brackets instead: `<>`
5+
| ^^ only `Fn` traits may use parentheses
96

107
error[E0107]: wrong number of type arguments: expected 1, found 0
118
--> $DIR/unboxed-closure-sugar-used-on-struct-1.rs:8:16

src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct-3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn bar() {
1212
let b = Bar::<isize, usize>::new(); // OK
1313

1414
let b = Bar::(isize, usize)::new(); // OK too (for the parser)
15-
//~^ ERROR parenthesized parameters may only be used with a trait
15+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
1616
}
1717

1818
fn main() {}

src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct-3.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0214]: parenthesized parameters may only be used with a trait
1+
error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
22
--> $DIR/unboxed-closure-sugar-used-on-struct-3.rs:14:18
33
|
44
LL | let b = Bar::(isize, usize)::new(); // OK too (for the parser)
55
| ^^^^^^^^^^^^^^
66
| |
7-
| only traits may use parentheses
7+
| only `Fn` traits may use parentheses
88
| help: use angle brackets instead: `<isize, usize>`
99

1010
error: aborting due to previous error

src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ struct Bar<A> {
55
}
66

77
fn foo(b: Box<Bar()>) {
8-
//~^ ERROR parenthesized parameters may only be used with a trait
8+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
99
//~| ERROR wrong number of type arguments: expected 1, found 0
1010
}
1111

src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct.stderr

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0214]: parenthesized parameters may only be used with a trait
1+
error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
22
--> $DIR/unboxed-closure-sugar-used-on-struct.rs:7:18
33
|
44
LL | fn foo(b: Box<Bar()>) {
5-
| ^^
6-
| |
7-
| only traits may use parentheses
8-
| help: use angle brackets instead: `<>`
5+
| ^^ only `Fn` traits may use parentheses
96

107
error[E0107]: wrong number of type arguments: expected 1, found 0
118
--> $DIR/unboxed-closure-sugar-used-on-struct.rs:7:15

0 commit comments

Comments
 (0)