Skip to content

Commit a6f850e

Browse files
committed
feat: debug_ensure{,_eq,_ne} macros combining debug_assert* and anyhow::ensure (#6907)
We have some debug assertions already, but we also want the corresponding errors in the release configuration so that it's not less reliable than non-optimized one. This doesn't change any function signatures, only debug assertions in functions returning `Result` are replaced.
1 parent 416131b commit a6f850e

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

src/contact.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::mimeparser::AvatarAction;
3737
use crate::param::{Param, Params};
3838
use crate::sync::{self, Sync::*};
3939
use crate::tools::{duration_to_str, get_abs_path, time, SystemTime};
40-
use crate::{chat, chatlist_events, stock_str};
40+
use crate::{chat, chatlist_events, debug_ensure_ne, stock_str};
4141

4242
/// Time during which a contact is considered as seen recently.
4343
const SEEN_RECENTLY_SECONDS: i64 = 600;
@@ -1905,9 +1905,10 @@ pub(crate) async fn mark_contact_id_as_verified(
19051905
contact_id: ContactId,
19061906
verifier_id: ContactId,
19071907
) -> Result<()> {
1908-
debug_assert_ne!(
1909-
contact_id, verifier_id,
1910-
"Contact cannot be verified by self"
1908+
debug_ensure_ne!(
1909+
contact_id,
1910+
verifier_id,
1911+
"Contact cannot be verified by self",
19111912
);
19121913
context
19131914
.sql

src/mimefactory.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::constants::ASM_SUBJECT;
2121
use crate::constants::{Chattype, DC_FROM_HANDSHAKE};
2222
use crate::contact::{Contact, ContactId, Origin};
2323
use crate::context::Context;
24+
use crate::debug_ensure;
2425
use crate::e2ee::EncryptHelper;
2526
use crate::ephemeral::Timer as EphemeralTimer;
2627
use crate::key::self_fingerprint;
@@ -310,7 +311,7 @@ impl MimeFactory {
310311
} else if id == ContactId::SELF {
311312
member_fingerprints.push(self_fingerprint.to_string());
312313
} else {
313-
debug_assert!(member_fingerprints.is_empty(), "If some past member is a key-contact, all other past members should be key-contacts too");
314+
debug_ensure!(member_fingerprints.is_empty(), "If some past member is a key-contact, all other past members should be key-contacts too");
314315
}
315316
}
316317
member_timestamps.push(add_timestamp);
@@ -361,16 +362,16 @@ impl MimeFactory {
361362
// if we are leaving the group.
362363
past_member_fingerprints.push(self_fingerprint.to_string());
363364
} else {
364-
debug_assert!(past_member_fingerprints.is_empty(), "If some past member is a key-contact, all other past members should be key-contacts too");
365+
debug_ensure!(past_member_fingerprints.is_empty(), "If some past member is a key-contact, all other past members should be key-contacts too");
365366
}
366367
}
367368
}
368369
}
369370
}
370371
}
371372

372-
debug_assert!(member_timestamps.len() >= to.len());
373-
debug_assert!(member_fingerprints.is_empty() || member_fingerprints.len() >= to.len());
373+
debug_ensure!(member_timestamps.len() >= to.len());
374+
debug_ensure!(member_fingerprints.is_empty() || member_fingerprints.len() >= to.len());
374375

