Improve parse errors for stray lifetimes in type position by fmease · Pull Request #139854 · rust-lang/rust (original) (raw)
While technically & syntactically speaking lifetimes do begin1 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 #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 #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.
Footnotes
- For example, in the context of decl macros, this implies that a lone
'a
always matches syntax fragmentty
("even if" there's a later macro matcher expecting syntax fragmentlifetime
). Rephrased, lifetimes (in type contexts) commit to the type parser. ↩