Change verify macro to debug panic
Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
parent
2b979d3b88
commit
023ecd595b
8 changed files with 40 additions and 87 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -4778,7 +4778,6 @@ dependencies = [
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"sum_tree",
|
"sum_tree",
|
||||||
"util",
|
"util",
|
||||||
"verify",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -6786,6 +6785,7 @@ name = "util"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"backtrace",
|
||||||
"futures 0.3.24",
|
"futures 0.3.24",
|
||||||
"git2",
|
"git2",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
|
@ -6845,14 +6845,6 @@ version = "0.8.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "verify"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"backtrace",
|
|
||||||
"log",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "version_check"
|
name = "version_check"
|
||||||
version = "0.9.4"
|
version = "0.9.4"
|
||||||
|
|
|
@ -52,7 +52,6 @@ members = [
|
||||||
"crates/theme_selector",
|
"crates/theme_selector",
|
||||||
"crates/theme_testbench",
|
"crates/theme_testbench",
|
||||||
"crates/util",
|
"crates/util",
|
||||||
"crates/verify",
|
|
||||||
"crates/vim",
|
"crates/vim",
|
||||||
"crates/workspace",
|
"crates/workspace",
|
||||||
"crates/zed",
|
"crates/zed",
|
||||||
|
|
|
@ -12,7 +12,7 @@ smallvec = { version = "1.6", features = ["union"] }
|
||||||
sum_tree = { path = "../sum_tree" }
|
sum_tree = { path = "../sum_tree" }
|
||||||
arrayvec = "0.7.1"
|
arrayvec = "0.7.1"
|
||||||
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
|
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
|
||||||
verify = { path = "../verify" }
|
util = { path = "../util" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
rand = "0.8.3"
|
rand = "0.8.3"
|
||||||
|
|
|
@ -12,7 +12,7 @@ use std::{
|
||||||
str,
|
str,
|
||||||
};
|
};
|
||||||
use sum_tree::{Bias, Dimension, SumTree};
|
use sum_tree::{Bias, Dimension, SumTree};
|
||||||
use verify::{verify, verify_not};
|
use util::debug_panic;
|
||||||
|
|
||||||
pub use offset_utf16::OffsetUtf16;
|
pub use offset_utf16::OffsetUtf16;
|
||||||
pub use point::Point;
|
pub use point::Point;
|
||||||
|
@ -681,9 +681,10 @@ impl Chunk {
|
||||||
let mut offset = 0;
|
let mut offset = 0;
|
||||||
let mut point = Point::new(0, 0);
|
let mut point = Point::new(0, 0);
|
||||||
for ch in self.0.chars() {
|
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;
|
return offset;
|
||||||
});
|
}
|
||||||
|
|
||||||
if point == target {
|
if point == target {
|
||||||
break;
|
break;
|
||||||
|
@ -693,13 +694,13 @@ impl Chunk {
|
||||||
point.row += 1;
|
point.row += 1;
|
||||||
point.column = 0;
|
point.column = 0;
|
||||||
|
|
||||||
verify_not!(
|
if point.row > target.row {
|
||||||
point.row > target.row,
|
debug_panic!(
|
||||||
("point {target:?} is beyond the end of a line with length {}", point.column),
|
"point {target:?} is beyond the end of a line with length {}",
|
||||||
else {
|
point.column
|
||||||
return offset;
|
);
|
||||||
}
|
return offset;
|
||||||
);
|
}
|
||||||
} else {
|
} else {
|
||||||
point.column += ch.len_utf8() as u32;
|
point.column += ch.len_utf8() as u32;
|
||||||
}
|
}
|
||||||
|
@ -743,35 +744,26 @@ impl Chunk {
|
||||||
point.row += 1;
|
point.row += 1;
|
||||||
point.column = 0;
|
point.column = 0;
|
||||||
|
|
||||||
if clip {
|
if point.row > target.row {
|
||||||
if point.row > target.row {
|
if !clip {
|
||||||
// Return the offset of the newline
|
debug_panic!(
|
||||||
return offset;
|
"point {target:?} is beyond the end of a line with length {}",
|
||||||
|
point.column
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} else {
|
// Return the offset of the newline
|
||||||
verify_not!(
|
return offset;
|
||||||
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;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
point.column += ch.len_utf16() as u32;
|
point.column += ch.len_utf16() as u32;
|
||||||
}
|
}
|
||||||
|
|
||||||
if clip {
|
if point > target {
|
||||||
if point > target {
|
if !clip {
|
||||||
// Return the offset of the codepoint which we have landed within, bias left
|
debug_panic!("point {target:?} is inside of codepoint {ch:?}");
|
||||||
return offset;
|
|
||||||
}
|
}
|
||||||
} else {
|
// Return the offset of the codepoint which we have landed within, bias left
|
||||||
verify_not!(point > target, ("point {target:?} is inside of codepoint {ch:?}"), else {
|
return offset;
|
||||||
// Return the offset of the codepoint which we have landed within, bias left
|
|
||||||
return offset;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
offset += ch.len_utf8();
|
offset += ch.len_utf8();
|
||||||
|
|
|
@ -11,6 +11,7 @@ test-support = ["serde_json", "tempdir", "git2"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.38"
|
anyhow = "1.0.38"
|
||||||
|
backtrace = "0.3"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
|
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
#[cfg(any(test, feature = "test-support"))]
|
||||||
pub mod test;
|
pub mod test;
|
||||||
|
|
||||||
|
pub use backtrace::Backtrace;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use rand::{seq::SliceRandom, Rng};
|
use rand::{seq::SliceRandom, Rng};
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -10,6 +11,18 @@ use std::{
|
||||||
task::{Context, Poll},
|
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 {
|
pub fn truncate(s: &str, max_chars: usize) -> &str {
|
||||||
match s.char_indices().nth(max_chars) {
|
match s.char_indices().nth(max_chars) {
|
||||||
None => s,
|
None => s,
|
||||||
|
|
|
@ -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"
|
|
|
@ -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)
|
|
||||||
};
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue