Skip to content

Commit

Permalink
Auto merge of rust-lang#112117 - bryangarza:track-caller-feature-gate…
Browse files Browse the repository at this point in the history
…, 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
  • Loading branch information
bors committed Aug 4, 2023
2 parents e4c1446 + 673ab17 commit e173a8e
Show file tree
Hide file tree
Showing 20 changed files with 326 additions and 63 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,14 +657,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

/// Forwards a possible `#[track_caller]` annotation from `outer_hir_id` to
/// `inner_hir_id` in case the `closure_track_caller` feature is enabled.
/// `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: hir::HirId,
inner_hir_id: hir::HirId,
) {
if self.tcx.features().closure_track_caller
if self.tcx.features().async_fn_track_caller
&& let Some(attrs) = self.attrs.get(&outer_hir_id.local_id)
&& attrs.into_iter().any(|attr| attr.has_name(sym::track_caller))
{
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
owner: NodeId,
f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
) {
let allow_gen_future = Some(if self.tcx.features().async_fn_track_caller {
[sym::gen_future, sym::closure_track_caller][..].into()
} else {
[sym::gen_future][..].into()
});
let mut lctx = LoweringContext {
// Pseudo-globals.
tcx: self.tcx,
Expand Down Expand Up @@ -83,7 +88,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
impl_trait_defs: Vec::new(),
impl_trait_bounds: Vec::new(),
allow_try_trait: Some([sym::try_trait_v2, sym::yeet_desugar_details][..].into()),
allow_gen_future: Some([sym::gen_future, sym::closure_track_caller][..].into()),
allow_gen_future,
generics_def_id_map: Default::default(),
};
lctx.with_hir_id_owner(owner, |lctx| f(lctx));
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_codegen_ssa/src/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,19 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
}
sym::thread_local => codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL,
sym::track_caller => {
if !tcx.is_closure(did.to_def_id())
let is_closure = tcx.is_closure(did.to_def_id());

if !is_closure
&& let Some(fn_sig) = fn_sig()
&& fn_sig.skip_binder().abi() != abi::Abi::Rust
{
struct_span_err!(tcx.sess, attr.span, E0737, "`#[track_caller]` requires Rust ABI")
.emit();
}
if tcx.is_closure(did.to_def_id()) && !tcx.features().closure_track_caller {
if is_closure
&& !tcx.features().closure_track_caller
&& !attr.span.allows_unstable(sym::closure_track_caller)
{
feature_err(
&tcx.sess.parse_sess,
sym::closure_track_caller,
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ declare_features! (
(active, async_closure, "1.37.0", Some(62290), None),
/// Allows async functions to be declared, implemented, and used in traits.
(active, async_fn_in_trait, "1.66.0", Some(91611), None),
/// Allows `#[track_caller]` on async functions.
(active, async_fn_track_caller, "CURRENT_RUSTC_VERSION", Some(110011), None),
/// Allows builtin # foo() syntax
(active, builtin_syntax, "1.71.0", Some(110680), None),
/// Allows `c"foo"` literals.
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1259,8 +1259,8 @@ impl<'tcx> LateLintPass<'tcx> for UnstableFeatures {

declare_lint! {
/// The `ungated_async_fn_track_caller` lint warns when the
/// `#[track_caller]` attribute is used on an async function, method, or
/// closure, without enabling the corresponding unstable feature flag.
/// `#[track_caller]` attribute is used on an async function
/// without enabling the corresponding unstable feature flag.
///
/// ### Example
///
Expand All @@ -1274,13 +1274,13 @@ declare_lint! {
/// ### Explanation
///
/// The attribute must be used in conjunction with the
/// [`closure_track_caller` feature flag]. Otherwise, the `#[track_caller]`
/// [`async_fn_track_caller` feature flag]. Otherwise, the `#[track_caller]`
/// annotation will function as a no-op.
///
/// [`closure_track_caller` feature flag]: https://doc.rust-lang.org/beta/unstable-book/language-features/closure-track-caller.html
/// [`async_fn_track_caller` feature flag]: https://doc.rust-lang.org/beta/unstable-book/language-features/async-fn-track-caller.html
UNGATED_ASYNC_FN_TRACK_CALLER,
Warn,
"enabling track_caller on an async fn is a no-op unless the closure_track_caller feature is enabled"
"enabling track_caller on an async fn is a no-op unless the async_fn_track_caller feature is enabled"
}

declare_lint_pass!(
Expand All @@ -1300,7 +1300,7 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
def_id: LocalDefId,
) {
if fn_kind.asyncness() == IsAsync::Async
&& !cx.tcx.features().closure_track_caller
&& !cx.tcx.features().async_fn_track_caller
// Now, check if the function has the `#[track_caller]` attribute
&& let Some(attr) = cx.tcx.get_attr(def_id, sym::track_caller)
{
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl<'a> DecorateLint<'a, ()> for BuiltinUngatedAsyncFnTrackCaller<'_> {
rustc_session::parse::add_feature_diagnostics(
diag,
&self.parse_sess,
sym::closure_track_caller,
sym::async_fn_track_caller,
);
diag
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ symbols! {
async_await,
async_closure,
async_fn_in_trait,
async_fn_track_caller,
atomic,
atomic_mod,
atomics,
Expand Down
30 changes: 30 additions & 0 deletions tests/ui/async-await/track-caller/async-block.afn.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-block.rs:8:13
|
LL | let _ = #[track_caller] async {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-block.rs:15:13
|
LL | let _ = #[track_caller] async {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-block.rs:23:17
|
LL | let _ = #[track_caller] async {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
30 changes: 30 additions & 0 deletions tests/ui/async-await/track-caller/async-block.nofeat.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-block.rs:8:13
|
LL | let _ = #[track_caller] async {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-block.rs:15:13
|
LL | let _ = #[track_caller] async {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-block.rs:23:17
|
LL | let _ = #[track_caller] async {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
18 changes: 18 additions & 0 deletions tests/ui/async-await/track-caller/async-block.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
// edition:2021
// revisions: afn nofeat

#![feature(stmt_expr_attributes)]
#![cfg_attr(afn, feature(async_fn_track_caller))]

fn main() {
let _ = #[track_caller] async {
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
};
}

#[track_caller]
async fn foo() {
let _ = #[track_caller] async {
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
};
}

#[track_caller]
async fn foo2() {
let _ = async {
let _ = #[track_caller] async {
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
};
};
}
12 changes: 0 additions & 12 deletions tests/ui/async-await/track-caller/async-block.stderr

This file was deleted.

57 changes: 57 additions & 0 deletions tests/ui/async-await/track-caller/async-closure-gate.afn.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-closure-gate.rs:8:13
|
LL | let _ = #[track_caller] async || {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-closure-gate.rs:15:13
|
LL | let _ = #[track_caller] async || {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-closure-gate.rs:21:13
|
LL | let _ = #[track_caller] || {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-closure-gate.rs:28:17
|
LL | let _ = #[track_caller] || {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-closure-gate.rs:36:9
|
LL | #[track_caller] || {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-closure-gate.rs:45:13
|
LL | #[track_caller] || {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0658`.
57 changes: 57 additions & 0 deletions tests/ui/async-await/track-caller/async-closure-gate.nofeat.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-closure-gate.rs:8:13
|
LL | let _ = #[track_caller] async || {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-closure-gate.rs:15:13
|
LL | let _ = #[track_caller] async || {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-closure-gate.rs:21:13
|
LL | let _ = #[track_caller] || {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-closure-gate.rs:28:17
|
LL | let _ = #[track_caller] || {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-closure-gate.rs:36:9
|
LL | #[track_caller] || {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error[E0658]: `#[track_caller]` on closures is currently unstable
--> $DIR/async-closure-gate.rs:45:13
|
LL | #[track_caller] || {
| ^^^^^^^^^^^^^^^
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0658`.
41 changes: 41 additions & 0 deletions tests/ui/async-await/track-caller/async-closure-gate.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
// edition:2021
// revisions: afn nofeat

#![feature(async_closure, stmt_expr_attributes)]
#![cfg_attr(afn, feature(async_fn_track_caller))]

fn main() {
let _ = #[track_caller] async || {
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
};
}

#[track_caller]
async fn foo() {
let _ = #[track_caller] async || {
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
};
}

async fn foo2() {
let _ = #[track_caller] || {
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
};
}

fn foo3() {
async {
let _ = #[track_caller] || {
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
};
}
}

async fn foo4() {
let _ = || {
#[track_caller] || {
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
};
};
}

fn foo5() {
async {
let _ = || {
#[track_caller] || {
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
};
};
}
}
Loading

0 comments on commit e173a8e

Please sign in to comment.