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
|
@ -30,7 +30,6 @@ pub(crate) struct AmbiguousPlus {
|
|||
#[derive(Diagnostic)]
|
||||
#[diag(parse_maybe_recover_from_bad_type_plus, code = E0178)]
|
||||
pub(crate) struct BadTypePlus {
|
||||
pub ty: String,
|
||||
#[primary_span]
|
||||
pub span: Span,
|
||||
#[subdiagnostic]
|
||||
|
@ -2806,6 +2805,8 @@ pub(crate) struct ReturnTypesUseThinArrow {
|
|||
pub(crate) struct NeedPlusAfterTraitObjectLifetime {
|
||||
#[primary_span]
|
||||
pub span: Span,
|
||||
#[suggestion(code = " + /* Trait */", applicability = "has-placeholders")]
|
||||
pub suggestion: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ use rustc_ast::{
|
|||
Pinnedness, PolyTraitRef, PreciseCapturingArg, TraitBoundModifiers, TraitObjectSyntax, Ty,
|
||||
TyKind, UnsafeBinderTy,
|
||||
};
|
||||
use rustc_errors::{Applicability, PResult};
|
||||
use rustc_errors::{Applicability, Diag, PResult};
|
||||
use rustc_span::{ErrorGuaranteed, Ident, Span, kw, sym};
|
||||
use thin_vec::{ThinVec, thin_vec};
|
||||
|
||||
|
@ -411,6 +411,9 @@ impl<'a> Parser<'a> {
|
|||
TyKind::Path(None, path) if maybe_bounds => {
|
||||
self.parse_remaining_bounds_path(ThinVec::new(), path, lo, true)
|
||||
}
|
||||
// For `('a) + …`, we know that `'a` in type position already lead to an error being
|
||||
// emitted. To reduce output, let's indirectly suppress E0178 (bad `+` in type) and
|
||||
// other irrelevant consequential errors.
|
||||
TyKind::TraitObject(bounds, TraitObjectSyntax::None)
|
||||
if maybe_bounds && bounds.len() == 1 && !trailing_plus =>
|
||||
{
|
||||
|
@ -425,12 +428,60 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
fn parse_bare_trait_object(&mut self, lo: Span, allow_plus: AllowPlus) -> PResult<'a, TyKind> {
|
||||
let lt_no_plus = self.check_lifetime() && !self.look_ahead(1, |t| t.is_like_plus());
|
||||
let bounds = self.parse_generic_bounds_common(allow_plus)?;
|
||||
if lt_no_plus {
|
||||
self.dcx().emit_err(NeedPlusAfterTraitObjectLifetime { span: lo });
|
||||
// A lifetime only begins a bare trait object type if it is followed by `+`!
|
||||
if self.token.is_lifetime() && !self.look_ahead(1, |t| t.is_like_plus()) {
|
||||
// In Rust 2021 and beyond, we assume that the user didn't intend to write a bare trait
|
||||
// object type with a leading lifetime bound since that seems very unlikely given the
|
||||
// fact that `dyn`-less trait objects are *semantically* invalid.
|
||||
if self.psess.edition.at_least_rust_2021() {
|
||||
let lt = self.expect_lifetime();
|
||||
let mut err = self.dcx().struct_span_err(lo, "expected type, found lifetime");
|
||||
err.span_label(lo, "expected type");
|
||||
return Ok(match self.maybe_recover_ref_ty_no_leading_ampersand(lt, lo, err) {
|
||||
Ok(ref_ty) => ref_ty,
|
||||
Err(err) => TyKind::Err(err.emit()),
|
||||
});
|
||||
}
|
||||
|
||||
self.dcx().emit_err(NeedPlusAfterTraitObjectLifetime {
|
||||
span: lo,
|
||||
suggestion: lo.shrink_to_hi(),
|
||||
});
|
||||
}
|
||||
Ok(TyKind::TraitObject(
|
||||
self.parse_generic_bounds_common(allow_plus)?,
|
||||
TraitObjectSyntax::None,
|
||||
))
|
||||
}
|
||||
|
||||
fn maybe_recover_ref_ty_no_leading_ampersand<'cx>(
|
||||
&mut self,
|
||||
lt: Lifetime,
|
||||
lo: Span,
|
||||
mut err: Diag<'cx>,
|
||||
) -> Result<TyKind, Diag<'cx>> {
|
||||
if !self.may_recover() {
|
||||
return Err(err);
|
||||
}
|
||||
let snapshot = self.create_snapshot_for_diagnostic();
|
||||
let mutbl = self.parse_mutability();
|
||||
match self.parse_ty_no_plus() {
|
||||
Ok(ty) => {
|
||||
err.span_suggestion_verbose(
|
||||
lo.shrink_to_lo(),
|
||||
"you might have meant to write a reference type here",
|
||||
"&",
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
err.emit();
|
||||
Ok(TyKind::Ref(Some(lt), MutTy { ty, mutbl }))
|
||||
}
|
||||
Err(diag) => {
|
||||
diag.cancel();
|
||||
self.restore_snapshot(snapshot);
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::None))
|
||||
}
|
||||
|
||||
fn parse_remaining_bounds_path(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue