Change verify macro to debug panic

Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Julia 2022-11-30 13:03:15 -05:00
parent 2b979d3b88
commit 023ecd595b
8 changed files with 40 additions and 87 deletions

10
Cargo.lock generated
View File

@ -4778,7 +4778,6 @@ dependencies = [
"smallvec",
"sum_tree",
"util",
"verify",
]
[[package]]
@ -6786,6 +6785,7 @@ name = "util"
version = "0.1.0"
dependencies = [
"anyhow",
"backtrace",
"futures 0.3.24",
"git2",
"lazy_static",
@ -6845,14 +6845,6 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "verify"
version = "0.1.0"
dependencies = [
"backtrace",
"log",
]
[[package]]
name = "version_check"
version = "0.9.4"

View File

@ -52,7 +52,6 @@ members = [
"crates/theme_selector",
"crates/theme_testbench",
"crates/util",
"crates/verify",
"crates/vim",
"crates/workspace",
"crates/zed",

View File

@ -12,7 +12,7 @@ smallvec = { version = "1.6", features = ["union"] }
sum_tree = { path = "../sum_tree" }
arrayvec = "0.7.1"
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
verify = { path = "../verify" }
util = { path = "../util" }
[dev-dependencies]
rand = "0.8.3"

View File

@ -12,7 +12,7 @@ use std::{
str,
};
use sum_tree::{Bias, Dimension, SumTree};
use verify::{verify, verify_not};
use util::debug_panic;
pub use offset_utf16::OffsetUtf16;
pub use point::Point;
@ -681,9 +681,10 @@ impl Chunk {
let mut offset = 0;
let mut point = Point::new(0, 0);
for ch in self.0.chars() {
verify_not!(point > target, ("point {target:?} is inside of character {ch:?}"), else {
if point > target {
debug_panic!("point {target:?} is inside of character {ch:?}");
return offset;
});
}
if point == target {
break;
@ -693,13 +694,13 @@ impl Chunk {
point.row += 1;
point.column = 0;
verify_not!(
point.row > target.row,
("point {target:?} is beyond the end of a line with length {}", point.column),
else {
return offset;
}
);
if point.row > target.row {
debug_panic!(
"point {target:?} is beyond the end of a line with length {}",
point.column
);
return offset;
}
} else {
point.column += ch.len_utf8() as u32;
}
@ -743,35 +744,26 @@ impl Chunk {
point.row += 1;
point.column = 0;
if clip {
if point.row > target.row {
// Return the offset of the newline
return offset;
if point.row > target.row {
if !clip {
debug_panic!(
"point {target:?} is beyond the end of a line with length {}",
point.column
);
}
} else {
verify_not!(
point.row > target.row,
("point {target:?} is beyond the end of a line with length {}", point.column),
else {
// Return the offset of the newline
return offset;
}
)
// Return the offset of the newline
return offset;
}
} else {
point.column += ch.len_utf16() as u32;
}
if clip {
if point > target {
// Return the offset of the codepoint which we have landed within, bias left
return offset;
if point > target {
if !clip {
debug_panic!("point {target:?} is inside of codepoint {ch:?}");
}
} else {
verify_not!(point > target, ("point {target:?} is inside of codepoint {ch:?}"), else {
// Return the offset of the codepoint which we have landed within, bias left
return offset;
});
// Return the offset of the codepoint which we have landed within, bias left
return offset;
}
offset += ch.len_utf8();

View File

@ -11,6 +11,7 @@ test-support = ["serde_json", "tempdir", "git2"]
[dependencies]
anyhow = "1.0.38"
backtrace = "0.3"
futures = "0.3"
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
lazy_static = "1.4.0"

View File

@ -1,6 +1,7 @@
#[cfg(any(test, feature = "test-support"))]
pub mod test;
pub use backtrace::Backtrace;
use futures::Future;
use rand::{seq::SliceRandom, Rng};
use std::{
@ -10,6 +11,18 @@ use std::{
task::{Context, Poll},
};
#[macro_export]
macro_rules! debug_panic {
( $($fmt_arg:tt)* ) => {
if cfg!(debug_assertions) {
panic!( $($fmt_arg)* );
} else {
let backtrace = $crate::Backtrace::new();
log::error!("{}\n{:?}", format_args!($($fmt_arg)*), backtrace);
}
};
}
pub fn truncate(s: &str, max_chars: usize) -> &str {
match s.char_indices().nth(max_chars) {
None => s,

View File

@ -1,11 +0,0 @@
[package]
name = "verify"
version = "0.1.0"
edition = "2021"
[lib]
path = "src/verify.rs"
[dependencies]
backtrace = "0.3"
log = "0.4"

View File

@ -1,33 +0,0 @@
pub use backtrace::Backtrace;
#[macro_export]
macro_rules! verify {
( $expression:expr, else $block:expr ) => {
verify!($expression, (""), else $block)
};
( $expression:expr, ( $($fmt_arg:tt)* ), else $block:expr ) => {{
let verify_str = stringify!($expression);
if !$expression {
if cfg!(debug_assertions) {
panic!("Claim failed {:?}: {}", verify_str, format_args!($($fmt_arg)*));
} else {
let backtrace = $crate::Backtrace::new();
log::error!("Claim failed {:?}\n{:?}", verify_str, backtrace);
$block
}
}
}};
}
#[macro_export]
macro_rules! verify_not {
( $expression:expr, else $block:expr ) => {
verify_not!($expression, (""), else $block)
};
( $expression:expr, ( $($fmt_arg:tt)* ), else $block:expr ) => {
verify!(!$expression, ( $($fmt_arg)* ), else $block)
};
}