Use constants for error message strings

This commit is contained in:
Richard Feldman 2024-06-29 21:15:23 -04:00
parent 99eb5bfe1e
commit 863c97330d
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B

View file

@ -91,6 +91,12 @@ pub fn error_and_exit(args: fmt::Arguments) -> ! {
}
}
pub const INTERNAL_ERROR_MESSAGE: &'static str = concat!(
"An internal compiler expectation was broken.\n",
"This is definitely a compiler bug.\n",
"Please file an issue here: <https://github.com/roc-lang/roc/issues/new/choose>\n",
);
/// `internal_error!` should be used whenever a compiler invariant is broken.
/// It is a wrapper around panic that tells the user to file a bug.
/// This should only be used in cases where there would be a compiler bug and the user can't fix it.
@ -99,49 +105,35 @@ pub fn error_and_exit(args: fmt::Arguments) -> ! {
#[macro_export]
macro_rules! internal_error {
() => ({
$crate::error_and_exit(format_args!(
concat!(
"An internal compiler expectation was broken.\n",
"This is definitely a compiler bug.\n",
"Please file an issue here: <https://github.com/roc-lang/roc/issues/new/choose>"
)
))
$crate::error_and_exit(format_args!("{}", $crate::INTERNAL_ERROR_MESSAGE))
});
($($arg:tt)*) => ({
$crate::error_and_exit(format_args!(
concat!(
"An internal compiler expectation was broken.\n",
"This is definitely a compiler bug.\n",
"Please file an issue here: <https://github.com/roc-lang/roc/issues/new/choose>\n",
"{}"
),
"{}{}",
$crate::INTERNAL_ERROR_MESSAGE,
format_args!($($arg)*)
))
})
}
pub const USER_ERROR_MESSAGE: &'static str = concat!(
"We ran into an issue while compiling your code.\n",
"Sadly, we don't have a pretty error message for this case yet.\n",
"If you can't figure out the problem from the context below, please reach out at <https://roc.zulipchat.com>\n",
);
/// `user_error!` should only ever be used temporarily.
/// It is a way to document locations where we do not yet have nice error reporting.
/// All cases of `user_error!` should eventually be replaced with pretty error printing using roc_reporting.
#[macro_export]
macro_rules! user_error {
() => ({
$crate::error_and_exit(format_args!(
concat!(
"We ran into an issue while compiling your code.\n",
"Sadly, we don't have a pretty error message for this case yet.\n",
"If you can't figure out the problem from the context below, please reach out at <https://roc.zulipchat.com>\n",
)
))
$crate::error_and_exit(format_args!("{}", $crate::USER_ERROR_MESSAGE))
});
($($arg:tt)*) => ({
$crate::error_and_exit(format_args!(
concat!(
"We ran into an issue while compiling your code.\n",
"Sadly, we don't have a pretty error message for this case yet.\n",
"If you can't figure out the problem from the context below, please reach out at <https://roc.zulipchat.com>\n",
"{}"
),
"{}{}",
$crate::USER_ERROR_MESSAGE,
format_args!($($arg)*)
))
})