Skip to content

Commit f72b8a4

Browse files
committed
Use span of the closure args in free region errors
1 parent ddcf17e commit f72b8a4

File tree

6 files changed

+91
-55
lines changed

6 files changed

+91
-55
lines changed

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

+53-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use borrow_check::nll::region_infer::RegionInferenceContext;
1212
use borrow_check::nll::ToRegionVid;
13+
use borrow_check::nll::universal_regions::DefiningTy;
1314
use rustc::hir;
1415
use rustc::hir::def_id::DefId;
1516
use rustc::infer::InferCtxt;
@@ -72,7 +73,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
7273
})
7374
.or_else(|| {
7475
self.give_name_if_anonymous_region_appears_in_output(
75-
infcx.tcx, mir, fr, counter, diag)
76+
infcx.tcx, mir, mir_def_id, fr, counter, diag)
7677
})
7778
.unwrap_or_else(|| span_bug!(mir.span, "can't make a name for free region {:?}", fr))
7879
}
@@ -107,13 +108,46 @@ impl<'tcx> RegionInferenceContext<'tcx> {
107108
},
108109

109110
ty::BoundRegion::BrEnv => {
110-
let closure_span = tcx.hir.span_if_local(mir_def_id).unwrap();
111-
let region_name = self.synthesize_region_name(counter);
112-
diag.span_label(
113-
closure_span,
114-
format!("lifetime `{}` represents the closure body", region_name),
115-
);
116-
Some(region_name)
111+
let mir_node_id = tcx.hir.as_local_node_id(mir_def_id).expect("non-local mir");
112+
let def_ty = self.universal_regions.defining_ty;
113+
114+
if let DefiningTy::Closure(def_id, substs) = def_ty {
115+
let args_span = if let hir::ExprKind::Closure(_, _, _, span, _)
116+
= tcx.hir.expect_expr(mir_node_id).node
117+
{
118+
span
119+
} else {
120+
bug!("Closure is not defined by a closure expr");
121+
};
122+
let region_name = self.synthesize_region_name(counter);
123+
diag.span_label(
124+
args_span,
125+
format!("lifetime `{}` represents this closure's body", region_name),
126+
);
127+
128+
let closure_kind_ty = substs.closure_kind_ty(def_id, tcx);
129+
let note = match closure_kind_ty.to_opt_closure_kind() {
130+
Some(ty::ClosureKind::Fn) => {
131+
"closure implements `Fn`, so references to captured variables \
132+
can't escape the closure"
133+
}
134+
Some(ty::ClosureKind::FnMut) => {
135+
"closure implements `FnMut`, so references to captured variables \
136+
can't escape the closure"
137+
}
138+
Some(ty::ClosureKind::FnOnce) => {
139+
bug!("BrEnv in a `FnOnce` closure");
140+
}
141+
None => bug!("Closure kind not inferred in borrow check"),
142+
};
143+
144+
diag.note(note);
145+
146+
Some(region_name)
147+
} else {
148+
// Can't have BrEnv in functions, constants or generators.
149+
bug!("BrEnv outside of closure.");
150+
}
117151
}
118152

