1
Fork 0

Use proper HirId for async track_caller attribute check

This commit is contained in:
Gary Guo 2022-12-02 16:40:57 +00:00
parent 9c0bc3028a
commit 4c73b64632
3 changed files with 45 additions and 20 deletions

View file

@ -147,6 +147,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
), ),
ExprKind::Async(capture_clause, closure_node_id, block) => self.make_async_expr( ExprKind::Async(capture_clause, closure_node_id, block) => self.make_async_expr(
*capture_clause, *capture_clause,
None,
*closure_node_id, *closure_node_id,
None, None,
e.span, e.span,
@ -581,6 +582,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
pub(super) fn make_async_expr( pub(super) fn make_async_expr(
&mut self, &mut self,
capture_clause: CaptureBy, capture_clause: CaptureBy,
outer_hir_id: Option<hir::HirId>,
closure_node_id: NodeId, closure_node_id: NodeId,
ret_ty: Option<hir::FnRetTy<'hir>>, ret_ty: Option<hir::FnRetTy<'hir>>,
span: Span, span: Span,
@ -647,18 +649,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ExprKind::Closure(c) hir::ExprKind::Closure(c)
}; };
let parent_has_track_caller = self
.attrs
.values()
.find(|attrs| attrs.into_iter().find(|attr| attr.has_name(sym::track_caller)).is_some())
.is_some();
let unstable_span =
self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone());
let hir_id = if parent_has_track_caller { let track_caller = outer_hir_id
let generator_hir_id = self.lower_node_id(closure_node_id); .and_then(|id| self.attrs.get(&id.local_id))
.map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)));
let hir_id = self.lower_node_id(closure_node_id);
if track_caller {
let unstable_span = self.mark_span_with_reason(
DesugaringKind::Async,
span,
self.allow_gen_future.clone(),
);
self.lower_attrs( self.lower_attrs(
generator_hir_id, hir_id,
&[Attribute { &[Attribute {
kind: AttrKind::Normal(ptr::P(NormalAttr { kind: AttrKind::Normal(ptr::P(NormalAttr {
item: AttrItem { item: AttrItem {
@ -673,10 +677,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
span: unstable_span, span: unstable_span,
}], }],
); );
generator_hir_id }
} else {
self.lower_node_id(closure_node_id)
};
let generator = hir::Expr { hir_id, kind: generator_kind, span: self.lower_span(span) }; let generator = hir::Expr { hir_id, kind: generator_kind, span: self.lower_span(span) };
@ -1012,6 +1013,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let async_body = this.make_async_expr( let async_body = this.make_async_expr(
capture_clause, capture_clause,
None,
inner_closure_id, inner_closure_id,
async_ret_ty, async_ret_ty,
body.span, body.span,

View file

@ -268,8 +268,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
// only cares about the input argument patterns in the function // only cares about the input argument patterns in the function
// declaration (decl), not the return types. // declaration (decl), not the return types.
let asyncness = header.asyncness; let asyncness = header.asyncness;
let body_id = let body_id = this.lower_maybe_async_body(
this.lower_maybe_async_body(span, &decl, asyncness, body.as_deref()); span,
hir_id,
&decl,
asyncness,
body.as_deref(),
);
let mut itctx = ImplTraitContext::Universal; let mut itctx = ImplTraitContext::Universal;
let (generics, decl) = this.lower_generics(generics, id, &mut itctx, |this| { let (generics, decl) = this.lower_generics(generics, id, &mut itctx, |this| {
@ -789,7 +794,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
AssocItemKind::Fn(box Fn { sig, generics, body: Some(body), .. }) => { AssocItemKind::Fn(box Fn { sig, generics, body: Some(body), .. }) => {
let asyncness = sig.header.asyncness; let asyncness = sig.header.asyncness;
let body_id = 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( let (generics, sig) = self.lower_method_sig(
generics, generics,
sig, sig,
@ -863,6 +868,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// Since `default impl` is not yet implemented, this is always true in impls. // Since `default impl` is not yet implemented, this is always true in impls.
let has_value = true; let has_value = true;
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value); let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
let hir_id = self.lower_node_id(i.id);
let (generics, kind) = match &i.kind { let (generics, kind) = match &i.kind {
AssocItemKind::Const(_, ty, expr) => { AssocItemKind::Const(_, ty, expr) => {
@ -875,8 +881,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
AssocItemKind::Fn(box Fn { sig, generics, body, .. }) => { AssocItemKind::Fn(box Fn { sig, generics, body, .. }) => {
self.current_item = Some(i.span); self.current_item = Some(i.span);
let asyncness = sig.header.asyncness; let asyncness = sig.header.asyncness;
let body_id = let body_id = self.lower_maybe_async_body(
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, body.as_deref()); i.span,
hir_id,
&sig.decl,
asyncness,
body.as_deref(),
);
let (generics, sig) = self.lower_method_sig( let (generics, sig) = self.lower_method_sig(
generics, generics,
sig, sig,
@ -909,7 +920,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
AssocItemKind::MacCall(..) => panic!("`TyMac` should have been expanded by now"), 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); self.lower_attrs(hir_id, &i.attrs);
let item = hir::ImplItem { let item = hir::ImplItem {
owner_id: hir_id.expect_owner(), owner_id: hir_id.expect_owner(),
@ -1043,6 +1053,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_maybe_async_body( fn lower_maybe_async_body(
&mut self, &mut self,
span: Span, span: Span,
fn_id: hir::HirId,
decl: &FnDecl, decl: &FnDecl,
asyncness: Async, asyncness: Async,
body: Option<&Block>, body: Option<&Block>,
@ -1193,6 +1204,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let async_expr = this.make_async_expr( let async_expr = this.make_async_expr(
CaptureBy::Value, CaptureBy::Value,
Some(fn_id),
closure_id, closure_id,
None, None,
body.span, body.span,

View file

@ -0,0 +1,11 @@
// check-pass
// edition:2021
#[track_caller]
fn f() {
let _ = async {};
}
fn main() {
f();
}