Skip to content

Commit

Permalink
Pass down parent hirId
Browse files Browse the repository at this point in the history
  • Loading branch information
bryangarza committed Dec 6, 2022
1 parent c0ff47b commit 882873a
Showing 5 changed files with 94 additions and 12 deletions.
14 changes: 10 additions & 4 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ use super::errors::{
use super::ResolverAstLoweringExt;
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
use crate::{FnDeclKind, ImplTraitPosition};
use hir::HirId;
use rustc_ast::attr;
use rustc_ast::ptr::P as AstP;
use rustc_ast::*;
@@ -153,6 +154,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
capture_clause,
closure_node_id,
None,
None,
block.span,
hir::AsyncGeneratorKind::Block,
|this| this.with_new_scopes(|this| this.lower_block_expr(block)),
@@ -588,6 +590,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
&mut self,
capture_clause: CaptureBy,
closure_node_id: NodeId,
// The ID of the item that originally contained the async expr (which
// could be an async fn, for example)
maybe_item_hir_id: Option<HirId>,
ret_ty: Option<AstP<Ty>>,
span: Span,
async_gen_kind: hir::AsyncGeneratorKind,
@@ -661,10 +666,10 @@ impl<'hir> LoweringContext<'_, 'hir> {

let hir_id = self.lower_node_id(closure_node_id);
if self.tcx.features().closure_track_caller {
let parent_has_track_caller = self
.attrs
.values()
.any(|attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)));
let parent_has_track_caller = maybe_item_hir_id.map(|item_hir_id| {
let maybe_item_attrs = self.attrs.get(&item_hir_id.local_id);
maybe_item_attrs.map(|item_attrs| item_attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)))
}).flatten().unwrap_or(false);
if parent_has_track_caller {
self.lower_attrs(
hir_id,
@@ -1007,6 +1012,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let async_body = this.make_async_expr(
capture_clause,
inner_closure_id,
None,
async_ret_ty,
body.span,
hir::AsyncGeneratorKind::Closure,
15 changes: 9 additions & 6 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ use super::ResolverAstLoweringExt;
use super::{Arena, AstOwner, ImplTraitContext, ImplTraitPosition};
use super::{FnDeclKind, LoweringContext, ParamMode};

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

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

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

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

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

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

let hir_id = self.lower_node_id(i.id);
self.lower_attrs(hir_id, &i.attrs);
let item = hir::ImplItem {
owner_id: hir_id.expect_owner(),
ident: self.lower_ident(i.ident),
@@ -1064,6 +1065,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_maybe_async_body(
&mut self,
span: Span,
hir_id: HirId,
decl: &FnDecl,
asyncness: Async,
body: Option<&Block>,
@@ -1215,6 +1217,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let async_expr = this.make_async_expr(
CaptureBy::Value,
closure_id,
Some(hir_id),
None,
body.span,
hir::AsyncGeneratorKind::Fn,
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
@@ -1364,7 +1364,7 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
span: Span,
hir_id: HirId,
) {
if let HirFnKind::ItemFn(_, _, _) = fn_kind && fn_kind.asyncness() == IsAsync::Async && !cx.tcx.features().closure_track_caller {
if fn_kind.asyncness() == IsAsync::Async && !cx.tcx.features().closure_track_caller {
// Now, check if the function has the `#[track_caller]` attribute
let attrs = cx.tcx.hir().attrs(hir_id);
let maybe_track_caller = attrs.iter().find(|attr| attr.has_name(sym::track_caller));
Original file line number Diff line number Diff line change
@@ -10,5 +10,25 @@ LL | | }
|
= note: `#[warn(ungated_async_fn_track_caller)]` on by default

warning: 1 warning emitted
warning: `#[track_caller]` on async functions is a no-op
--> $DIR/panic-track-caller.rs:71:5
|
LL | #[track_caller]
| ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller`
LL | / async fn baz_track_caller() {
LL | | panic!()
LL | | }
| |_____- this function will not propagate the caller location

warning: `#[track_caller]` on async functions is a no-op
--> $DIR/panic-track-caller.rs:88:5
|
LL | #[track_caller]
| ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller`
LL | / async fn baz_self_track_caller(&self) {
LL | | panic!()
LL | | }
| |_____- this function will not propagate the caller location

warning: 3 warnings emitted

53 changes: 53 additions & 0 deletions src/test/ui/async-await/track-caller/panic-track-caller.rs
Original file line number Diff line number Diff line change
@@ -56,6 +56,46 @@ async fn foo_track_caller() {
bar_track_caller().await
}

struct Xyz;

impl Xyz {

async fn baz() {
panic!()
}

async fn qux() {
Xyz::baz().await
}

#[track_caller] //[nofeat]~ WARN `#[track_caller]` on async functions is a no-op
async fn baz_track_caller() {
panic!()
}

async fn qux_track_caller() {
Xyz::baz_track_caller().await
}

async fn baz_self(&self) {
panic!()
}

async fn qux_self(&self) {
self.baz_self().await
}

#[track_caller] //[nofeat]~ WARN `#[track_caller]` on async functions is a no-op
async fn baz_self_track_caller(&self) {
panic!()
}

async fn qux_self_track_caller(&self) {
self.baz_self_track_caller().await
}

}

fn panicked_at(f: impl FnOnce() + panic::UnwindSafe) -> u32 {
let loc = Arc::new(Mutex::new(None));

@@ -78,4 +118,17 @@ fn main() {
assert_eq!(panicked_at(|| block_on(foo_track_caller())), 56);
#[cfg(nofeat)]
assert_eq!(panicked_at(|| block_on(foo_track_caller())), 52);

assert_eq!(panicked_at(|| block_on(Xyz::qux())), 64);
#[cfg(feat)]
assert_eq!(panicked_at(|| block_on(Xyz::qux_track_caller())), 77);
#[cfg(nofeat)]
assert_eq!(panicked_at(|| block_on(Xyz::qux_track_caller())), 73);

let xyz = Xyz;
assert_eq!(panicked_at(|| block_on(xyz.qux_self())), 81);
#[cfg(feat)]
assert_eq!(panicked_at(|| block_on(xyz.qux_self_track_caller())), 94);
#[cfg(nofeat)]
assert_eq!(panicked_at(|| block_on(xyz.qux_self_track_caller())), 90);
}

0 comments on commit 882873a

Please sign in to comment.