Remove an `Option` and instead eagerly create error lifetimes

This commit is contained in:
Oli Scherer 2024-05-31 13:09:18 +00:00
parent 9d387d14e0
commit abd308b886
5 changed files with 43 additions and 55 deletions

View File

@ -18,7 +18,7 @@ use rustc_ast::Recovered;
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_data_structures::unord::UnordMap;
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, StashKey};
use rustc_errors::{struct_span_code_err, Applicability, Diag, ErrorGuaranteed, StashKey, E0228};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId};
@ -378,8 +378,27 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
false
}
fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> {
None
fn re_infer(
&self,
_: Option<&ty::GenericParamDef>,
span: Span,
object_lifetime_default: bool,
) -> ty::Region<'tcx> {
if object_lifetime_default {
let e = struct_span_code_err!(
self.tcx().dcx(),
span,
E0228,
"the lifetime bound for this object type cannot be deduced \
from context; please supply an explicit bound"
)
.emit();
self.set_tainted_by_errors(e);
ty::Region::new_error(self.tcx(), e)
} else {
// This indicates an illegal lifetime in a non-assoc-trait position
ty::Region::new_error_with_message(self.tcx(), span, "unelided lifetime in signature")
}
}
fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {

View File

@ -96,8 +96,14 @@ pub trait HirTyLowerer<'tcx> {
fn allow_infer(&self) -> bool;
/// Returns the region to use when a lifetime is omitted (and not elided).
fn re_infer(&self, param: Option<&ty::GenericParamDef>, span: Span)
-> Option<ty::Region<'tcx>>;
///
/// The `object_lifetime_default` argument states whether this lifetime is from a reference.
fn re_infer(
&self,
param: Option<&ty::GenericParamDef>,
span: Span,
object_lifetime_default: bool,
) -> ty::Region<'tcx>;
/// Returns the type to use when a type is omitted.
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx>;
@ -292,21 +298,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
Some(rbv::ResolvedArg::Error(guar)) => ty::Region::new_error(tcx, guar),
None => {
self.re_infer(def, lifetime.ident.span).unwrap_or_else(|| {
debug!(?lifetime, "unelided lifetime in signature");
// This indicates an illegal lifetime
// elision. `resolve_lifetime` should have
// reported an error in this case -- but if
// not, let's error out.
ty::Region::new_error_with_message(
tcx,
lifetime.ident.span,
"unelided lifetime in signature",
)
})
}
None => self.re_infer(def, lifetime.ident.span, false),
}
}
@ -513,20 +505,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}
}
match param.kind {
GenericParamDefKind::Lifetime => self
.lowerer
.re_infer(Some(param), self.span)
.unwrap_or_else(|| {
debug!(?param, "unelided lifetime in signature");
// This indicates an illegal lifetime in a non-assoc-trait position
ty::Region::new_error_with_message(
tcx,
self.span,
"unelided lifetime in signature",
)
})
.into(),
GenericParamDefKind::Lifetime => {
self.lowerer.re_infer(Some(param), self.span, false).into()
}
GenericParamDefKind::Type { has_default, .. } => {
if !infer_args && has_default {
// No type parameter provided, but a default exists.

View File

@ -327,24 +327,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
if tcx.named_bound_var(lifetime.hir_id).is_some() {
self.lower_lifetime(lifetime, None)
} else {
self.re_infer(None, span).unwrap_or_else(|| {
let err = struct_span_code_err!(
tcx.dcx(),
span,
E0228,
"the lifetime bound for this object type cannot be deduced \
from context; please supply an explicit bound"
);
let e = if borrowed {
// We will have already emitted an error E0106 complaining about a
// missing named lifetime in `&dyn Trait`, so we elide this one.
err.delay_as_bug()
} else {
err.emit()
};
self.set_tainted_by_errors(e);
ty::Region::new_error(tcx, e)
})
self.re_infer(None, span, !borrowed)
}
})
};

View File

@ -1325,7 +1325,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let tcx = self.fcx.tcx();
match param.kind {
GenericParamDefKind::Lifetime => {
self.fcx.re_infer(Some(param), self.span).unwrap().into()
self.fcx.re_infer(Some(param), self.span, false).into()
}
GenericParamDefKind::Type { has_default, .. } => {
if !infer_args && has_default {

View File

@ -226,12 +226,17 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
true
}
fn re_infer(&self, def: Option<&ty::GenericParamDef>, span: Span) -> Option<ty::Region<'tcx>> {
fn re_infer(
&self,
def: Option<&ty::GenericParamDef>,
span: Span,
_object_lifetime_default: bool,
) -> ty::Region<'tcx> {
let v = match def {
Some(def) => infer::RegionParameterDefinition(span, def.name),
None => infer::MiscVariable(span),
};
Some(self.next_region_var(v))
self.next_region_var(v)
}
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {