Skip to content

Commit 85bc5a7

Browse files
committed
Remove an Option and instead eagerly create error lifetimes
1 parent c293b45 commit 85bc5a7

File tree

7 files changed

+59
-56
lines changed

7 files changed

+59
-56
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+24-3
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,29 @@ 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+
// We will have already emitted an error E0106 complaining about a
389+
// missing named lifetime in `&dyn Trait`, so we elide this one.
390+
let e = struct_span_code_err!(
391+
self.tcx().dcx(),
392+
span,
393+
E0228,
394+
"the lifetime bound for this object type cannot be deduced \
395+
from context; please supply an explicit bound"
396+
)
397+
.emit();
398+
self.set_tainted_by_errors(e);
399+
ty::Region::new_error(self.tcx(), e)
400+
} else {
401+
// This indicates an illegal lifetime in a non-assoc-trait position
402+
ty::Region::new_error_with_message(self.tcx(), span, "unelided lifetime in signature")
403+
}
383404
}
384405

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

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+12-31
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 `borrowed` 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

@@ -501,20 +493,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
501493
) -> ty::GenericArg<'tcx> {
502494
let tcx = self.lowerer.tcx();
503495
match param.kind {
504-
GenericParamDefKind::Lifetime => self
505-
.lowerer
506-
.re_infer(Some(param), self.span)
507-
.unwrap_or_else(|| {
508-
debug!(?param, "unelided lifetime in signature");
509-
510-
// This indicates an illegal lifetime in a non-assoc-trait position
511-
ty::Region::new_error_with_message(
512-
tcx,
513-
self.span,
514-
"unelided lifetime in signature",
515-
)
516-
})
517-
.into(),
496+
GenericParamDefKind::Lifetime => {
497+
self.lowerer.re_infer(Some(param), self.span, false).into()
498+
}
518499
GenericParamDefKind::Type { has_default, .. } => {
519500
if !infer_args && has_default {
520501
// No type parameter provided, but a default exists.

compiler/rustc_hir_analysis/src/hir_ty_lowering/object_safety.rs

+1-18
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13211321
let tcx = self.fcx.tcx();
13221322
match param.kind {
13231323
GenericParamDefKind::Lifetime => {
1324-
self.fcx.re_infer(Some(param), self.span).unwrap().into()
1324+
self.fcx.re_infer(Some(param), self.span, false).into()
13251325
}
13261326
GenericParamDefKind::Type { has_default, .. } => {
13271327
if !infer_args && has_default {

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+7-2
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> {

tests/ui/lifetimes/unusual-rib-combinations.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ fn d<const C: S>() {}
2626
trait Foo<'a> {}
2727
struct Bar<const N: &'a (dyn for<'a> Foo<'a>)>;
2828
//~^ ERROR the type of const parameters must not depend on other generic parameters
29+
//~| ERROR `&dyn for<'a> Foo<'a>` is forbidden as the type of a const generic parameter
2930

3031
fn main() {}

tests/ui/lifetimes/unusual-rib-combinations.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,19 @@ help: add `#![feature(adt_const_params)]` to the crate attributes to enable more
6161
LL + #![feature(adt_const_params)]
6262
|
6363

64-
error: aborting due to 8 previous errors
64+
error: `&dyn for<'a> Foo<'a>` is forbidden as the type of a const generic parameter
65+
--> $DIR/unusual-rib-combinations.rs:27:21
66+
|
67+
LL | struct Bar<const N: &'a (dyn for<'a> Foo<'a>)>;
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
69+
|
70+
= note: the only supported types are integers, `bool` and `char`
71+
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
72+
|
73+
LL + #![feature(adt_const_params)]
74+
|
75+
76+
error: aborting due to 9 previous errors
6577

6678
Some errors have detailed explanations: E0106, E0214, E0308, E0770.
6779
For more information about an error, try `rustc --explain E0106`.

0 commit comments

Comments
 (0)