Gracefully handle mistyping -> as => in function return type

This commit is contained in:
mibac138 2020-10-14 22:27:48 +02:00
parent c4926d01ad
commit 5404deeb64
9 changed files with 96 additions and 6 deletions

View file

@ -43,6 +43,12 @@ pub(super) enum RecoverQPath {
No,
}
#[derive(PartialEq)]
pub(super) enum RecoverFatArrow {
Yes,
No,
}
// Is `...` (`CVarArgs`) legal at this level of type parsing?
#[derive(PartialEq)]
enum AllowCVariadic {
@ -87,11 +93,26 @@ impl<'a> Parser<'a> {
&mut self,
allow_plus: AllowPlus,
recover_qpath: RecoverQPath,
recover_fat_arrow: RecoverFatArrow,
) -> PResult<'a, FnRetTy> {
Ok(if self.eat(&token::RArrow) {
// FIXME(Centril): Can we unconditionally `allow_plus`?
let ty = self.parse_ty_common(allow_plus, recover_qpath, AllowCVariadic::No)?;
FnRetTy::Ty(ty)
} else if recover_fat_arrow == RecoverFatArrow::Yes && self.token == token::FatArrow {
// Don't `eat` to prevent `=>` from being added as an expected token which isn't
// actually expected and could only confuse users
self.bump();
self.struct_span_err(self.prev_token.span, "return types are denoted using `->`")
.span_suggestion_short(
self.prev_token.span,
"use `->` instead",
"->".to_string(),
Applicability::MachineApplicable,
)
.emit();
let ty = self.parse_ty_common(allow_plus, recover_qpath, AllowCVariadic::No)?;
FnRetTy::Ty(ty)
} else {
FnRetTy::Default(self.token.span.shrink_to_lo())
})