Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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);
```
  • Loading branch information
estebank committed Jun 12, 2017
1 parent a6d3215 commit 3a7dbf4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/librustc_errors/diagnostic.rs
Expand Up @@ -248,6 +248,10 @@ impl Diagnostic {
self.message.iter().map(|i| i.0.to_owned()).collect::<String>()
}

pub fn set_message(&mut self, message: &str) {
self.message = vec![(message.to_owned(), Style::NoStyle)];
}

pub fn styled_message(&self) -> &Vec<(String, Style)> {
&self.message
}
Expand Down
21 changes: 19 additions & 2 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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!("(<expression>)")
};
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) => {
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions src/test/ui/issue-22644.stderr
@@ -0,0 +1,20 @@
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);

warning: `<` is interpreted as a start of generic arguments for `usize`, not comparison
--> $DIR/issue-22644.rs:17:33
|
17 | println!("{}", a as usize < 4);
| -^ unexpected token
| |
| expected one of `>`, identifier, lifetime, or type here
|
help: if you want to compare the casted value then write
| println!("{}", (a as usize) < 4);

0 comments on commit 3a7dbf4

Please sign in to comment.