Skip to content

Commit 3a7dbf4

Browse files
committed
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); ```
1 parent a6d3215 commit 3a7dbf4

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

src/librustc_errors/diagnostic.rs

+4
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ impl Diagnostic {
248248
self.message.iter().map(|i| i.0.to_owned()).collect::<String>()
249249
}
250250

251+
pub fn set_message(&mut self, message: &str) {
252+
self.message = vec![(message.to_owned(), Style::NoStyle)];
253+
}
254+
251255
pub fn styled_message(&self) -> &Vec<(String, Style)> {
252256
&self.message
253257
}

src/libsyntax/parse/parser.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use ast::RangeEnd;
4242
use {ast, attr};
4343
use codemap::{self, CodeMap, Spanned, respan};
4444
use syntax_pos::{self, Span, BytePos};
45-
use errors::{self, DiagnosticBuilder};
45+
use errors::{self, DiagnosticBuilder, Level};
4646
use parse::{self, classify, token};
4747
use parse::common::SeqSep;
4848
use parse::lexer::TokenAndSpan;
@@ -2840,7 +2840,24 @@ impl<'a> Parser<'a> {
28402840
let path = match self.parse_path_without_generics(PathStyle::Type) {
28412841
Ok(path) => {
28422842
// Successfully parsed the type leaving a `<` yet to parse
2843-
err.cancel();
2843+
let codemap = self.sess.codemap();
2844+
let suggestion_span = lhs_span.to(self.prev_span);
2845+
let suggestion = match codemap.span_to_snippet(suggestion_span) {
2846+
Ok(lstring) => format!("({})", lstring),
2847+
_ => format!("(<expression>)")
2848+
};
2849+
let warn_message = match codemap.span_to_snippet(self.prev_span) {
2850+
Ok(lstring) => format!("`{}`", lstring),
2851+
_ => "a type".to_string(),
2852+
};
2853+
err.span_suggestion(suggestion_span,
2854+
"if you want to compare the casted value then write",
2855+
suggestion);
2856+
err.level = Level::Warning;
2857+
err.set_message(&format!("`<` is interpreted as a start of generic \
2858+
arguments for {}, not a comparison",
2859+
warn_message));
2860+
err.emit();
28442861
path
28452862
}
28462863
Err(mut path_err) => {
File renamed without changes.

src/test/ui/issue-22644.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
warning: `<` is interpreted as a start of generic arguments for `usize`, not comparison
2+
--> $DIR/issue-22644.rs:16:33
3+
|
4+
16 | println!("{}", a as usize < b);
5+
| ^ expected one of `!`, `(`, `+`, `,`, `::`, or `>` here
6+
|
7+
help: if you want to compare the casted value then write
8+
| println!("{}", (a as usize) < b);
9+
10+
warning: `<` is interpreted as a start of generic arguments for `usize`, not comparison
11+
--> $DIR/issue-22644.rs:17:33
12+
|
13+
17 | println!("{}", a as usize < 4);
14+
| -^ unexpected token
15+
| |
16+
| expected one of `>`, identifier, lifetime, or type here
17+
|
18+
help: if you want to compare the casted value then write
19+
| println!("{}", (a as usize) < 4);
20+

0 commit comments

Comments
 (0)