only assert in debug builds

these are primarily used to detect bugs during e.g. fuzzing.

most of these asserts have negligible impact on performance, but if they
are not debug asserts it is not obvious that they dont affect
performance of release builds
This commit is contained in:
Noah Hellman 2023-02-26 18:10:20 +01:00
parent 3f011b9367
commit 1e5e56c463
5 changed files with 12 additions and 12 deletions

View file

@ -215,7 +215,7 @@ impl<'s> TreeParser<'s> {
// close list if a non list item or a list item of new type appeared
if let Some(OpenList { ty, depth, .. }) = self.open_lists.last() {
assert!(usize::from(*depth) <= self.tree.depth());
debug_assert!(usize::from(*depth) <= self.tree.depth());
if self.tree.depth() == (*depth).into()
&& !matches!(kind, Kind::ListItem { ty: ty_new, .. } if *ty == ty_new)
{
@ -405,7 +405,7 @@ impl<'s> TreeParser<'s> {
}
if let Some(OpenList { depth, .. }) = self.open_lists.last() {
assert!(usize::from(*depth) <= self.tree.depth());
debug_assert!(usize::from(*depth) <= self.tree.depth());
if self.tree.depth() == (*depth).into() {
self.prev_blankline = false;
self.prev_loose = false;

View file

@ -96,7 +96,7 @@ impl Render for Renderer {
Container::DescriptionList => out.write_str("<dl")?,
Container::DescriptionDetails => out.write_str("<dd")?,
Container::Footnote { number, .. } => {
assert!(self.footnote_number.is_none());
debug_assert!(self.footnote_number.is_none());
self.footnote_number = Some((*number).try_into().unwrap());
if !self.encountered_footnote {
self.encountered_footnote = true;

View file

@ -504,7 +504,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
)
)
{
assert_eq!(self.events[e_opener].span, event_closer.span);
debug_assert_eq!(self.events[e_opener].span, event_closer.span);
event_closer.span = inner_span;
self.events[e_opener].span = inner_span;
}

View file

@ -993,7 +993,7 @@ impl<'s> Parser<'s> {
class: (!ev.span.is_empty()).then(|| content),
},
block::Container::Footnote => {
assert!(enter);
debug_assert!(enter);
self.footnotes.insert(content, self.tree.take_branch());
self.block_attributes = Attributes::new();
continue;

View file

@ -79,7 +79,7 @@ impl<C: Clone, A: Clone> Tree<C, A> {
std::iter::from_fn(move || {
head.take().map(|h| {
let n = &self.nodes[h.index()];
assert!(matches!(n.kind, NodeKind::Inline));
debug_assert!(matches!(n.kind, NodeKind::Inline));
head = n.next;
n.span
})
@ -126,7 +126,7 @@ pub struct NodeIndex(std::num::NonZeroUsize);
impl NodeIndex {
fn new(i: usize) -> Self {
assert_ne!(i, usize::MAX);
debug_assert_ne!(i, usize::MAX);
Self((i + 1).try_into().unwrap())
}
@ -246,7 +246,7 @@ impl<C, A> Builder<C, A> {
}
} else {
let last = self.branch.pop();
assert_ne!(last, None);
debug_assert_ne!(last, None);
}
}
@ -314,7 +314,7 @@ impl<C, A> Builder<C, A> {
}
pub(super) fn finish(self) -> Tree<C, A> {
assert_eq!(self.depth, 0);
debug_assert_eq!(self.depth, 0);
let head = self.nodes[NodeIndex::root().index()].next;
Tree {
nodes: self.nodes.into_boxed_slice().into(),
@ -331,19 +331,19 @@ impl<C, A> Builder<C, A> {
match &mut head.kind {
NodeKind::Root | NodeKind::Inline | NodeKind::Atom(_) => {
// set next pointer of previous node
assert_eq!(head.next, None);
debug_assert_eq!(head.next, None);
head.next = Some(ni);
}
NodeKind::Container(_, child) => {
self.branch.push(*head_ni);
// set child pointer of current container
assert_eq!(*child, None);
debug_assert_eq!(*child, None);
*child = Some(ni);
}
}
} else if let Some(block) = self.branch.pop() {
let mut block = &mut self.nodes[block.index()];
assert!(matches!(block.kind, NodeKind::Container(..)));
debug_assert!(matches!(block.kind, NodeKind::Container(..)));
block.next = Some(ni);
} else {
panic!()