Skip to content

Commit 34ff352

Browse files
committed
don't refer to async as 'generators'
and give return of async fn a better span
1 parent 62bfcfd commit 34ff352

File tree

3 files changed

+92
-6
lines changed

3 files changed

+92
-6
lines changed

compiler/rustc_mir/src/borrow_check/diagnostics/region_name.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -657,15 +657,33 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
657657

658658
let (return_span, mir_description) = match tcx.hir().get(self.mir_hir_id()) {
659659
hir::Node::Expr(hir::Expr {
660-
kind: hir::ExprKind::Closure(_, return_ty, _, span, gen_move),
660+
kind: hir::ExprKind::Closure(_, return_ty, body_id, span, _),
661661
..
662-
}) => (
663-
match return_ty.output {
662+
}) => {
663+
let mut span = match return_ty.output {
664664
hir::FnRetTy::DefaultReturn(_) => tcx.sess.source_map().end_point(*span),
665665
hir::FnRetTy::Return(_) => return_ty.output.span(),
666-
},
667-
if gen_move.is_some() { " of generator" } else { " of closure" },
668-
),
666+
};
667+
let mir_description = match tcx.hir().body(*body_id).generator_kind {
668+
Some(hir::GeneratorKind::Async(gen)) => match gen {
669+
hir::AsyncGeneratorKind::Block => " of async block",
670+
hir::AsyncGeneratorKind::Closure => " of async closure",
671+
hir::AsyncGeneratorKind::Fn => {
672+
span = tcx
673+
.hir()
674+
.get(tcx.hir().get_parent_item(mir_hir_id))
675+
.fn_decl()
676+
.expect("generator lowered from async fn should be in fn")
677+
.output
678+
.span();
679+
" of async function"
680+
}
681+
},
682+
Some(hir::GeneratorKind::Gen) => " of generator",
683+
None => " of closure",
684+
};
685+
(span, mir_description)
686+
}
669687
hir::Node::ImplItem(hir::ImplItem {
670688
kind: hir::ImplItemKind::Fn(method_sig, _),
671689
..
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// edition:2018
2+
#![feature(async_closure)]
3+
use std::future::Future;
4+
5+
// test the quality of annotations giving lifetimes names (`'1`) when async constructs are involved
6+
7+
pub async fn async_fn(x: &mut i32) -> &i32 {
8+
let y = &*x;
9+
*x += 1; //~ ERROR cannot assign to `*x` because it is borrowed
10+
y
11+
}
12+
13+
pub fn async_closure(x: &mut i32) -> impl Future<Output=&i32> {
14+
(async move || {
15+
let y = &*x;
16+
*x += 1; //~ ERROR cannot assign to `*x` because it is borrowed
17+
y
18+
})()
19+
}
20+
21+
pub fn async_block(x: &mut i32) -> impl Future<Output=&i32> {
22+
async move {
23+
let y = &*x;
24+
*x += 1; //~ ERROR cannot assign to `*x` because it is borrowed
25+
y
26+
}
27+
}
28+
29+
fn main() {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0506]: cannot assign to `*x` because it is borrowed
2+
--> $DIR/issue-74072-lifetime-name-annotations.rs:9:5
3+
|
4+
LL | pub async fn async_fn(x: &mut i32) -> &i32 {
5+
| ---- return type of async function is &'1 i32
6+
LL | let y = &*x;
7+
| --- borrow of `*x` occurs here
8+
LL | *x += 1;
9+
| ^^^^^^^ assignment to borrowed `*x` occurs here
10+
LL | y
11+
| - returning this value requires that `*x` is borrowed for `'1`
12+
13+
error[E0506]: cannot assign to `*x` because it is borrowed
14+
--> $DIR/issue-74072-lifetime-name-annotations.rs:16:9
15+
|
16+
LL | let y = &*x;
17+
| --- borrow of `*x` occurs here
18+
LL | *x += 1;
19+
| ^^^^^^^ assignment to borrowed `*x` occurs here
20+
LL | y
21+
| - returning this value requires that `*x` is borrowed for `'1`
22+
LL | })()
23+
| - return type of async closure is &'1 i32
24+
25+
error[E0506]: cannot assign to `*x` because it is borrowed
26+
--> $DIR/issue-74072-lifetime-name-annotations.rs:24:9
27+
|
28+
LL | let y = &*x;
29+
| --- borrow of `*x` occurs here
30+
LL | *x += 1;
31+
| ^^^^^^^ assignment to borrowed `*x` occurs here
32+
LL | y
33+
| - returning this value requires that `*x` is borrowed for `'1`
34+
LL | }
35+
| - return type of async block is &'1 i32
36+
37+
error: aborting due to 3 previous errors
38+
39+
For more information about this error, try `rustc --explain E0506`.

0 commit comments

Comments
 (0)