From 3a7dbf48feb325bbe8517bc0fd7546e80931c8ed Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Sun, 11 Jun 2017 23:47:26 -0700 Subject: Suggest non-ambiguous comparison after cast ``` warning: `<` is interpreted as a start of generic arguments for `usize`, not comparison --> $DIR/issue-22644.rs:16:33 | 16 | println!("{}", a as usize < b); | ^ expected one of `!`, `(`, `+`, `,`, `::`, or `>` here | help: if you want to compare the casted value then write | println!("{}", (a as usize) < b); ``` --- src/libsyntax/parse/parser.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src/libsyntax/parse/parser.rs') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index a6ecd304dbd..76b14071ec3 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}; +use errors::{self, DiagnosticBuilder, Level}; use parse::{self, classify, token}; use parse::common::SeqSep; use parse::lexer::TokenAndSpan; @@ -2840,7 +2840,24 @@ impl<'a> Parser<'a> { 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) { + Ok(lstring) => format!("({})", lstring), + _ => format!("()") + }; + let warn_message = match codemap.span_to_snippet(self.prev_span) { + Ok(lstring) => format!("`{}`", lstring), + _ => "a type".to_string(), + }; + err.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(); path } Err(mut path_err) => { -- cgit 1.4.1-3-g733a5