]> git.lizzy.rs Git - rust.git/commitdiff
Point out correct turbofish usage on `Foo<Bar<Baz>>`
authorEsteban Küber <esteban@kuber.com.ar>
Tue, 14 Mar 2017 02:07:47 +0000 (19:07 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Tue, 14 Mar 2017 19:09:21 +0000 (12:09 -0700)
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.

src/libsyntax/parse/parser.rs
src/test/ui/did_you_mean/issue-40396.rs [new file with mode: 0644]
src/test/ui/did_you_mean/issue-40396.stderr [new file with mode: 0644]

index 8f66c1a2b8cf630eada9a2627ac6360b1d2dd72b..208db8391441c7938c0365c16ae056e88b2d984b 100644 (file)
@@ -2919,7 +2919,10 @@ fn check_no_chained_comparison(&mut self, lhs: &Expr, outer_op: &AssocOp) {
                 let op_span = mk_sp(op.span.lo, self.span.hi);
                 let mut err = self.diagnostic().struct_span_err(op_span,
                     "chained comparison operators require parentheses");
-                if op.node == BinOpKind::Lt && *outer_op == AssocOp::Greater {
+                if op.node == BinOpKind::Lt &&
+                    *outer_op == AssocOp::Less ||  // Include `<` to provide this recommendation
+                    *outer_op == AssocOp::Greater  // even in a case like the following:
+                {                                  //     Foo<Bar<Baz<Qux, ()>>>
                     err.help(
                         "use `::<...>` instead of `<...>` if you meant to specify type arguments");
                 }
diff --git a/src/test/ui/did_you_mean/issue-40396.rs b/src/test/ui/did_you_mean/issue-40396.rs
new file mode 100644 (file)
index 0000000..1eae180
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn foo() {
+    println!("{:?}", (0..13).collect<Vec<i32>>());
+}
+
+fn bar() {
+    println!("{:?}", Vec<i32>::new());
+}
+
+fn qux() {
+    println!("{:?}", (0..13).collect<Vec<i32>());
+}
+
+fn main() {}
diff --git a/src/test/ui/did_you_mean/issue-40396.stderr b/src/test/ui/did_you_mean/issue-40396.stderr
new file mode 100644 (file)
index 0000000..1a0c74d
--- /dev/null
@@ -0,0 +1,34 @@
+error: chained comparison operators require parentheses
+  --> $DIR/issue-40396.rs:12:37
+   |
+12 |     println!("{:?}", (0..13).collect<Vec<i32>>());
+   |                                     ^^^^^^^^
+   |
+   = help: use `::<...>` instead of `<...>` if you meant to specify type arguments
+
+error: chained comparison operators require parentheses
+  --> $DIR/issue-40396.rs:16:25
+   |
+16 |     println!("{:?}", Vec<i32>::new());
+   |                         ^^^^^^^
+   |
+   = help: use `::<...>` instead of `<...>` if you meant to specify type arguments
+
+error: chained comparison operators require parentheses
+  --> $DIR/issue-40396.rs:20:37
+   |
+20 |     println!("{:?}", (0..13).collect<Vec<i32>());
+   |                                     ^^^^^^^^
+   |
+   = help: use `::<...>` instead of `<...>` if you meant to specify type arguments
+
+error: chained comparison operators require parentheses
+  --> $DIR/issue-40396.rs:20:41
+   |
+20 |     println!("{:?}", (0..13).collect<Vec<i32>());
+   |                                         ^^^^^^
+   |
+   = help: use `::<...>` instead of `<...>` if you meant to specify type arguments
+
+error: aborting due to 4 previous errors
+