Skip to content

Commit 1a18158

Browse files
committed
Don't crash when reporting nice region errors for generic const items
1 parent 617821a commit 1a18158

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,15 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
2929
// version new_ty of its type where the anonymous region is replaced
3030
// with the named one.
3131
let (named, anon, anon_param_info, region_info) = if sub.has_name()
32-
&& self.tcx().is_suitable_region(sup).is_some()
33-
&& self.find_param_with_region(sup, sub).is_some()
32+
&& let Some(region_info) = self.tcx().is_suitable_region(sup)
33+
&& let Some(anon_param_info) = self.find_param_with_region(sup, sub)
3434
{
35-
(
36-
sub,
37-
sup,
38-
self.find_param_with_region(sup, sub).unwrap(),
39-
self.tcx().is_suitable_region(sup).unwrap(),
40-
)
35+
(sub, sup, anon_param_info, region_info)
4136
} else if sup.has_name()
42-
&& self.tcx().is_suitable_region(sub).is_some()
43-
&& self.find_param_with_region(sub, sup).is_some()
37+
&& let Some(region_info) = self.tcx().is_suitable_region(sub)
38+
&& let Some(anon_param_info) = self.find_param_with_region(sub, sup)
4439
{
45-
(
46-
sup,
47-
sub,
48-
self.find_param_with_region(sub, sup).unwrap(),
49-
self.tcx().is_suitable_region(sub).unwrap(),
50-
)
40+
(sup, sub, anon_param_info, region_info)
5141
} else {
5242
return None; // inapplicable
5343
};

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn find_param_with_region<'tcx>(
6464
let body_id = hir.maybe_body_owned_by(def_id)?;
6565

6666
let owner_id = hir.body_owner(body_id);
67-
let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap();
67+
let fn_decl = hir.fn_decl_by_hir_id(owner_id)?;
6868
let poly_fn_sig = tcx.fn_sig(id).instantiate_identity();
6969

7070
let fn_sig = tcx.liberate_late_bound_regions(id, poly_fn_sig);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Test that we catch that the reference outlives the referent and we
2+
// successfully emit a diagnostic. Regression test for issue #114714.
3+
4+
#![feature(generic_const_items)]
5+
#![allow(incomplete_features)]
6+
7+
const Q<'a, 'b>: &'a &'b () = &&(); //~ ERROR reference has a longer lifetime than the data it references
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0491]: in type `&'a &'b ()`, reference has a longer lifetime than the data it references
2+
--> $DIR/reference-outlives-referent.rs:7:18
3+
|
4+
LL | const Q<'a, 'b>: &'a &'b () = &&();
5+
| ^^^^^^^^^^
6+
|
7+
note: the pointer is valid for the lifetime `'a` as defined here
8+
--> $DIR/reference-outlives-referent.rs:7:9
9+
|
10+
LL | const Q<'a, 'b>: &'a &'b () = &&();
11+
| ^^
12+
note: but the referenced data is only valid for the lifetime `'b` as defined here
13+
--> $DIR/reference-outlives-referent.rs:7:13
14+
|
15+
LL | const Q<'a, 'b>: &'a &'b () = &&();
16+
| ^^
17+
18+
error: aborting due to previous error
19+
20+
For more information about this error, try `rustc --explain E0491`.

0 commit comments

Comments
 (0)