-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch
#[track_caller]
back to a no-op unless feature gate is enabled
This patch fixes a regression, in which `#[track_caller]`, which was previously a no-op, was changed to actually turn on the behavior. This should instead only happen behind the `closure_track_caller` feature gate. Also, add a warning for the user to understand how their code will compile depending on the feature gate being turned on or not. Fixes #104588
- Loading branch information
1 parent
1d12c3c
commit 04926e0
Showing
6 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// run-pass | ||
// edition:2021 | ||
// needs-unwind | ||
|
||
|
||
use std::future::Future; | ||
use std::panic; | ||
use std::sync::{Arc, Mutex}; | ||
use std::task::{Context, Poll, Wake}; | ||
use std::thread::{self, Thread}; | ||
|
||
/// A waker that wakes up the current thread when called. | ||
struct ThreadWaker(Thread); | ||
|
||
impl Wake for ThreadWaker { | ||
fn wake(self: Arc<Self>) { | ||
self.0.unpark(); | ||
} | ||
} | ||
|
||
/// Run a future to completion on the current thread. | ||
fn block_on<T>(fut: impl Future<Output = T>) -> T { | ||
// Pin the future so it can be polled. | ||
let mut fut = Box::pin(fut); | ||
|
||
// Create a new context to be passed to the future. | ||
let t = thread::current(); | ||
let waker = Arc::new(ThreadWaker(t)).into(); | ||
let mut cx = Context::from_waker(&waker); | ||
|
||
// Run the future to completion. | ||
loop { | ||
match fut.as_mut().poll(&mut cx) { | ||
Poll::Ready(res) => return res, | ||
Poll::Pending => thread::park(), | ||
} | ||
} | ||
} | ||
|
||
async fn bar() { | ||
panic!() | ||
} | ||
|
||
async fn foo() { | ||
bar().await | ||
} | ||
|
||
#[track_caller] //~ WARN `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled [ungated_async_fn_track_caller] | ||
async fn bar_track_caller() { | ||
panic!() | ||
} | ||
|
||
async fn foo_track_caller() { | ||
bar_track_caller().await | ||
} | ||
|
||
fn panicked_at(f: impl FnOnce() + panic::UnwindSafe) -> u32 { | ||
let loc = Arc::new(Mutex::new(None)); | ||
|
||
let hook = panic::take_hook(); | ||
{ | ||
let loc = loc.clone(); | ||
panic::set_hook(Box::new(move |info| { | ||
*loc.lock().unwrap() = info.location().map(|loc| loc.line()) | ||
})); | ||
} | ||
panic::catch_unwind(f).unwrap_err(); | ||
panic::set_hook(hook); | ||
let x = loc.lock().unwrap().unwrap(); | ||
x | ||
} | ||
|
||
fn main() { | ||
assert_eq!(panicked_at(|| block_on(foo())), 41); | ||
// Since the `closure_track_caller` feature is not enabled, the | ||
// `track_caller annotation does nothing. | ||
assert_eq!(panicked_at(|| block_on(foo_track_caller())), 50); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
warning: `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled | ||
--> $DIR/issue-104588-no-op-panic-track-caller.rs:48:16 | ||
| | ||
LL | #[track_caller] | ||
| ________________^ | ||
LL | | async fn bar_track_caller() { | ||
| |_ | ||
| | ||
= note: `#[warn(ungated_async_fn_track_caller)]` on by default | ||
|
||
warning: 1 warning emitted | ||
|
9 changes: 9 additions & 0 deletions
9
src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// check-pass | ||
// edition:2021 | ||
|
||
#[track_caller] //~ WARN `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled | ||
async fn foo() {} | ||
|
||
fn main() { | ||
foo(); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
warning: `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled | ||
--> $DIR/issue-104588-no-op-track-caller.rs:4:16 | ||
| | ||
LL | #[track_caller] | ||
| ________________^ | ||
LL | | async fn foo() {} | ||
| |_ | ||
| | ||
= note: `#[warn(ungated_async_fn_track_caller)]` on by default | ||
|
||
warning: 1 warning emitted | ||
|