119153
ty::BoundRegion::BrAnon(_) | ty::BoundRegion::BrFresh(_) => None,
@@ -545,6 +579,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
545579
&self,
546580
tcx: TyCtxt<'_, '_, 'tcx>,
547581
mir: &Mir<'tcx>,
582+
mir_def_id: DefId,
548583
fr: RegionVid,
549584
counter: &mut usize,
550585
diag: &mut DiagnosticBuilder<'_>,
@@ -558,9 +593,18 @@ impl<'tcx> RegionInferenceContext<'tcx> {
558593
return None;
559594
}
560595

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+
= tcx.hir.expect_expr(mir_node_id).node
599+
{
600+
span
601+
} else {
602+
mir.span
603+
};
604+
561605
let region_name = self.synthesize_region_name(counter);
562606
diag.span_label(
563-
mir.span,
607+
args_span,
564608
format!("lifetime `{}` appears in return type", region_name),
565609
);
566610

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ 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-
| ----------^^^^^-----------------
12-
| | | |
13-
| | | requires that `'1` must outlive `'2`
11+
| ------ ^^^^^ requires that `'1` must outlive `'2`
12+
| | |
1413
| | has type `&'1 i32`
1514
| lifetime `'2` appears in return type
1615

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

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
error: unsatisfied lifetime constraints
22
--> $DIR/issue-40510-1.rs:18:9
33
|
4-
LL | || {
5-
| _____-
6-
| |_____|
7-
| ||
8-
LL | || &mut x
9-
| || ^^^^^^ return requires that `'1` must outlive `'2`
10-
LL | || };
11-
| || -
12-
| ||_____|
13-
| |______lifetime `'1` represents the closure body
14-
| lifetime `'2` appears in return type
4+
LL | || {
5+
| --
6+
| |
7+
| lifetime `'1` represents this closure's body
8+
| lifetime `'2` appears in return type
9+
LL | &mut x
10+
| ^^^^^^ return requires that `'1` must outlive `'2`
11+
|
12+
= note: closure implements `FnMut`, so references to captured variables can't escape the closure
1513

1614
error: aborting due to previous error
1715

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

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
error: unsatisfied lifetime constraints
22
--> $DIR/issue-40510-3.rs:18:9
33
|
4-
LL | || {
5-
| _____-
6-
| |_____|
7-
| ||
8-
LL | || || {
9-
| ||_________^
10-
LL | ||| x.push(())
11-
LL | ||| }
12-
| |||_________^ requires that `'1` must outlive `'2`
13-
LL | || };
14-
| || -
15-
| ||_____|
16-
| |______lifetime `'1` represents the closure body
17-
| lifetime `'2` appears in return type
4+
LL | || {
5+
| --
6+
| |
7+
| lifetime `'1` represents this closure's body
8+
| lifetime `'2` appears in return type
9+
LL | / || {
10+
LL | | x.push(())
11+
LL | | }
12+
| |_________^ requires that `'1` must outlive `'2`
13+
|
14+
= note: closure implements `FnMut`, so references to captured variables can't escape the closure
1815

1916
error: aborting due to previous error
2017

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

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
error: unsatisfied lifetime constraints
22
--> $DIR/issue-49824.rs:22:9
33
|
4-
LL | || {
5-
| _____-
6-
| |_____|
7-
| ||
8-
LL | || || {
9-
| ||_________^
10-
LL | ||| let _y = &mut x;
11-
LL | ||| }
12-
| |||_________^ requires that `'1` must outlive `'2`
13-
LL | || };
14-
| || -
15-
| ||_____|
16-
| |______lifetime `'1` represents the closure body
17-
| lifetime `'2` appears in return type
4+
LL | || {
5+
| --
6+
| |
7+
| lifetime `'1` represents this closure's body
8+
| lifetime `'2` appears in return type
9+
LL | / || {
10+
LL | | let _y = &mut x;
11+
LL | | }
12+
| |_________^ requires that `'1` must outlive `'2`
13+
|
14+
= note: closure implements `FnMut`, so references to captured variables can't escape the closure
1815

1916
error: aborting due to previous error
2017

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ error: unsatisfied lifetime constraints
22
--> $DIR/issue-48238.rs:21:13
33
|
44
LL | move || use_val(&orig); //~ ERROR
5-
| --------^^^^^^^^^^^^^^
6-
| | |
7-
| | argument requires that `'1` must outlive `'2`
8-
| lifetime `'1` represents the closure body
5+
| ------- ^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'2`
6+
| |
7+
| lifetime `'1` represents this closure's body
98
| lifetime `'2` appears in return type
9+
|
10+
= note: closure implements `Fn`, so references to captured variables can't escape the closure
1011

1112
error: aborting due to previous error
1213

0 commit comments

Comments
 (0)