Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3634,6 +3634,7 @@ version = "0.0.0"
dependencies = [
"rustc_abi",
"rustc_ast",
"rustc_attr_ir",
"rustc_attr_parsing",
"rustc_data_structures",
"rustc_errors",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast_lowering/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ doctest = false
# tidy-alphabetical-start
rustc_abi = { path = "../rustc_abi" }
rustc_ast = { path = "../rustc_ast" }
rustc_attr_ir = { path = "../rustc_attr_ir" }
rustc_attr_parsing = { path = "../rustc_attr_parsing" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" }
Expand Down
42 changes: 12 additions & 30 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ use std::ops::ControlFlow;
use std::sync::Arc;

use rustc_ast::node_id::NodeMap;
use rustc_ast::visit::{Visitor, walk_expr};
use rustc_ast::*;
use rustc_attr_ir::lang_items::LangItem;
use rustc_attr_ir::target::Target;
use rustc_errors::msg;
use rustc_hir as hir;
use rustc_hir::attrs::lang_items::LangItem;
use rustc_hir::HirId;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{HirId, Target, find_attr};
use rustc_middle::span_bug;
use rustc_middle::ty::TyCtxt;
use rustc_session::diagnostics::report_lit_error;
use rustc_span::{ByteSymbol, DUMMY_SP, DesugaringKind, Ident, Span, Spanned, Symbol, respan, sym};
use thin_vec::{ThinVec, thin_vec};
use visit::{Visitor, walk_expr};

mod closure;

use crate::diagnostics::{
Expand Down Expand Up @@ -882,35 +882,17 @@ impl<'hir> LoweringContext<'_, 'hir> {

/// Forwards a possible `#[track_caller]` annotation from `outer_hir_id` to
/// `inner_hir_id` in case the `async_fn_track_caller` feature is enabled.
pub(super) fn maybe_forward_track_caller(
&mut self,
span: Span,
outer_hir_id: HirId,
inner_hir_id: HirId,
) {
pub(super) fn maybe_forward_track_caller(&mut self, outer_hir_id: HirId, inner_hir_id: HirId) {
if self.tcx.features().async_fn_track_caller()
&& let Some(attrs) = self.attrs.get(&outer_hir_id.local_id)
&& find_attr!(*attrs, TrackCaller(_))
&& let Some(t) = attrs.iter().find(|a| {

@JonathanBrouwer JonathanBrouwer Aug 31, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda sad that we need to expand the find_attr! here but this code is so much better than whatever was going on before, so thanks!

View changes since the review

matches!(
a,
rustc_attr_ir::Attribute::Parsed(rustc_attr_ir::AttributeKind::TrackCaller(_))
)
})
{
let unstable_span = self.mark_span_with_reason(
DesugaringKind::Async,
span,
Some(Arc::clone(&self.allow_gen_future)),
);
self.lower_attrs(
inner_hir_id,
&[Attribute {
kind: AttrKind::Normal(Box::new(NormalAttr::from_ident(Ident::new(
sym::track_caller,
span,
)))),
id: self.tcx.sess.psess.attr_id_generator.mk_attr_id(),
style: AttrStyle::Outer,
span: unstable_span,
}],
span,
Target::Fn,
);
self.attrs.insert(inner_hir_id.local_id, std::slice::from_ref(t));
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/expr/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
)
});

this.maybe_forward_track_caller(body.span, closure_hir_id, expr.hir_id);
this.maybe_forward_track_caller(closure_hir_id, expr.hir_id);

(parameters, expr)
});
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

// FIXME(async_fn_track_caller): Can this be moved above?
let hir_id = expr.hir_id;
this.maybe_forward_track_caller(body.span, fn_id, hir_id);
this.maybe_forward_track_caller(fn_id, hir_id);

(parameters, expr)
})
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,15 @@ impl NoArgsAttributeParser for TrackCallerParser {
});
}
}
Target::Closure if !cx.features().closure_track_caller() => {
feature_err(
cx.sess(),
sym::closure_track_caller,
attr_span,
"`#[track_caller]` on closures is currently unstable",
)
.emit();
}
_ => {}
}
}
Expand Down
15 changes: 1 addition & 14 deletions compiler/rustc_codegen_ssa/src/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use rustc_middle::middle::codegen_fn_attrs::{
use rustc_middle::mono::Visibility;
use rustc_middle::query::Providers;
use rustc_middle::ty::{self as ty, TyCtxt};
use rustc_session::diagnostics::feature_err;
use rustc_span::{Span, sym};
use rustc_span::Span;
use rustc_target::spec::Os;

use crate::diagnostics;
Expand Down Expand Up @@ -155,18 +154,6 @@ fn process_builtin_attrs(
// This error is already reported in `rustc_ast_passes/src/ast_validation.rs`.
tcx.dcx().delayed_bug("`#[track_caller]` requires the Rust ABI");
}
if is_closure
&& !tcx.features().closure_track_caller()
&& !attr_span.allows_unstable(sym::closure_track_caller)

@JonathanBrouwer JonathanBrouwer Aug 31, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does technically make the feature gate slightly stricter, since we were previously checking for attr_span.allows_unstable(sym::closure_track_caller).
I think that's fine though, I'm not expecting anyone to be relying on this, and we can add it back if anyone complains :)

View changes since the review

{
feature_err(
&tcx.sess,
sym::closure_track_caller,
*attr_span,
"`#[track_caller]` on closures is currently unstable",
)
.emit();
}
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER
}
AttributeKind::Used { used_by } => match used_by {
Expand Down
Loading