What changed, and why it matters
This commit adds Rust's #[track_caller] attribute to arithmetic operations in the units crate. This is a debugging/quality-of-life improvement: when an operation panics (for example, adding two numbers that overflow), the error message will now point to the caller's code instead of deep inside the library. It does not change when panics happen, what values are computed, or fix any security vulnerability. It simply makes crash reports more useful for developers.
No security action required. Treat as a normal code-quality/debugging improvement. If reviewing, verify that all panicking operator impls in the units crate are consistently annotated and that no non-panicking paths were accidentally marked.
Security signals we found
No change to arithmetic semantics or bounds checking
No change to public API surface
No cryptographic or consensus-critical logic modified
Pure diagnostic/ergonomic improvement via #[track_caller]
No memory-safety, type-safety, or panic-safety behavior altered
Evidence from the diff
The patch annotates panicking arithmetic operator implementations (Add, Sub, Mul, Div, Rem, and their assignment variants) on BlockHeight, BlockHeightInterval, BlockMtp, BlockMtpInterval, FeeRate, Work, Weight, and NumOpResult with #[track_caller]. In Rust, #[track_caller] propagates the caller’s location to panic machinery, so panic! messages and backtraces report the call site rather than the library frame. The underlying overflow/underflow/division-by-zero behavior is unchanged; no bounds checks, return types, or cryptographic logic are modified. This mirrors std’s use of #[track_caller] on integer ops.
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 +33 / −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
@@ -85,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 }
}
};
@@ -169,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.