Skip to content

Commit 6417f50

Browse files
authored
Unrolled build for #142825
Rollup merge of #142825 - jdonszelmann:track-caller, r=oli-obk Port `#[track_caller]` to the new attribute system r? ``@oli-obk`` depends on #142493 Closes #142783 (didn't add a test for this, this situation should simply never come up again, the code was simply wrong. lmk if I should add it, but it won't test something very useful)
2 parents 2c2bb99 + f9cdf3f commit 6417f50

File tree

14 files changed

+92
-60
lines changed

14 files changed

+92
-60
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
@@ -268,5 +268,8 @@ pub enum AttributeKind {
268268
/// Span of the attribute.
269269
span: Span,
270270
},
271+
272+
/// Represents `#[track_caller]`
273+
TrackCaller(Span),
271274
// tidy-alphabetical-end
272275
}

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_attr_data_structures::{AttributeKind, OptimizeAttr};
22
use rustc_feature::{AttributeTemplate, template};
33
use rustc_session::parse::feature_err;
4-
use rustc_span::{Span, sym};
4+
use rustc_span::{Span, Symbol, sym};
55

66
use super::{AcceptMapping, AttributeOrder, AttributeParser, OnDuplicate, SingleAttributeParser};
77
use crate::context::{AcceptContext, FinalizeContext, Stage};
@@ -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);
@@ -166,6 +166,24 @@ impl<S: Stage> AttributeParser<S> for NakedParser {
166166
}
167167
}
168168

169+
pub(crate) struct TrackCallerParser;
170+
171+
impl<S: Stage> SingleAttributeParser<S> for TrackCallerParser {
172+
const PATH: &[Symbol] = &[sym::track_caller];
173+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
174+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
175+
const TEMPLATE: AttributeTemplate = template!(Word);
176+
177+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
178+
if let Err(span) = args.no_args() {
179+
cx.expected_no_args(span);
180+
return None;
181+
}
182+
183+
Some(AttributeKind::TrackCaller(cx.attr_span))
184+
}
185+
}
186+
169187
pub(crate) struct NoMangleParser;
170188

