Skip to content

Commit abd308b

Browse files
committed
Remove an Option and instead eagerly create error lifetimes
1 parent 9d387d1 commit abd308b

File tree

5 files changed

+43
-55
lines changed

5 files changed

+43
-55
lines changed

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_ast::Recovered;
1818
use rustc_data_structures::captures::Captures;
1919
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
2020
use rustc_data_structures::unord::UnordMap;
21-
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, StashKey};
21+
use rustc_errors::{struct_span_code_err, Applicability, Diag, ErrorGuaranteed, StashKey, E0228};
2222
use rustc_hir as hir;
2323
use rustc_hir::def::DefKind;
2424
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -378,8 +378,27 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
378378
false
379379
}
380380

381-
fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> {
382-
None
381+
fn re_infer(
382+
&self,
383+
_: Option<&ty::GenericParamDef>,
384+
span: Span,
385+
object_lifetime_default: bool,
386+
) -> ty::Region<'tcx> {
387+
if object_lifetime_default {
388+
let e = struct_span_code_err!(
389+
self.tcx().dcx(),
390+
span,
391+
E0228,
392+
"the lifetime bound for this object type cannot be deduced \
393+
from context; please supply an explicit bound"
394+
)
395+
.emit();
396+
self.set_tainted_by_errors(e);
397+
ty::Region::new_error(self.tcx(), e)
398+
} else {
399+
// This indicates an illegal lifetime in a non-assoc-trait position
400+
ty::Region::new_error_with_message(self.tcx(), span, "unelided lifetime in signature")
401+
}
383402
}
384403

385404
fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,14 @@ pub trait HirTyLowerer<'tcx> {
9696
fn allow_infer(&self) -> bool;
9797

9898
/// Returns the region to use when a lifetime is omitted (and not elided).
99-
fn re_infer(&self, param: Option<&ty::GenericParamDef>, span: Span)
100-
-> Option<ty::Region<'tcx>>;
99+
///
100+
/// The `object_lifetime_default` argument states whether this lifetime is from a reference.
101+
fn re_infer(
102+
&self,
103+
param: Option<&ty::GenericParamDef>,
104+
span: Span,
105+
object_lifetime_default: bool,
106+
) -> ty::Region<'tcx>;
101107

102108
/// Returns the type to use when a type is omitted.
103109
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx>;
@@ -292,21 +298,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
292298

293299
Some(rbv::ResolvedArg::Error(guar)) => ty::Region::new_error(tcx, guar),
294300

295-
None => {
296-
self.re_infer(def, lifetime.ident.span).unwrap_or_else(|| {
297-
debug!(?lifetime, "unelided lifetime in signature");
298-
299-
// This indicates an illegal lifetime
300-
// elision. `resolve_lifetime` should have
301-
// reported an error in this case -- but if
302-
// not, let's error out.
303-
ty::Region::new_error_with_message(
304-
tcx,
305-
lifetime.ident.span,
306-
"unelided lifetime in signature",
307-
)
308-
})
309-
}
301+
None => self.re_infer(def, lifetime.ident.span, false),
310302
}
311303
}
312304

@@ -513,20 +505,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
513505
}
514506
}
515507
match param.kind {
516-
GenericParamDefKind::Lifetime => self
517-
.lowerer
518-
.re_infer(Some(param), self.span)
519-
.unwrap_or_else(|| {
520-
debug!(?param, "unelided lifetime in signature");
521-
522-
// This indicates an illegal lifetime in a non-assoc-trait position
523-
ty::Region::new_error_with_message(
524-
tcx,
525-
self.span,
526-
"unelided lifetime in signature",
527-
)
528-
})
529-
.into(),
508+
GenericParamDefKind::Lifetime => {
509+
self.lowerer.re_infer(Some(param), self.span, false).into()
510+
}
530511
GenericParamDefKind::Type { has_default, .. } => {
531512
if !infer_args && has_default {
532513
// No type parameter provided, but a default exists.

compiler/rustc_hir_analysis/src/hir_ty_lowering/object_safety.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -327,24 +327,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
327327
if tcx.named_bound_var(lifetime.hir_id).is_some() {
328328
self.lower_lifetime(lifetime, None)
329329
} else {
330-
self.re_infer(None, span).unwrap_or_else(|| {
331-
let err = struct_span_code_err!(
332-
tcx.dcx(),
333-
span,
334-
E0228,
335-
"the lifetime bound for this object type cannot be deduced \
336-
from context; please supply an explicit bound"
337-
);
338-
let e = if borrowed {
339-
// We will have already emitted an error E0106 complaining about a
340-
// missing named lifetime in `&dyn Trait`, so we elide this one.
341-
err.delay_as_bug()
342-
} else {
343-
err.emit()
344-
};
345-
self.set_tainted_by_errors(e);
346-
ty::Region::new_error(tcx, e)
347-
})
330+
self.re_infer(None, span, !borrowed)
348331
}
349332
})
350333
};

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13251325
let tcx = self.fcx.tcx();
13261326
match param.kind {
13271327
GenericParamDefKind::Lifetime => {
1328-
self.fcx.re_infer(Some(param), self.span).unwrap().into()
1328+
self.fcx.re_infer(Some(param), self.span, false).into()
13291329
}
13301330
GenericParamDefKind::Type { has_default, .. } => {
13311331
if !infer_args && has_default {

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,17 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
226226
true
227227
}
228228

229-
fn re_infer(&self, def: Option<&ty::GenericParamDef>, span: Span) -> Option<ty::Region<'tcx>> {
229+
fn re_infer(
230+
&self,
231+
def: Option<&ty::GenericParamDef>,
232+
span: Span,
233+
_object_lifetime_default: bool,
234+
) -> ty::Region<'tcx> {
230235
let v = match def {
231236
Some(def) => infer::RegionParameterDefinition(span, def.name),
232237
None => infer::MiscVariable(span),
233238
};
234-
Some(self.next_region_var(v))
239+
self.next_region_var(v)
235240
}
236241

237242
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {

0 commit comments

Comments
 (0)