-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(compiler-base): reuse kclvm bug macro. (#151)
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
Showing
7 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ edition = "2021" | |
|
||
[workspace] | ||
members = [ | ||
"macros", | ||
"span", | ||
"error", | ||
"3rdparty/rustc_errors", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[macro_use] | ||
pub mod bug; | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
|