]> git.lizzy.rs Git - rust.git/blob - src/libstd/macros.rs
Auto merge of #52712 - oli-obk:const_eval_cleanups, r=RalfJung
[rust.git] / src / libstd / macros.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Standard library macros
12 //!
13 //! This modules contains a set of macros which are exported from the standard
14 //! library. Each macro is available for use when linking against the standard
15 //! library.
16
17 /// The entry point for panic of Rust threads.
18 ///
19 /// This allows a program to to terminate immediately and provide feedback
20 /// to the caller of the program. `panic!` should be used when a program reaches
21 /// an unrecoverable problem.
22 ///
23 /// This macro is the perfect way to assert conditions in example code and in
24 /// tests.  `panic!` is closely tied with the `unwrap` method of both [`Option`]
25 /// and [`Result`][runwrap] enums.  Both implementations call `panic!` when they are set
26 /// to None or Err variants.
27 ///
28 /// This macro is used to inject panic into a Rust thread, causing the thread to
29 /// panic entirely. Each thread's panic can be reaped as the `Box<Any>` type,
30 /// and the single-argument form of the `panic!` macro will be the value which
31 /// is transmitted.
32 ///
33 /// [`Result`] enum is often a better solution for recovering from errors than
34 /// using the `panic!` macro.  This macro should be used to avoid proceeding using
35 /// incorrect values, such as from external sources.  Detailed information about
36 /// error handling is found in the [book].
37 ///
38 /// The multi-argument form of this macro panics with a string and has the
39 /// [`format!`] syntax for building a string.
40 ///
41 /// See also the macro [`compile_error!`], for raising errors during compilation.
42 ///
43 /// [runwrap]: ../std/result/enum.Result.html#method.unwrap
44 /// [`Option`]: ../std/option/enum.Option.html#method.unwrap
45 /// [`Result`]: ../std/result/enum.Result.html
46 /// [`format!`]: ../std/macro.format.html
47 /// [`compile_error!`]: ../std/macro.compile_error.html
48 /// [book]: ../book/second-edition/ch09-01-unrecoverable-errors-with-panic.html
49 ///
50 /// # Current implementation
51 ///
52 /// If the main thread panics it will terminate all your threads and end your
53 /// program with code `101`.
54 ///
55 /// # Examples
56 ///
57 /// ```should_panic
58 /// # #![allow(unreachable_code)]
59 /// panic!();
60 /// panic!("this is a terrible mistake!");
61 /// panic!(4); // panic with the value of 4 to be collected elsewhere
62 /// panic!("this is a {} {message}", "fancy", message = "message");
63 /// ```
64 #[macro_export]
65 #[stable(feature = "rust1", since = "1.0.0")]
66 #[allow_internal_unstable]
67 macro_rules! panic {
68     () => ({
69         panic!("explicit panic")
70     });
71     ($msg:expr) => ({
72         $crate::rt::begin_panic($msg, &(file!(), line!(), __rust_unstable_column!()))
73     });
74     ($msg:expr,) => ({
75         panic!($msg)
76     });
77     ($fmt:expr, $($arg:tt)+) => ({
78         $crate::rt::begin_panic_fmt(&format_args!($fmt, $($arg)+),
79                                     &(file!(), line!(), __rust_unstable_column!()))
80     });
81 }
82
83 /// Macro for printing to the standard output.
84 ///
85 /// Equivalent to the [`println!`] macro except that a newline is not printed at
86 /// the end of the message.
87 ///
88 /// Note that stdout is frequently line-buffered by default so it may be
89 /// necessary to use [`io::stdout().flush()`][flush] to ensure the output is emitted
90 /// immediately.
91 ///
92 /// Use `print!` only for the primary output of your program.  Use
93 /// [`eprint!`] instead to print error and progress messages.
94 ///
95 /// [`println!`]: ../std/macro.println.html
96 /// [flush]: ../std/io/trait.Write.html#tymethod.flush
97 /// [`eprint!`]: ../std/macro.eprint.html
98 ///
99 /// # Panics
100 ///
101 /// Panics if writing to `io::stdout()` fails.
102 ///
103 /// # Examples
104 ///
105 /// ```
106 /// use std::io::{self, Write};
107 ///
108 /// print!("this ");
109 /// print!("will ");
110 /// print!("be ");
111 /// print!("on ");
112 /// print!("the ");
113 /// print!("same ");
114 /// print!("line ");
115 ///
116 /// io::stdout().flush().unwrap();
117 ///
118 /// print!("this string has a newline, why not choose println! instead?\n");
119 ///
120 /// io::stdout().flush().unwrap();
121 /// ```
122 #[macro_export]
123 #[stable(feature = "rust1", since = "1.0.0")]
124 #[allow_internal_unstable]
125 macro_rules! print {
126     ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)));
127 }
128
129 /// Macro for printing to the standard output, with a newline.
130 ///
131 /// On all platforms, the newline is the LINE FEED character (`\n`/`U+000A`) alone
132 /// (no additional CARRIAGE RETURN (`\r`/`U+000D`).
133 ///
134 /// Use the [`format!`] syntax to write data to the standard output.
135 /// See [`std::fmt`] for more information.
136 ///
137 /// Use `println!` only for the primary output of your program.  Use
138 /// [`eprintln!`] instead to print error and progress messages.
139 ///
140 /// [`format!`]: ../std/macro.format.html
141 /// [`std::fmt`]: ../std/fmt/index.html
142 /// [`eprintln!`]: ../std/macro.eprint.html
143 /// # Panics
144 ///
145 /// Panics if writing to `io::stdout` fails.
146 ///
147 /// # Examples
148 ///
149 /// ```
150 /// println!(); // prints just a newline
151 /// println!("hello there!");
152 /// println!("format {} arguments", "some");
153 /// ```
154 #[macro_export]
155 #[stable(feature = "rust1", since = "1.0.0")]
156 #[allow_internal_unstable]
157 macro_rules! println {
158     () => (print!("\n"));
159     ($($arg:tt)*) => ({
160         $crate::io::_print(format_args_nl!($($arg)*));
161     })
162 }
163
164 /// Macro for printing to the standard error.
165 ///
166 /// Equivalent to the [`print!`] macro, except that output goes to
167 /// [`io::stderr`] instead of `io::stdout`.  See [`print!`] for
168 /// example usage.
169 ///
170 /// Use `eprint!` only for error and progress messages.  Use `print!`
171 /// instead for the primary output of your program.
172 ///
173 /// [`io::stderr`]: ../std/io/struct.Stderr.html
174 /// [`print!`]: ../std/macro.print.html
175 ///
176 /// # Panics
177 ///
178 /// Panics if writing to `io::stderr` fails.
179 ///
180 /// # Examples
181 ///
182 /// ```
183 /// eprint!("Error: Could not complete task");
184 /// ```
185 #[macro_export]
186 #[stable(feature = "eprint", since = "1.19.0")]
187 #[allow_internal_unstable]
188 macro_rules! eprint {
189     ($($arg:tt)*) => ($crate::io::_eprint(format_args!($($arg)*)));
190 }
191
192 /// Macro for printing to the standard error, with a newline.
193 ///
194 /// Equivalent to the [`println!`] macro, except that output goes to
195 /// [`io::stderr`] instead of `io::stdout`.  See [`println!`] for
196 /// example usage.
197 ///
198 /// Use `eprintln!` only for error and progress messages.  Use `println!`
199 /// instead for the primary output of your program.
200 ///
201 /// [`io::stderr`]: ../std/io/struct.Stderr.html
202 /// [`println!`]: ../std/macro.println.html
203 ///
204 /// # Panics
205 ///
206 /// Panics if writing to `io::stderr` fails.
207 ///
208 /// # Examples
209 ///
210 /// ```
211 /// eprintln!("Error: Could not complete task");
212 /// ```
213 #[macro_export]
214 #[stable(feature = "eprint", since = "1.19.0")]
215 #[allow_internal_unstable]
216 macro_rules! eprintln {
217     () => (eprint!("\n"));
218     ($($arg:tt)*) => ({
219         $crate::io::_eprint(format_args_nl!($($arg)*));
220     })
221 }
222
223 #[macro_export]
224 #[unstable(feature = "await_macro", issue = "50547")]
225 #[allow_internal_unstable]
226 #[allow_internal_unsafe]
227 macro_rules! await {
228     ($e:expr) => { {
229         let mut pinned = $e;
230         let mut pinned = unsafe { $crate::mem::PinMut::new_unchecked(&mut pinned) };
231         loop {
232             match $crate::future::poll_in_task_cx(&mut pinned) {
233                 // FIXME(cramertj) prior to stabilizing await, we have to ensure that this
234                 // can't be used to create a generator on stable via `|| await!()`.
235                 $crate::task::Poll::Pending => yield,
236                 $crate::task::Poll::Ready(x) => break x,
237             }
238         }
239     } }
240 }
241
242 /// A macro to select an event from a number of receivers.
243 ///
244 /// This macro is used to wait for the first event to occur on a number of
245 /// receivers. It places no restrictions on the types of receivers given to
246 /// this macro, this can be viewed as a heterogeneous select.
247 ///
248 /// # Examples
249 ///
250 /// ```
251 /// #![feature(mpsc_select)]
252 ///
253 /// use std::thread;
254 /// use std::sync::mpsc;
255 ///
256 /// // two placeholder functions for now
257 /// fn long_running_thread() {}
258 /// fn calculate_the_answer() -> u32 { 42 }
259 ///
260 /// let (tx1, rx1) = mpsc::channel();
261 /// let (tx2, rx2) = mpsc::channel();
262 ///
263 /// thread::spawn(move|| { long_running_thread(); tx1.send(()).unwrap(); });
264 /// thread::spawn(move|| { tx2.send(calculate_the_answer()).unwrap(); });
265 ///
266 /// select! {
267 ///     _ = rx1.recv() => println!("the long running thread finished first"),
268 ///     answer = rx2.recv() => {
269 ///         println!("the answer was: {}", answer.unwrap());
270 ///     }
271 /// }
272 /// # drop(rx1.recv());
273 /// # drop(rx2.recv());
274 /// ```
275 ///
276 /// For more information about select, see the `std::sync::mpsc::Select` structure.
277 #[macro_export]
278 #[unstable(feature = "mpsc_select", issue = "27800")]
279 macro_rules! select {
280     (
281         $($name:pat = $rx:ident.$meth:ident() => $code:expr),+
282     ) => ({
283         use $crate::sync::mpsc::Select;
284         let sel = Select::new();
285         $( let mut $rx = sel.handle(&$rx); )+
286         unsafe {
287             $( $rx.add(); )+
288         }
289         let ret = sel.wait();
290         $( if ret == $rx.id() { let $name = $rx.$meth(); $code } else )+
291         { unreachable!() }
292     })
293 }
294
295 #[cfg(test)]
296 macro_rules! assert_approx_eq {
297     ($a:expr, $b:expr) => ({
298         let (a, b) = (&$a, &$b);
299         assert!((*a - *b).abs() < 1.0e-6,
300                 "{} is not approximately equal to {}", *a, *b);
301     })
302 }
303
304 /// Built-in macros to the compiler itself.
305 ///
306 /// These macros do not have any corresponding definition with a `macro_rules!`
307 /// macro, but are documented here. Their implementations can be found hardcoded
308 /// into libsyntax itself.
309 #[cfg(dox)]
310 mod builtin {
311
312     /// Unconditionally causes compilation to fail with the given error message when encountered.
313     ///
314     /// This macro should be used when a crate uses a conditional compilation strategy to provide
315     /// better error messages for erroneous conditions. It's the compiler-level form of [`panic!`],
316     /// which emits an error at *runtime*, rather than during compilation.
317     ///
318     /// # Examples
319     ///
320     /// Two such examples are macros and `#[cfg]` environments.
321     ///
322     /// Emit better compiler error if a macro is passed invalid values. Without the final branch,
323     /// the compiler would still emit an error, but the error's message would not mention the two
324     /// valid values.
325     ///
326     /// ```compile_fail
327     /// macro_rules! give_me_foo_or_bar {
328     ///     (foo) => {};
329     ///     (bar) => {};
330     ///     ($x:ident) => {
331     ///         compile_error!("This macro only accepts `foo` or `bar`");
332     ///     }
333     /// }
334     ///
335     /// give_me_foo_or_bar!(neither);
336     /// // ^ will fail at compile time with message "This macro only accepts `foo` or `bar`"
337     /// ```
338     ///
339     /// Emit compiler error if one of a number of features isn't available.
340     ///
341     /// ```compile_fail
342     /// #[cfg(not(any(feature = "foo", feature = "bar")))]
343     /// compile_error!("Either feature \"foo\" or \"bar\" must be enabled for this crate.")
344     /// ```
345     ///
346     /// [`panic!`]: ../std/macro.panic.html
347     #[stable(feature = "compile_error_macro", since = "1.20.0")]
348     #[rustc_doc_only_macro]
349     macro_rules! compile_error {
350         ($msg:expr) => ({ /* compiler built-in */ });
351         ($msg:expr,) => ({ /* compiler built-in */ });
352     }
353
354     /// The core macro for formatted string creation & output.
355     ///
356     /// This macro functions by taking a formatting string literal containing
357     /// `{}` for each additional argument passed.  `format_args!` prepares the
358     /// additional parameters to ensure the output can be interpreted as a string
359     /// and canonicalizes the arguments into a single type.  Any value that implements
360     /// the [`Display`] trait can be passed to `format_args!`, as can any
361     /// [`Debug`] implementation be passed to a `{:?}` within the formatting string.
362     ///
363     /// This macro produces a value of type [`fmt::Arguments`]. This value can be
364     /// passed to the macros within [`std::fmt`] for performing useful redirection.
365     /// All other formatting macros ([`format!`], [`write!`], [`println!`], etc) are
366     /// proxied through this one.  `format_args!`, unlike its derived macros, avoids
367     /// heap allocations.
368     ///
369     /// You can use the [`fmt::Arguments`] value that `format_args!` returns
370     /// in `Debug` and `Display` contexts as seen below. The example also shows
371     /// that `Debug` and `Display` format to the same thing: the interpolated
372     /// format string in `format_args!`.
373     ///
374     /// ```rust
375     /// let debug = format!("{:?}", format_args!("{} foo {:?}", 1, 2));
376     /// let display = format!("{}", format_args!("{} foo {:?}", 1, 2));
377     /// assert_eq!("1 foo 2", display);
378     /// assert_eq!(display, debug);
379     /// ```
380     ///
381     /// For more information, see the documentation in [`std::fmt`].
382     ///
383     /// [`Display`]: ../std/fmt/trait.Display.html
384     /// [`Debug`]: ../std/fmt/trait.Debug.html
385     /// [`fmt::Arguments`]: ../std/fmt/struct.Arguments.html
386     /// [`std::fmt`]: ../std/fmt/index.html
387     /// [`format!`]: ../std/macro.format.html
388     /// [`write!`]: ../std/macro.write.html
389     /// [`println!`]: ../std/macro.println.html
390     ///
391     /// # Examples
392     ///
393     /// ```
394     /// use std::fmt;
395     ///
396     /// let s = fmt::format(format_args!("hello {}", "world"));
397     /// assert_eq!(s, format!("hello {}", "world"));
398     /// ```
399     #[stable(feature = "rust1", since = "1.0.0")]
400     #[rustc_doc_only_macro]
401     macro_rules! format_args {
402         ($fmt:expr) => ({ /* compiler built-in */ });
403         ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ });
404     }
405
406     /// Inspect an environment variable at compile time.
407     ///
408     /// This macro will expand to the value of the named environment variable at
409     /// compile time, yielding an expression of type `&'static str`.
410     ///
411     /// If the environment variable is not defined, then a compilation error
412     /// will be emitted. To not emit a compile error, use the [`option_env!`]
413     /// macro instead.
414     ///
415     /// [`option_env!`]: ../std/macro.option_env.html
416     ///
417     /// # Examples
418     ///
419     /// ```
420     /// let path: &'static str = env!("PATH");
421     /// println!("the $PATH variable at the time of compiling was: {}", path);
422     /// ```
423     ///
424     /// You can customize the error message by passing a string as the second
425     /// parameter:
426     ///
427     /// ```compile_fail
428     /// let doc: &'static str = env!("documentation", "what's that?!");
429     /// ```
430     ///
431     /// If the `documentation` environment variable is not defined, you'll get
432     /// the following error:
433     ///
434     /// ```text
435     /// error: what's that?!
436     /// ```
437     #[stable(feature = "rust1", since = "1.0.0")]
438     #[rustc_doc_only_macro]
439     macro_rules! env {
440         ($name:expr) => ({ /* compiler built-in */ });
441         ($name:expr,) => ({ /* compiler built-in */ });
442     }
443
444     /// Optionally inspect an environment variable at compile time.
445     ///
446     /// If the named environment variable is present at compile time, this will
447     /// expand into an expression of type `Option<&'static str>` whose value is
448     /// `Some` of the value of the environment variable. If the environment
449     /// variable is not present, then this will expand to `None`.  See
450     /// [`Option<T>`][option] for more information on this type.
451     ///
452     /// A compile time error is never emitted when using this macro regardless
453     /// of whether the environment variable is present or not.
454     ///
455     /// [option]: ../std/option/enum.Option.html
456     ///
457     /// # Examples
458     ///
459     /// ```
460     /// let key: Option<&'static str> = option_env!("SECRET_KEY");
461     /// println!("the secret key might be: {:?}", key);
462     /// ```
463     #[stable(feature = "rust1", since = "1.0.0")]
464     #[rustc_doc_only_macro]
465     macro_rules! option_env {
466         ($name:expr) => ({ /* compiler built-in */ });
467         ($name:expr,) => ({ /* compiler built-in */ });
468     }
469
470     /// Concatenate identifiers into one identifier.
471     ///
472     /// This macro takes any number of comma-separated identifiers, and
473     /// concatenates them all into one, yielding an expression which is a new
474     /// identifier. Note that hygiene makes it such that this macro cannot
475     /// capture local variables. Also, as a general rule, macros are only
476     /// allowed in item, statement or expression position. That means while
477     /// you may use this macro for referring to existing variables, functions or
478     /// modules etc, you cannot define a new one with it.
479     ///
480     /// # Examples
481     ///
482     /// ```
483     /// #![feature(concat_idents)]
484     ///
485     /// # fn main() {
486     /// fn foobar() -> u32 { 23 }
487     ///
488     /// let f = concat_idents!(foo, bar);
489     /// println!("{}", f());
490     ///
491     /// // fn concat_idents!(new, fun, name) { } // not usable in this way!
492     /// # }
493     /// ```
494     #[unstable(feature = "concat_idents_macro", issue = "29599")]
495     #[rustc_doc_only_macro]
496     macro_rules! concat_idents {
497         ($($e:ident),+) => ({ /* compiler built-in */ });
498         ($($e:ident,)+) => ({ /* compiler built-in */ });
499     }
500
501     /// Concatenates literals into a static string slice.
502     ///
503     /// This macro takes any number of comma-separated literals, yielding an
504     /// expression of type `&'static str` which represents all of the literals
505     /// concatenated left-to-right.
506     ///
507     /// Integer and floating point literals are stringified in order to be
508     /// concatenated.
509     ///
510     /// # Examples
511     ///
512     /// ```
513     /// let s = concat!("test", 10, 'b', true);
514     /// assert_eq!(s, "test10btrue");
515     /// ```
516     #[stable(feature = "rust1", since = "1.0.0")]
517     #[rustc_doc_only_macro]
518     macro_rules! concat {
519         ($($e:expr),*) => ({ /* compiler built-in */ });
520         ($($e:expr,)*) => ({ /* compiler built-in */ });
521     }
522
523     /// A macro which expands to the line number on which it was invoked.
524     ///
525     /// With [`column!`] and [`file!`], these macros provide debugging information for
526     /// developers about the location within the source.
527     ///
528     /// The expanded expression has type `u32` and is 1-based, so the first line
529     /// in each file evaluates to 1, the second to 2, etc. This is consistent
530     /// with error messages by common compilers or popular editors.
531     /// The returned line is *not necessarily* the line of the `line!` invocation itself,
532     /// but rather the first macro invocation leading up to the invocation
533     /// of the `line!` macro.
534     ///
535     /// [`column!`]: macro.column.html
536     /// [`file!`]: macro.file.html
537     ///
538     /// # Examples
539     ///
540     /// ```
541     /// let current_line = line!();
542     /// println!("defined on line: {}", current_line);
543     /// ```
544     #[stable(feature = "rust1", since = "1.0.0")]
545     #[rustc_doc_only_macro]
546     macro_rules! line { () => ({ /* compiler built-in */ }) }
547
548     /// A macro which expands to the column number on which it was invoked.
549     ///
550     /// With [`line!`] and [`file!`], these macros provide debugging information for
551     /// developers about the location within the source.
552     ///
553     /// The expanded expression has type `u32` and is 1-based, so the first column
554     /// in each line evaluates to 1, the second to 2, etc. This is consistent
555     /// with error messages by common compilers or popular editors.
556     /// The returned column is *not necessarily* the line of the `column!` invocation itself,
557     /// but rather the first macro invocation leading up to the invocation
558     /// of the `column!` macro.
559     ///
560     /// [`line!`]: macro.line.html
561     /// [`file!`]: macro.file.html
562     ///
563     /// # Examples
564     ///
565     /// ```
566     /// let current_col = column!();
567     /// println!("defined on column: {}", current_col);
568     /// ```
569     #[stable(feature = "rust1", since = "1.0.0")]
570     #[rustc_doc_only_macro]
571     macro_rules! column { () => ({ /* compiler built-in */ }) }
572
573     /// A macro which expands to the file name from which it was invoked.
574     ///
575     /// With [`line!`] and [`column!`], these macros provide debugging information for
576     /// developers about the location within the source.
577     ///
578     ///
579     /// The expanded expression has type `&'static str`, and the returned file
580     /// is not the invocation of the `file!` macro itself, but rather the
581     /// first macro invocation leading up to the invocation of the `file!`
582     /// macro.
583     ///
584     /// [`line!`]: macro.line.html
585     /// [`column!`]: macro.column.html
586     ///
587     /// # Examples
588     ///
589     /// ```
590     /// let this_file = file!();
591     /// println!("defined in file: {}", this_file);
592     /// ```
593     #[stable(feature = "rust1", since = "1.0.0")]
594     #[rustc_doc_only_macro]
595     macro_rules! file { () => ({ /* compiler built-in */ }) }
596
597     /// A macro which stringifies its arguments.
598     ///
599     /// This macro will yield an expression of type `&'static str` which is the
600     /// stringification of all the tokens passed to the macro. No restrictions
601     /// are placed on the syntax of the macro invocation itself.
602     ///
603     /// Note that the expanded results of the input tokens may change in the
604     /// future. You should be careful if you rely on the output.
605     ///
606     /// # Examples
607     ///
608     /// ```
609     /// let one_plus_one = stringify!(1 + 1);
610     /// assert_eq!(one_plus_one, "1 + 1");
611     /// ```
612     #[stable(feature = "rust1", since = "1.0.0")]
613     #[rustc_doc_only_macro]
614     macro_rules! stringify { ($($t:tt)*) => ({ /* compiler built-in */ }) }
615
616     /// Includes a utf8-encoded file as a string.
617     ///
618     /// The file is located relative to the current file. (similarly to how
619     /// modules are found)
620     ///
621     /// This macro will yield an expression of type `&'static str` which is the
622     /// contents of the file.
623     ///
624     /// # Examples
625     ///
626     /// Assume there are two files in the same directory with the following
627     /// contents:
628     ///
629     /// File 'spanish.in':
630     ///
631     /// ```text
632     /// adiรณs
633     /// ```
634     ///
635     /// File 'main.rs':
636     ///
637     /// ```ignore (cannot-doctest-external-file-dependency)
638     /// fn main() {
639     ///     let my_str = include_str!("spanish.in");
640     ///     assert_eq!(my_str, "adiรณs\n");
641     ///     print!("{}", my_str);
642     /// }
643     /// ```
644     ///
645     /// Compiling 'main.rs' and running the resulting binary will print "adiรณs".
646     #[stable(feature = "rust1", since = "1.0.0")]
647     #[rustc_doc_only_macro]
648     macro_rules! include_str {
649         ($file:expr) => ({ /* compiler built-in */ });
650         ($file:expr,) => ({ /* compiler built-in */ });
651     }
652
653     /// Includes a file as a reference to a byte array.
654     ///
655     /// The file is located relative to the current file. (similarly to how
656     /// modules are found)
657     ///
658     /// This macro will yield an expression of type `&'static [u8; N]` which is
659     /// the contents of the file.
660     ///
661     /// # Examples
662     ///
663     /// Assume there are two files in the same directory with the following
664     /// contents:
665     ///
666     /// File 'spanish.in':
667     ///
668     /// ```text
669     /// adiรณs
670     /// ```
671     ///
672     /// File 'main.rs':
673     ///
674     /// ```ignore (cannot-doctest-external-file-dependency)
675     /// fn main() {
676     ///     let bytes = include_bytes!("spanish.in");
677     ///     assert_eq!(bytes, b"adi\xc3\xb3s\n");
678     ///     print!("{}", String::from_utf8_lossy(bytes));
679     /// }
680     /// ```
681     ///
682     /// Compiling 'main.rs' and running the resulting binary will print "adiรณs".
683     #[stable(feature = "rust1", since = "1.0.0")]
684     #[rustc_doc_only_macro]
685     macro_rules! include_bytes {
686         ($file:expr) => ({ /* compiler built-in */ });
687         ($file:expr,) => ({ /* compiler built-in */ });
688     }
689
690     /// Expands to a string that represents the current module path.
691     ///
692     /// The current module path can be thought of as the hierarchy of modules
693     /// leading back up to the crate root. The first component of the path
694     /// returned is the name of the crate currently being compiled.
695     ///
696     /// # Examples
697     ///
698     /// ```
699     /// mod test {
700     ///     pub fn foo() {
701     ///         assert!(module_path!().ends_with("test"));
702     ///     }
703     /// }
704     ///
705     /// test::foo();
706     /// ```
707     #[stable(feature = "rust1", since = "1.0.0")]
708     #[rustc_doc_only_macro]
709     macro_rules! module_path { () => ({ /* compiler built-in */ }) }
710
711     /// Boolean evaluation of configuration flags, at compile-time.
712     ///
713     /// In addition to the `#[cfg]` attribute, this macro is provided to allow
714     /// boolean expression evaluation of configuration flags. This frequently
715     /// leads to less duplicated code.
716     ///
717     /// The syntax given to this macro is the same syntax as [the `cfg`
718     /// attribute](../book/first-edition/conditional-compilation.html).
719     ///
720     /// # Examples
721     ///
722     /// ```
723     /// let my_directory = if cfg!(windows) {
724     ///     "windows-specific-directory"
725     /// } else {
726     ///     "unix-directory"
727     /// };
728     /// ```
729     #[stable(feature = "rust1", since = "1.0.0")]
730     #[rustc_doc_only_macro]
731     macro_rules! cfg { ($($cfg:tt)*) => ({ /* compiler built-in */ }) }
732
733     /// Parse a file as an expression or an item according to the context.
734     ///
735     /// The file is located relative to the current file (similarly to how
736     /// modules are found).
737     ///
738     /// Using this macro is often a bad idea, because if the file is
739     /// parsed as an expression, it is going to be placed in the
740     /// surrounding code unhygienically. This could result in variables
741     /// or functions being different from what the file expected if
742     /// there are variables or functions that have the same name in
743     /// the current file.
744     ///
745     /// # Examples
746     ///
747     /// Assume there are two files in the same directory with the following
748     /// contents:
749     ///
750     /// File 'monkeys.in':
751     ///
752     /// ```ignore (only-for-syntax-highlight)
753     /// ['๐Ÿ™ˆ', '๐Ÿ™Š', '๐Ÿ™‰']
754     ///     .iter()
755     ///     .cycle()
756     ///     .take(6)
757     ///     .collect::<String>()
758     /// ```
759     ///
760     /// File 'main.rs':
761     ///
762     /// ```ignore (cannot-doctest-external-file-dependency)
763     /// fn main() {
764     ///     let my_string = include!("monkeys.in");
765     ///     assert_eq!("๐Ÿ™ˆ๐Ÿ™Š๐Ÿ™‰๐Ÿ™ˆ๐Ÿ™Š๐Ÿ™‰", my_string);
766     ///     println!("{}", my_string);
767     /// }
768     /// ```
769     ///
770     /// Compiling 'main.rs' and running the resulting binary will print
771     /// "๐Ÿ™ˆ๐Ÿ™Š๐Ÿ™‰๐Ÿ™ˆ๐Ÿ™Š๐Ÿ™‰".
772     #[stable(feature = "rust1", since = "1.0.0")]
773     #[rustc_doc_only_macro]
774     macro_rules! include {
775         ($file:expr) => ({ /* compiler built-in */ });
776         ($file:expr,) => ({ /* compiler built-in */ });
777     }
778
779     /// Ensure that a boolean expression is `true` at runtime.
780     ///
781     /// This will invoke the [`panic!`] macro if the provided expression cannot be
782     /// evaluated to `true` at runtime.
783     ///
784     /// # Uses
785     ///
786     /// Assertions are always checked in both debug and release builds, and cannot
787     /// be disabled. See [`debug_assert!`] for assertions that are not enabled in
788     /// release builds by default.
789     ///
790     /// Unsafe code relies on `assert!` to enforce run-time invariants that, if
791     /// violated could lead to unsafety.
792     ///
793     /// Other use-cases of `assert!` include [testing] and enforcing run-time
794     /// invariants in safe code (whose violation cannot result in unsafety).
795     ///
796     /// # Custom Messages
797     ///
798     /// This macro has a second form, where a custom panic message can
799     /// be provided with or without arguments for formatting.  See [`std::fmt`]
800     /// for syntax for this form.
801     ///
802     /// [`panic!`]: macro.panic.html
803     /// [`debug_assert!`]: macro.debug_assert.html
804     /// [testing]: ../book/second-edition/ch11-01-writing-tests.html#checking-results-with-the-assert-macro
805     /// [`std::fmt`]: ../std/fmt/index.html
806     ///
807     /// # Examples
808     ///
809     /// ```
810     /// // the panic message for these assertions is the stringified value of the
811     /// // expression given.
812     /// assert!(true);
813     ///
814     /// fn some_computation() -> bool { true } // a very simple function
815     ///
816     /// assert!(some_computation());
817     ///
818     /// // assert with a custom message
819     /// let x = true;
820     /// assert!(x, "x wasn't true!");
821     ///
822     /// let a = 3; let b = 27;
823     /// assert!(a + b == 30, "a = {}, b = {}", a, b);
824     /// ```
825     #[stable(feature = "rust1", since = "1.0.0")]
826     #[rustc_doc_only_macro]
827     macro_rules! assert {
828         ($cond:expr) => ({ /* compiler built-in */ });
829         ($cond:expr,) => ({ /* compiler built-in */ });
830         ($cond:expr, $($arg:tt)+) => ({ /* compiler built-in */ });
831     }
832 }
833
834 /// A macro for defining `#[cfg]` if-else statements.
835 ///
836 /// This is similar to the `if/elif` C preprocessor macro by allowing definition
837 /// of a cascade of `#[cfg]` cases, emitting the implementation which matches
838 /// first.
839 ///
840 /// This allows you to conveniently provide a long list `#[cfg]`'d blocks of code
841 /// without having to rewrite each clause multiple times.
842 macro_rules! cfg_if {
843     ($(
844         if #[cfg($($meta:meta),*)] { $($it:item)* }
845     ) else * else {
846         $($it2:item)*
847     }) => {
848         __cfg_if_items! {
849             () ;
850             $( ( ($($meta),*) ($($it)*) ), )*
851             ( () ($($it2)*) ),
852         }
853     }
854 }
855
856 macro_rules! __cfg_if_items {
857     (($($not:meta,)*) ; ) => {};
858     (($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => {
859         __cfg_if_apply! { cfg(all(not(any($($not),*)), $($m,)*)), $($it)* }
860         __cfg_if_items! { ($($not,)* $($m,)*) ; $($rest)* }
861     }
862 }
863
864 macro_rules! __cfg_if_apply {
865     ($m:meta, $($it:item)*) => {
866         $(#[$m] $it)*
867     }
868 }