Merge rust-bitcoin/rust-bitcoin#7012: units: add track_caller to panic ops
What changed, and why it matters
This change adds Rust's #[track_caller] annotation to arithmetic operations in the rust-bitcoin 'units' crate that can panic on invalid input (for example, subtracting a larger number from a smaller one). This annotation does not change whether or when panics happen; it only improves the quality of panic error messages by pointing to the caller's code instead of deep inside the library. It is a debugging/diagnostics improvement, not a security fix.
No security action required. Treat as a normal quality/debugging improvement. If reviewing, verify that #[track_caller] was applied consistently to all panicking operator impls and that the macro attribute propagation does not accidentally copy attributes that should remain on the primary impl only.
Security signals we found
No security-relevant behavioral change: only panic location metadata is affected
No new unsafe code or cryptographic operations
No changes to bounds checks, overflow handling, or arithmetic semantics
Change is explicitly described by the vendor as a policy/diagnostics improvement, not a vulnerability fix
Evidence from the diff
The commit adds #[track_caller] to operator trait implementations (Add, Sub, Mul, Div, Rem, AddAssign, SubAssign, etc.) for types such as BlockHeight, BlockHeightInterval, BlockMtp, BlockMtpInterval, FeeRate, Work, Weight, and NumOpResult. It also updates the impl_op_for_references! macro to propagate function attributes to generated reference-taking impls. #[track_caller] affects panic location reporting only; it does not alter arithmetic semantics, bounds checking, or overflow behavior. No unsafe code, no cryptographic code, and no input validation logic changes are present.
Changed components
units/src/block.rsunits/src/fee_rate/mod.rsunits/src/internal_macros.rsunits/src/pow.rsunits/src/result.rsunits/src/weight.rsInspect captured patch +40 / −0
### units/src/block.rs
@@ -488,6 +488,7 @@ crate::internal_macros::impl_op_for_references! {
impl ops::Sub<BlockHeight> for BlockHeight {
type Output = BlockHeightInterval;
+ #[track_caller]
fn sub(self, rhs: BlockHeight) -> Self::Output {
let interval = self.to_u32() - rhs.to_u32();
BlockHeightInterval::from_u32(interval)
@@ -498,6 +499,7 @@ crate::internal_macros::impl_op_for_references! {
impl ops::Add<BlockHeightInterval> for BlockHeight {
type Output = BlockHeight;
+ #[track_caller]
fn add(self, rhs: BlockHeightInterval) -> Self::Output {
let height = self.to_u32() + rhs.to_u32();
BlockHeight::from_u32(height)
@@ -508,6 +510,7 @@ crate::internal_macros::impl_op_for_references! {
impl ops::Sub<BlockHeightInterval> for BlockHeight {
type Output = BlockHeight;
+ #[track_caller]
fn sub(self, rhs: BlockHeightInterval) -> Self::Output {
let height = self.to_u32() - rhs.to_u32();
BlockHeight::from_u32(height)
@@ -518,6 +521,7 @@ crate::internal_macros::impl_op_for_references! {
impl ops::Add<BlockHeightInterval> for BlockHeightInterval {
type Output = BlockHeightInterval;
+ #[track_caller]
fn add(self, rhs: BlockHeightInterval) -> Self::Output {
let height = self.to_u32() + rhs.to_u32();
BlockHeightInterval::from_u32(height)
@@ -528,6 +532,7 @@ crate::internal_macros::impl_op_for_references! {
impl ops::Sub<BlockHeightInterval> for BlockHeightInterval {
type Output = BlockHeightInterval;
+ #[track_caller]
fn sub(self, rhs: BlockHeightInterval) -> Self::Output {
let height = self.to_u32() - rhs.to_u32();
BlockHeightInterval::from_u32(height)
@@ -538,6 +543,7 @@ crate::internal_macros::impl_op_for_references! {
impl ops::Sub<BlockMtp> for BlockMtp {
type Output = BlockMtpInterval;
+ #[track_caller]
fn sub(self, rhs: BlockMtp) -> Self::Output {
let interval = self.to_u32() - rhs.to_u32();
BlockMtpInterval::from_u32(interval)
@@ -548,6 +554,7 @@ crate::internal_macros::impl_op_for_references! {
impl ops::Add<BlockMtpInterval> for BlockMtp {
type Output = BlockMtp;
+ #[track_caller]
fn add(self, rhs: BlockMtpInterval) -> Self::Output {
let height = self.to_u32() + rhs.to_u32();
BlockMtp::from_u32(height)
@@ -558,6 +565,7 @@ crate::internal_macros::impl_op_for_references! {
impl ops::Sub<BlockMtpInterval> for BlockMtp {
type Output = BlockMtp;
+ #[track_caller]
fn sub(self, rhs: BlockMtpInterval) -> Self::Output {
let height = self.to_u32() - rhs.to_u32();
BlockMtp::from_u32(height)
@@ -568,6 +576,7 @@ crate::internal_macros::impl_op_for_references! {
impl ops::Add<BlockMtpInterval> for BlockMtpInterval {
type Output = BlockMtpInterval;
+ #[track_caller]
fn add(self, rhs: BlockMtpInterval) -> Self::Output {
let height = self.to_u32() + rhs.to_u32();
BlockMtpInterval::from_u32(height)
@@ -578,6 +587,7 @@ crate::internal_macros::impl_op_for_references! {
impl ops::Sub<BlockMtpInterval> for BlockMtpInterval {
type Output = BlockMtpInterval;
+ #[track_caller]
fn sub(self, rhs: BlockMtpInterval) -> Self::Output {
let height = self.to_u32() - rhs.to_u32();
BlockMtpInterval::from_u32(height)
### units/src/fee_rate/mod.rs
@@ -260,12 +260,14 @@ crate::internal_macros::impl_op_for_references! {
impl ops::Add<FeeRate> for FeeRate {
type Output = FeeRate;
+ #[track_caller]
fn add(self, rhs: FeeRate) -> Self::Output { FeeRate::from_sat_per_mvb(self.to_sat_per_mvb() + rhs.to_sat_per_mvb()) }
}
impl ops::Sub<FeeRate> for FeeRate {
type Output = FeeRate;
+ #[track_caller]
fn sub(self, rhs: FeeRate) -> Self::Output { FeeRate::from_sat_per_mvb(self.to_sat_per_mvb() - rhs.to_sat_per_mvb()) }
}
### units/src/internal_macros.rs
@@ -13,6 +13,8 @@
/// - `Add<&$other_ty> for $ty`
/// - `Add<&$other_ty> for &$ty`
///
+/// Attributes on the `fn` are copied to all four impls.
+///
/// # Limitations
///
/// You must specify `$other_ty` and you may not use `Self`. So e.g. you need
@@ -25,6 +27,7 @@ macro_rules! impl_op_for_references {
$(where ($($bounds:tt)*))?
{
type Output = $($main_output:ty)*;
+ $(#[$attr:meta])*
fn $op:ident($($main_args:tt)*) -> Self::Output {
$($main_impl:tt)*
}
@@ -35,6 +38,7 @@ macro_rules! impl_op_for_references {
{
type Output = $($main_output)*;
#[inline]
+ $(#[$attr])*
fn $op($($main_args)*) -> Self::Output {
$($main_impl)*
}
@@ -45,6 +49,7 @@ macro_rules! impl_op_for_references {
{
type Output = <$ty as $($op_trait)::+<$other_ty>>::Output;
#[inline]
+ $(#[$attr])*
fn $op(self, rhs: $other_ty) -> Self::Output {
(*self).$op(rhs)
}
@@ -55,6 +60,7 @@ macro_rules! impl_op_for_references {
{
type Output = <$ty as $($op_trait)::+<$other_ty>>::Output;
#[inline]
+ $(#[$attr])*
fn $op(self, rhs: &$other_ty) -> Self::Output {
self.$op(*rhs)
}
@@ -65,6 +71,7 @@ macro_rules! impl_op_for_references {
{
type Output = <$ty as $($op_trait)::+<$other_ty>>::Output;
#[inline]
+ $(#[$attr])*
fn $op(self, rhs: &$other_ty) -> Self::Output {
(*self).$op(*rhs)
}
@@ -78,11 +85,13 @@ macro_rules! impl_add_assign {
($ty:ident) => {
impl core::ops::AddAssign<$ty> for $ty {
#[inline]
+ #[track_caller]
fn add_assign(&mut self, rhs: $ty) { *self = *self + rhs }
}
impl core::ops::AddAssign<&$ty> for $ty {
#[inline]
+ #[track_caller]
fn add_assign(&mut self, rhs: &$ty) { *self = *self + *rhs }
}
};
@@ -162,11 +171,13 @@ macro_rules! impl_sub_assign {
($ty:ident) => {
impl core::ops::SubAssign<$ty> for $ty {
#[inline]
+ #[track_caller]
fn sub_assign(&mut self, rhs: $ty) { *self = *self - rhs }
}
impl core::ops::SubAssign<&$ty> for $ty {
#[inline]
+ #[track_caller]
fn sub_assign(&mut self, rhs: &$ty) { *self = *self - *rhs }
}
};
### units/src/pow.rs
@@ -127,12 +127,14 @@ impl_fmt_traits_for_u32_wrapper!(Work);
impl Add for Work {
type Output = Self;
#[inline]
+ #[track_caller]
fn add(self, rhs: Self) -> Self { Self(self.0 + rhs.0) }
}
impl Sub for Work {
type Output = Self;
#[inline]
+ #[track_caller]
fn sub(self, rhs: Self) -> Self { Self(self.0 - rhs.0) }
}
### units/src/result.rs
@@ -305,6 +305,7 @@ crate::internal_macros::impl_op_for_references! {
// Implement AddAssign on NumOpResults for all wrapped types that already implement AddAssign on themselves
impl<T: ops::AddAssign> ops::AddAssign<T> for NumOpResult<T> {
#[inline]
+ #[track_caller]
fn add_assign(&mut self, rhs: T) {
if let Self::Valid(ref mut lhs) = self {
*lhs += rhs;
@@ -314,6 +315,7 @@ impl<T: ops::AddAssign> ops::AddAssign<T> for NumOpResult<T> {
impl<T: ops::AddAssign + Copy> ops::AddAssign<Self> for NumOpResult<T> {
#[inline]
+ #[track_caller]
fn add_assign(&mut self, rhs: Self) {
match (&self, rhs) {
(Self::Valid(_), Self::Valid(rhs)) => *self += rhs,
@@ -326,6 +328,7 @@ impl<T: ops::AddAssign + Copy> ops::AddAssign<Self> for NumOpResult<T> {
// Implement SubAssign on NumOpResults for all wrapped types that already implement SubAssign on themselves
impl<T: ops::SubAssign> ops::SubAssign<T> for NumOpResult<T> {
#[inline]
+ #[track_caller]
fn sub_assign(&mut self, rhs: T) {
if let Self::Valid(ref mut lhs) = self {
*lhs -= rhs;
@@ -335,6 +338,7 @@ impl<T: ops::SubAssign> ops::SubAssign<T> for NumOpResult<T> {
impl<T: ops::SubAssign + Copy> ops::SubAssign<Self> for NumOpResult<T> {
#[inline]
+ #[track_caller]
fn sub_assign(&mut self, rhs: Self) {
match (&self, rhs) {
(Self::Valid(_), Self::Valid(rhs)) => *self -= rhs,
### units/src/weight.rs
@@ -214,42 +214,50 @@ crate::internal_macros::impl_op_for_references! {
impl ops::Add<Weight> for Weight {
type Output = Weight;
+ #[track_caller]
fn add(self, rhs: Weight) -> Self::Output { Weight::from_wu(self.to_wu() + rhs.to_wu()) }
}
impl ops::Sub<Weight> for Weight {
type Output = Weight;
+ #[track_caller]
fn sub(self, rhs: Weight) -> Self::Output { Weight::from_wu(self.to_wu() - rhs.to_wu()) }
}
impl ops::Mul<u64> for Weight {
type Output = Weight;
+ #[track_caller]
fn mul(self, rhs: u64) -> Self::Output { Weight::from_wu(self.to_wu() * rhs) }
}
impl ops::Mul<Weight> for u64 {
type Output = Weight;
+ #[track_caller]
fn mul(self, rhs: Weight) -> Self::Output { Weight::from_wu(self * rhs.to_wu()) }
}
impl ops::Div<u64> for Weight {
type Output = Weight;
+ #[track_caller]
fn div(self, rhs: u64) -> Self::Output { Weight::from_wu(self.to_wu() / rhs) }
}
impl ops::Div<Weight> for Weight {
type Output = u64;
+ #[track_caller]
fn div(self, rhs: Weight) -> Self::Output { self.to_wu() / rhs.to_wu() }
}
impl ops::Rem<u64> for Weight {
type Output = Weight;
+ #[track_caller]
fn rem(self, rhs: u64) -> Self::Output { Weight::from_wu(self.to_wu() % rhs) }
}
impl ops::Rem<Weight> for Weight {
type Output = Weight;
+ #[track_caller]
fn rem(self, rhs: Weight) -> Self::Output { Weight::from_wu(self.to_wu() % rhs.to_wu()) }
}
impl ops::Div<NonZeroU64> for Weight {
@@ -263,16 +271,19 @@ crate::internal_macros::impl_sub_assign!(Weight);
impl ops::MulAssign<u64> for Weight {
#[inline]
+ #[track_caller]
fn mul_assign(&mut self, rhs: u64) { *self = Self::from_wu(self.to_wu() * rhs); }
}
impl ops::DivAssign<u64> for Weight {
#[inline]
+ #[track_caller]
fn div_assign(&mut self, rhs: u64) { *self = Self::from_wu(self.to_wu() / rhs); }
}
impl ops::RemAssign<u64> for Weight {
#[inline]
+ #[track_caller]
fn rem_assign(&mut self, rhs: u64) { *self = Self::from_wu(self.to_wu() % rhs); }
}
Why this scored 19/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.