Skip to content

Commit

Permalink
Feat(compiler-base): reuse kclvm bug macro. (#151)
Browse files Browse the repository at this point in the history
feat(compiler-base): reuse kclvm bug macro.

init crate 'macros' in compiler_base.
reuse bug macro 'bug!()' in compiler_base/macros/src/bug.rs.
add test cases for 'bug!()' in compiler_base/macros/src/tests.rs.

issue #115
  • Loading branch information
zong-zhe committed Aug 15, 2022
1 parent 83b6ad4 commit 7e2e0ff
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 2 deletions.
1 change: 1 addition & 0 deletions kclvm/compiler_base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"

[workspace]
members = [
"macros",
"span",
"error",
"3rdparty/rustc_errors",
Expand Down
3 changes: 2 additions & 1 deletion kclvm/compiler_base/error/src/diagnostic/components.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! 'components.rs' defines all components that builtin in compiler_base_error.
use super::{style::DiagnosticStyle, Component};
use rustc_errors::styled_buffer::StyledBuffer;

use super::{style::DiagnosticStyle, Component};

/// `Label` can be considered as a component of diagnostic to display a short label message in `Diagnositc`.
/// `Label` provides "error", "warning", "note" and "Help" four kinds of labels.
///
Expand Down
8 changes: 8 additions & 0 deletions kclvm/compiler_base/macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "compiler_base_macros"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
48 changes: 48 additions & 0 deletions kclvm/compiler_base/macros/src/bug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::{error, fmt, panic};

/// `bug!` macro is used to report compiler internal bug.
/// You can use bug! macros directly by adding `#[macro_use]extern crate kclvm_error;`
/// in the lib.rs, and then call as follows:
/// ```no_check
/// bug!();
/// bug!("an error msg");
/// bug!("an error msg with string format {}", "msg");
/// ```
#[macro_export]
macro_rules! bug {
() => ( $crate::bug::bug("impossible case reached") );
($msg:expr) => ({ $crate::bug::bug(&format!($msg)) });
($msg:expr,) => ({ $crate::bug::bug($msg) });
($fmt:expr, $($arg:tt)+) => ({
$crate::bug::bug(&format!($fmt, $($arg)+))
});
}

/// Signifies that the compiler died with an explicit call to `.bug`
/// rather than a failed assertion, etc.
#[derive(Clone, Debug)]
pub struct ExplicitBug {
msg: String,
}

impl fmt::Display for ExplicitBug {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Internal error, please report a bug to us. The error message is: {}",
self.msg
)
}
}

impl error::Error for ExplicitBug {}

#[inline]
pub fn bug(msg: &str) -> ! {
panic!(
"{}",
ExplicitBug {
msg: msg.to_string()
}
);
}
5 changes: 5 additions & 0 deletions kclvm/compiler_base/macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[macro_use]
pub mod bug;

#[cfg(test)]
mod tests;
46 changes: 46 additions & 0 deletions kclvm/compiler_base/macros/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
mod test_bug {
use crate::bug;

#[test]
fn test_bug_macro() {
std::panic::set_hook(Box::new(|_| {}));
let result = std::panic::catch_unwind(|| {
bug!();
});
assert!(result.is_err());
let result = std::panic::catch_unwind(|| {
bug!("an error msg");
});
assert!(result.is_err());
match result {
Ok(_) => panic!("test bug!() failed"),
Err(panic_err) => {
let err_message = if let Some(s) = panic_err.downcast_ref::<String>() {
(*s).clone()
} else {
panic!("test bug!() failed")
};
assert_eq!(
err_message,
"Internal error, please report a bug to us. The error message is: an error msg"
);
}
}

let result = std::panic::catch_unwind(|| {
bug!("an error msg with string format {}", "msg");
});
assert!(result.is_err());
match result {
Ok(_) => panic!("test bug!() failed"),
Err(panic_err) => {
let err_message = if let Some(s) = panic_err.downcast_ref::<String>() {
(*s).clone()
} else {
panic!("test bug!() failed")
};
assert_eq!(err_message, "Internal error, please report a bug to us. The error message is: an error msg with string format msg");
}
}
}
}
2 changes: 1 addition & 1 deletion kclvm/compiler_base/span/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "span"
name = "compiler_base_span"
version = "0.1.0"
edition = "2021"

Expand Down

0 comments on commit 7e2e0ff

Please sign in to comment.