Skip to content

Commit 44c343b

Browse files
committed
Give compile_error macro examples
1 parent a4fa23a commit 44c343b

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/libcore/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,9 @@ mod builtin {
596596

597597
/// Unconditionally causes compilation to fail with the given error message when encountered.
598598
///
599-
/// For more information, see the [RFC].
599+
/// For more information, see the documentation for [`std::compile_error!`].
600600
///
601-
/// [RFC]: https://github.com/rust-lang/rfcs/blob/master/text/1695-add-error-macro.md
601+
/// [`std::compile_error!`]: ../std/macro.compile_error.html
602602
#[stable(feature = "compile_error_macro", since = "1.20.0")]
603603
#[macro_export]
604604
#[cfg(dox)]

src/libstd/macros.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,26 @@ pub mod builtin {
282282

283283
/// Unconditionally causes compilation to fail with the given error message when encountered.
284284
///
285-
/// For more information, see the [RFC].
285+
/// This macro should be used when a crate uses a conditional compilation strategy to provide
286+
/// better error messages for errornous conditions.
286287
///
287-
/// [RFC]: https://github.com/rust-lang/rfcs/blob/master/text/1695-add-error-macro.md
288+
/// # Examples
289+
/// Two such examples are macros and `#[cfg]` environments.
290+
///
291+
/// ```
292+
/// macro_rules! give_me_foo_or_bar {
293+
/// (foo) => {};
294+
/// (bar) => {};
295+
/// ($x:ident) => {
296+
/// compile_error!("This macro only accepts `foo` or `bar`");
297+
/// }
298+
/// }
299+
/// ```
300+
///
301+
/// ```compile_fail
302+
/// #[cfg(not(any(feature = "foo", feature = "bar")))]
303+
/// compile_error!("Either feature \"foo\" or \"bar\" must be enabled for this crate.")
304+
/// ```
288305
#[stable(feature = "compile_error_macro", since = "1.20.0")]
289306
#[macro_export]
290307
macro_rules! compile_error { ($msg:expr) => ({ /* compiler built-in */ }) }

0 commit comments

Comments
 (0)