171189
impl<S: Stage> SingleAttributeParser<S> for NoMangleParser {

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, NoMangleParser, OptimizeParser};
18+
use crate::attributes::codegen_attrs::{
19+
ColdParser, NakedParser, NoMangleParser, OptimizeParser, TrackCallerParser,
20+
};
1921
use crate::attributes::confusables::ConfusablesParser;
2022
use crate::attributes::deprecation::DeprecationParser;
2123
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
@@ -121,6 +123,7 @@ attribute_parsers!(
121123
Single<PubTransparentParser>,
122124
Single<RustcForceInlineParser>,
123125
Single<SkipDuringMethodDispatchParser>,
126+
Single<TrackCallerParser>,
124127
Single<TransparencyParser>,
125128
// tidy-alphabetical-end
126129
];

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
};
@@ -142,6 +140,29 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
142140
});
143141
}
144142
}
143+
AttributeKind::TrackCaller(attr_span) => {
144+
let is_closure = tcx.is_closure_like(did.to_def_id());
145+
146+
if !is_closure
147+
&& let Some(fn_sig) = fn_sig(*attr_span)
148+
&& fn_sig.skip_binder().abi() != ExternAbi::Rust
149+
{
150+
tcx.dcx().emit_err(errors::RequiresRustAbi { span: *attr_span });
151+
}
152+
if is_closure
153+
&& !tcx.features().closure_track_caller()
154+
&& !attr_span.allows_unstable(sym::closure_track_caller)
155+
{
156+
feature_err(
157+
&tcx.sess,
158+
sym::closure_track_caller,
159+
*attr_span,
160+
"`#[track_caller]` on closures is currently unstable",
161+
)
162+
.emit();
163+
}
164+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER
165+
}
145166
_ => {}
146167
}
147168
}
@@ -202,29 +223,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
202223
}
203224
}
204225
sym::thread_local => codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL,
205-
sym::track_caller => {
206-
let is_closure = tcx.is_closure_like(did.to_def_id());
207-
208-
if !is_closure
209-
&& let Some(fn_sig) = fn_sig()
210-
&& fn_sig.skip_binder().abi() != ExternAbi::Rust
211-
{
212-
tcx.dcx().emit_err(errors::RequiresRustAbi { span: attr.span() });
213-
}
214-
if is_closure
215-
&& !tcx.features().closure_track_caller()
216-
&& !attr.span().allows_unstable(sym::closure_track_caller)
217-
{
218-
feature_err(
219-
&tcx.sess,
220-
sym::closure_track_caller,
221-
attr.span(),
222-
"`#[track_caller]` on closures is currently unstable",
223-
)
224-
.emit();
225-
}
226-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER
227-
}
228226
sym::export_name => {
229227
if let Some(s) = attr.value_str() {
230228
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,11 +1184,11 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
11841184
if fn_kind.asyncness().is_async()
11851185
&& !cx.tcx.features().async_fn_track_caller()
11861186
// Now, check if the function has the `#[track_caller]` attribute
1187-
&& let Some(attr) = cx.tcx.get_attr(def_id, sym::track_caller)
1187+
&& let Some(attr_span) = find_attr!(cx.tcx.get_all_attrs(def_id), AttributeKind::TrackCaller(span) => *span)
11881188
{
11891189
cx.emit_span_lint(
11901190
UNGATED_ASYNC_FN_TRACK_CALLER,
1191-
attr.span(),
1191+
attr_span,
11921192
BuiltinUngatedAsyncFnTrackCaller { label: span, session: &cx.tcx.sess },
11931193
);
11941194
}

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ fn emit_malformed_attribute(
301301
| sym::naked
302302
| sym::no_mangle
303303
| sym::must_use
304+
| sym::track_caller
304305
) {
305306
return;
306307
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
169169
Attribute::Parsed(AttributeKind::Naked(attr_span)) => {
170170
self.check_naked(hir_id, *attr_span, span, target)
171171
}
172+
Attribute::Parsed(AttributeKind::TrackCaller(attr_span)) => {
173+
self.check_track_caller(hir_id, *attr_span, attrs, span, target)
174+
}
172175
Attribute::Parsed(
173176
AttributeKind::BodyStability { .. }
174177
| AttributeKind::ConstStabilityIndirect
@@ -205,9 +208,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
205208
self.check_target_feature(hir_id, attr, span, target, attrs)
206209
}
207210
[sym::thread_local, ..] => self.check_thread_local(attr, span, target),
208-
[sym::track_caller, ..] => {
209-
self.check_track_caller(hir_id, attr.span(), attrs, span, target)
210-
}
211211
[sym::doc, ..] => self.check_doc_attrs(
212212
attr,
213213
attr_item.style,
@@ -722,9 +722,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
722722
// erroneously allowed it and some crates used it accidentally, to be compatible
723723
// with crates depending on them, we can't throw an error here.
724724
Target::Field | Target::Arm | Target::MacroDef => {
725-
for attr in attrs {
726-
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "track_caller");
727-
}
725+
self.inline_attr_str_error_with_macro_def(hir_id, attr_span, "track_caller");
728726
}
729727
_ => {
730728
self.dcx().emit_err(errors::TrackedCallerWrongLocation {

src/tools/clippy/clippy_lints/src/eta_reduction.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use clippy_utils::{
77
get_path_from_caller_to_method_type, is_adjusted, is_no_std_crate, path_to_local, path_to_local_id,
88
};
99
use rustc_abi::ExternAbi;
10+
use rustc_attr_data_structures::{AttributeKind, find_attr};
1011
use rustc_errors::Applicability;
1112
use rustc_hir::{BindingMode, Expr, ExprKind, FnRetTy, GenericArgs, Param, PatKind, QPath, Safety, TyKind};
1213
use rustc_infer::infer::TyCtxtInferExt;
@@ -155,7 +156,7 @@ fn check_closure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tcx
155156
let sig = match callee_ty_adjusted.kind() {
156157
ty::FnDef(def, _) => {
157158
// Rewriting `x(|| f())` to `x(f)` where f is marked `#[track_caller]` moves the `Location`
158-
if cx.tcx.has_attr(*def, sym::track_caller) {
159+
if find_attr!(cx.tcx.get_all_attrs(*def), AttributeKind::TrackCaller(..)) {
159160
return;
160161
}
161162

@@ -236,7 +237,7 @@ fn check_closure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tcx
236237
},
237238
ExprKind::MethodCall(path, self_, args, _) if check_inputs(typeck, body.params, Some(self_), args) => {
238239
if let Some(method_def_id) = typeck.type_dependent_def_id(body.value.hir_id)
239-
&& !cx.tcx.has_attr(method_def_id, sym::track_caller)
240+
&& !find_attr!(cx.tcx.get_all_attrs(method_def_id), AttributeKind::TrackCaller(..))
240241
&& check_sig(closure_sig, cx.tcx.fn_sig(method_def_id).skip_binder().skip_binder())
241242
{
242243
let mut app = Applicability::MachineApplicable;

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,6 @@ note: attribute also specified here
8989
LL | #[automatically_derived]
9090
| ^^^^^^^^^^^^^^^^^^^^^^^^
9191

92-
error: unused attribute
93-
--> $DIR/unused-attr-duplicate.rs:79:1
94-
|
95-
LL | #[track_caller]
96-
| ^^^^^^^^^^^^^^^ help: remove this attribute
97-
|
98-
note: attribute also specified here
99-
--> $DIR/unused-attr-duplicate.rs:78:1
100-
|
101-
LL | #[track_caller]
102-
| ^^^^^^^^^^^^^^^
103-
10492
error: unused attribute
10593
--> $DIR/unused-attr-duplicate.rs:92:1
10694
|
@@ -277,6 +265,18 @@ note: attribute also specified here
277265
LL | #[cold]
278266
| ^^^^^^^
279267

268+
error: unused attribute
269+
--> $DIR/unused-attr-duplicate.rs:79:1
270+
|
271+
LL | #[track_caller]
272+
| ^^^^^^^^^^^^^^^ help: remove this attribute
273+
|
274+
note: attribute also specified here
275+
--> $DIR/unused-attr-duplicate.rs:78:1
276+
|
277+
LL | #[track_caller]
278+
| ^^^^^^^^^^^^^^^
279+
280280
error: unused attribute
281281
--> $DIR/unused-attr-duplicate.rs:98:1
282282
|
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
error: malformed `track_caller` attribute input
1+
error[E0565]: malformed `track_caller` attribute input
22
--> $DIR/error-odd-syntax.rs:1:1
33
|
44
LL | #[track_caller(1)]
5-
| ^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[track_caller]`
5+
| ^^^^^^^^^^^^^^---^
6+
| | |
7+
| | didn't expect any arguments here
8+
| help: must be of the form: `#[track_caller]`
69

710
error: aborting due to 1 previous error
811

12+
For more information about this error, try `rustc --explain E0565`.

0 commit comments

Comments
 (0)