refactor: use #[track_caller] directly instead of conditional cfg_attr

This commit is contained in:
François Garillot 2025-11-19 19:17:50 -05:00
parent 9bdf23a659
commit 09e1090b2e
No known key found for this signature in database
GPG Key ID: E7645C6B883A1E9A
6 changed files with 30 additions and 37 deletions

View File

@ -1,8 +1,6 @@
fn main() {
// track_caller is stable since Rust 1.46 (2020), so no version check needed
println!("cargo:rustc-cfg=track_caller");
// track_caller is always available with our MSRV (Rust ≥ 1.82)
// Add check-cfg for conditional configurations
println!("cargo:rustc-check-cfg=cfg(doc_cfg)");
println!("cargo:rustc-check-cfg=cfg(track_caller)");
}

View File

@ -14,7 +14,7 @@ mod ext {
use super::*;
pub trait Diag {
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn ext_report<D>(self, msg: D) -> Report
where
D: Display + Send + Sync + 'static;
@ -24,7 +24,7 @@ mod ext {
where
E: Diagnostic + Send + Sync + 'static,
{
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn ext_report<D>(self, msg: D) -> Report
where
D: Display + Send + Sync + 'static,
@ -34,7 +34,7 @@ mod ext {
}
impl Diag for Report {
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn ext_report<D>(self, msg: D) -> Report
where
D: Display + Send + Sync + 'static,
@ -45,7 +45,7 @@ mod ext {
}
impl<T> WrapErr<T, Infallible> for Option<T> {
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn wrap_err<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
@ -56,7 +56,7 @@ impl<T> WrapErr<T, Infallible> for Option<T> {
}
}
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn wrap_err_with<D, F>(self, msg: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
@ -68,7 +68,7 @@ impl<T> WrapErr<T, Infallible> for Option<T> {
}
}
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn context<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
@ -76,7 +76,7 @@ impl<T> WrapErr<T, Infallible> for Option<T> {
self.wrap_err(msg)
}
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn with_context<D, F>(self, msg: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
@ -90,7 +90,7 @@ impl<T, E> WrapErr<T, E> for Result<T, E>
where
E: ext::Diag + Send + Sync + 'static,
{
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn wrap_err<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
@ -101,7 +101,7 @@ where
}
}
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn wrap_err_with<D, F>(self, msg: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
@ -113,7 +113,7 @@ where
}
}
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn context<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
@ -121,7 +121,7 @@ where
self.wrap_err(msg)
}
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn with_context<D, F>(self, msg: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,

View File

@ -23,7 +23,7 @@ impl Report {
///
/// If the error type does not provide a backtrace, a backtrace will be
/// created here to ensure that a backtrace exists.
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
#[cold]
pub fn new<E>(error: E) -> Self
where
@ -69,7 +69,7 @@ impl Report {
/// .await
/// }
/// ```
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
#[cold]
pub fn msg<M>(message: M) -> Self
where
@ -85,7 +85,7 @@ impl Report {
///
/// Boxed `Diagnostic`s don't implement `Diagnostic` themselves due to trait coherence issues.
/// This method allows you to create a `Report` from a boxed `Diagnostic`.
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
pub fn new_boxed(error: Box<dyn Diagnostic + Send + Sync + 'static>) -> Self {
Report::from_boxed(error)
}
@ -111,7 +111,7 @@ impl Report {
unsafe { Report::construct(error, vtable, handler) }
}
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
#[cold]
pub(crate) fn from_adhoc<M>(message: M) -> Self
where
@ -136,7 +136,7 @@ impl Report {
unsafe { Report::construct(error, vtable, handler) }
}
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
#[cold]
pub(crate) fn from_msg<D, E>(msg: D, error: E) -> Self
where
@ -161,7 +161,7 @@ impl Report {
unsafe { Report::construct(error, vtable, handler) }
}
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
#[cold]
pub(crate) fn from_boxed(error: Box<dyn Diagnostic + Send + Sync>) -> Self {
use super::wrapper::BoxedError;

View File

@ -38,13 +38,13 @@ inaccessible. If you have a type implementing [`Diagnostic`] consider simply ret
pub trait IntoDiagnostic<T, E> {
/// Converts [`Result`] types that return regular [`std::error::Error`]s
/// into a [`Result`] that returns a [`Diagnostic`].
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn into_diagnostic(self) -> Result<T, Report>;
}
#[cfg(feature = "std")]
impl<T, E: std::error::Error + Send + Sync + 'static> IntoDiagnostic<T, E> for Result<T, E> {
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn into_diagnostic(self) -> Result<T, Report> {
self.map_err(|e| DiagnosticError(Box::new(e)).into())
}
@ -52,7 +52,7 @@ impl<T, E: std::error::Error + Send + Sync + 'static> IntoDiagnostic<T, E> for R
#[cfg(not(feature = "std"))]
impl<T, E: Error + Send + Sync + 'static> IntoDiagnostic<T, E> for Result<T, E> {
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn into_diagnostic(self) -> Result<T, Report> {
self.map_err(|e| DiagnosticError(Box::new(e)).into())
}

View File

@ -71,7 +71,7 @@ pub trait AdhocKind: Sized {
impl<T> AdhocKind for &T where T: ?Sized + Display + Debug + Send + Sync + 'static {}
impl Adhoc {
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
#[cold]
pub fn new<M>(self, message: M) -> Report
where
@ -93,7 +93,7 @@ pub trait TraitKind: Sized {
impl<E> TraitKind for E where E: Into<Report> {}
impl Trait {
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
#[cold]
pub fn new<E>(self, error: E) -> Report
where
@ -115,7 +115,7 @@ pub trait BoxedKind: Sized {
impl BoxedKind for Box<dyn Diagnostic + Send + Sync> {}
impl Boxed {
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
#[cold]
pub fn new(self, error: Box<dyn Diagnostic + Send + Sync>) -> Report {
Report::from_boxed(error)

View File

@ -105,7 +105,7 @@ pub(crate) fn capture_handler(error: &(dyn Diagnostic + 'static)) -> Box<dyn Rep
hook(error)
}
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
pub(crate) fn capture_handler_with_location(
error: &(dyn Diagnostic + 'static),
) -> Box<dyn ReportHandler> {
@ -115,16 +115,11 @@ pub(crate) fn capture_handler_with_location(
DEFAULT.get().unwrap()
});
#[cfg(track_caller)]
{
let mut handler = hook(error);
handler.track_caller(core::panic::Location::caller());
handler
}
#[cfg(not(track_caller))]
{
hook(error)
}
}
fn get_default_printer(_err: &(dyn Diagnostic + 'static)) -> Box<dyn ReportHandler + 'static> {
@ -454,27 +449,27 @@ pub type Result<T, E = Report> = core::result::Result<T, E>;
/// ```
pub trait WrapErr<T, E>: context::private::Sealed {
/// Wrap the error value with a new adhoc error
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn wrap_err<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static;
/// Wrap the error value with a new adhoc error that is evaluated lazily
/// only once an error does occur.
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn wrap_err_with<D, F>(self, f: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D;
/// Compatibility re-export of `wrap_err()` for interop with `anyhow`
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn context<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static;
/// Compatibility re-export of `wrap_err_with()` for interop with `anyhow`
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
fn with_context<D, F>(self, f: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
@ -496,7 +491,7 @@ pub mod private {
pub use super::super::kind::BoxedKind;
}
#[cfg_attr(track_caller, track_caller)]
#[track_caller]
#[cold]
pub fn new_adhoc<M>(message: M) -> Report
where