Skip to content

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

Merged
merged 5 commits into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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);
```
  • Loading branch information
estebank committed Jun 12, 2017
commit 46a6af12aa8d688aa52279647b01be6035adf22c
56 changes: 33 additions & 23 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
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, Level};
use errors::{self, DiagnosticBuilder};
use parse::{self, classify, token};
use parse::common::SeqSep;
use parse::lexer::TokenAndSpan;
Expand Down Expand Up @@ -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 {
Copy link
Contributor

@petrochenkov petrochenkov Jun 13, 2017

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.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed. Using Parser.clone() and mem::replace() instead of 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,
Expand Down Expand Up @@ -2835,11 +2842,13 @@ impl<'a> Parser<'a> {
// Rewind to before attempting to parse the type with generics, to get
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move the error handling logic into a separate function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) {
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct_span_err please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for this case as well?

Expand Down Expand Up @@ -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;
}
}
10 changes: 6 additions & 4 deletions src/test/ui/issue-22644.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down