Skip to content

Commit 4bb3ae3

Browse files
authored
Rollup merge of #118035 - ouz-a:november_ice2, r=compiler-errors
Fix early param lifetimes in generic_const_exprs In cases like below, we never actually be able to capture region name for two reasons, first `'static` becomes anonymous lifetime and second we never capture region if it doesn't have a name so this results in ICE. ``` struct DataWrapper<'static> { data: &'a [u8; Self::SIZE], } impl DataWrapper<'a> { ``` Fixes #118021
2 parents cbadb2e + f68c6c9 commit 4bb3ae3

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::session_diagnostics::{
3535
LifetimeReturnCategoryErr, RequireStaticErr, VarHereDenote,
3636
};
3737

38-
use super::{OutlivesSuggestionBuilder, RegionName};
38+
use super::{OutlivesSuggestionBuilder, RegionName, RegionNameSource};
3939
use crate::region_infer::{BlameConstraint, ExtraConstraintInfo};
4040
use crate::{
4141
nll::ConstraintDescription,
@@ -763,7 +763,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
763763
let err = LifetimeOutliveErr { span: *span };
764764
let mut diag = self.infcx.tcx.sess.create_err(err);
765765

766-
let fr_name = self.give_region_a_name(*fr).unwrap();
766+
// In certain scenarios, such as the one described in issue #118021,
767+
// we might encounter a lifetime that cannot be named.
768+
// These situations are bound to result in errors.
769+
// To prevent an immediate ICE, we opt to create a dummy name instead.
770+
let fr_name = self.give_region_a_name(*fr).unwrap_or(RegionName {
771+
name: kw::UnderscoreLifetime,
772+
source: RegionNameSource::Static,
773+
});
767774
fr_name.highlight_region_name(&mut diag);
768775
let outlived_fr_name = self.give_region_a_name(*outlived_fr).unwrap();
769776
outlived_fr_name.highlight_region_name(&mut diag);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(generic_const_exprs)]
2+
//~^ WARN the feature `generic_const_exprs` is incomplete
3+
4+
struct DataWrapper<'static> {
5+
//~^ ERROR invalid lifetime parameter name: `'static`
6+
data: &'a [u8; Self::SIZE],
7+
//~^ ERROR use of undeclared lifetime name `'a`
8+
//~^^ ERROR lifetime may not live long enough
9+
}
10+
11+
impl DataWrapper<'a> {
12+
//~^ ERROR undeclared lifetime
13+
const SIZE: usize = 14;
14+
}
15+
16+
fn main(){}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
error[E0262]: invalid lifetime parameter name: `'static`
2+
--> $DIR/generic_const_early_param.rs:4:20
3+
|
4+
LL | struct DataWrapper<'static> {
5+
| ^^^^^^^ 'static is a reserved lifetime name
6+
7+
error[E0261]: use of undeclared lifetime name `'a`
8+
--> $DIR/generic_const_early_param.rs:6:12
9+
|
10+
LL | struct DataWrapper<'static> {
11+
| - help: consider introducing lifetime `'a` here: `'a,`
12+
LL |
13+
LL | data: &'a [u8; Self::SIZE],
14+
| ^^ undeclared lifetime
15+
16+
error[E0261]: use of undeclared lifetime name `'a`
17+
--> $DIR/generic_const_early_param.rs:11:18
18+
|
19+
LL | impl DataWrapper<'a> {
20+
| - ^^ undeclared lifetime
21+
| |
22+
| help: consider introducing lifetime `'a` here: `<'a>`
23+
24+
warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes
25+
--> $DIR/generic_const_early_param.rs:1:12
26+
|
27+
LL | #![feature(generic_const_exprs)]
28+
| ^^^^^^^^^^^^^^^^^^^
29+
|
30+
= note: see issue #76560 <https://github.com/rust-lang/rust/issues/76560> for more information
31+
= note: `#[warn(incomplete_features)]` on by default
32+
33+
error: lifetime may not live long enough
34+
--> $DIR/generic_const_early_param.rs:6:20
35+
|
36+
LL | data: &'a [u8; Self::SIZE],
37+
| ^^^^^^^^^^ requires that `'_` must outlive `'static`
38+
39+
error: aborting due to 4 previous errors; 1 warning emitted
40+
41+
Some errors have detailed explanations: E0261, E0262.
42+
For more information about an error, try `rustc --explain E0261`.

0 commit comments

Comments
 (0)