375376
if to.len() > 1 {
376377
if let Some(position) = to.iter().position(|(_, x)| x == &from_addr) {
@@ -447,7 +448,7 @@ impl MimeFactory {
447448
};
448449
let attach_selfavatar = Self::should_attach_selfavatar(context, &msg).await;
449450

450-
debug_assert!(
451+
debug_ensure!(
451452
member_timestamps.is_empty()
452453
|| to.len() + past_members.len() == member_timestamps.len()
453454
);
@@ -670,7 +671,7 @@ impl MimeFactory {
670671
));
671672
}
672673

673-
debug_assert!(
674+
debug_ensure!(
674675
self.member_timestamps.is_empty()
675676
|| to.len() + past_members.len() == self.member_timestamps.len()
676677
);

src/receive_imf.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use crate::simplify;
4242
use crate::stock_str;
4343
use crate::sync::Sync::*;
4444
use crate::tools::{self, buf_compress, remove_subject_prefix};
45-
use crate::{chatlist_events, location};
45+
use crate::{chatlist_events, debug_ensure, debug_ensure_eq, location};
4646
use crate::{contact, imap};
4747

4848
/// This is the struct that is returned after receiving one email (aka MIME message).
@@ -1435,7 +1435,7 @@ async fn do_chat_assignment(
14351435
false => None,
14361436
};
14371437
if let Some(chat) = chat {
1438-
debug_assert!(chat.typ == Chattype::Single);
1438+
debug_ensure!(chat.typ == Chattype::Single);
14391439
let mut new_protection = match verified_encryption {
14401440
VerifiedEncryption::Verified => ProtectionStatus::Protected,
14411441
VerifiedEncryption::NotVerified(_) => ProtectionStatus::Unprotected,
@@ -2100,7 +2100,7 @@ RETURNING id
21002100
// afterwards insert additional parts.
21012101
replace_msg_id = None;
21022102

2103-
debug_assert!(!row_id.is_special());
2103+
debug_ensure!(!row_id.is_special());
21042104
created_db_entries.push(row_id);
21052105
}
21062106

@@ -2363,7 +2363,7 @@ async fn lookup_chat_by_reply(
23632363
// lookup by reply should never be needed
23642364
// as we can directly assign the message to the chat
23652365
// by its group ID.
2366-
debug_assert!(mime_parser.get_chat_group_id().is_none() || !mime_parser.was_encrypted());
2366+
debug_ensure!(mime_parser.get_chat_group_id().is_none() || !mime_parser.was_encrypted());
23672367

23682368
// Try to assign message to the same chat as the parent message.
23692369
let Some(parent_chat_id) = ChatId::lookup_by_message(parent) else {
@@ -3623,7 +3623,7 @@ async fn add_or_lookup_key_contacts_by_address_list(
36233623
}
36243624
}
36253625

3626-
debug_assert_eq!(contact_ids.len(), address_list.len());
3626+
debug_ensure_eq!(contact_ids.len(), address_list.len(),);
36273627
Ok(contact_ids)
36283628
}
36293629

@@ -3777,7 +3777,7 @@ async fn lookup_key_contacts_by_address_list(
37773777
contact_ids.push(contact_id);
37783778
}
37793779
}
3780-
debug_assert_eq!(address_list.len(), contact_ids.len());
3780+
debug_ensure_eq!(address_list.len(), contact_ids.len(),);
37813781
Ok(contact_ids)
37823782
}
37833783

src/tools.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,5 +766,43 @@ pub(crate) fn inc_and_check<T: PrimInt + AddAssign + std::fmt::Debug>(
766766
Ok(())
767767
}
768768

769+
/// Return early with an error if a condition is not satisfied.
770+
/// In non-optimized builds, panics instead if so.
771+
#[macro_export]
772+
macro_rules! debug_ensure {
773+
($($arg:tt)*) => {
774+
debug_assert!($($arg)*);
775+
anyhow::ensure!($($arg)*);
776+
};
777+
}
778+
779+
/// Return early with an error on two expressions inequality.
780+
/// In non-optimized builds, panics instead if so.
781+
#[macro_export]
782+
macro_rules! debug_ensure_eq {
783+
($left:expr, $right:expr, $($arg:tt)*) => {
784+
match (&$left, &$right) {
785+
(left_val, right_val) => {
786+
debug_assert_eq!(left_val, right_val, $($arg)*);
787+
anyhow::ensure!(left_val == right_val, $($arg)*);
788+
}
789+
}
790+
};
791+
}
792+
793+
/// Return early with an error on two expressions equality.
794+
/// In non-optimized builds, panics instead if so.
795+
#[macro_export]
796+
macro_rules! debug_ensure_ne {
797+
($left:expr, $right:expr, $($arg:tt)*) => {
798+
match (&$left, &$right) {
799+
(left_val, right_val) => {
800+
debug_assert_ne!(left_val, right_val, $($arg)*);
801+
anyhow::ensure!(left_val != right_val, $($arg)*);
802+
}
803+
}
804+
};
805+
}
806+
769807
#[cfg(test)]
770808
mod tools_tests;

0 commit comments

Comments
 (0)