Skip to content

Commit 0c0fb22

Browse files
committed
Support #[track_caller] on async closures
1 parent 7632db0 commit 0c0fb22

File tree

7 files changed

+77
-11
lines changed

7 files changed

+77
-11
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
3131

3232
pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> {
3333
ensure_sufficient_stack(|| {
34+
let hir_id = self.lower_node_id(e.id);
35+
self.lower_attrs(hir_id, &e.attrs);
36+
3437
let kind = match &e.kind {
3538
ExprKind::Box(inner) => hir::ExprKind::Box(self.lower_expr(inner)),
3639
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
@@ -147,7 +150,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
147150
),
148151
ExprKind::Async(capture_clause, closure_node_id, block) => self.make_async_expr(
149152
*capture_clause,
150-
None,
153+
hir_id,
151154
*closure_node_id,
152155
None,
153156
e.span,
@@ -184,6 +187,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
184187
binder,
185188
*capture_clause,
186189
e.id,
190+
hir_id,
187191
*closure_id,
188192
fn_decl,
189193
body,
@@ -310,8 +314,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
310314
ExprKind::MacCall(_) => panic!("{:?} shouldn't exist here", e.span),
311315
};
312316

313-
let hir_id = self.lower_node_id(e.id);
314-
self.lower_attrs(hir_id, &e.attrs);
315317
hir::Expr { hir_id, kind, span: self.lower_span(e.span) }
316318
})
317319
}
@@ -576,7 +578,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
576578
pub(super) fn make_async_expr(
577579
&mut self,
578580
capture_clause: CaptureBy,
579-
outer_hir_id: Option<hir::HirId>,
581+
outer_hir_id: hir::HirId,
580582
closure_node_id: NodeId,
581583
ret_ty: Option<hir::FnRetTy<'hir>>,
582584
span: Span,
@@ -669,8 +671,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
669671
hir::ExprKind::Closure(c)
670672
};
671673

672-
let track_caller = outer_hir_id
673-
.and_then(|id| self.attrs.get(&id.local_id))
674+
let track_caller = self
675+
.attrs
676+
.get(&outer_hir_id.local_id)
674677
.map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)));
675678

676679
let hir_id = self.lower_node_id(closure_node_id);
@@ -985,6 +988,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
985988
binder: &ClosureBinder,
986989
capture_clause: CaptureBy,
987990
closure_id: NodeId,
991+
closure_hir_id: hir::HirId,
988992
inner_closure_id: NodeId,
989993
decl: &FnDecl,
990994
body: &Expr,
@@ -1018,9 +1022,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10181022

10191023
let async_body = this.make_async_expr(
10201024
capture_clause,
1021-
// FIXME(nbdd0121): This should also use a proper HIR id so `#[track_caller]`
1022-
// can be applied on async closures as well.
1023-
None,
1025+
closure_hir_id,
10241026
inner_closure_id,
10251027
async_ret_ty,
10261028
body.span,

compiler/rustc_ast_lowering/src/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11391139

11401140
let async_expr = this.make_async_expr(
11411141
CaptureBy::Value,
1142-
Some(fn_id),
1142+
fn_id,
11431143
closure_id,
11441144
None,
11451145
body.span,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// edition:2021
2+
3+
#![feature(closure_track_caller, stmt_expr_attributes)]
4+
5+
fn main() {
6+
let _ = #[track_caller] async {
7+
//~^ ERROR attribute should be applied to a function definition [E0739]
8+
};
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0739]: attribute should be applied to a function definition
2+
--> $DIR/async-block.rs:6:13
3+
|
4+
LL | let _ = #[track_caller] async {
5+
| _____________^^^^^^^^^^^^^^^_-
6+
LL | |
7+
LL | | };
8+
| |_____- not a function definition
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0739`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// edition:2021
2+
3+
#![feature(async_closure, stmt_expr_attributes)]
4+
5+
fn main() {
6+
let _ = #[track_caller] async || {
7+
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
8+
//~| ERROR `#[track_caller]` on closures is currently unstable [E0658]
9+
};
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0658]: `#[track_caller]` on closures is currently unstable
2+
--> $DIR/async-closure-gate.rs:6:13
3+
|
4+
LL | let _ = #[track_caller] async || {
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
8+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
9+
10+
error[E0658]: `#[track_caller]` on closures is currently unstable
11+
--> $DIR/async-closure-gate.rs:6:38
12+
|
13+
LL | let _ = #[track_caller] async || {
14+
| ______________________________________^
15+
LL | |
16+
LL | |
17+
LL | | };
18+
| |_____^
19+
|
20+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
21+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
22+
23+
error: aborting due to 2 previous errors
24+
25+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/async-await/track-caller/panic-track-caller.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// run-pass
22
// edition:2021
33
// needs-unwind
4-
#![feature(closure_track_caller)]
4+
#![feature(closure_track_caller, async_closure, stmt_expr_attributes)]
55

66
use std::future::Future;
77
use std::panic;
@@ -67,6 +67,13 @@ async fn foo_assoc() {
6767
Foo::bar_assoc().await
6868
}
6969

70+
async fn foo_closure() {
71+
let c = #[track_caller] async || {
72+
panic!();
73+
};
74+
c().await
75+
}
76+
7077
fn panicked_at(f: impl FnOnce() + panic::UnwindSafe) -> u32 {
7178
let loc = Arc::new(Mutex::new(None));
7279

@@ -87,4 +94,5 @@ fn main() {
8794
assert_eq!(panicked_at(|| block_on(foo())), 41);
8895
assert_eq!(panicked_at(|| block_on(foo_track_caller())), 54);
8996
assert_eq!(panicked_at(|| block_on(foo_assoc())), 67);
97+
assert_eq!(panicked_at(|| block_on(foo_closure())), 74);
9098
}

0 commit comments

Comments
 (0)