Skip to content

Commit b13e3f8

Browse files
committed
Name return type in free region messages
1 parent f72b8a4 commit b13e3f8

File tree

6 files changed

+36
-23
lines changed

6 files changed

+36
-23
lines changed

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
7373
})
7474
.or_else(|| {
7575
self.give_name_if_anonymous_region_appears_in_output(
76-
infcx.tcx, mir, mir_def_id, fr, counter, diag)
76+
infcx, mir, mir_def_id, fr, counter, diag)
7777
})
7878
.unwrap_or_else(|| span_bug!(mir.span, "can't make a name for free region {:?}", fr))
7979
}
@@ -577,38 +577,51 @@ impl<'tcx> RegionInferenceContext<'tcx> {
577577
/// or be early bound (named, not in argument).
578578
fn give_name_if_anonymous_region_appears_in_output(
579579
&self,
580-
tcx: TyCtxt<'_, '_, 'tcx>,
580+
infcx: &InferCtxt<'_, '_, 'tcx>,
581581
mir: &Mir<'tcx>,
582582
mir_def_id: DefId,
583583
fr: RegionVid,
584584
counter: &mut usize,
585585
diag: &mut DiagnosticBuilder<'_>,
586586
) -> Option<InternedString> {
587+
let tcx = infcx.tcx;
588+
587589
let return_ty = self.universal_regions.unnormalized_output_ty;
588590
debug!(
589591
"give_name_if_anonymous_region_appears_in_output: return_ty = {:?}",
590592
return_ty
591593
);
592-
if !tcx.any_free_region_meets(&return_ty, |r| r.to_region_vid() == fr) {
594+
if !infcx.tcx.any_free_region_meets(&return_ty, |r| r.to_region_vid() == fr) {
593595
return None;
594596
}
595597

596-
let mir_node_id = tcx.hir.as_local_node_id(mir_def_id).expect("non-local mir");
597-
let args_span = if let hir::ExprKind::Closure(_, _, _, span, _)
598+
let type_name = with_highlight_region(fr, *counter, || {
599+
infcx.extract_type_name(&return_ty)
600+
});
601+
602+
let mir_node_id = tcx.hir.as_local_node_id(mir_def_id).expect("non-local mir");
603+
604+
let (return_span, mir_description) = if let hir::ExprKind::Closure(_, _, _, span, gen_move)
598605
= tcx.hir.expect_expr(mir_node_id).node
599606
{
600-
span
607+
(
608+
tcx.sess.codemap().end_point(span),
609+
if gen_move.is_some() { " of generator" } else { " of closure" }
610+
)
601611
} else {
602-
mir.span
612+
// unreachable?
613+
(mir.span, "")
603614
};
604615

605-
let region_name = self.synthesize_region_name(counter);
606616
diag.span_label(
607-
args_span,
608-
format!("lifetime `{}` appears in return type", region_name),
617+
return_span,
618+
format!("return type{} is {}", mir_description, type_name),
609619
);
610620

611-
Some(region_name)
621+
// This counter value will already have been used, so this function will increment it
622+
// so the next value will be used next and return the region name that would have been
623+
// used.
624+
Some(self.synthesize_region_name(counter))
612625
}
613626

614627
/// Create a synthetic region named `'1`, incrementing the

src/test/ui/error-codes/E0621-does-not-trigger-for-closures.nll.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ error: unsatisfied lifetime constraints
88
--> $DIR/E0621-does-not-trigger-for-closures.rs:25:26
99
|
1010
LL | invoke(&x, |a, b| if a > b { a } else { b }); //~ ERROR E0495
11-
| ------ ^^^^^ requires that `'1` must outlive `'2`
12-
| | |
13-
| | has type `&'1 i32`
14-
| lifetime `'2` appears in return type
11+
| -- ^^^^^ requires that `'1` must outlive `'2`
12+
| ||
13+
| |return type of closure is &'2 i32
14+
| has type `&'1 i32`
1515

1616
error: aborting due to previous error
1717

src/test/ui/issue-40510-1.nll.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ error: unsatisfied lifetime constraints
33
|
44
LL | || {
55
| --
6-
| |
6+
| ||
7+
| |return type of closure is &'2 mut std::boxed::Box<()>
78
| lifetime `'1` represents this closure's body
8-
| lifetime `'2` appears in return type
99
LL | &mut x
1010
| ^^^^^^ return requires that `'1` must outlive `'2`
1111
|

src/test/ui/issue-40510-3.nll.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ error: unsatisfied lifetime constraints
33
|
44
LL | || {
55
| --
6-
| |
6+
| ||
7+
| |return type of closure is [closure@$DIR/issue-40510-3.rs:18:9: 20:10 x:&'2 mut std::vec::Vec<()>]
78
| lifetime `'1` represents this closure's body
8-
| lifetime `'2` appears in return type
99
LL | / || {
1010
LL | | x.push(())
1111
LL | | }

src/test/ui/issue-49824.nll.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ error: unsatisfied lifetime constraints
33
|
44
LL | || {
55
| --
6-
| |
6+
| ||
7+
| |return type of closure is [closure@$DIR/issue-49824.rs:22:9: 24:10 x:&'2 mut i32]
78
| lifetime `'1` represents this closure's body
8-
| lifetime `'2` appears in return type
99
LL | / || {
1010
LL | | let _y = &mut x;
1111
LL | | }

src/test/ui/nll/issue-48238.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ error: unsatisfied lifetime constraints
33
|
44
LL | move || use_val(&orig); //~ ERROR
55
| ------- ^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'2`
6-
| |
6+
| | |
7+
| | return type of closure is &'2 u8
78
| lifetime `'1` represents this closure's body
8-
| lifetime `'2` appears in return type
99
|
1010
= note: closure implements `Fn`, so references to captured variables can't escape the closure
1111

0 commit comments

Comments
 (0)