From d37a6d83e1626db51041b6337328cde603f1bc19 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Esteban=20K=C3=BCber?= Date: Sat, 19 Jan 2019 19:25:28 -0800 Subject: [PATCH] Suggest usage of angle brackets --- src/librustc/hir/lowering.rs | 15 ++++++++++++--- src/libsyntax/parse/parser.rs | 2 +- src/test/ui/error-codes/E0214.stderr | 5 ++++- src/test/ui/issues/issue-23589.stderr | 5 ++++- src/test/ui/issues/issue-32995.stderr | 8 ++++---- .../parser/type-parameters-in-field-exprs.stderr | 12 ++++++------ src/test/ui/span/macro-ty-params.stderr | 8 ++++---- .../unboxed-closure-sugar-used-on-struct-1.stderr | 5 ++++- .../unboxed-closure-sugar-used-on-struct-3.stderr | 7 +++++-- .../unboxed-closure-sugar-used-on-struct.stderr | 5 ++++- 10 files changed, 48 insertions(+), 24 deletions(-) diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index b7624b09eee..9d3438289a0 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -31,6 +31,7 @@ //! in the HIR, especially for multiple identifiers. use dep_graph::DepGraph; +use errors::Applicability; use hir::{self, ParamName}; use hir::HirVec; use hir::map::{DefKey, DefPathData, Definitions}; @@ -1823,9 +1824,17 @@ fn lower_path_segment( (hir::GenericArgs::none(), true) } ParenthesizedGenericArgs::Err => { - struct_span_err!(self.sess, data.span, E0214, "{}", msg) - .span_label(data.span, "only traits may use parentheses") - .emit(); + let mut err = struct_span_err!(self.sess, data.span, E0214, "{}", msg); + err.span_label(data.span, "only traits may use parentheses"); + if let Ok(snippet) = self.sess.source_map().span_to_snippet(data.span) { + err.span_suggestion_with_applicability( + data.span, + "use angle brackets instead", + format!("<{}>", &snippet[1..snippet.len() - 1]), + Applicability::MaybeIncorrect, + ); + }; + err.emit(); (self.lower_angle_bracketed_parameter_data( &data.as_angle_bracketed_args(), param_mode, diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 7e15b231276..e09b7a9dd7b 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2176,11 +2176,11 @@ fn parse_path_segment(&mut self, style: PathStyle, enable_warning: bool) style != PathStyle::Mod && self.check(&token::ModSep) && self.look_ahead(1, |t| is_args_start(t)) { // Generic arguments are found - `<`, `(`, `::<` or `::(`. - let lo = self.span; if self.eat(&token::ModSep) && style == PathStyle::Type && enable_warning { self.diagnostic().struct_span_warn(self.prev_span, "unnecessary path disambiguator") .span_label(self.prev_span, "try removing `::`").emit(); } + let lo = self.span; let args = if self.eat_lt() { // `<'a, T, A = U>` diff --git a/src/test/ui/error-codes/E0214.stderr b/src/test/ui/error-codes/E0214.stderr index 08a98b1c3bf..0172dc706ac 100644 --- a/src/test/ui/error-codes/E0214.stderr +++ b/src/test/ui/error-codes/E0214.stderr @@ -2,7 +2,10 @@ error[E0214]: parenthesized parameters may only be used with a trait --> $DIR/E0214.rs:2:15 | LL | let v: Vec(&str) = vec!["foo"]; - | ^^^^^^ only traits may use parentheses + | ^^^^^^ + | | + | only traits may use parentheses + | help: use angle brackets instead: `<&str>` error: aborting due to previous error diff --git a/src/test/ui/issues/issue-23589.stderr b/src/test/ui/issues/issue-23589.stderr index c71061606df..932e8eedad1 100644 --- a/src/test/ui/issues/issue-23589.stderr +++ b/src/test/ui/issues/issue-23589.stderr @@ -2,7 +2,10 @@ error[E0214]: parenthesized parameters may only be used with a trait --> $DIR/issue-23589.rs:2:15 | LL | let v: Vec(&str) = vec!['1', '2']; - | ^^^^^^ only traits may use parentheses + | ^^^^^^ + | | + | only traits may use parentheses + | help: use angle brackets instead: `<&str>` error[E0308]: mismatched types --> $DIR/issue-23589.rs:2:29 diff --git a/src/test/ui/issues/issue-32995.stderr b/src/test/ui/issues/issue-32995.stderr index 12551bb7f1f..e27f2d9553a 100644 --- a/src/test/ui/issues/issue-32995.stderr +++ b/src/test/ui/issues/issue-32995.stderr @@ -18,19 +18,19 @@ LL | let b: ::std::boxed()::Box<_> = Box::new(1); = note: for more information, see issue #42238 error: parenthesized parameters may only be used with a trait - --> $DIR/issue-32995.rs:12:23 + --> $DIR/issue-32995.rs:12:25 | LL | let p = ::std::str::()::from_utf8(b"foo").unwrap(); - | ^^^^ + | ^^ | = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #42238 error: parenthesized parameters may only be used with a trait - --> $DIR/issue-32995.rs:16:34 + --> $DIR/issue-32995.rs:16:36 | LL | let p = ::std::str::from_utf8::()(b"foo").unwrap(); - | ^^^^ + | ^^ | = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #42238 diff --git a/src/test/ui/parser/type-parameters-in-field-exprs.stderr b/src/test/ui/parser/type-parameters-in-field-exprs.stderr index e68e1f3204a..2183c74da0a 100644 --- a/src/test/ui/parser/type-parameters-in-field-exprs.stderr +++ b/src/test/ui/parser/type-parameters-in-field-exprs.stderr @@ -1,20 +1,20 @@ error: field expressions may not have generic arguments - --> $DIR/type-parameters-in-field-exprs.rs:13:8 + --> $DIR/type-parameters-in-field-exprs.rs:13:10 | LL | f.x::; - | ^^^^^^^^^ + | ^^^^^^^ error: field expressions may not have generic arguments - --> $DIR/type-parameters-in-field-exprs.rs:15:8 + --> $DIR/type-parameters-in-field-exprs.rs:15:10 | LL | f.x::<>; - | ^^^^ + | ^^ error: field expressions may not have generic arguments - --> $DIR/type-parameters-in-field-exprs.rs:17:8 + --> $DIR/type-parameters-in-field-exprs.rs:17:10 | LL | f.x::(); - | ^^^^ + | ^^ error: aborting due to 3 previous errors diff --git a/src/test/ui/span/macro-ty-params.stderr b/src/test/ui/span/macro-ty-params.stderr index 23fdde06e8f..965ca7000be 100644 --- a/src/test/ui/span/macro-ty-params.stderr +++ b/src/test/ui/span/macro-ty-params.stderr @@ -1,14 +1,14 @@ error: generic arguments in macro path - --> $DIR/macro-ty-params.rs:10:8 + --> $DIR/macro-ty-params.rs:10:10 | LL | foo::!(); //~ ERROR generic arguments in macro path - | ^^^^^ + | ^^^ error: generic arguments in macro path - --> $DIR/macro-ty-params.rs:11:8 + --> $DIR/macro-ty-params.rs:11:10 | LL | foo::<>!(); //~ ERROR generic arguments in macro path - | ^^^^ + | ^^ error: unexpected generic arguments in path --> $DIR/macro-ty-params.rs:12:8 diff --git a/src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct-1.stderr b/src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct-1.stderr index ab2a6f03cf7..87832d839b9 100644 --- a/src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct-1.stderr +++ b/src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct-1.stderr @@ -2,7 +2,10 @@ error[E0214]: parenthesized parameters may only be used with a trait --> $DIR/unboxed-closure-sugar-used-on-struct-1.rs:8:19 | LL | let x: Box = panic!(); - | ^^ only traits may use parentheses + | ^^ + | | + | only traits may use parentheses + | help: use angle brackets instead: `<>` error[E0107]: wrong number of type arguments: expected 1, found 0 --> $DIR/unboxed-closure-sugar-used-on-struct-1.rs:8:16 diff --git a/src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct-3.stderr b/src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct-3.stderr index 395f6596cfd..c9cc60717a6 100644 --- a/src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct-3.stderr +++ b/src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct-3.stderr @@ -1,8 +1,11 @@ error[E0214]: parenthesized parameters may only be used with a trait - --> $DIR/unboxed-closure-sugar-used-on-struct-3.rs:14:16 + --> $DIR/unboxed-closure-sugar-used-on-struct-3.rs:14:18 | LL | let b = Bar::(isize, usize)::new(); // OK too (for the parser) - | ^^^^^^^^^^^^^^^^ only traits may use parentheses + | ^^^^^^^^^^^^^^ + | | + | only traits may use parentheses + | help: use angle brackets instead: `` error: aborting due to previous error diff --git a/src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct.stderr b/src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct.stderr index 7bcf86774c7..a09c2b75386 100644 --- a/src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct.stderr +++ b/src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct.stderr @@ -2,7 +2,10 @@ error[E0214]: parenthesized parameters may only be used with a trait --> $DIR/unboxed-closure-sugar-used-on-struct.rs:7:18 | LL | fn foo(b: Box) { - | ^^ only traits may use parentheses + | ^^ + | | + | only traits may use parentheses + | help: use angle brackets instead: `<>` error[E0107]: wrong number of type arguments: expected 1, found 0 --> $DIR/unboxed-closure-sugar-used-on-struct.rs:7:15 -- 2.44.0