Skip to content

Commit bb239e2

Browse files
authored
Rollup merge of #46512 - Havvy:doc-compile_fail, r=kennytm
Give compile_error macro examples I cannot get Rust to build at all with it complaining about GCC not being a valid C compiler or something, so letting TravisCI be my tester... Fixes #46171
2 parents 872c025 + aaaea2c commit bb239e2

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/libcore/macros.rs

+2-2
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

+27-2
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,34 @@ 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+
///
290+
/// Two such examples are macros and `#[cfg]` environments.
291+
///
292+
/// Emit better compiler error if a macro is passed invalid values.
293+
///
294+
/// ```compile_fail
295+
/// macro_rules! give_me_foo_or_bar {
296+
/// (foo) => {};
297+
/// (bar) => {};
298+
/// ($x:ident) => {
299+
/// compile_error!("This macro only accepts `foo` or `bar`");
300+
/// }
301+
/// }
302+
///
303+
/// give_me_foo_or_bar!(neither);
304+
/// // ^ will fail at compile time with message "This macro only accepts `foo` or `bar`"
305+
/// ```
306+
///
307+
/// Emit compiler error if one of a number of features isn't available.
308+
///
309+
/// ```compile_fail
310+
/// #[cfg(not(any(feature = "foo", feature = "bar")))]
311+
/// compile_error!("Either feature \"foo\" or \"bar\" must be enabled for this crate.")
312+
/// ```
288313
#[stable(feature = "compile_error_macro", since = "1.20.0")]
289314
#[macro_export]
290315
macro_rules! compile_error { ($msg:expr) => ({ /* compiler built-in */ }) }

0 commit comments

Comments
 (0)