Commit f06e8e15 authored by bors's avatar bors

Auto merge of #74687 - estebank:bracketless-turbofish, r=matthewjasper

Detect turbofish missing surrounding angle brackets

Fix #74065.
parents fe08fb7b d090e5ed
use super::ty::AllowPlus;
use super::{BlockMode, Parser, PathStyle, SemiColonMode, SeqSep, TokenExpectType, TokenType};
use rustc_ast::ast::{self, BinOpKind, BindingMode, BlockCheckMode, Expr, ExprKind, Item, Param};
use rustc_ast::ast::{AttrVec, ItemKind, Mutability, Pat, PatKind, PathSegment, QSelf, Ty, TyKind};
use rustc_ast::ast::{
self, AngleBracketedArgs, AttrVec, BinOpKind, BindingMode, BlockCheckMode, Expr, ExprKind,
Item, ItemKind, Mutability, Param, Pat, PatKind, PathSegment, QSelf, Ty, TyKind,
};
use rustc_ast::ptr::P;
use rustc_ast::token::{self, Lit, LitKind, TokenKind};
use rustc_ast::util::parser::AssocOp;
......@@ -488,6 +490,57 @@ impl<'a> Parser<'a> {
false
}
/// Check if a method call with an intended turbofish has been written without surrounding
/// angle brackets.
pub(super) fn check_turbofish_missing_angle_brackets(&mut self, segment: &mut PathSegment) {
if token::ModSep == self.token.kind && segment.args.is_none() {
let snapshot = self.clone();
self.bump();
let lo = self.token.span;
match self.parse_angle_args() {
Ok(args) => {
let span = lo.to(self.prev_token.span);
// Detect trailing `>` like in `x.collect::Vec<_>>()`.
let mut trailing_span = self.prev_token.span.shrink_to_hi();
while self.token.kind == token::BinOp(token::Shr)
|| self.token.kind == token::Gt
{
trailing_span = trailing_span.to(self.token.span);
self.bump();
}
if self.token.kind == token::OpenDelim(token::Paren) {
// Recover from bad turbofish: `foo.collect::Vec<_>()`.
let args = AngleBracketedArgs { args, span }.into();
segment.args = args;
self.struct_span_err(
span,
"generic parameters without surrounding angle brackets",
)
.multipart_suggestion(
"surround the type parameters with angle brackets",
vec![
(span.shrink_to_lo(), "<".to_string()),
(trailing_span, ">".to_string()),
],
Applicability::MachineApplicable,
)
.emit();
} else {
// This doesn't look like an invalid turbofish, can't recover parse state.
*self = snapshot;
}
}
Err(mut err) => {
// We could't parse generic parameters, unlikely to be a turbofish. Rely on
// generic parse error instead.
err.cancel();
*self = snapshot;
}
}
}
}
/// Check to see if a pair of chained operators looks like an attempt at chained comparison,
/// e.g. `1 < x <= 3`. If so, suggest either splitting the comparison into two, or
/// parenthesising the leftmost comparison.
......
......@@ -909,8 +909,9 @@ impl<'a> Parser<'a> {
}
let fn_span_lo = self.token.span;
let segment = self.parse_path_segment(PathStyle::Expr)?;
let mut segment = self.parse_path_segment(PathStyle::Expr)?;
self.check_trailing_angle_brackets(&segment, &[&token::OpenDelim(token::Paren)]);
self.check_turbofish_missing_angle_brackets(&mut segment);
if self.check(&token::OpenDelim(token::Paren)) {
// Method call `expr.f()`
......
......@@ -387,7 +387,7 @@ impl<'a> Parser<'a> {
/// Parses (possibly empty) list of generic arguments / associated item constraints,
/// possibly including trailing comma.
fn parse_angle_args(&mut self) -> PResult<'a, Vec<AngleBracketedArg>> {
pub(super) fn parse_angle_args(&mut self) -> PResult<'a, Vec<AngleBracketedArg>> {
let mut args = Vec::new();
while let Some(arg) = self.parse_angle_arg()? {
args.push(arg);
......
fn main() {
let _ = vec![1, 2, 3].into_iter().collect::Vec<_>();
//~^ ERROR generic parameters without surrounding angle brackets
let _ = vec![1, 2, 3].into_iter().collect::Vec<_>>>>();
//~^ ERROR generic parameters without surrounding angle brackets
let _ = vec![1, 2, 3].into_iter().collect::Vec<_>>>();
//~^ ERROR generic parameters without surrounding angle brackets
let _ = vec![1, 2, 3].into_iter().collect::Vec<_>>();
//~^ ERROR generic parameters without surrounding angle brackets
}
error: generic parameters without surrounding angle brackets
--> $DIR/recover-missing-turbofish-surrounding-angle-braket.rs:2:48
|
LL | let _ = vec![1, 2, 3].into_iter().collect::Vec<_>();
| ^^^^^^
|
help: surround the type parameters with angle brackets
|
LL | let _ = vec![1, 2, 3].into_iter().collect::<Vec<_>>();
| ^ ^
error: generic parameters without surrounding angle brackets
--> $DIR/recover-missing-turbofish-surrounding-angle-braket.rs:4:48
|
LL | let _ = vec![1, 2, 3].into_iter().collect::Vec<_>>>>();
| ^^^^^^
|
help: surround the type parameters with angle brackets
|
LL | let _ = vec![1, 2, 3].into_iter().collect::<Vec<_>>();
| ^ ^
error: generic parameters without surrounding angle brackets
--> $DIR/recover-missing-turbofish-surrounding-angle-braket.rs:6:48
|
LL | let _ = vec![1, 2, 3].into_iter().collect::Vec<_>>>();
| ^^^^^^
|
help: surround the type parameters with angle brackets
|
LL | let _ = vec![1, 2, 3].into_iter().collect::<Vec<_>>();
| ^ ^
error: generic parameters without surrounding angle brackets
--> $DIR/recover-missing-turbofish-surrounding-angle-braket.rs:8:48
|
LL | let _ = vec![1, 2, 3].into_iter().collect::Vec<_>>();
| ^^^^^^
|
help: surround the type parameters with angle brackets
|
LL | let _ = vec![1, 2, 3].into_iter().collect::<Vec<_>>();
| ^ ^
error: aborting due to 4 previous errors
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment