From 5da3a642a21dcf0104f702a25582c1eb6b656615 Mon Sep 17 00:00:00 2001 From: Raphael Darley Date: Sun, 8 Sep 2024 23:39:42 +0100 Subject: [PATCH] Add `fail!()` macro and test (#4699) Co-authored-by: Tobie Morgan Hitchcock --- core/src/doc/compute.rs | 2 +- core/src/doc/process.rs | 2 +- core/src/mac/mod.rs | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/core/src/doc/compute.rs b/core/src/doc/compute.rs index b2a83c2c..aa56ff01 100644 --- a/core/src/doc/compute.rs +++ b/core/src/doc/compute.rs @@ -88,7 +88,7 @@ impl Document { // two separtate UNIQUE index definitions, and it // wasn't possible to detect which record was the // correct one to be updated - let _ = chn.send(Err(Error::Unreachable("Internal error"))).await; + let _ = chn.send(Err(fail!("Internal error"))).await; // Break the loop Ok(()) } diff --git a/core/src/doc/process.rs b/core/src/doc/process.rs index 0f91ca1f..7a164205 100644 --- a/core/src/doc/process.rs +++ b/core/src/doc/process.rs @@ -80,6 +80,6 @@ impl Document { // two separtate UNIQUE index definitions, and it // wasn't possible to detect which record was the // correct one to be updated - Err(Error::Unreachable("Internal error")) + Err(fail!("Internal error")) } } diff --git a/core/src/mac/mod.rs b/core/src/mac/mod.rs index 146ca0b4..33d50e9b 100644 --- a/core/src/mac/mod.rs +++ b/core/src/mac/mod.rs @@ -1,3 +1,10 @@ +/// Throws an unreachable error with location details +macro_rules! fail { + ($msg: literal) => { + $crate::err::Error::Unreachable(concat!(file!(), ":", line!(), ": ", $msg)) + }; +} + /// Converts some text into a new line byte string macro_rules! bytes { ($expression:expr) => { @@ -203,6 +210,8 @@ macro_rules! async_defer{ #[cfg(test)] mod test { + use crate::err::Error; + #[tokio::test] async fn async_defer_basic() { let mut counter = 0; @@ -241,4 +250,12 @@ mod test { }) .await; } + + #[test] + fn fail() { + let Error::Unreachable(msg) = fail!("Reached unreachable code") else { + panic!() + }; + assert_eq!("core/src/mac/mod.rs:256: Reached unreachable code", msg); + } }