Skip to content

Commit 16ca0b9

Browse files
committed
Auto merge of #58180 - davidtwco:issue-58053, r=estebank
Fix span for closure return type when annotated. Fixes #58053. This PR adjusts the span used to label closure return types so that if the user specifies the return type, i.e. `|_| -> X {}` instead of `|_| {}`, we correctly highlight all of it and not just the last character. r? @pnkfelix
2 parents 57d7cfc + b377d0b commit 16ca0b9

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -681,10 +681,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
681681

682682
let (return_span, mir_description) = match tcx.hir().get(mir_node_id) {
683683
hir::Node::Expr(hir::Expr {
684-
node: hir::ExprKind::Closure(_, _, _, span, gen_move),
684+
node: hir::ExprKind::Closure(_, return_ty, _, span, gen_move),
685685
..
686686
}) => (
687-
tcx.sess.source_map().end_point(*span),
687+
match return_ty.output {
688+
hir::FunctionRetTy::DefaultReturn(_) => tcx.sess.source_map().end_point(*span),
689+
hir::FunctionRetTy::Return(_) => return_ty.output.span(),
690+
},
688691
if gen_move.is_some() {
689692
" of generator"
690693
} else {

src/test/ui/nll/issue-58053.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![allow(warnings)]
2+
#![feature(nll)]
3+
4+
fn main() {
5+
let i = &3;
6+
7+
let f = |x: &i32| -> &i32 { x };
8+
//~^ ERROR lifetime may not live long enough
9+
let j = f(i);
10+
11+
let g = |x: &i32| { x };
12+
//~^ ERROR lifetime may not live long enough
13+
let k = g(i);
14+
}

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

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/issue-58053.rs:7:33
3+
|
4+
LL | let f = |x: &i32| -> &i32 { x };
5+
| - ---- ^ returning this value requires that `'1` must outlive `'2`
6+
| | |
7+
| | return type of closure is &'2 i32
8+
| let's call the lifetime of this reference `'1`
9+
10+
error: lifetime may not live long enough
11+
--> $DIR/issue-58053.rs:11:25
12+
|
13+
LL | let g = |x: &i32| { x };
14+
| - - ^ returning this value requires that `'1` must outlive `'2`
15+
| | |
16+
| | return type of closure is &'2 i32
17+
| let's call the lifetime of this reference `'1`
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)