1
Fork 0

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:
Bryan Garza 2022-11-24 03:39:47 +00:00
parent 04926e0534
commit dc2c4ce578
8 changed files with 51 additions and 106 deletions

View file

@ -350,7 +350,8 @@ lint_builtin_mutable_transmutes =
lint_builtin_unstable_features = unstable feature 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} lint_builtin_unreachable_pub = unreachable `pub` {$what}
.suggestion = consider restricting its visibility .suggestion = consider restricting its visibility

View file

@ -1404,10 +1404,16 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
if let Some(attr) = maybe_track_caller { if let Some(attr) = maybe_track_caller {
cx.struct_span_lint( cx.struct_span_lint(
UNGATED_ASYNC_FN_TRACK_CALLER, UNGATED_ASYNC_FN_TRACK_CALLER,
span.with_hi(attr.span.hi()), attr.span,
fluent::lint_ungated_async_fn_track_caller, 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
}
);
} }
} }
} }

View file

@ -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);
}

View file

@ -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

View file

@ -1,7 +1,7 @@
// check-pass // check-pass
// edition:2021 // 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() {} async fn foo() {}
fn main() { fn main() {

View file

@ -1,10 +1,10 @@
warning: `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled warning: `#[track_caller]` on async functions is a no-op
--> $DIR/issue-104588-no-op-track-caller.rs:4:16 --> $DIR/issue-104588-no-op-track-caller.rs:4:1
| |
LL | #[track_caller] LL | #[track_caller]
| ________________^ | ^^^^^^^^^^^^^^^ help: enable this feature: `#[closure_track_caller]`
LL | | async fn foo() {} LL | async fn foo() {}
| |_ | ----------------- this function will not propagate the caller location
| |
= note: `#[warn(ungated_async_fn_track_caller)]` on by default = note: `#[warn(ungated_async_fn_track_caller)]` on by default

View file

@ -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

View file

@ -1,7 +1,9 @@
// run-pass // run-pass
// edition:2021 // edition:2021
// revisions: feat nofeat
// needs-unwind // 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::future::Future;
use std::panic; use std::panic;
@ -45,7 +47,7 @@ async fn foo() {
bar().await bar().await
} }
#[track_caller] #[track_caller] //[nofeat]~ WARN `#[track_caller]` on async functions is a no-op
async fn bar_track_caller() { async fn bar_track_caller() {
panic!() panic!()
} }
@ -91,8 +93,20 @@ fn panicked_at(f: impl FnOnce() + panic::UnwindSafe) -> u32 {
} }
fn main() { fn main() {
assert_eq!(panicked_at(|| block_on(foo())), 41); assert_eq!(panicked_at(|| block_on(foo())), 43);
assert_eq!(panicked_at(|| block_on(foo_track_caller())), 54);
assert_eq!(panicked_at(|| block_on(foo_assoc())), 67); #[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); assert_eq!(panicked_at(|| block_on(foo_closure())), 74);
} }