Update code based on PR comments
This patch does the following: - Refactor some repeated lines into a single one - Split the `ungated_async_fn_caller` lint into multiple lines, and make one of those lines only print out on nightly - Use test revisions instead of copying an existing test
This commit is contained in:
parent
04926e0534
commit
dc2c4ce578
8 changed files with 51 additions and 106 deletions
|
@ -350,7 +350,8 @@ lint_builtin_mutable_transmutes =
|
|||
|
||||
lint_builtin_unstable_features = unstable feature
|
||||
|
||||
lint_ungated_async_fn_track_caller = `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled
|
||||
lint_ungated_async_fn_track_caller = `#[track_caller]` on async functions is a no-op
|
||||
.suggestion = enable this feature
|
||||
|
||||
lint_builtin_unreachable_pub = unreachable `pub` {$what}
|
||||
.suggestion = consider restricting its visibility
|
||||
|
|
|
@ -1404,9 +1404,15 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
|
|||
if let Some(attr) = maybe_track_caller {
|
||||
cx.struct_span_lint(
|
||||
UNGATED_ASYNC_FN_TRACK_CALLER,
|
||||
span.with_hi(attr.span.hi()),
|
||||
attr.span,
|
||||
fluent::lint_ungated_async_fn_track_caller,
|
||||
|lint| lint,
|
||||
|lint| {
|
||||
lint.span_label(span, "this function will not propagate the caller location");
|
||||
if cx.tcx.sess.is_nightly_build() {
|
||||
lint.span_suggestion(attr.span, fluent::suggestion, "#[closure_track_caller]", Applicability::MachineApplicable);
|
||||
}
|
||||
lint
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,78 +0,0 @@
|
|||
// 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);
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
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
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
// check-pass
|
||||
// edition:2021
|
||||
|
||||
#[track_caller] //~ WARN `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled
|
||||
#[track_caller] //~ WARN `#[track_caller]` on async functions is a no-op
|
||||
async fn foo() {}
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
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
|
||||
warning: `#[track_caller]` on async functions is a no-op
|
||||
--> $DIR/issue-104588-no-op-track-caller.rs:4:1
|
||||
|
|
||||
LL | #[track_caller]
|
||||
| ________________^
|
||||
LL | | async fn foo() {}
|
||||
| |_
|
||||
| ^^^^^^^^^^^^^^^ help: enable this feature: `#[closure_track_caller]`
|
||||
LL | async fn foo() {}
|
||||
| ----------------- this function will not propagate the caller location
|
||||
|
|
||||
= note: `#[warn(ungated_async_fn_track_caller)]` on by default
|
||||
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
warning: `#[track_caller]` on async functions is a no-op
|
||||
--> $DIR/panic-track-caller.rs:50:1
|
||||
|
|
||||
LL | #[track_caller]
|
||||
| ^^^^^^^^^^^^^^^ help: enable this feature: `#[closure_track_caller]`
|
||||
LL | / async fn bar_track_caller() {
|
||||
LL | | panic!()
|
||||
LL | | }
|
||||
| |_- this function will not propagate the caller location
|
||||
|
|
||||
= note: `#[warn(ungated_async_fn_track_caller)]` on by default
|
||||
|
||||
warning: 1 warning emitted
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
// run-pass
|
||||
// edition:2021
|
||||
// revisions: feat nofeat
|
||||
// needs-unwind
|
||||
#![feature(closure_track_caller, async_closure, stmt_expr_attributes)]
|
||||
#![feature(async_closure, stmt_expr_attributes)]
|
||||
#![cfg_attr(feat, feature(closure_track_caller))]
|
||||
|
||||
use std::future::Future;
|
||||
use std::panic;
|
||||
|
@ -45,7 +47,7 @@ async fn foo() {
|
|||
bar().await
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
#[track_caller] //[nofeat]~ WARN `#[track_caller]` on async functions is a no-op
|
||||
async fn bar_track_caller() {
|
||||
panic!()
|
||||
}
|
||||
|
@ -91,8 +93,20 @@ fn panicked_at(f: impl FnOnce() + panic::UnwindSafe) -> u32 {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(panicked_at(|| block_on(foo())), 41);
|
||||
assert_eq!(panicked_at(|| block_on(foo_track_caller())), 54);
|
||||
assert_eq!(panicked_at(|| block_on(foo_assoc())), 67);
|
||||
assert_eq!(panicked_at(|| block_on(foo())), 43);
|
||||
|
||||
#[cfg(feat)]
|
||||
assert_eq!(panicked_at(|| block_on(foo_track_caller())), 56);
|
||||
#[cfg(nofeat)]
|
||||
assert_eq!(panicked_at(|| block_on(foo_track_caller())), 52);
|
||||
|
||||
#[cfg(feat)]
|
||||
assert_eq!(panicked_at(|| block_on(foo_assoc())), 69);
|
||||
#[cfg(nofeat)]
|
||||
assert_eq!(panicked_at(|| block_on(foo_assoc())), 64);
|
||||
|
||||
#[cfg(feat)]
|
||||
assert_eq!(panicked_at(|| block_on(foo_closure())), 76);
|
||||
#[cfg(feat)]
|
||||
assert_eq!(panicked_at(|| block_on(foo_closure())), 74);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue