Rollup merge of #139854 - fmease:modern-diag-for-lt-in-ty, r=davidtwco
Improve parse errors for stray lifetimes in type position While technically & syntactically speaking lifetimes do begin[^1] types in type contexts (this essentially excludes generic argument lists) and require a following `+` to form a complete type (`'a +` denotes a bare trait object type), the likelihood that a user meant to write a lifetime-prefixed bare trait object type in *modern* editions (Rust ≥2021) when placing a lifetime into a type context is incredibly low (they would need to add at least three tokens to turn it into a *semantically* well-formed TOT: `'a` → `dyn 'a + Trait`). Therefore let's *lie* in modern editions (just like in PR https://github.com/rust-lang/rust/pull/131239, a precedent if you will) by stating "*expected type, found lifetime*" in such cases which is a lot more a approachable, digestible and friendly compared to "*lifetime in trait object type must be followed by `+`*" (as added in PR https://github.com/rust-lang/rust/pull/69760). I've also added recovery for "ampersand-less" reference types (e.g., `'a ()`, `'a mut Ty`) in modern editions because it was trivial to do and I think it's not unlikely to occur in practice. Fixes #133413. [^1]: For example, in the context of decl macros, this implies that a lone `'a` always matches syntax fragment `ty` ("even if" there's a later macro matcher expecting syntax fragment `lifetime`). Rephrased, lifetimes (in type contexts) *commit* to the type parser.
This commit is contained in:
commit
7ab385e2e1
25 changed files with 283 additions and 92 deletions
|
@ -1657,19 +1657,19 @@ impl<'a> Parser<'a> {
|
|||
|
||||
self.bump(); // `+`
|
||||
let _bounds = self.parse_generic_bounds()?;
|
||||
let sum_span = ty.span.to(self.prev_token.span);
|
||||
|
||||
let sub = match &ty.kind {
|
||||
TyKind::Ref(_lifetime, mut_ty) => {
|
||||
let lo = mut_ty.ty.span.shrink_to_lo();
|
||||
let hi = self.prev_token.span.shrink_to_hi();
|
||||
BadTypePlusSub::AddParen { suggestion: AddParen { lo, hi } }
|
||||
}
|
||||
TyKind::Ptr(..) | TyKind::BareFn(..) => BadTypePlusSub::ForgotParen { span: sum_span },
|
||||
_ => BadTypePlusSub::ExpectPath { span: sum_span },
|
||||
TyKind::Ptr(..) | TyKind::BareFn(..) => {
|
||||
BadTypePlusSub::ForgotParen { span: ty.span.to(self.prev_token.span) }
|
||||
}
|
||||
_ => BadTypePlusSub::ExpectPath { span: ty.span },
|
||||
};
|
||||
|
||||
self.dcx().emit_err(BadTypePlus { ty: pprust::ty_to_string(ty), span: sum_span, sub });
|
||||
self.dcx().emit_err(BadTypePlus { span: ty.span, sub });
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue