Skip to content

Commit 1deedb1

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 e0607b3 commit 1deedb1

File tree

5 files changed

+58
-18
lines changed

5 files changed

+58
-18
lines changed

src/chat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use crate::tools::{
4646
truncate_msg_text, IsNoneOrEmpty, SystemTime,
4747
};
4848
use crate::webxdc::StatusUpdateSerial;
49-
use crate::{chatlist_events, imap};
49+
use crate::{chatlist_events, debug_ensure, imap};
5050

5151
/// An chat item, such as a message or a marker.
5252
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
@@ -1336,7 +1336,7 @@ impl ChatId {
13361336
{
13371337
let contact = Contact::get_by_id(context, *contact_id).await?;
13381338
let addr = contact.get_addr();
1339-
debug_assert!(contact.is_pgp_contact());
1339+
debug_ensure!(contact.is_pgp_contact());
13401340
let fingerprint = contact
13411341
.fingerprint()
13421342
.context("Contact does not have a fingerprint in encrypted chat")?;

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;
@@ -1879,9 +1879,10 @@ pub(crate) async fn mark_contact_id_as_verified(
18791879
contact_id: ContactId,
18801880
verifier_id: ContactId,
18811881
) -> Result<()> {
1882-
debug_assert_ne!(
1883-
contact_id, verifier_id,
1884-
"Contact cannot be verified by self"
1882+
debug_ensure_ne!(
1883+
contact_id,
1884+
verifier_id,
1885+
"Contact cannot be verified by self",
18851886
);
18861887
context
18871888
.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;
@@ -309,7 +310,7 @@ impl MimeFactory {
309310
} else if id == ContactId::SELF {
310311
member_fingerprints.push(self_fingerprint.to_string());
311312
} else {
312-
debug_assert!(member_fingerprints.is_empty(), "If some past member is a PGP-contact, all other past members should be PGP-contacts too");
313+
debug_ensure!(member_fingerprints.is_empty(), "If some past member is a PGP-contact, all other past members should be PGP-contacts too");
313314
}
314315
}
315316
member_timestamps.push(add_timestamp);
@@ -360,16 +361,16 @@ impl MimeFactory {
360361
// if we are leaving the group.
361362
past_member_fingerprints.push(self_fingerprint.to_string());
362363
} else {
363-
debug_assert!(past_member_fingerprints.is_empty(), "If some past member is a PGP-contact, all other past members should be PGP-contacts too");
364+
debug_ensure!(past_member_fingerprints.is_empty(), "If some past member is a PGP-contact, all other past members should be PGP-contacts too");
364365
}
365366
}
366367
}
367368
}
368369
}
369370
}
370371

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

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

449-
debug_assert!(
450+
debug_ensure!(
450451
member_timestamps.is_empty()
451452
|| to.len() + past_members.len() == member_timestamps.len()
452453
);
@@ -669,7 +670,7 @@ impl MimeFactory {
669670
));
670671
}
671672

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

src/receive_imf.rs

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

4747
/// This is the struct that is returned after receiving one email (aka MIME message).
@@ -1413,7 +1413,7 @@ async fn do_chat_assignment(
14131413
false => None,
14141414
};
14151415
if let Some(chat) = chat {
1416-
debug_assert!(chat.typ == Chattype::Single);
1416+
debug_ensure!(chat.typ == Chattype::Single);
14171417
let mut new_protection = match verified_encryption {
14181418
VerifiedEncryption::Verified => ProtectionStatus::Protected,
14191419
VerifiedEncryption::NotVerified(_) => ProtectionStatus::Unprotected,
@@ -2078,7 +2078,7 @@ RETURNING id
20782078
// afterwards insert additional parts.
20792079
replace_msg_id = None;
20802080

2081-
debug_assert!(!row_id.is_special());
2081+
debug_ensure!(!row_id.is_special());
20822082
created_db_entries.push(row_id);
20832083
}
20842084

@@ -2341,7 +2341,7 @@ async fn lookup_chat_by_reply(
23412341
// lookup by reply should never be needed
23422342
// as we can directly assign the message to the chat
23432343
// by its group ID.
2344-
debug_assert!(mime_parser.get_chat_group_id().is_none() || !mime_parser.was_encrypted());
2344+
debug_ensure!(mime_parser.get_chat_group_id().is_none() || !mime_parser.was_encrypted());
23452345

23462346
// Try to assign message to the same chat as the parent message.
23472347
let Some(parent_chat_id) = ChatId::lookup_by_message(parent) else {
@@ -3587,7 +3587,7 @@ async fn add_or_lookup_pgp_contacts_by_address_list(
35873587
}
35883588
}
35893589

3590-
debug_assert_eq!(contact_ids.len(), address_list.len());
3590+
debug_ensure_eq!(contact_ids.len(), address_list.len(),);
35913591
Ok(contact_ids)
35923592
}
35933593

@@ -3741,7 +3741,7 @@ async fn lookup_pgp_contacts_by_address_list(
37413741
contact_ids.push(contact_id);
37423742
}
37433743
}
3744-
debug_assert_eq!(address_list.len(), contact_ids.len());
3744+
debug_ensure_eq!(address_list.len(), contact_ids.len(),);
37453745
Ok(contact_ids)
37463746
}
37473747

src/tools.rs

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

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

0 commit comments

Comments
 (0)