Skip to content

Commit 4c73b64

Browse files
committed
Use proper HirId for async track_caller attribute check
1 parent 9c0bc30 commit 4c73b64

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+16-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,
@@ -581,6 +582,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
581582
pub(super) fn make_async_expr(
582583
&mut self,
583584
capture_clause: CaptureBy,
585+
outer_hir_id: Option<hir::HirId>,
584586
closure_node_id: NodeId,
585587
ret_ty: Option<hir::FnRetTy<'hir>>,
586588
span: Span,
@@ -647,18 +649,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
647649

648650
hir::ExprKind::Closure(c)
649651
};
650-
let parent_has_track_caller = self
651-
.attrs
652-
.values()
653-
.find(|attrs| attrs.into_iter().find(|attr| attr.has_name(sym::track_caller)).is_some())
654-
.is_some();
655-
let unstable_span =
656-
self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone());
657652

658-
let hir_id = if parent_has_track_caller {
659-
let generator_hir_id = self.lower_node_id(closure_node_id);
653+
let track_caller = outer_hir_id
654+
.and_then(|id| self.attrs.get(&id.local_id))
655+
.map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)));
656+
657+
let hir_id = self.lower_node_id(closure_node_id);
658+
if track_caller {
659+
let unstable_span = self.mark_span_with_reason(
660+
DesugaringKind::Async,
661+
span,
662+
self.allow_gen_future.clone(),
663+
);
660664
self.lower_attrs(
661-
generator_hir_id,
665+
hir_id,
662666
&[Attribute {
663667
kind: AttrKind::Normal(ptr::P(NormalAttr {
664668
item: AttrItem {
@@ -673,10 +677,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
673677
span: unstable_span,
674678
}],
675679
);
676-
generator_hir_id
677-
} else {
678-
self.lower_node_id(closure_node_id)
679-
};
680+
}
680681

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

@@ -1012,6 +1013,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10121013

10131014
let async_body = this.make_async_expr(
10141015
capture_clause,
1016+
None,
10151017
inner_closure_id,
10161018
async_ret_ty,
10171019
body.span,

compiler/rustc_ast_lowering/src/item.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
268268
// only cares about the input argument patterns in the function
269269
// declaration (decl), not the return types.
270270
let asyncness = header.asyncness;
271-
let body_id =
272-
this.lower_maybe_async_body(span, &decl, asyncness, body.as_deref());
271+
let body_id = this.lower_maybe_async_body(
272+
span,
273+
hir_id,
274+
&decl,
275+
asyncness,
276+
body.as_deref(),
277+
);
273278

274279
let mut itctx = ImplTraitContext::Universal;
275280
let (generics, decl) = this.lower_generics(generics, id, &mut itctx, |this| {
@@ -789,7 +794,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
789794
AssocItemKind::Fn(box Fn { sig, generics, body: Some(body), .. }) => {
790795
let asyncness = sig.header.asyncness;
791796
let body_id =
792-
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, Some(&body));
797+
self.lower_maybe_async_body(i.span, hir_id, &sig.decl, asyncness, Some(&body));
793798
let (generics, sig) = self.lower_method_sig(
794799
generics,
795800
sig,
@@ -863,6 +868,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
863868
// Since `default impl` is not yet implemented, this is always true in impls.
864869
let has_value = true;
865870
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
871+
let hir_id = self.lower_node_id(i.id);
866872

867873
let (generics, kind) = match &i.kind {
868874
AssocItemKind::Const(_, ty, expr) => {
@@ -875,8 +881,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
875881
AssocItemKind::Fn(box Fn { sig, generics, body, .. }) => {
876882
self.current_item = Some(i.span);
877883
let asyncness = sig.header.asyncness;
878-
let body_id =
879-
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, body.as_deref());
884+
let body_id = self.lower_maybe_async_body(
885+
i.span,
886+
hir_id,
887+
&sig.decl,
888+
asyncness,
889+
body.as_deref(),
890+
);
880891
let (generics, sig) = self.lower_method_sig(
881892
generics,
882893
sig,
@@ -909,7 +920,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
909920
AssocItemKind::MacCall(..) => panic!("`TyMac` should have been expanded by now"),
910921
};
911922

912-
let hir_id = self.lower_node_id(i.id);
913923
self.lower_attrs(hir_id, &i.attrs);
914924
let item = hir::ImplItem {
915925
owner_id: hir_id.expect_owner(),
@@ -1043,6 +1053,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10431053
fn lower_maybe_async_body(
10441054
&mut self,
10451055
span: Span,
1056+
fn_id: hir::HirId,
10461057
decl: &FnDecl,
10471058
asyncness: Async,
10481059
body: Option<&Block>,
@@ -1193,6 +1204,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11931204

11941205
let async_expr = this.make_async_expr(
11951206
CaptureBy::Value,
1207+
Some(fn_id),
11961208
closure_id,
11971209
None,
11981210
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+
}

0 commit comments

Comments
 (0)