-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Give compile_error macro examples #46512
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -282,9 +282,26 @@ pub mod builtin { | |
|
||
/// Unconditionally causes compilation to fail with the given error message when encountered. | ||
/// | ||
/// For more information, see the [RFC]. | ||
/// This macro should be used when a crate uses a conditional compilation strategy to provide | ||
/// better error messages for errornous conditions. | ||
/// | ||
/// [RFC]: https://github.com/rust-lang/rfcs/blob/master/text/1695-add-error-macro.md | ||
/// # Examples | ||
/// Two such examples are macros and `#[cfg]` environments. | ||
/// | ||
/// ``` | ||
/// macro_rules! give_me_foo_or_bar { | ||
/// (foo) => {}; | ||
/// (bar) => {}; | ||
/// ($x:ident) => { | ||
/// compile_error!("This macro only accepts `foo` or `bar`"); | ||
/// } | ||
/// } | ||
/// ``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example is currently giving an give_me_foo_or_bar!(neither);
// ^ will fail at compile time with message "This macro only accepts `foo` or `bar`" |
||
/// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a sentence in here (no need for a big one!) so we don't just have two code blocks following each other. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
/// ```compile_fail | ||
/// #[cfg(not(any(feature = "foo", feature = "bar")))] | ||
/// compile_error!("Either feature \"foo\" or \"bar\" must be enabled for this crate.") | ||
/// ``` | ||
#[stable(feature = "compile_error_macro", since = "1.20.0")] | ||
#[macro_export] | ||
macro_rules! compile_error { ($msg:expr) => ({ /* compiler built-in */ }) } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add an empty line before this one.