-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Learn to parse a as usize < b
#42578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
a6d3215
3a7dbf4
5fcfa08
46a6af1
ad260ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
<
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); ```
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>, Span, TokenCursor, Vec<TokenType>); | ||
struct RewindPoint { | ||
token: token::Token, | ||
span: Span, | ||
meta_var_span: Option<Span>, | ||
prev_span: Span, | ||
token_cursor: TokenCursor, | ||
expected_tokens: Vec<TokenType>, | ||
} | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you move the error handling logic into a separate function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
// 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test for this case as well? |
||
|
@@ -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; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, could you just clone
Parser
instead? Is it very heavy? (as TYPE <
shouldn't happen often so it may be ok.)(Some comment about the purpose of
RewindPoint
would be useful regardless.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed. Using
Parser.clone()
andmem::replace()
instead ofRewindPoint
.