Skip to content

Commit 6711f7a

Browse files
committed
Auto merge of rust-lang#106188 - bryangarza:bug-104588-async-track-caller_beta-backport, r=compiler-errors
Backport: Switch `#[track_caller]` back to a no-op unless feature gate is enabled Backport of rust-lang#104741 r? `@compiler-errors` since you reviewed the original PR requested by `@Mark-Simulacrum` rust-lang#104741 (comment)
2 parents b364405 + 8b76871 commit 6711f7a

File tree

8 files changed

+149
-17
lines changed

8 files changed

+149
-17
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -645,17 +645,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
645645
hir::ExprKind::Closure(c)
646646
};
647647

648-
let track_caller = outer_hir_id
649-
.and_then(|id| self.attrs.get(&id.local_id))
650-
.map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)));
651-
652648
let hir_id = self.lower_node_id(closure_node_id);
653-
if track_caller {
654-
let unstable_span = self.mark_span_with_reason(
655-
DesugaringKind::Async,
656-
span,
657-
self.allow_gen_future.clone(),
658-
);
649+
let unstable_span =
650+
self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone());
651+
652+
if self.tcx.features().closure_track_caller
653+
&& let Some(outer_hir_id) = outer_hir_id
654+
&& let Some(attrs) = self.attrs.get(&outer_hir_id.local_id)
655+
&& attrs.into_iter().any(|attr| attr.has_name(sym::track_caller))
656+
{
659657
self.lower_attrs(
660658
hir_id,
661659
&[Attribute {

compiler/rustc_error_messages/locales/en-US/lint.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ lint_builtin_mutable_transmutes =
350350
351351
lint_builtin_unstable_features = unstable feature
352352
353+
lint_ungated_async_fn_track_caller = `#[track_caller]` on async functions is a no-op
354+
.label = this function will not propagate the caller location
355+
353356
lint_builtin_unreachable_pub = unreachable `pub` {$what}
354357
.suggestion = consider restricting its visibility
355358
.help = or consider exporting it for use by other crates

compiler/rustc_lint/src/builtin.rs

+71-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::{
2525
types::{transparent_newtype_field, CItemKind},
2626
EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext,
2727
};
28+
use hir::IsAsync;
2829
use rustc_ast::attr;
2930
use rustc_ast::tokenstream::{TokenStream, TokenTree};
3031
use rustc_ast::visit::{FnCtxt, FnKind};
@@ -40,7 +41,10 @@ use rustc_feature::{deprecated_attributes, AttributeGate, BuiltinAttribute, Gate
4041
use rustc_hir as hir;
4142
use rustc_hir::def::{DefKind, Res};
4243
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID};
43-
use rustc_hir::{ForeignItemKind, GenericParamKind, HirId, Node, PatKind, PredicateOrigin};
44+
use rustc_hir::intravisit::FnKind as HirFnKind;
45+
use rustc_hir::{
46+
Body, FnDecl, ForeignItemKind, GenericParamKind, HirId, Node, PatKind, PredicateOrigin,
47+
};
4448
use rustc_index::vec::Idx;
4549
use rustc_middle::lint::in_external_macro;
4650
use rustc_middle::ty::layout::{LayoutError, LayoutOf};
@@ -1337,6 +1341,72 @@ impl<'tcx> LateLintPass<'tcx> for UnstableFeatures {
13371341
}
13381342
}
13391343

1344+
declare_lint! {
1345+
/// The `ungated_async_fn_track_caller` lint warns when the
1346+
/// `#[track_caller]` attribute is used on an async function, method, or
1347+
/// closure, without enabling the corresponding unstable feature flag.
1348+
///
1349+
/// ### Example
1350+
///
1351+
/// ```rust
1352+
/// #[track_caller]
1353+
/// async fn foo() {}
1354+
/// ```
1355+
///
1356+
/// {{produces}}
1357+
///
1358+
/// ### Explanation
1359+
///
1360+
/// The attribute must be used in conjunction with the
1361+
/// [`closure_track_caller` feature flag]. Otherwise, the `#[track_caller]`
1362+
/// annotation will function as as no-op.
1363+
///
1364+
/// [`closure_track_caller` feature flag]: https://doc.rust-lang.org/beta/unstable-book/language-features/closure-track-caller.html
1365+
UNGATED_ASYNC_FN_TRACK_CALLER,
1366+
Warn,
1367+
"enabling track_caller on an async fn is a no-op unless the closure_track_caller feature is enabled"
1368+
}
1369+
1370+
declare_lint_pass!(
1371+
/// Explains corresponding feature flag must be enabled for the `#[track_caller] attribute to
1372+
/// do anything
1373+
UngatedAsyncFnTrackCaller => [UNGATED_ASYNC_FN_TRACK_CALLER]
1374+
);
1375+
1376+
impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
1377+
fn check_fn(
1378+
&mut self,
1379+
cx: &LateContext<'_>,
1380+
fn_kind: HirFnKind<'_>,
1381+
_: &'tcx FnDecl<'_>,
1382+
_: &'tcx Body<'_>,
1383+
span: Span,
1384+
hir_id: HirId,
1385+
) {
1386+
if fn_kind.asyncness() == IsAsync::Async
1387+
&& !cx.tcx.features().closure_track_caller
1388+
&& let attrs = cx.tcx.hir().attrs(hir_id)
1389+
// Now, check if the function has the `#[track_caller]` attribute
1390+
&& let Some(attr) = attrs.iter().find(|attr| attr.has_name(sym::track_caller))
1391+
{
1392+
cx.struct_span_lint(
1393+
UNGATED_ASYNC_FN_TRACK_CALLER,
1394+
attr.span,
1395+
fluent::lint_ungated_async_fn_track_caller,
1396+
|lint| {
1397+
lint.span_label(span, fluent::label);
1398+
rustc_session::parse::add_feature_diagnostics(
1399+
lint,
1400+
&cx.tcx.sess.parse_sess,
1401+
sym::closure_track_caller,
1402+
);
1403+
lint
1404+
},
1405+
);
1406+
}
1407+
}
1408+
}
1409+
13401410
declare_lint! {
13411411
/// The `unreachable_pub` lint triggers for `pub` items not reachable from
13421412
/// the crate root.

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ late_lint_methods!(
219219
// May Depend on constants elsewhere
220220
UnusedBrokenConst: UnusedBrokenConst,
221221
UnstableFeatures: UnstableFeatures,
222+
UngatedAsyncFnTrackCaller: UngatedAsyncFnTrackCaller,
222223
ArrayIntoIter: ArrayIntoIter::default(),
223224
DropTraitConstraints: DropTraitConstraints,
224225
TemporaryCStringAsPtr: TemporaryCStringAsPtr,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// edition:2021
2+
3+
#![feature(async_closure, stmt_expr_attributes)]
4+
5+
fn main() {
6+
let _ = #[track_caller] async || {
7+
//~^ ERROR `#[track_caller]` on closures is currently unstable [E0658]
8+
};
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: `#[track_caller]` on closures is currently unstable
2+
--> $DIR/async-closure-gate.rs:6: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: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
warning: `#[track_caller]` on async functions is a no-op
2+
--> $DIR/panic-track-caller.rs:50:1
3+
|
4+
LL | #[track_caller]
5+
| ^^^^^^^^^^^^^^^
6+
LL | / async fn bar_track_caller() {
7+
LL | | panic!()
8+
LL | | }
9+
| |_- this function will not propagate the caller location
10+
|
11+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
12+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
13+
= note: `#[warn(ungated_async_fn_track_caller)]` on by default
14+
15+
warning: `#[track_caller]` on async functions is a no-op
16+
--> $DIR/panic-track-caller.rs:62:5
17+
|
18+
LL | #[track_caller]
19+
| ^^^^^^^^^^^^^^^
20+
LL | / async fn bar_assoc() {
21+
LL | | panic!();
22+
LL | | }
23+
| |_____- this function will not propagate the caller location
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+
warning: 2 warnings emitted
29+

src/test/ui/async-await/track-caller/panic-track-caller.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// run-pass
22
// edition:2021
3+
// revisions: feat nofeat
34
// needs-unwind
4-
#![feature(closure_track_caller)]
5+
#![feature(async_closure, stmt_expr_attributes)]
6+
#![cfg_attr(feat, feature(closure_track_caller))]
57

68
use std::future::Future;
79
use std::panic;
@@ -45,7 +47,7 @@ async fn foo() {
4547
bar().await
4648
}
4749

48-
#[track_caller]
50+
#[track_caller] //[nofeat]~ WARN `#[track_caller]` on async functions is a no-op
4951
async fn bar_track_caller() {
5052
panic!()
5153
}
@@ -57,7 +59,7 @@ async fn foo_track_caller() {
5759
struct Foo;
5860

5961
impl Foo {
60-
#[track_caller]
62+
#[track_caller] //[nofeat]~ WARN `#[track_caller]` on async functions is a no-op
6163
async fn bar_assoc() {
6264
panic!();
6365
}
@@ -84,7 +86,15 @@ fn panicked_at(f: impl FnOnce() + panic::UnwindSafe) -> u32 {
8486
}
8587

8688
fn main() {
87-
assert_eq!(panicked_at(|| block_on(foo())), 41);
88-
assert_eq!(panicked_at(|| block_on(foo_track_caller())), 54);
89-
assert_eq!(panicked_at(|| block_on(foo_assoc())), 67);
89+
assert_eq!(panicked_at(|| block_on(foo())), 43);
90+
91+
#[cfg(feat)]
92+
assert_eq!(panicked_at(|| block_on(foo_track_caller())), 56);
93+
#[cfg(nofeat)]
94+
assert_eq!(panicked_at(|| block_on(foo_track_caller())), 52);
95+
96+
#[cfg(feat)]
97+
assert_eq!(panicked_at(|| block_on(foo_assoc())), 69);
98+
#[cfg(nofeat)]
99+
assert_eq!(panicked_at(|| block_on(foo_assoc())), 64);
90100
}

0 commit comments

Comments
 (0)