1
Fork 0

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::captures::Captures;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap}; use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_data_structures::unord::UnordMap; 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 as hir;
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
@ -378,8 +378,27 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
false false
} }
fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> { fn re_infer(
None &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> { 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; fn allow_infer(&self) -> bool;
/// Returns the region to use when a lifetime is omitted (and not elided). /// 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. /// Returns the type to use when a type is omitted.
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx>; 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), Some(rbv::ResolvedArg::Error(guar)) => ty::Region::new_error(tcx, guar),
None => { None => self.re_infer(def, lifetime.ident.span, false),
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",
)
})
}
} }
} }
@ -513,20 +505,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
} }
} }
match param.kind { match param.kind {
GenericParamDefKind::Lifetime => self GenericParamDefKind::Lifetime => {
.lowerer self.lowerer.re_infer(Some(param), self.span, false).into()
.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::Type { has_default, .. } => { GenericParamDefKind::Type { has_default, .. } => {
if !infer_args && has_default { if !infer_args && has_default {
// No type parameter provided, but a default exists. // 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() { if tcx.named_bound_var(lifetime.hir_id).is_some() {
self.lower_lifetime(lifetime, None) self.lower_lifetime(lifetime, None)
} else { } else {
self.re_infer(None, span).unwrap_or_else(|| { self.re_infer(None, span, !borrowed)
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)
})
} }
}) })
}; };

View file

@ -1325,7 +1325,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let tcx = self.fcx.tcx(); let tcx = self.fcx.tcx();
match param.kind { match param.kind {
GenericParamDefKind::Lifetime => { 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, .. } => { GenericParamDefKind::Type { has_default, .. } => {
if !infer_args && has_default { if !infer_args && has_default {

View file

@ -226,12 +226,17 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
true 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 { let v = match def {
Some(def) => infer::RegionParameterDefinition(span, def.name), Some(def) => infer::RegionParameterDefinition(span, def.name),
None => infer::MiscVariable(span), 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> { fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {