Skip to content

Commit b5282d6

Browse files
committed
Remove an Option and instead eagerly create error lifetimes
1 parent a103b46 commit b5282d6

File tree

7 files changed

+59
-56
lines changed

7 files changed

+59
-56
lines changed

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 24 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};
@@ -376,8 +376,29 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
376376
false
377377
}
378378

379-
fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> {
380-
None
379+
fn re_infer(
380+
&self,
381+
_: Option<&ty::GenericParamDef>,
382+
span: Span,
383+
object_lifetime_default: bool,
384+
) -> ty::Region<'tcx> {
385+
if object_lifetime_default {
386+
// We will have already emitted an error E0106 complaining about a
387+
// missing named lifetime in `&dyn Trait`, so we elide this one.
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+
}
381402
}
382403

383404
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 `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>;
@@ -294,21 +300,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
294300

295301
Some(rbv::ResolvedArg::Error(guar)) => ty::Region::new_error(tcx, guar),
296302

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

@@ -502,20 +494,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
502494
) -> ty::GenericArg<'tcx> {
503495
let tcx = self.lowerer.tcx();
504496
match param.kind {
505-
GenericParamDefKind::Lifetime => self
506-
.lowerer
507-
.re_infer(Some(param), self.span)
508-
.unwrap_or_else(|| {
509-
debug!(?param, "unelided lifetime in signature");
510-
511-
// This indicates an illegal lifetime in a non-assoc-trait position
512-
ty::Region::new_error_with_message(
513-
tcx,
514-
self.span,
515-
"unelided lifetime in signature",
516-
)
517-
})
518-
.into(),
497+
GenericParamDefKind::Lifetime => {
498+
self.lowerer.re_infer(Some(param), self.span, false).into()
499+
}
519500
GenericParamDefKind::Type { has_default, .. } => {
520501
if !infer_args && has_default {
521502
// 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
@@ -1320,7 +1320,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13201320
let tcx = self.fcx.tcx();
13211321
match param.kind {
13221322
GenericParamDefKind::Lifetime => {
1323-
self.fcx.re_infer(Some(param), self.span).unwrap().into()
1323+
self.fcx.re_infer(Some(param), self.span, false).into()
13241324
}
13251325
GenericParamDefKind::Type { has_default, .. } => {
13261326
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> {

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 13 additions & 1 deletion
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)