Skip to content

Commit 7462bc4

Browse files
committed
Rewrite #[track_caller]
1 parent 9cd2d02 commit 7462bc4

File tree

12 files changed

+82
-52
lines changed

12 files changed

+82
-52
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3282,6 +3282,7 @@ dependencies = [
32823282
"rustc_abi",
32833283
"rustc_ast",
32843284
"rustc_ast_pretty",
3285+
"rustc_attr_data_structures",
32853286
"rustc_attr_parsing",
32863287
"rustc_data_structures",
32873288
"rustc_errors",

compiler/rustc_ast_lowering/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ doctest = false
1111
rustc_abi = { path = "../rustc_abi" }
1212
rustc_ast = { path = "../rustc_ast" }
1313
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
14+
rustc_attr_data_structures = { path = "../rustc_attr_data_structures" }
1415
rustc_attr_parsing = { path = "../rustc_attr_parsing" }
1516
rustc_data_structures = { path = "../rustc_data_structures" }
1617
rustc_errors = { path = "../rustc_errors" }

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::sync::Arc;
44
use rustc_ast::ptr::P as AstP;
55
use rustc_ast::*;
66
use rustc_ast_pretty::pprust::expr_to_string;
7+
use rustc_attr_data_structures::{AttributeKind, find_attr};
78
use rustc_data_structures::stack::ensure_sufficient_stack;
89
use rustc_hir as hir;
910
use rustc_hir::HirId;
@@ -831,7 +832,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
831832
) {
832833
if self.tcx.features().async_fn_track_caller()
833834
&& let Some(attrs) = self.attrs.get(&outer_hir_id.local_id)
834-
&& attrs.into_iter().any(|attr| attr.has_name(sym::track_caller))
835+
&& find_attr!(*attrs, AttributeKind::TrackCaller(_))
835836
{
836837
let unstable_span = self.mark_span_with_reason(
837838
DesugaringKind::Async,

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,5 +248,8 @@ pub enum AttributeKind {
248248
/// Span of the attribute.
249249
span: Span,
250250
},
251+
252+
/// Represents `#[track_caller]`
253+
TrackCaller(Span),
251254
// tidy-alphabetical-end
252255
}

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::session_diagnostics::NakedFunctionIncompatibleAttribute;
1111
pub(crate) struct OptimizeParser;
1212

1313
impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
14-
const PATH: &[rustc_span::Symbol] = &[sym::optimize];
14+
const PATH: &[Symbol] = &[sym::optimize];
1515
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
1616
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
1717
const TEMPLATE: AttributeTemplate = template!(List: "size|speed|none");
@@ -44,7 +44,7 @@ impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
4444
pub(crate) struct ColdParser;
4545

4646
impl<S: Stage> SingleAttributeParser<S> for ColdParser {
47-
const PATH: &[rustc_span::Symbol] = &[sym::cold];
47+
const PATH: &[Symbol] = &[sym::cold];
4848
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
4949
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
5050
const TEMPLATE: AttributeTemplate = template!(Word);
@@ -161,3 +161,21 @@ impl<S: Stage> AttributeParser<S> for NakedParser {
161161
Some(AttributeKind::Naked(span))
162162
}
163163
}
164+
165+
pub(crate) struct TrackCallerParser;
166+
167+
impl<S: Stage> SingleAttributeParser<S> for TrackCallerParser {
168+
const PATH: &[Symbol] = &[sym::track_caller];
169+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
170+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
171+
const TEMPLATE: AttributeTemplate = template!(Word);
172+
173+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
174+
if !args.no_args() {
175+
cx.expected_no_args(args.span().unwrap_or(cx.attr_span));
176+
return None;
177+
}
178+
179+
Some(AttributeKind::TrackCaller(cx.attr_span))
180+
}
181+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use rustc_session::Session;
1515
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1616

1717
use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
18-
use crate::attributes::codegen_attrs::{ColdParser, NakedParser, OptimizeParser};
18+
use crate::attributes::codegen_attrs::{
19+
ColdParser, NakedParser, OptimizeParser, TrackCallerParser,
20+
};
1921
use crate::attributes::confusables::ConfusablesParser;
2022
use crate::attributes::deprecation::DeprecationParser;
2123
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
@@ -113,6 +115,7 @@ attribute_parsers!(
113115
Single<InlineParser>,
114116
Single<OptimizeParser>,
115117
Single<RustcForceInlineParser>,
118+
Single<TrackCallerParser>,
116119
Single<TransparencyParser>,
117120
// tidy-alphabetical-end
118121
];

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,15 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
9595
// In these cases, we bail from performing further checks that are only meaningful for
9696
// functions (such as calling `fn_sig`, which ICEs if given a non-function). We also
9797
// report a delayed bug, just in case `check_attr` isn't doing its job.
98-
let fn_sig = || {
98+
let fn_sig = |attr_span| {
9999
use DefKind::*;
100100

101101
let def_kind = tcx.def_kind(did);
102102
if let Fn | AssocFn | Variant | Ctor(..) = def_kind {
103103
Some(tcx.fn_sig(did))
104104
} else {
105-
tcx.dcx().span_delayed_bug(
106-
attr.span(),
107-
"this attribute can only be applied to functions",
108-
);
105+
tcx.dcx()
106+
.span_delayed_bug(attr_span, "this attribute can only be applied to functions");
109107
None
110108
}
111109
};
@@ -123,6 +121,29 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
123121
AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
124122
AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
125123
AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
124+
AttributeKind::TrackCaller(attr_span) => {
125+
let is_closure = tcx.is_closure_like(did.to_def_id());
126+
127+
if !is_closure
128+
&& let Some(fn_sig) = fn_sig(*attr_span)
129+
&& fn_sig.skip_binder().abi() != ExternAbi::Rust
130+
{
131+
tcx.dcx().emit_err(errors::RequiresRustAbi { span: *attr_span });
132+
}
133+
if is_closure
134+
&& !tcx.features().closure_track_caller()
135+
&& !attr_span.allows_unstable(sym::closure_track_caller)
136+
{
137+
feature_err(
138+
&tcx.sess,
139+
sym::closure_track_caller,
140+
*attr_span,
141+
"`#[track_caller]` on closures is currently unstable",
142+
)
143+
.emit();
144+
}
145+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER
146+
}
126147
_ => {}
127148
}
128149
}
@@ -205,29 +226,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
205226
}
206227
}
207228
sym::thread_local => codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL,
208-
sym::track_caller => {
209-
let is_closure = tcx.is_closure_like(did.to_def_id());
210-
211-
if !is_closure
212-
&& let Some(fn_sig) = fn_sig()
213-
&& fn_sig.skip_binder().abi() != ExternAbi::Rust
214-
{
215-
tcx.dcx().emit_err(errors::RequiresRustAbi { span: attr.span() });
216-
}
217-
if is_closure
218-
&& !tcx.features().closure_track_caller()
219-
&& !attr.span().allows_unstable(sym::closure_track_caller)
220-
{
221-
feature_err(
222-
&tcx.sess,
223-
sym::closure_track_caller,
224-
attr.span(),
225-
"`#[track_caller]` on closures is currently unstable",
226-
)
227-
.emit();
228-
}
229-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER
230-
}
231229
sym::export_name => {
232230
if let Some(s) = attr.value_str() {
233231
if s.as_str().contains('\0') {

compiler/rustc_hir_analysis/src/check/entry.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use std::ops::Not;
22

33
use rustc_abi::ExternAbi;
4+
use rustc_attr_data_structures::{AttributeKind, find_attr};
45
use rustc_hir as hir;
56
use rustc_hir::Node;
67
use rustc_infer::infer::TyCtxtInferExt;
78
use rustc_middle::span_bug;
89
use rustc_middle::ty::{self, TyCtxt, TypingMode};
910
use rustc_session::config::EntryFnType;
11+
use rustc_span::Span;
1012
use rustc_span::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
11-
use rustc_span::{Span, sym};
1213
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
1314
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode};
1415

@@ -98,8 +99,10 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
9899
error = true;
99100
}
100101

101-
for attr in tcx.get_attrs(main_def_id, sym::track_caller) {
102-
tcx.dcx().emit_err(errors::TrackCallerOnMain { span: attr.span(), annotated: main_span });
102+
if let Some(attr_span) =
103+
find_attr!(tcx.get_all_attrs(main_def_id), AttributeKind::TrackCaller(span) => *span)
104+
{
105+
tcx.dcx().emit_err(errors::TrackCallerOnMain { span: attr_span, annotated: main_span });
103106
error = true;
104107
}
105108

compiler/rustc_lint/src/builtin.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree};
2121
use rustc_ast::visit::{FnCtxt, FnKind};
2222
use rustc_ast::{self as ast, *};
2323
use rustc_ast_pretty::pprust::expr_to_string;
24+
use rustc_attr_data_structures::{AttributeKind, find_attr};
2425
use rustc_errors::{Applicability, LintDiagnostic};
2526
use rustc_feature::GateIssue;
2627
use rustc_hir as hir;
@@ -1181,11 +1182,11 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
11811182
if fn_kind.asyncness().is_async()
11821183
&& !cx.tcx.features().async_fn_track_caller()
11831184
// Now, check if the function has the `#[track_caller]` attribute
1184-
&& let Some(attr) = cx.tcx.get_attr(def_id, sym::track_caller)
1185+
&& let Some(attr_span) = find_attr!(cx.tcx.get_all_attrs(def_id), AttributeKind::TrackCaller(span) => *span)
11851186
{
11861187
cx.emit_span_lint(
11871188
UNGATED_ASYNC_FN_TRACK_CALLER,
1188-
attr.span(),
1189+
attr_span,
11891190
BuiltinUngatedAsyncFnTrackCaller { label: span, session: &cx.tcx.sess },
11901191
);
11911192
}

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ fn emit_malformed_attribute(
293293
| sym::deprecated
294294
| sym::optimize
295295
| sym::cold
296+
| sym::track_caller
296297
) {
297298
return;
298299
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
158158
Attribute::Parsed(AttributeKind::Naked(attr_span)) => {
159159
self.check_naked(hir_id, *attr_span, span, target)
160160
}
161+
Attribute::Parsed(AttributeKind::TrackCaller(attr_span)) => {
162+
self.check_track_caller(hir_id, *attr_span, attrs, span, target)
163+
}
161164
Attribute::Parsed(
162165
AttributeKind::BodyStability { .. }
163166
| AttributeKind::ConstStabilityIndirect
@@ -184,9 +187,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
184187
self.check_target_feature(hir_id, attr, span, target, attrs)
185188
}
186189
[sym::thread_local, ..] => self.check_thread_local(attr, span, target),
187-
[sym::track_caller, ..] => {
188-
self.check_track_caller(hir_id, attr.span(), attrs, span, target)
189-
}
190190
[sym::doc, ..] => self.check_doc_attrs(
191191
attr,
192192
hir_id,

tests/ui/lint/unused/unused-attr-duplicate.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,6 @@ note: attribute also specified here
102102
LL | #[automatically_derived]
103103
| ^^^^^^^^^^^^^^^^^^^^^^^^
104104

105-
error: unused attribute
106-
--> $DIR/unused-attr-duplicate.rs:79:1
107-
|
108-
LL | #[track_caller]
109-
| ^^^^^^^^^^^^^^^ help: remove this attribute
110-
|
111-
note: attribute also specified here
112-
--> $DIR/unused-attr-duplicate.rs:78:1
113-
|
114-
LL | #[track_caller]
115-
| ^^^^^^^^^^^^^^^
116-
117105
error: unused attribute
118106
--> $DIR/unused-attr-duplicate.rs:92:1
119107
|
@@ -289,5 +277,17 @@ note: attribute also specified here
289277
LL | #[cold]
290278
| ^^^^^^^
291279

280+
error: unused attribute
281+
--> $DIR/unused-attr-duplicate.rs:79:1
282+
|
283+
LL | #[track_caller]
284+
| ^^^^^^^^^^^^^^^ help: remove this attribute
285+
|
286+
note: attribute also specified here
287+
--> $DIR/unused-attr-duplicate.rs:78:1
288+
|
289+
LL | #[track_caller]
290+
| ^^^^^^^^^^^^^^^
291+
292292
error: aborting due to 23 previous errors
293293

0 commit comments

Comments
 (0)