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