Skip to content

Commit 78cf0b9

Browse files
authored
Rollup merge of #105180 - nbdd0121:async_track_caller, r=compiler-errors
Use proper HirId for async track_caller attribute check Fix #105134
2 parents f4643f5 + 34c3773 commit 78cf0b9

File tree

4 files changed

+63
-22
lines changed

4 files changed

+63
-22
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
147147
),
148148
ExprKind::Async(capture_clause, closure_node_id, block) => self.make_async_expr(
149149
*capture_clause,
150+
None,
150151
*closure_node_id,
151152
None,
152153
e.span,
@@ -584,6 +585,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
584585
pub(super) fn make_async_expr(
585586
&mut self,
586587
capture_clause: CaptureBy,
588+
outer_hir_id: Option<hir::HirId>,
587589
closure_node_id: NodeId,
588590
ret_ty: Option<hir::FnRetTy<'hir>>,
589591
span: Span,
@@ -651,18 +653,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
651653

652654
hir::ExprKind::Closure(c)
653655
};
654-
let parent_has_track_caller = self
655-
.attrs
656-
.values()
657-
.find(|attrs| attrs.into_iter().find(|attr| attr.has_name(sym::track_caller)).is_some())
658-
.is_some();
659-
let unstable_span =
660-
self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone());
661656

662-
let hir_id = if parent_has_track_caller {
663-
let generator_hir_id = self.lower_node_id(closure_node_id);
657+
let track_caller = outer_hir_id
658+
.and_then(|id| self.attrs.get(&id.local_id))
659+
.map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)));
660+
661+
let hir_id = self.lower_node_id(closure_node_id);
662+
if track_caller {
663+
let unstable_span = self.mark_span_with_reason(
664+
DesugaringKind::Async,
665+
span,
666+
self.allow_gen_future.clone(),
667+
);
664668
self.lower_attrs(
665-
generator_hir_id,
669+
hir_id,
666670
&[Attribute {
667671
kind: AttrKind::Normal(ptr::P(NormalAttr {
668672
item: AttrItem {
@@ -677,10 +681,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
677681
span: unstable_span,
678682
}],
679683
);
680-
generator_hir_id
681-
} else {
682-
self.lower_node_id(closure_node_id)
683-
};
684+
}
684685

685686
let generator = hir::Expr { hir_id, kind: generator_kind, span: self.lower_span(span) };
686687

@@ -1019,6 +1020,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
10191020

10201021
let async_body = this.make_async_expr(
10211022
capture_clause,
1023+
// FIXME(nbdd0121): This should also use a proper HIR id so `#[track_caller]`
1024+
// can be applied on async closures as well.
1025+
None,
10221026
inner_closure_id,
10231027
async_ret_ty,
10241028
body.span,

compiler/rustc_ast_lowering/src/item.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
253253
// only cares about the input argument patterns in the function
254254
// declaration (decl), not the return types.
255255
let asyncness = header.asyncness;
256-
let body_id =
257-
this.lower_maybe_async_body(span, &decl, asyncness, body.as_deref());
256+
let body_id = this.lower_maybe_async_body(
257+
span,
258+
hir_id,
259+
&decl,
260+
asyncness,
261+
body.as_deref(),
262+
);
258263

259264
let mut itctx = ImplTraitContext::Universal;
260265
let (generics, decl) = this.lower_generics(generics, id, &mut itctx, |this| {
@@ -701,6 +706,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
701706

702707
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
703708
let hir_id = self.lower_node_id(i.id);
709+
self.lower_attrs(hir_id, &i.attrs);
704710
let trait_item_def_id = hir_id.expect_owner();
705711

706712
let (generics, kind, has_default) = match &i.kind {
@@ -724,7 +730,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
724730
AssocItemKind::Fn(box Fn { sig, generics, body: Some(body), .. }) => {
725731
let asyncness = sig.header.asyncness;
726732
let body_id =
727-
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, Some(&body));
733+
self.lower_maybe_async_body(i.span, hir_id, &sig.decl, asyncness, Some(&body));
728734
let (generics, sig) = self.lower_method_sig(
729735
generics,
730736
sig,
@@ -759,7 +765,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
759765
AssocItemKind::MacCall(..) => panic!("macro item shouldn't exist at this point"),
760766
};
761767

762-
self.lower_attrs(hir_id, &i.attrs);
763768
let item = hir::TraitItem {
764769
owner_id: trait_item_def_id,
765770
ident: self.lower_ident(i.ident),
@@ -798,6 +803,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
798803
// Since `default impl` is not yet implemented, this is always true in impls.
799804
let has_value = true;
800805
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
806+
let hir_id = self.lower_node_id(i.id);
807+
self.lower_attrs(hir_id, &i.attrs);
801808

802809
let (generics, kind) = match &i.kind {
803810
AssocItemKind::Const(_, ty, expr) => {
@@ -810,8 +817,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
810817
AssocItemKind::Fn(box Fn { sig, generics, body, .. }) => {
811818
self.current_item = Some(i.span);
812819
let asyncness = sig.header.asyncness;
813-
let body_id =
814-
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, body.as_deref());
820+
let body_id = self.lower_maybe_async_body(
821+
i.span,
822+
hir_id,
823+
&sig.decl,
824+
asyncness,
825+
body.as_deref(),
826+
);
815827
let (generics, sig) = self.lower_method_sig(
816828
generics,
817829
sig,
@@ -844,8 +856,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
844856
AssocItemKind::MacCall(..) => panic!("`TyMac` should have been expanded by now"),
845857
};
846858

847-
let hir_id = self.lower_node_id(i.id);
848-
self.lower_attrs(hir_id, &i.attrs);
849859
let item = hir::ImplItem {
850860
owner_id: hir_id.expect_owner(),
851861
ident: self.lower_ident(i.ident),
@@ -978,6 +988,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
978988
fn lower_maybe_async_body(
979989
&mut self,
980990
span: Span,
991+
fn_id: hir::HirId,
981992
decl: &FnDecl,
982993
asyncness: Async,
983994
body: Option<&Block>,
@@ -1128,6 +1139,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11281139

11291140
let async_expr = this.make_async_expr(
11301141
CaptureBy::Value,
1142+
Some(fn_id),
11311143
closure_id,
11321144
None,
11331145
body.span,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// check-pass
2+
// edition:2021
3+
4+
#[track_caller]
5+
fn f() {
6+
let _ = async {};
7+
}
8+
9+
fn main() {
10+
f();
11+
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ async fn foo_track_caller() {
5454
bar_track_caller().await
5555
}
5656

57+
struct Foo;
58+
59+
impl Foo {
60+
#[track_caller]
61+
async fn bar_assoc() {
62+
panic!();
63+
}
64+
}
65+
66+
async fn foo_assoc() {
67+
Foo::bar_assoc().await
68+
}
69+
5770
fn panicked_at(f: impl FnOnce() + panic::UnwindSafe) -> u32 {
5871
let loc = Arc::new(Mutex::new(None));
5972

@@ -73,4 +86,5 @@ fn panicked_at(f: impl FnOnce() + panic::UnwindSafe) -> u32 {
7386
fn main() {
7487
assert_eq!(panicked_at(|| block_on(foo())), 41);
7588
assert_eq!(panicked_at(|| block_on(foo_track_caller())), 54);
89+
assert_eq!(panicked_at(|| block_on(foo_assoc())), 67);
7690
}

0 commit comments

Comments
 (0)