8000 When encountering chained operators use heuristics to recover from bad turbofish by estebank · Pull Request #64909 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

When encountering chained operators use heuristics to recover from bad turbofish #64909

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 8 commits into from
Oct 6, 2019
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
Account for missing turbofish in paths too
  • Loading branch information
estebank committed Oct 3, 2019
commit d7dceaa0c57759d37a48b5a0aa1064b7c89f957b
47 changes: 43 additions & 4 deletions src/libsyntax/parse/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,12 +585,51 @@ impl<'a> Parser<'a> {
let early_return = vec![token::Eof];
self.consume_tts(1, &modifiers[..], &early_return[..]);

if self.token.kind != token::OpenDelim(token::Paren) {
// We don't have `foo< bar >(`, so we rewind the parser and bail out.
if !&[
token::OpenDelim(token::Paren),
token::ModSep,
].contains(&self.token.kind) {
// We don't have `foo< bar >(` or `foo< bar >::`, so we rewind the
// parser and bail out.
mem::replace(self, snapshot.clone());
}
}
if self.token.kind == token::OpenDelim(token::Paren) {
if token::ModSep == self.token.kind {
// We have some certainty that this was a bad turbofish at this point.
// `foo< bar >::`
err.span_suggestion(
op_span.shrink_to_lo(),
msg,
"::".to_string(),
Applicability::MaybeIncorrect,
);

let snapshot = self.clone();

self.bump(); // `::`
// Consume the rest of the likely `foo<bar>::new()` or return at `foo<bar>`.
match self.parse_expr() {
Ok(_) => {
// 99% certain that the suggestion is correct, continue parsing.
err.emit();
// FIXME: actually check that the two expressions in the binop are
// paths and resynthesize new fn call expression instead of using
// `ExprKind::Err` placeholder.
return Ok(Some(self.mk_expr(
lhs.span.to(self.prev_span),
ExprKind::Err,
ThinVec::new(),
)));
}
Err(mut err) => {
err.cancel();
// Not entirely sure now, but we bubble the error up with the
// suggestion.
mem::replace(self, snapshot);
return Err(err);
}
}
} else if token::OpenDelim(token::Paren) == self.token.kind {
// We have high certainty that this was a bad turbofish at this point.
// `foo< bar >(`
err.span_suggestion(
Expand All @@ -601,14 +640,14 @@ impl<'a> Parser<'a> {
);

let snapshot = self.clone();
self.bump(); // `(`

// Consume the fn call arguments.
let modifiers = vec![
(token::OpenDelim(token::Paren), 1),
(token::CloseDelim(token::Paren), -1),
];
let early_return = vec![token::Eof];
self.bump(); // `(`
self.consume_tts(1, &modifiers[..], &early_return[..]);

if self.token.kind == token::Eof {
Expand Down
5 changes: 3 additions & 2 deletions src/test/ui/did_you_mean/issue-40396.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ error: chained comparison operators require parentheses
|
LL | Vec<i32>::new();
| ^^^^^
help: use `::<...>` instead of `<...>` if you meant to specify type arguments
|
= help: use `::<...>` instead of `<...>` if you meant to specify type arguments
= help: or use `(...)` if you meant to specify fn arguments
LL | Vec::<i32>::new();
| ^^

error: chained comparison operators require parentheses
--> $DIR/issue-40396.rs:12:20
Expand Down
0