From 09e1090b2ed3605ea337dfc26466bbb0f2bd311b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Wed, 19 Nov 2025 19:17:50 -0500 Subject: [PATCH] refactor: use #[track_caller] directly instead of conditional cfg_attr --- build.rs | 4 +--- src/eyreish/context.rs | 22 +++++++++++----------- src/eyreish/error.rs | 12 ++++++------ src/eyreish/into_diagnostic.rs | 6 +++--- src/eyreish/kind.rs | 6 +++--- src/eyreish/mod.rs | 17 ++++++----------- 6 files changed, 30 insertions(+), 37 deletions(-) diff --git a/build.rs b/build.rs index 23e7655..fb54b74 100644 --- a/build.rs +++ b/build.rs @@ -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)"); } diff --git a/src/eyreish/context.rs b/src/eyreish/context.rs index 86db506..c92d651 100644 --- a/src/eyreish/context.rs +++ b/src/eyreish/context.rs @@ -14,7 +14,7 @@ mod ext { use super::*; pub trait Diag { - #[cfg_attr(track_caller, track_caller)] + #[track_caller] fn ext_report(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(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(self, msg: D) -> Report where D: Display + Send + Sync + 'static, @@ -45,7 +45,7 @@ mod ext { } impl WrapErr for Option { - #[cfg_attr(track_caller, track_caller)] + #[track_caller] fn wrap_err(self, msg: D) -> Result where D: Display + Send + Sync + 'static, @@ -56,7 +56,7 @@ impl WrapErr for Option { } } - #[cfg_attr(track_caller, track_caller)] + #[track_caller] fn wrap_err_with(self, msg: F) -> Result where D: Display + Send + Sync + 'static, @@ -68,7 +68,7 @@ impl WrapErr for Option { } } - #[cfg_attr(track_caller, track_caller)] + #[track_caller] fn context(self, msg: D) -> Result where D: Display + Send + Sync + 'static, @@ -76,7 +76,7 @@ impl WrapErr for Option { self.wrap_err(msg) } - #[cfg_attr(track_caller, track_caller)] + #[track_caller] fn with_context(self, msg: F) -> Result where D: Display + Send + Sync + 'static, @@ -90,7 +90,7 @@ impl WrapErr for Result where E: ext::Diag + Send + Sync + 'static, { - #[cfg_attr(track_caller, track_caller)] + #[track_caller] fn wrap_err(self, msg: D) -> Result where D: Display + Send + Sync + 'static, @@ -101,7 +101,7 @@ where } } - #[cfg_attr(track_caller, track_caller)] + #[track_caller] fn wrap_err_with(self, msg: F) -> Result where D: Display + Send + Sync + 'static, @@ -113,7 +113,7 @@ where } } - #[cfg_attr(track_caller, track_caller)] + #[track_caller] fn context(self, msg: D) -> Result 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(self, msg: F) -> Result where D: Display + Send + Sync + 'static, diff --git a/src/eyreish/error.rs b/src/eyreish/error.rs index 2dc0a3c..ac84975 100644 --- a/src/eyreish/error.rs +++ b/src/eyreish/error.rs @@ -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(error: E) -> Self where @@ -69,7 +69,7 @@ impl Report { /// .await /// } /// ``` - #[cfg_attr(track_caller, track_caller)] + #[track_caller] #[cold] pub fn msg(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) -> 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(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(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) -> Self { use super::wrapper::BoxedError; diff --git a/src/eyreish/into_diagnostic.rs b/src/eyreish/into_diagnostic.rs index 83262a5..c9264ec 100644 --- a/src/eyreish/into_diagnostic.rs +++ b/src/eyreish/into_diagnostic.rs @@ -38,13 +38,13 @@ inaccessible. If you have a type implementing [`Diagnostic`] consider simply ret pub trait IntoDiagnostic { /// 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; } #[cfg(feature = "std")] impl IntoDiagnostic for Result { - #[cfg_attr(track_caller, track_caller)] + #[track_caller] fn into_diagnostic(self) -> Result { self.map_err(|e| DiagnosticError(Box::new(e)).into()) } @@ -52,7 +52,7 @@ impl IntoDiagnostic for R #[cfg(not(feature = "std"))] impl IntoDiagnostic for Result { - #[cfg_attr(track_caller, track_caller)] + #[track_caller] fn into_diagnostic(self) -> Result { self.map_err(|e| DiagnosticError(Box::new(e)).into()) } diff --git a/src/eyreish/kind.rs b/src/eyreish/kind.rs index 2c1f49a..d3d1df1 100644 --- a/src/eyreish/kind.rs +++ b/src/eyreish/kind.rs @@ -71,7 +71,7 @@ pub trait AdhocKind: Sized { impl 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(self, message: M) -> Report where @@ -93,7 +93,7 @@ pub trait TraitKind: Sized { impl TraitKind for E where E: Into {} impl Trait { - #[cfg_attr(track_caller, track_caller)] + #[track_caller] #[cold] pub fn new(self, error: E) -> Report where @@ -115,7 +115,7 @@ pub trait BoxedKind: Sized { impl BoxedKind for Box {} impl Boxed { - #[cfg_attr(track_caller, track_caller)] + #[track_caller] #[cold] pub fn new(self, error: Box) -> Report { Report::from_boxed(error) diff --git a/src/eyreish/mod.rs b/src/eyreish/mod.rs index bf0076d..586fc61 100644 --- a/src/eyreish/mod.rs +++ b/src/eyreish/mod.rs @@ -105,7 +105,7 @@ pub(crate) fn capture_handler(error: &(dyn Diagnostic + 'static)) -> Box Box { @@ -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 { @@ -454,27 +449,27 @@ pub type Result = core::result::Result; /// ``` pub trait WrapErr: context::private::Sealed { /// Wrap the error value with a new adhoc error - #[cfg_attr(track_caller, track_caller)] + #[track_caller] fn wrap_err(self, msg: D) -> Result 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(self, f: F) -> Result 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(self, msg: D) -> Result 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(self, f: F) -> Result 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(message: M) -> Report where