Skip to content

Commit e3b8550

Browse files
committed
Point out correct turbofish usage on Foo<Bar<Baz>>
Whenever we parse a chain of binary operations, as long as the first operation is `<` and the subsequent operations are either `>` or `<`, present the following diagnostic help: use `::<...>` instead of `<...>` if you meant to specify type arguments This will lead to spurious recommendations on situations like `2 < 3 < 4` but should be clear from context that the help doesn't apply in that case.
1 parent 6f10e2f commit e3b8550

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

src/libsyntax/parse/parser.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2919,7 +2919,10 @@ impl<'a> Parser<'a> {
29192919
let op_span = mk_sp(op.span.lo, self.span.hi);
29202920
let mut err = self.diagnostic().struct_span_err(op_span,
29212921
"chained comparison operators require parentheses");
2922-
if op.node == BinOpKind::Lt && *outer_op == AssocOp::Greater {
2922+
if op.node == BinOpKind::Lt &&
2923+
*outer_op == AssocOp::Less || // Include `<` to provide this recommendation
2924+
*outer_op == AssocOp::Greater // even in a case like the following:
2925+
{ // Foo<Bar<Baz<Qux, ()>>>
29232926
err.help(
29242927
"use `::<...>` instead of `<...>` if you meant to specify type arguments");
29252928
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2017 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+
fn foo() {
12+
println!("{:?}", (0..13).collect<Vec<i32>>());
13+
}
14+
15+
fn bar() {
16+
println!("{:?}", Vec<i32>::new());
17+
}
18+
19+
fn qux() {
20+
println!("{:?}", (0..13).collect<Vec<i32>());
21+
}
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: chained comparison operators require parentheses
2+
--> $DIR/issue-40396.rs:12:37
3+
|
4+
12 | println!("{:?}", (0..13).collect<Vec<i32>>());
5+
| ^^^^^^^^
6+
|
7+
= help: use `::<...>` instead of `<...>` if you meant to specify type arguments
8+
9+
error: chained comparison operators require parentheses
10+
--> $DIR/issue-40396.rs:16:25
11+
|
12+
16 | println!("{:?}", Vec<i32>::new());
13+
| ^^^^^^^
14+
|
15+
= help: use `::<...>` instead of `<...>` if you meant to specify type arguments
16+
17+
error: chained comparison operators require parentheses
18+
--> $DIR/issue-40396.rs:20:37
19+
|
20+
20 | println!("{:?}", (0..13).collect<Vec<i32>());
21+
| ^^^^^^^^
22+
|
23+
= help: use `::<...>` instead of `<...>` if you meant to specify type arguments
24+
25+
error: chained comparison operators require parentheses
26+
--> $DIR/issue-40396.rs:20:41
27+
|
28+
20 | println!("{:?}", (0..13).collect<Vec<i32>());
29+
| ^^^^^^
30+
|
31+
= help: use `::<...>` instead of `<...>` if you meant to specify type arguments
32+
33+
error: aborting due to 4 previous errors
34+

0 commit comments

Comments
 (0)