From 46a6af12aa8d688aa52279647b01be6035adf22c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 12 Jun 2017 10:22:08 -0700 Subject: [PATCH] Change `<` interpreted as generic arg start warning ``` warning: `<` is interpreted as a start of generic arguments for `usize`, not a comparison --> $DIR/issue-22644.rs:16:33 | 16 | println!("{}", a as usize < b); | - ^ interpreted as generic argument | | | not interpreted as comparison | help: if you want to compare the casted value then write: | println!("{}", (a as usize) < b); ``` --- src/libsyntax/parse/parser.rs | 56 ++++++++++++++++++++-------------- src/test/ui/issue-22644.stderr | 10 +++--- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index ee74d9ec2e6..b3623fbe3ac 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -42,7 +42,7 @@ use ast::RangeEnd; use {ast, attr}; use codemap::{self, CodeMap, Spanned, respan}; use syntax_pos::{self, Span, BytePos}; -use errors::{self, DiagnosticBuilder, Level}; +use errors::{self, DiagnosticBuilder}; use parse::{self, classify, token}; use parse::common::SeqSep; use parse::lexer::TokenAndSpan; @@ -441,7 +441,14 @@ fn dummy_arg(span: Span) -> Arg { Arg { ty: P(ty), pat: pat, id: ast::DUMMY_NODE_ID } } -type RewindPoint = (token::Token, Span, Option, Span, TokenCursor, Vec); +struct RewindPoint { + token: token::Token, + span: Span, + meta_var_span: Option, + prev_span: Span, + token_cursor: TokenCursor, + expected_tokens: Vec, +} impl<'a> Parser<'a> { pub fn new(sess: &'a ParseSess, @@ -2835,11 +2842,13 @@ impl<'a> Parser<'a> { // Rewind to before attempting to parse the type with generics, to get // arround #22644. let rp_err = self.get_rewind_point(); + let sp = rp_err.span.clone(); self.rewind(rp); let lo = self.span; let path = match self.parse_path_without_generics(PathStyle::Type) { Ok(path) => { // Successfully parsed the type leaving a `<` yet to parse + err.cancel(); let codemap = self.sess.codemap(); let suggestion_span = lhs_span.to(self.prev_span); let suggestion = match codemap.span_to_snippet(suggestion_span) { @@ -2850,15 +2859,17 @@ impl<'a> Parser<'a> { Ok(lstring) => format!("`{}`", lstring), _ => "a type".to_string(), }; - err.span_suggestion(suggestion_span, + let msg = format!("`<` is interpreted as a start of generic \ + arguments for {}, not a comparison", + warn_message); + let mut warn = self.sess.span_diagnostic.struct_span_warn(sp, &msg); + warn.span_label(sp, "interpreted as generic argument"); + warn.span_label(self.span, "not interpreted as comparison"); + warn.span_suggestion(suggestion_span, "if you want to compare the casted value \ then write:", suggestion); - err.level = Level::Warning; - err.set_message(&format!("`<` is interpreted as a start of generic \ - arguments for {}, not a comparison", - warn_message)); - err.emit(); + warn.emit(); path } Err(mut path_err) => { @@ -6255,23 +6266,22 @@ impl<'a> Parser<'a> { } fn get_rewind_point(&mut self) -> RewindPoint { - ( - self.token.clone(), - self.span, - self.meta_var_span, - self.prev_span, - self.token_cursor.clone(), - self.expected_tokens.clone(), - ) + RewindPoint { + token: self.token.clone(), + span: self.span, + meta_var_span: self.meta_var_span, + prev_span: self.prev_span, + token_cursor: self.token_cursor.clone(), + expected_tokens: self.expected_tokens.clone(), + } } fn rewind(&mut self, rp: RewindPoint) { - let (token, span, meta_var_span, prev_span, token_cursor, expected_tokens,) = rp; - self.token = token; - self.span = span; - self.meta_var_span = meta_var_span; - self.prev_span = prev_span; - self.token_cursor = token_cursor; - self.expected_tokens = expected_tokens; + self.token = rp.token; + self.span = rp.span; + self.meta_var_span = rp.meta_var_span; + self.prev_span = rp.prev_span; + self.token_cursor = rp.token_cursor; + self.expected_tokens = rp.expected_tokens; } } diff --git a/src/test/ui/issue-22644.stderr b/src/test/ui/issue-22644.stderr index 2cbb4265356..1e786175587 100644 --- a/src/test/ui/issue-22644.stderr +++ b/src/test/ui/issue-22644.stderr @@ -2,7 +2,9 @@ warning: `<` is interpreted as a start of generic arguments for `usize`, not a c --> $DIR/issue-22644.rs:16:33 | 16 | println!("{}", a as usize < b); - | ^ expected one of `!`, `(`, `+`, `,`, `::`, or `>` here + | - ^ interpreted as generic argument + | | + | not interpreted as comparison | help: if you want to compare the casted value then write: | println!("{}", (a as usize) < b); @@ -11,9 +13,9 @@ warning: `<` is interpreted as a start of generic arguments for `usize`, not a c --> $DIR/issue-22644.rs:17:33 | 17 | println!("{}", a as usize < 4); - | -^ unexpected token - | | - | expected one of `>`, identifier, lifetime, or type here + | - ^ interpreted as generic argument + | | + | not interpreted as comparison | help: if you want to compare the casted value then write: | println!("{}", (a as usize) < 4);