Skip to content

Commit 3dec58b

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 ee83649 commit 3dec58b

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::{SystemTime, duration_to_str, get_abs_path, time};
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;
@@ -1907,9 +1907,10 @@ pub(crate) async fn mark_contact_id_as_verified(
19071907
contact_id: ContactId,
19081908
verifier_id: ContactId,
19091909
) -> Result<()> {
1910-
debug_assert_ne!(
1911-
contact_id, verifier_id,
1912-
"Contact cannot be verified by self"
1910+
debug_ensure_ne!(
1911+
contact_id,
1912+
verifier_id,
1913+
"Contact cannot be verified by self",
19131914
);
19141915
context
19151916
.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,
@@ -2105,7 +2105,7 @@ RETURNING id
21052105
// afterwards insert additional parts.
21062106
replace_msg_id = None;
21072107

2108-
debug_assert!(!row_id.is_special());
2108+
debug_ensure!(!row_id.is_special());
21092109
created_db_entries.push(row_id);
21102110
}
21112111

@@ -2368,7 +2368,7 @@ async fn lookup_chat_by_reply(
23682368
// lookup by reply should never be needed
23692369
// as we can directly assign the message to the chat
23702370
// by its group ID.
2371-
debug_assert!(mime_parser.get_chat_group_id().is_none() || !mime_parser.was_encrypted());
2371+
debug_ensure!(mime_parser.get_chat_group_id().is_none() || !mime_parser.was_encrypted());
23722372

23732373
// Try to assign message to the same chat as the parent message.
23742374
let Some(parent_chat_id) = ChatId::lookup_by_message(parent) else {
@@ -3628,7 +3628,7 @@ async fn add_or_lookup_key_contacts_by_address_list(
36283628
}
36293629
}
36303630

3631-
debug_assert_eq!(contact_ids.len(), address_list.len());
3631+
debug_ensure_eq!(contact_ids.len(), address_list.len(),);
36323632
Ok(contact_ids)
36333633
}
36343634

@@ -3782,7 +3782,7 @@ async fn lookup_key_contacts_by_address_list(
37823782
contact_ids.push(contact_id);
37833783
}
37843784
}
3785-
debug_assert_eq!(address_list.len(), contact_ids.len());
3785+
debug_ensure_eq!(address_list.len(), contact_ids.len(),);
37863786
Ok(contact_ids)
37873787
}
37883788

src/tools.rs

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

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

0 commit comments

Comments
 (0)