Skip to content
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

Merged
merged 3 commits into from
Dec 6, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libcore/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,9 @@ mod builtin {

/// Unconditionally causes compilation to fail with the given error message when encountered.
///
/// For more information, see the [RFC].
/// For more information, see the documentation for [`std::compile_error!`].
///
/// [RFC]: https://github.com/rust-lang/rfcs/blob/master/text/1695-add-error-macro.md
/// [`std::compile_error!`]: ../std/macro.compile_error.html
#[stable(feature = "compile_error_macro", since = "1.20.0")]
#[macro_export]
#[cfg(dox)]
Expand Down
21 changes: 19 additions & 2 deletions src/libstd/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

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.

///
/// ```
/// macro_rules! give_me_foo_or_bar {
/// (foo) => {};
/// (bar) => {};
/// ($x:ident) => {
/// compile_error!("This macro only accepts `foo` or `bar`");
/// }
/// }
/// ```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is currently giving an unused_macros warning. Please do use the macro e.g.

give_me_foo_or_bar!(neither);
// ^ will fail at compile time with message "This macro only accepts `foo` or `bar`"

///
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 */ }) }
Expand Down