Skip to content

Commit e173a8e

Browse files
committed
Auto merge of rust-lang#112117 - bryangarza:track-caller-feature-gate, r=compiler-errors
Add separate feature gate for async fn track caller This patch adds a feature gate `async_fn_track_caller` that is separate from `closure_track_caller`. This is to allow enabling `async_fn_track_caller` separately. Fixes rust-lang#110009
2 parents e4c1446 + 673ab17 commit e173a8e

20 files changed

+326
-63
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -657,14 +657,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
657657
}
658658

659659
/// Forwards a possible `#[track_caller]` annotation from `outer_hir_id` to
660-
/// `inner_hir_id` in case the `closure_track_caller` feature is enabled.
660+
/// `inner_hir_id` in case the `async_fn_track_caller` feature is enabled.
661661
pub(super) fn maybe_forward_track_caller(
662662
&mut self,
663663
span: Span,
664664
outer_hir_id: hir::HirId,
665665
inner_hir_id: hir::HirId,
666666
) {
667-
if self.tcx.features().closure_track_caller
667+
if self.tcx.features().async_fn_track_caller
668668
&& let Some(attrs) = self.attrs.get(&outer_hir_id.local_id)
669669
&& attrs.into_iter().any(|attr| attr.has_name(sym::track_caller))
670670
{

compiler/rustc_ast_lowering/src/item.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
5656
owner: NodeId,
5757
f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
5858
) {
59+
let allow_gen_future = Some(if self.tcx.features().async_fn_track_caller {
60+
[sym::gen_future, sym::closure_track_caller][..].into()
61+
} else {
62+
[sym::gen_future][..].into()
63+
});
5964
let mut lctx = LoweringContext {
6065
// Pseudo-globals.
6166
tcx: self.tcx,
@@ -83,7 +88,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
8388
impl_trait_defs: Vec::new(),
8489
impl_trait_bounds: Vec::new(),
8590
allow_try_trait: Some([sym::try_trait_v2, sym::yeet_desugar_details][..].into()),
86-
allow_gen_future: Some([sym::gen_future, sym::closure_track_caller][..].into()),
91+
allow_gen_future,
8792
generics_def_id_map: Default::default(),
8893
};
8994
lctx.with_hir_id_owner(owner, |lctx| f(lctx));

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,19 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
215215
}
216216
sym::thread_local => codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL,
217217
sym::track_caller => {
218-
if !tcx.is_closure(did.to_def_id())
218+
let is_closure = tcx.is_closure(did.to_def_id());
219+
220+
if !is_closure
219221
&& let Some(fn_sig) = fn_sig()
220222
&& fn_sig.skip_binder().abi() != abi::Abi::Rust
221223
{
222224
struct_span_err!(tcx.sess, attr.span, E0737, "`#[track_caller]` requires Rust ABI")
223225
.emit();
224226
}
225-
if tcx.is_closure(did.to_def_id()) && !tcx.features().closure_track_caller {
227+
if is_closure
228+
&& !tcx.features().closure_track_caller
229+
&& !attr.span.allows_unstable(sym::closure_track_caller)
230+
{
226231
feature_err(
227232
&tcx.sess.parse_sess,
228233
sym::closure_track_caller,

compiler/rustc_feature/src/active.rs

+2
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ declare_features! (
339339
(active, async_closure, "1.37.0", Some(62290), None),
340340
/// Allows async functions to be declared, implemented, and used in traits.
341341
(active, async_fn_in_trait, "1.66.0", Some(91611), None),
342+
/// Allows `#[track_caller]` on async functions.
343+
(active, async_fn_track_caller, "CURRENT_RUSTC_VERSION", Some(110011), None),
342344
/// Allows builtin # foo() syntax
343345
(active, builtin_syntax, "1.71.0", Some(110680), None),
344346
/// Allows `c"foo"` literals.

compiler/rustc_lint/src/builtin.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1259,8 +1259,8 @@ impl<'tcx> LateLintPass<'tcx> for UnstableFeatures {
12591259

12601260
declare_lint! {
12611261
/// The `ungated_async_fn_track_caller` lint warns when the
1262-
/// `#[track_caller]` attribute is used on an async function, method, or
1263-
/// closure, without enabling the corresponding unstable feature flag.
1262+
/// `#[track_caller]` attribute is used on an async function
1263+
/// without enabling the corresponding unstable feature flag.
12641264
///
12651265
/// ### Example
12661266
///
@@ -1274,13 +1274,13 @@ declare_lint! {
12741274
/// ### Explanation
12751275
///
12761276
/// The attribute must be used in conjunction with the
1277-
/// [`closure_track_caller` feature flag]. Otherwise, the `#[track_caller]`
1277+
/// [`async_fn_track_caller` feature flag]. Otherwise, the `#[track_caller]`
12781278
/// annotation will function as a no-op.
12791279
///
1280-
/// [`closure_track_caller` feature flag]: https://doc.rust-lang.org/beta/unstable-book/language-features/closure-track-caller.html
1280+
/// [`async_fn_track_caller` feature flag]: https://doc.rust-lang.org/beta/unstable-book/language-features/async-fn-track-caller.html
12811281
UNGATED_ASYNC_FN_TRACK_CALLER,
12821282
Warn,
1283-
"enabling track_caller on an async fn is a no-op unless the closure_track_caller feature is enabled"
1283+
"enabling track_caller on an async fn is a no-op unless the async_fn_track_caller feature is enabled"
12841284
}
12851285

12861286
declare_lint_pass!(
@@ -1300,7 +1300,7 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
13001300
def_id: LocalDefId,
13011301
) {
13021302
if fn_kind.asyncness() == IsAsync::Async
1303-
&& !cx.tcx.features().closure_track_caller
1303+
&& !cx.tcx.features().async_fn_track_caller
13041304
// Now, check if the function has the `#[track_caller]` attribute
13051305
&& let Some(attr) = cx.tcx.get_attr(def_id, sym::track_caller)
13061306
{

compiler/rustc_lint/src/lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl<'a> DecorateLint<'a, ()> for BuiltinUngatedAsyncFnTrackCaller<'_> {
250250
rustc_session::parse::add_feature_diagnostics(
251251
diag,
252252
&self.parse_sess,
253-
sym::closure_track_caller,
253+
sym::async_fn_track_caller,
254254
);
255255
diag
256256
}

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ symbols! {
400400
async_await,
401401
async_closure,
402402
async_fn_in_trait,
403+
async_fn_track_caller,
403404
atomic,
404405
atomic_mod,
405406
atomics,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0658]: `#[track_caller]` on closures is currently unstable
2+
--> $DIR/async-block.rs:8:13
3+
|
4+
LL | let _ = #[track_caller] async {
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
8+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
9+
10+
error[E0658]: `#[track_caller]` on closures is currently unstable
11+
--> $DIR/async-block.rs:15:13
12+
|
13+
LL | let _ = #[track_caller] async {
14+
| ^^^^^^^^^^^^^^^
15+
|
16+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
17+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
18+
19+
error[E0658]: `#[track_caller]` on closures is currently unstable
20+
--> $DIR/async-block.rs:23:17
21+
|
22+
LL | let _ = #[track_caller] async {
23+
| ^^^^^^^^^^^^^^^
24+
|
25+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
26+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
27+
28+
error: aborting due to 3 previous errors
29+
30+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0658]: `#[track_caller]` on closures is currently unstable
2+
--> $DIR/async-block.rs:8:13
3+
|
4+
LL | let _ = #[track_caller] async {
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
8+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
9+
10+
error[E0658]: `#[track_caller]` on closures is currently unstable
11+
--> $DIR/async-block.rs:15:13
12+
|
13+
LL | let _ = #[track_caller] async {
14+
| ^^^^^^^^^^^^^^^
15+
|
16+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
17+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
18+
19+
error[E0658]: `#[track_caller]` on closures is currently unstable
20+
--> $DIR/async-block.rs:23:17
21+
|
22+
LL | let _ = #[track_caller] async {
23+
| ^^^^^^^^^^^^^^^
24+
|
25+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
26+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
27+
28+
error: aborting due to 3 previous errors
29+
30+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
// edition:2021
2+
// revisions: afn nofeat
23

34
#![feature(stmt_expr_attributes)]
5+
#![cfg_attr(afn, feature(async_fn_track_caller))]
46

57
fn main() {
68
let _ = #[track_caller] async {
79
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
810
};
911
}
12+
13+
#[track_caller]
14+
async fn foo() {
15+
let _ = #[track_caller] async {
16+
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
17+
};
18+
}
19+
20+
#[track_caller]
21+
async fn foo2() {
22+
let _ = async {
23+
let _ = #[track_caller] async {
24+
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
25+
};
26+
};
27+
}

tests/ui/async-await/track-caller/async-block.stderr

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
error[E0658]: `#[track_caller]` on closures is currently unstable
2+
--> $DIR/async-closure-gate.rs:8:13
3+
|
4+
LL | let _ = #[track_caller] async || {
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
8+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
9+
10+
error[E0658]: `#[track_caller]` on closures is currently unstable
11+
--> $DIR/async-closure-gate.rs:15:13
12+
|
13+
LL | let _ = #[track_caller] async || {
14+
| ^^^^^^^^^^^^^^^
15+
|
16+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
17+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
18+
19+
error[E0658]: `#[track_caller]` on closures is currently unstable
20+
--> $DIR/async-closure-gate.rs:21:13
21+
|
22+
LL | let _ = #[track_caller] || {
23+
| ^^^^^^^^^^^^^^^
24+
|
25+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
26+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
27+
28+
error[E0658]: `#[track_caller]` on closures is currently unstable
29+
--> $DIR/async-closure-gate.rs:28:17
30+
|
31+
LL | let _ = #[track_caller] || {
32+
| ^^^^^^^^^^^^^^^
33+
|
34+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
35+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
36+
37+
error[E0658]: `#[track_caller]` on closures is currently unstable
38+
--> $DIR/async-closure-gate.rs:36:9
39+
|
40+
LL | #[track_caller] || {
41+
| ^^^^^^^^^^^^^^^
42+
|
43+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
44+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
45+
46+
error[E0658]: `#[track_caller]` on closures is currently unstable
47+
--> $DIR/async-closure-gate.rs:45:13
48+
|
49+
LL | #[track_caller] || {
50+
| ^^^^^^^^^^^^^^^
51+
|
52+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
53+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
54+
55+
error: aborting due to 6 previous errors
56+
57+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
error[E0658]: `#[track_caller]` on closures is currently unstable
2+
--> $DIR/async-closure-gate.rs:8:13
3+
|
4+
LL | let _ = #[track_caller] async || {
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
8+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
9+
10+
error[E0658]: `#[track_caller]` on closures is currently unstable
11+
--> $DIR/async-closure-gate.rs:15:13
12+
|
13+
LL | let _ = #[track_caller] async || {
14+
| ^^^^^^^^^^^^^^^
15+
|
16+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
17+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
18+
19+
error[E0658]: `#[track_caller]` on closures is currently unstable
20+
--> $DIR/async-closure-gate.rs:21:13
21+
|
22+
LL | let _ = #[track_caller] || {
23+
| ^^^^^^^^^^^^^^^
24+
|
25+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
26+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
27+
28+
error[E0658]: `#[track_caller]` on closures is currently unstable
29+
--> $DIR/async-closure-gate.rs:28:17
30+
|
31+
LL | let _ = #[track_caller] || {
32+
| ^^^^^^^^^^^^^^^
33+
|
34+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
35+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
36+
37+
error[E0658]: `#[track_caller]` on closures is currently unstable
38+
--> $DIR/async-closure-gate.rs:36:9
39+
|
40+
LL | #[track_caller] || {
41+
| ^^^^^^^^^^^^^^^
42+
|
43+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
44+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
45+
46+
error[E0658]: `#[track_caller]` on closures is currently unstable
47+
--> $DIR/async-closure-gate.rs:45:13
48+
|
49+
LL | #[track_caller] || {
50+
| ^^^^^^^^^^^^^^^
51+
|
52+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
53+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
54+
55+
error: aborting due to 6 previous errors
56+
57+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,50 @@
11
// edition:2021
2+
// revisions: afn nofeat
23

34
#![feature(async_closure, stmt_expr_attributes)]
5+
#![cfg_attr(afn, feature(async_fn_track_caller))]
46

57
fn main() {
68
let _ = #[track_caller] async || {
79
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
810
};
911
}
12+
13+
#[track_caller]
14+
async fn foo() {
15+
let _ = #[track_caller] async || {
16+
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
17+
};
18+
}
19+
20+
async fn foo2() {
21+
let _ = #[track_caller] || {
22+
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
23+
};
24+
}
25+
26+
fn foo3() {
27+
async {
28+
let _ = #[track_caller] || {
29+
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
30+
};
31+
}
32+
}
33+
34+
async fn foo4() {
35+
let _ = || {
36+
#[track_caller] || {
37+
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
38+
};
39+
};
40+
}
41+
42+
fn foo5() {
43+
async {
44+
let _ = || {
45+
#[track_caller] || {
46+
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
47+
};
48+
};
49+
}
50+
}

0 commit comments

Comments
 (0)