Skip to content

Commit b84c392

Browse files
committed
feat: ensure_and_debug_assert{,_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 22258f7 commit b84c392

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-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, ensure_and_debug_assert_ne, stock_str};
4141

4242
/// Time during which a contact is considered as seen recently.
4343
const SEEN_RECENTLY_SECONDS: i64 = 600;
@@ -1922,9 +1922,10 @@ pub(crate) async fn mark_contact_id_as_verified(
19221922
contact_id: ContactId,
19231923
verifier_id: ContactId,
19241924
) -> Result<()> {
1925-
debug_assert_ne!(
1926-
contact_id, verifier_id,
1927-
"Contact cannot be verified by self"
1925+
ensure_and_debug_assert_ne!(
1926+
contact_id,
1927+
verifier_id,
1928+
"Contact cannot be verified by self",
19281929
);
19291930
context
19301931
.sql

src/mimefactory.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::constants::{Chattype, DC_FROM_HANDSHAKE};
2020
use crate::contact::{Contact, ContactId, Origin};
2121
use crate::context::Context;
2222
use crate::e2ee::EncryptHelper;
23+
use crate::ensure_and_debug_assert;
2324
use crate::ephemeral::Timer as EphemeralTimer;
2425
use crate::key::self_fingerprint;
2526
use crate::key::{DcKey, SignedPublicKey};
@@ -308,7 +309,7 @@ impl MimeFactory {
308309
} else if id == ContactId::SELF {
309310
member_fingerprints.push(self_fingerprint.to_string());
310311
} else {
311-
debug_assert!(member_fingerprints.is_empty(), "If some past member is a key-contact, all other past members should be key-contacts too");
312+
ensure_and_debug_assert!(member_fingerprints.is_empty(), "If some past member is a key-contact, all other past members should be key-contacts too");
312313
}
313314
}
314315
member_timestamps.push(add_timestamp);
@@ -359,16 +360,16 @@ impl MimeFactory {
359360
// if we are leaving the group.
360361
past_member_fingerprints.push(self_fingerprint.to_string());
361362
} else {
362-
debug_assert!(past_member_fingerprints.is_empty(), "If some past member is a key-contact, all other past members should be key-contacts too");
363+
ensure_and_debug_assert!(past_member_fingerprints.is_empty(), "If some past member is a key-contact, all other past members should be key-contacts too");
363364
}
364365
}
365366
}
366367
}
367368
}
368369
}
369370

370-
debug_assert!(member_timestamps.len() >= to.len());
371-
debug_assert!(member_fingerprints.is_empty() || member_fingerprints.len() >= to.len());
371+
ensure_and_debug_assert!(member_timestamps.len() >= to.len());
372+
ensure_and_debug_assert!(member_fingerprints.is_empty() || member_fingerprints.len() >= to.len());
372373

373374
if to.len() > 1 {
374375
if let Some(position) = to.iter().position(|(_, x)| x == &from_addr) {
@@ -445,7 +446,7 @@ impl MimeFactory {
445446
};
446447
let attach_selfavatar = Self::should_attach_selfavatar(context, &msg).await;
447448

448-
debug_assert!(
449+
ensure_and_debug_assert!(
449450
member_timestamps.is_empty()
450451
|| to.len() + past_members.len() == member_timestamps.len()
451452
);
@@ -668,7 +669,7 @@ impl MimeFactory {
668669
));
669670
}
670671

671-
debug_assert!(
672+
ensure_and_debug_assert!(
672673
self.member_timestamps.is_empty()
673674
|| to.len() + past_members.len() == self.member_timestamps.len()
674675
);

src/receive_imf.rs

Lines changed: 8 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, ensure_and_debug_assert, ensure_and_debug_assert_eq, location};
4646
use crate::{contact, imap};
4747

4848
/// This is the struct that is returned after receiving one email (aka MIME message).
@@ -1454,7 +1454,7 @@ async fn do_chat_assignment(
14541454
false => None,
14551455
};
14561456
if let Some(chat) = chat {
1457-
debug_assert!(chat.typ == Chattype::Single);
1457+
ensure_and_debug_assert!(chat.typ == Chattype::Single);
14581458
let mut new_protection = match verified_encryption {
14591459
VerifiedEncryption::Verified => ProtectionStatus::Protected,
14601460
VerifiedEncryption::NotVerified(_) => ProtectionStatus::Unprotected,
@@ -2137,7 +2137,7 @@ RETURNING id
21372137
// afterwards insert additional parts.
21382138
replace_msg_id = None;
21392139

2140-
debug_assert!(!row_id.is_special());
2140+
ensure_and_debug_assert!(!row_id.is_special());
21412141
created_db_entries.push(row_id);
21422142
}
21432143

@@ -2400,7 +2400,9 @@ async fn lookup_chat_by_reply(
24002400
// lookup by reply should never be needed
24012401
// as we can directly assign the message to the chat
24022402
// by its group ID.
2403-
debug_assert!(mime_parser.get_chat_group_id().is_none() || !mime_parser.was_encrypted());
2403+
ensure_and_debug_assert!(
2404+
mime_parser.get_chat_group_id().is_none() || !mime_parser.was_encrypted()
2405+
);
24042406

24052407
// Try to assign message to the same chat as the parent message.
24062408
let Some(parent_chat_id) = ChatId::lookup_by_message(parent) else {
@@ -3727,7 +3729,7 @@ async fn add_or_lookup_key_contacts_by_address_list(
37273729
}
37283730
}
37293731

3730-
debug_assert_eq!(contact_ids.len(), address_list.len());
3732+
ensure_and_debug_assert_eq!(contact_ids.len(), address_list.len(),);
37313733
Ok(contact_ids)
37323734
}
37333735

@@ -3881,7 +3883,7 @@ async fn lookup_key_contacts_by_address_list(
38813883
contact_ids.push(contact_id);
38823884
}
38833885
}
3884-
debug_assert_eq!(address_list.len(), contact_ids.len());
3886+
ensure_and_debug_assert_eq!(address_list.len(), contact_ids.len(),);
38853887
Ok(contact_ids)
38863888
}
38873889

src/tools.rs

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

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

0 commit comments

Comments
 (0)