Skip to content

Commit 35643d2

Browse files
committed
improve panic messages for arithmetic operations
Previously, the error message didn't make it immediately obvious what went wrong. Let's be a bit more explicit. Note that "attempt to add/subtract with overflow" is the panic message used by Rust for normal arithmetic operations.
1 parent f01a291 commit 35643d2

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/addr.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,12 @@ impl Add<u64> for VirtAddr {
366366
type Output = Self;
367367
#[inline]
368368
fn add(self, rhs: u64) -> Self::Output {
369-
VirtAddr::new(self.0.checked_add(rhs).unwrap())
369+
VirtAddr::try_new(
370+
self.0
371+
.checked_add(rhs)
372+
.expect("attempt to add with overflow"),
373+
)
374+
.expect("attempt to add resulted in non-canonical virtual address")
370375
}
371376
}
372377

@@ -381,7 +386,12 @@ impl Sub<u64> for VirtAddr {
381386
type Output = Self;
382387
#[inline]
383388
fn sub(self, rhs: u64) -> Self::Output {
384-
VirtAddr::new(self.0.checked_sub(rhs).unwrap())
389+
VirtAddr::try_new(
390+
self.0
391+
.checked_sub(rhs)
392+
.expect("attempt to subtract with overflow"),
393+
)
394+
.expect("attempt to subtract resulted in non-canonical virtual address")
385395
}
386396
}
387397

@@ -396,7 +406,9 @@ impl Sub<VirtAddr> for VirtAddr {
396406
type Output = u64;
397407
#[inline]
398408
fn sub(self, rhs: VirtAddr) -> Self::Output {
399-
self.as_u64().checked_sub(rhs.as_u64()).unwrap()
409+
self.as_u64()
410+
.checked_sub(rhs.as_u64())
411+
.expect("attempt to subtract with overflow")
400412
}
401413
}
402414

0 commit comments

Comments
 (0)