Skip to content

Commit 882873a

Browse files
committed
Pass down parent hirId
1 parent c0ff47b commit 882873a

File tree

5 files changed

+94
-12
lines changed

5 files changed

+94
-12
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::errors::{
77
use super::ResolverAstLoweringExt;
88
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
99
use crate::{FnDeclKind, ImplTraitPosition};
10+
use hir::HirId;
1011
use rustc_ast::attr;
1112
use rustc_ast::ptr::P as AstP;
1213
use rustc_ast::*;
@@ -153,6 +154,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
153154
capture_clause,
154155
closure_node_id,
155156
None,
157+
None,
156158
block.span,
157159
hir::AsyncGeneratorKind::Block,
158160
|this| this.with_new_scopes(|this| this.lower_block_expr(block)),
@@ -588,6 +590,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
588590
&mut self,
589591
capture_clause: CaptureBy,
590592
closure_node_id: NodeId,
593+
// The ID of the item that originally contained the async expr (which
594+
// could be an async fn, for example)
595+
maybe_item_hir_id: Option<HirId>,
591596
ret_ty: Option<AstP<Ty>>,
592597
span: Span,
593598
async_gen_kind: hir::AsyncGeneratorKind,
@@ -661,10 +666,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
661666

662667
let hir_id = self.lower_node_id(closure_node_id);
663668
if self.tcx.features().closure_track_caller {
664-
let parent_has_track_caller = self
665-
.attrs
666-
.values()
667-
.any(|attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)));
669+
let parent_has_track_caller = maybe_item_hir_id.map(|item_hir_id| {
670+
let maybe_item_attrs = self.attrs.get(&item_hir_id.local_id);
671+
maybe_item_attrs.map(|item_attrs| item_attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)))
672+
}).flatten().unwrap_or(false);
668673
if parent_has_track_caller {
669674
self.lower_attrs(
670675
hir_id,
@@ -1007,6 +1012,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10071012
let async_body = this.make_async_expr(
10081013
capture_clause,
10091014
inner_closure_id,
1015+
None,
10101016
async_ret_ty,
10111017
body.span,
10121018
hir::AsyncGeneratorKind::Closure,

compiler/rustc_ast_lowering/src/item.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::ResolverAstLoweringExt;
33
use super::{Arena, AstOwner, ImplTraitContext, ImplTraitPosition};
44
use super::{FnDeclKind, LoweringContext, ParamMode};
55

6+
use hir::HirId;
67
use rustc_ast::ptr::P;
78
use rustc_ast::visit::AssocCtxt;
89
use rustc_ast::*;
@@ -269,7 +270,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
269270
// declaration (decl), not the return types.
270271
let asyncness = header.asyncness;
271272
let body_id =
272-
this.lower_maybe_async_body(span, &decl, asyncness, body.as_deref());
273+
this.lower_maybe_async_body(span, hir_id, &decl, asyncness, body.as_deref());
273274

274275
let mut itctx = ImplTraitContext::Universal;
275276
let (generics, decl) = this.lower_generics(generics, id, &mut itctx, |this| {
@@ -781,6 +782,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
781782

782783
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
783784
let hir_id = self.lower_node_id(i.id);
785+
self.lower_attrs(hir_id, &i.attrs);
784786
let trait_item_def_id = hir_id.expect_owner();
785787

786788
let (generics, kind, has_default) = match i.kind {
@@ -804,7 +806,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
804806
AssocItemKind::Fn(box Fn { ref sig, ref generics, body: Some(ref body), .. }) => {
805807
let asyncness = sig.header.asyncness;
806808
let body_id =
807-
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, Some(&body));
809+
self.lower_maybe_async_body(i.span, hir_id, &sig.decl, asyncness, Some(&body));
808810
let (generics, sig) = self.lower_method_sig(
809811
generics,
810812
sig,
@@ -845,7 +847,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
845847
AssocItemKind::MacCall(..) => panic!("macro item shouldn't exist at this point"),
846848
};
847849

848-
self.lower_attrs(hir_id, &i.attrs);
849850
let item = hir::TraitItem {
850851
owner_id: trait_item_def_id,
851852
ident: self.lower_ident(i.ident),
@@ -884,6 +885,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
884885
// Since `default impl` is not yet implemented, this is always true in impls.
885886
let has_value = true;
886887
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
888+
let hir_id = self.lower_node_id(i.id);
889+
self.lower_attrs(hir_id, &i.attrs);
887890

888891
let (generics, kind) = match &i.kind {
889892
AssocItemKind::Const(_, ty, expr) => {
@@ -897,7 +900,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
897900
self.current_item = Some(i.span);
898901
let asyncness = sig.header.asyncness;
899902
let body_id =
900-
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, body.as_deref());
903+
self.lower_maybe_async_body(i.span, hir_id, &sig.decl, asyncness, body.as_deref());
901904
let (generics, sig) = self.lower_method_sig(
902905
generics,
903906
sig,
@@ -930,8 +933,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
930933
AssocItemKind::MacCall(..) => panic!("`TyMac` should have been expanded by now"),
931934
};
932935

933-
let hir_id = self.lower_node_id(i.id);
934-
self.lower_attrs(hir_id, &i.attrs);
935936
let item = hir::ImplItem {
936937
owner_id: hir_id.expect_owner(),
937938
ident: self.lower_ident(i.ident),
@@ -1064,6 +1065,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10641065
fn lower_maybe_async_body(
10651066
&mut self,
10661067
span: Span,
1068+
hir_id: HirId,
10671069
decl: &FnDecl,
10681070
asyncness: Async,
10691071
body: Option<&Block>,
@@ -1215,6 +1217,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12151217
let async_expr = this.make_async_expr(
12161218
CaptureBy::Value,
12171219
closure_id,
1220+
Some(hir_id),
12181221
None,
12191222
body.span,
12201223
hir::AsyncGeneratorKind::Fn,

compiler/rustc_lint/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,7 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
13641364
span: Span,
13651365
hir_id: HirId,
13661366
) {
1367-
if let HirFnKind::ItemFn(_, _, _) = fn_kind && fn_kind.asyncness() == IsAsync::Async && !cx.tcx.features().closure_track_caller {
1367+
if fn_kind.asyncness() == IsAsync::Async && !cx.tcx.features().closure_track_caller {
13681368
// Now, check if the function has the `#[track_caller]` attribute
13691369
let attrs = cx.tcx.hir().attrs(hir_id);
13701370
let maybe_track_caller = attrs.iter().find(|attr| attr.has_name(sym::track_caller));

src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr

+21-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,25 @@ LL | | }
1010
|
1111
= note: `#[warn(ungated_async_fn_track_caller)]` on by default
1212

13-
warning: 1 warning emitted
13+
warning: `#[track_caller]` on async functions is a no-op
14+
--> $DIR/panic-track-caller.rs:71:5
15+
|
16+
LL | #[track_caller]
17+
| ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller`
18+
LL | / async fn baz_track_caller() {
19+
LL | | panic!()
20+
LL | | }
21+
| |_____- this function will not propagate the caller location
22+
23+
warning: `#[track_caller]` on async functions is a no-op
24+
--> $DIR/panic-track-caller.rs:88:5
25+
|
26+
LL | #[track_caller]
27+
| ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller`
28+
LL | / async fn baz_self_track_caller(&self) {
29+
LL | | panic!()
30+
LL | | }
31+
| |_____- this function will not propagate the caller location
32+
33+
warning: 3 warnings emitted
1434

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

+53
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,46 @@ async fn foo_track_caller() {
5656
bar_track_caller().await
5757
}
5858

59+
struct Xyz;
60+
61+
impl Xyz {
62+
63+
async fn baz() {
64+
panic!()
65+
}
66+
67+
async fn qux() {
68+
Xyz::baz().await
69+
}
70+
71+
#[track_caller] //[nofeat]~ WARN `#[track_caller]` on async functions is a no-op
72+
async fn baz_track_caller() {
73+
panic!()
74+
}
75+
76+
async fn qux_track_caller() {
77+
Xyz::baz_track_caller().await
78+
}
79+
80+
async fn baz_self(&self) {
81+
panic!()
82+
}
83+
84+
async fn qux_self(&self) {
85+
self.baz_self().await
86+
}
87+
88+
#[track_caller] //[nofeat]~ WARN `#[track_caller]` on async functions is a no-op
89+
async fn baz_self_track_caller(&self) {
90+
panic!()
91+
}
92+
93+
async fn qux_self_track_caller(&self) {
94+
self.baz_self_track_caller().await
95+
}
96+
97+
}
98+
5999
fn panicked_at(f: impl FnOnce() + panic::UnwindSafe) -> u32 {
60100
let loc = Arc::new(Mutex::new(None));
61101

@@ -78,4 +118,17 @@ fn main() {
78118
assert_eq!(panicked_at(|| block_on(foo_track_caller())), 56);
79119
#[cfg(nofeat)]
80120
assert_eq!(panicked_at(|| block_on(foo_track_caller())), 52);
121+
122+
assert_eq!(panicked_at(|| block_on(Xyz::qux())), 64);
123+
#[cfg(feat)]
124+
assert_eq!(panicked_at(|| block_on(Xyz::qux_track_caller())), 77);
125+
#[cfg(nofeat)]
126+
assert_eq!(panicked_at(|| block_on(Xyz::qux_track_caller())), 73);
127+
128+
let xyz = Xyz;
129+
assert_eq!(panicked_at(|| block_on(xyz.qux_self())), 81);
130+
#[cfg(feat)]
131+
assert_eq!(panicked_at(|| block_on(xyz.qux_self_track_caller())), 94);
132+
#[cfg(nofeat)]
133+
assert_eq!(panicked_at(|| block_on(xyz.qux_self_track_caller())), 90);
81134
}

0 commit comments

Comments
 (0)