macros.rs 21.3 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
10 11 12 13 14 15 16

//! Standard library macros
//!
//! This modules contains a set of macros which are exported from the standard
//! library. Each macro is available for use when linking against the standard
//! library.

17
#![experimental]
18
#![macro_escape]
19

S
Steve Klabnik 已提交
20
/// The entry point for panic of Rust tasks.
21
///
S
Steve Klabnik 已提交
22 23 24
/// This macro is used to inject panic into a Rust task, causing the task to
/// unwind and panic entirely. Each task's panic can be reaped as the
/// `Box<Any>` type, and the single-argument form of the `panic!` macro will be
25
/// the value which is transmitted.
26
///
S
Steve Klabnik 已提交
27
/// The multi-argument form of this macro panics with a string and has the
J
Joseph Crail 已提交
28
/// `format!` syntax for building a string.
29 30 31 32
///
/// # Example
///
/// ```should_fail
33
/// # #![allow(unreachable_code)]
S
Steve Klabnik 已提交
34 35 36 37
/// panic!();
/// panic!("this is a terrible mistake!");
/// panic!(4i); // panic with the value of 4 to be collected elsewhere
/// panic!("this is a {} {message}", "fancy", message = "message");
38
/// ```
39
#[macro_export]
40
macro_rules! panic {
41
    () => ({
S
Steve Klabnik 已提交
42
        panic!("explicit panic")
43 44 45
    });
    ($msg:expr) => ({
        // static requires less code at runtime, more constant data
46 47
        static _FILE_LINE: (&'static str, uint) = (file!(), line!());
        ::std::rt::begin_unwind($msg, &_FILE_LINE)
48 49 50 51 52 53 54 55 56 57 58 59
    });
    ($fmt:expr, $($arg:tt)*) => ({
        // a closure can't have return type !, so we need a full
        // function to pass to format_args!, *and* we need the
        // file and line numbers right here; so an inner bare fn
        // is our only choice.
        //
        // LLVM doesn't tend to inline this, presumably because begin_unwind_fmt
        // is #[cold] and #[inline(never)] and because this is flagged as cold
        // as returning !. We really do want this to be inlined, however,
        // because it's just a tiny wrapper. Small wins (156K to 149K in size)
        // were seen when forcing this to be inlined, and that number just goes
S
Steve Klabnik 已提交
60
        // up with the number of calls to panic!()
61 62 63 64 65
        //
        // The leading _'s are to avoid dead code warnings if this is
        // used inside a dead function. Just `#[allow(dead_code)]` is
        // insufficient, since the user may have
        // `#[forbid(dead_code)]` and which cannot be overridden.
66
        #[inline(always)]
67 68 69
        fn _run_fmt(fmt: &::std::fmt::Arguments) -> ! {
            static _FILE_LINE: (&'static str, uint) = (file!(), line!());
            ::std::rt::begin_unwind_fmt(fmt, &_FILE_LINE)
70
        }
71
        format_args!(_run_fmt, $fmt, $($arg)*)
72
    });
73
}
74

75 76
/// Ensure that a boolean expression is `true` at runtime.
///
S
Steve Klabnik 已提交
77
/// This will invoke the `panic!` macro if the provided expression cannot be
78 79 80 81 82
/// evaluated to `true` at runtime.
///
/// # Example
///
/// ```
S
Steve Klabnik 已提交
83
/// // the panic message for these assertions is the stringified value of the
84 85 86 87 88 89 90 91
/// // expression given.
/// assert!(true);
/// # fn some_computation() -> bool { true }
/// assert!(some_computation());
///
/// // assert with a custom message
/// # let x = true;
/// assert!(x, "x wasn't true!");
92
/// # let a = 3i; let b = 27i;
93 94
/// assert!(a + b == 30, "a = {}, b = {}", a, b);
/// ```
95
#[macro_export]
96
macro_rules! assert {
97
    ($cond:expr) => (
98
        if !$cond {
S
Steve Klabnik 已提交
99
            panic!(concat!("assertion failed: ", stringify!($cond)))
100
        }
101 102
    );
    ($cond:expr, $($arg:expr),+) => (
103
        if !$cond {
S
Steve Klabnik 已提交
104
            panic!($($arg),+)
105
        }
106
    );
107
}
108

109 110 111
/// Asserts that two expressions are equal to each other, testing equality in
/// both directions.
///
S
Steve Klabnik 已提交
112
/// On panic, this macro will print the values of the expressions.
113 114 115 116
///
/// # Example
///
/// ```
117 118
/// let a = 3i;
/// let b = 1i + 2i;
119 120
/// assert_eq!(a, b);
/// ```
121
#[macro_export]
122
macro_rules! assert_eq {
123 124 125
    ($left:expr , $right:expr) => ({
        match (&($left), &($right)) {
            (left_val, right_val) => {
126
                // check both directions of equality....
127 128
                if !((*left_val == *right_val) &&
                     (*right_val == *left_val)) {
S
Steve Klabnik 已提交
129
                    panic!("assertion failed: `(left == right) && (right == left)` \
130
                           (left: `{}`, right: `{}`)", *left_val, *right_val)
131 132
                }
            }
133
        }
134
    })
135
}
136

137 138
/// Ensure that a boolean expression is `true` at runtime.
///
S
Steve Klabnik 已提交
139
/// This will invoke the `panic!` macro if the provided expression cannot be
140 141 142 143 144 145 146 147 148 149
/// evaluated to `true` at runtime.
///
/// Unlike `assert!`, `debug_assert!` statements can be disabled by passing
/// `--cfg ndebug` to the compiler. This makes `debug_assert!` useful for
/// checks that are too expensive to be present in a release build but may be
/// helpful during development.
///
/// # Example
///
/// ```
S
Steve Klabnik 已提交
150
/// // the panic message for these assertions is the stringified value of the
151 152 153 154 155 156 157 158
/// // expression given.
/// debug_assert!(true);
/// # fn some_expensive_computation() -> bool { true }
/// debug_assert!(some_expensive_computation());
///
/// // assert with a custom message
/// # let x = true;
/// debug_assert!(x, "x wasn't true!");
159
/// # let a = 3i; let b = 27i;
160 161 162
/// debug_assert!(a + b == 30, "a = {}, b = {}", a, b);
/// ```
#[macro_export]
163
macro_rules! debug_assert {
164
    ($($arg:tt)*) => (if cfg!(not(ndebug)) { assert!($($arg)*); })
165
}
166 167 168 169

/// Asserts that two expressions are equal to each other, testing equality in
/// both directions.
///
S
Steve Klabnik 已提交
170
/// On panic, this macro will print the values of the expressions.
171 172 173 174 175 176 177 178 179
///
/// Unlike `assert_eq!`, `debug_assert_eq!` statements can be disabled by
/// passing `--cfg ndebug` to the compiler. This makes `debug_assert_eq!`
/// useful for checks that are too expensive to be present in a release build
/// but may be helpful during development.
///
/// # Example
///
/// ```
180 181
/// let a = 3i;
/// let b = 1i + 2i;
182 183 184
/// debug_assert_eq!(a, b);
/// ```
#[macro_export]
185
macro_rules! debug_assert_eq {
186
    ($($arg:tt)*) => (if cfg!(not(ndebug)) { assert_eq!($($arg)*); })
187
}
188

189
/// A utility macro for indicating unreachable code.
190
///
191 192 193 194 195 196 197 198 199 200 201 202 203 204
/// This is useful any time that the compiler can't determine that some code is unreachable. For
/// example:
///
/// * Match arms with guard conditions.
/// * Loops that dynamically terminate.
/// * Iterators that dynamically terminate.
///
/// # Panics
///
/// This will always panic.
///
/// # Examples
///
/// Match arms:
205
///
206 207
/// ```rust
/// fn foo(x: Option<int>) {
A
Alex Crichton 已提交
208 209 210 211 212 213
///     match x {
///         Some(n) if n >= 0 => println!("Some(Non-negative)"),
///         Some(n) if n <  0 => println!("Some(Negative)"),
///         Some(_)           => unreachable!(), // compile error if commented out
///         None              => println!("None")
///     }
214 215 216 217 218 219
/// }
/// ```
///
/// Iterators:
///
/// ```rust
O
olivren 已提交
220 221 222 223
/// fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3
///     for i in std::iter::count(0_u32, 1) {
///         if 3*i < i { panic!("u32 overflow"); }
///         if x < 3*i { return i-1; }
224 225 226
///     }
///     unreachable!();
/// }
J
Jonas Hietala 已提交
227
/// ```
228
#[macro_export]
229
macro_rules! unreachable {
230 231 232 233 234 235 236 237 238
    () => ({
        panic!("internal error: entered unreachable code")
    });
    ($msg:expr) => ({
        unreachable!("{}", $msg)
    });
    ($fmt:expr, $($arg:tt)*) => ({
        panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
    });
239
}
240

S
Steve Klabnik 已提交
241
/// A standardised placeholder for marking unfinished code. It panics with the
B
Brendan Zabarauskas 已提交
242 243
/// message `"not yet implemented"` when executed.
#[macro_export]
244
macro_rules! unimplemented {
S
Steve Klabnik 已提交
245
    () => (panic!("not yet implemented"))
246
}
B
Brendan Zabarauskas 已提交
247

F
fort 已提交
248
/// Use the syntax described in `std::fmt` to create a value of type `String`.
249 250 251 252 253 254 255
/// See `std::fmt` for more information.
///
/// # Example
///
/// ```
/// format!("test");
/// format!("hello {}", "world!");
256
/// format!("x = {}, y = {y}", 10i, y = 30i);
257
/// ```
258
#[macro_export]
A
Alex Crichton 已提交
259
#[stable]
260
macro_rules! format {
261 262 263
    ($($arg:tt)*) => (
        format_args!(::std::fmt::format, $($arg)*)
    )
264
}
265

266 267 268 269 270 271
/// Use the `format!` syntax to write data into a buffer of type `&mut Writer`.
/// See `std::fmt` for more information.
///
/// # Example
///
/// ```
272
/// # #![allow(unused_must_use)]
273
///
D
Daniel Micay 已提交
274
/// let mut w = Vec::new();
275 276 277
/// write!(&mut w, "test");
/// write!(&mut w, "formatted {}", "arguments");
/// ```
278
#[macro_export]
A
Alex Crichton 已提交
279
#[stable]
280
macro_rules! write {
281
    ($dst:expr, $($arg:tt)*) => ({
A
Alex Crichton 已提交
282 283
        let dst = &mut *$dst;
        format_args!(|args| { dst.write_fmt(args) }, $($arg)*)
284
    })
285
}
286

287 288
/// Equivalent to the `write!` macro, except that a newline is appended after
/// the message is written.
289
#[macro_export]
A
Alex Crichton 已提交
290
#[stable]
291
macro_rules! writeln {
A
Alex Crichton 已提交
292 293 294
    ($dst:expr, $fmt:expr $($arg:tt)*) => (
        write!($dst, concat!($fmt, "\n") $($arg)*)
    )
295
}
296

297 298
/// Equivalent to the `println!` macro except that a newline is not printed at
/// the end of the message.
299
#[macro_export]
A
Alex Crichton 已提交
300
#[stable]
301
macro_rules! print {
302
    ($($arg:tt)*) => (format_args!(::std::io::stdio::print_args, $($arg)*))
303
}
304

305 306 307 308 309 310 311 312 313 314 315 316
/// Macro for printing to a task's stdout handle.
///
/// Each task can override its stdout handle via `std::io::stdio::set_stdout`.
/// The syntax of this macro is the same as that used for `format!`. For more
/// information, see `std::fmt` and `std::io::stdio`.
///
/// # Example
///
/// ```
/// println!("hello there!");
/// println!("format {} arguments", "some");
/// ```
317
#[macro_export]
A
Alex Crichton 已提交
318
#[stable]
319
macro_rules! println {
320
    ($($arg:tt)*) => (format_args!(::std::io::stdio::println_args, $($arg)*))
321
}
322

323 324 325
/// Helper macro for unwrapping `Result` values while returning early with an
/// error if the value of the expression is `Err`. For more information, see
/// `std::io`.
A
Alex Crichton 已提交
326
#[macro_export]
327
macro_rules! try {
328 329 330 331 332 333
    ($expr:expr) => ({
        match $expr {
            Ok(val) => val,
            Err(err) => return Err(::std::error::FromError::from_error(err))
        }
    })
334
}
335

D
Daniel Micay 已提交
336
/// Create a `std::vec::Vec` containing the arguments.
337
#[macro_export]
338
macro_rules! vec {
339
    ($($x:expr),*) => ({
A
Alex Crichton 已提交
340
        use std::slice::BoxedSliceExt;
341 342
        let xs: ::std::boxed::Box<[_]> = box [$($x),*];
        xs.into_vec()
343
    });
344
    ($($x:expr,)*) => (vec![$($x),*])
345
}
A
Alex Crichton 已提交
346

347
/// A macro to select an event from a number of receivers.
A
Alex Crichton 已提交
348 349
///
/// This macro is used to wait for the first event to occur on a number of
350 351
/// receivers. It places no restrictions on the types of receivers given to
/// this macro, this can be viewed as a heterogeneous select.
A
Alex Crichton 已提交
352 353 354 355
///
/// # Example
///
/// ```
356 357
/// use std::thread::Thread;
///
358 359
/// let (tx1, rx1) = channel();
/// let (tx2, rx2) = channel();
A
Alex Crichton 已提交
360
/// # fn long_running_task() {}
361
/// # fn calculate_the_answer() -> int { 42i }
A
Alex Crichton 已提交
362
///
363 364
/// Thread::spawn(move|| { long_running_task(); tx1.send(()) }).detach();
/// Thread::spawn(move|| { tx2.send(calculate_the_answer()) }).detach();
A
Alex Crichton 已提交
365 366
///
/// select! (
367 368
///     () = rx1.recv() => println!("the long running task finished first"),
///     answer = rx2.recv() => {
A
Alex Crichton 已提交
369 370 371 372 373 374 375 376 377 378
///         println!("the answer was: {}", answer);
///     }
/// )
/// ```
///
/// For more information about select, see the `std::comm::Select` structure.
#[macro_export]
#[experimental]
macro_rules! select {
    (
379
        $($name:pat = $rx:ident.$meth:ident() => $code:expr),+
A
Alex Crichton 已提交
380 381 382
    ) => ({
        use std::comm::Select;
        let sel = Select::new();
383
        $( let mut $rx = sel.handle(&$rx); )+
A
Alex Crichton 已提交
384
        unsafe {
385
            $( $rx.add(); )+
A
Alex Crichton 已提交
386 387
        }
        let ret = sel.wait();
388
        $( if ret == $rx.id() { let $name = $rx.$meth(); $code } else )+
A
Alex Crichton 已提交
389 390 391
        { unreachable!() }
    })
}
392 393 394 395 396 397 398

// When testing the standard library, we link to the liblog crate to get the
// logging macros. In doing so, the liblog crate was linked against the real
// version of libstd, and uses a different std::fmt module than the test crate
// uses. To get around this difference, we redefine the log!() macro here to be
// just a dumb version of what it should be.
#[cfg(test)]
399
macro_rules! log {
400 401 402
    ($lvl:expr, $($args:tt)*) => (
        if log_enabled!($lvl) { println!($($args)*) }
    )
403
}
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434

/// Built-in macros to the compiler itself.
///
/// These macros do not have any corresponding definition with a `macro_rules!`
/// macro, but are documented here. Their implementations can be found hardcoded
/// into libsyntax itself.
#[cfg(dox)]
pub mod builtin {
    /// The core macro for formatted string creation & output.
    ///
    /// This macro takes as its first argument a callable expression which will
    /// receive as its first argument a value of type `&fmt::Arguments`. This
    /// value can be passed to the functions in `std::fmt` for performing useful
    /// functions. All other formatting macros (`format!`, `write!`,
    /// `println!`, etc) are proxied through this one.
    ///
    /// For more information, see the documentation in `std::fmt`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::fmt;
    ///
    /// let s = format_args!(fmt::format, "hello {}", "world");
    /// assert_eq!(s, format!("hello {}", "world"));
    ///
    /// format_args!(|args| {
    ///     // pass `args` to another function, etc.
    /// }, "hello {}", "world");
    /// ```
    #[macro_export]
435
    macro_rules! format_args { ($closure:expr, $fmt:expr $($args:tt)*) => ({
436
        /* compiler built-in */
437
    }) }
438 439 440 441 442 443 444 445 446 447 448 449 450

    /// Inspect an environment variable at compile time.
    ///
    /// This macro will expand to the value of the named environment variable at
    /// compile time, yielding an expression of type `&'static str`.
    ///
    /// If the environment variable is not defined, then a compilation error
    /// will be emitted.  To not emit a compile error, use the `option_env!`
    /// macro instead.
    ///
    /// # Example
    ///
    /// ```rust
451 452
    /// let path: &'static str = env!("PATH");
    /// println!("the $PATH variable at the time of compiling was: {}", path);
453 454
    /// ```
    #[macro_export]
455
    macro_rules! env { ($name:expr) => ({ /* compiler built-in */ }) }
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

    /// Optionally inspect an environment variable at compile time.
    ///
    /// If the named environment variable is present at compile time, this will
    /// expand into an expression of type `Option<&'static str>` whose value is
    /// `Some` of the value of the environment variable. If the environment
    /// variable is not present, then this will expand to `None`.
    ///
    /// A compile time error is never emitted when using this macro regardless
    /// of whether the environment variable is present or not.
    ///
    /// # Example
    ///
    /// ```rust
    /// let key: Option<&'static str> = option_env!("SECRET_KEY");
    /// println!("the secret key might be: {}", key);
    /// ```
    #[macro_export]
474
    macro_rules! option_env { ($name:expr) => ({ /* compiler built-in */ }) }
475 476 477 478 479 480 481 482 483 484 485 486 487 488

    /// Concatenate literals into a static byte slice.
    ///
    /// This macro takes any number of comma-separated literal expressions,
    /// yielding an expression of type `&'static [u8]` which is the
    /// concatenation (left to right) of all the literals in their byte format.
    ///
    /// This extension currently only supports string literals, character
    /// literals, and integers less than 256. The byte slice returned is the
    /// utf8-encoding of strings and characters.
    ///
    /// # Example
    ///
    /// ```
489
    /// let rust = bytes!("r", 'u', "st", 255);
N
nham 已提交
490
    /// assert_eq!(rust[1], b'u');
A
Alex Crichton 已提交
491
    /// assert_eq!(rust[4], 255);
492 493
    /// ```
    #[macro_export]
494
    macro_rules! bytes { ($($e:expr),*) => ({ /* compiler built-in */ }) }
495 496 497 498 499 500 501 502 503 504 505 506 507

    /// Concatenate identifiers into one identifier.
    ///
    /// This macro takes any number of comma-separated identifiers, and
    /// concatenates them all into one, yielding an expression which is a new
    /// identifier. Note that hygiene makes it such that this macro cannot
    /// capture local variables, and macros are only allowed in item,
    /// statement or expression position, meaning this macro may be difficult to
    /// use in some situations.
    ///
    /// # Example
    ///
    /// ```
A
Alex Crichton 已提交
508 509 510
    /// #![feature(concat_idents)]
    ///
    /// # fn main() {
511 512 513 514
    /// fn foobar() -> int { 23 }
    ///
    /// let f = concat_idents!(foo, bar);
    /// println!("{}", f());
A
Alex Crichton 已提交
515
    /// # }
516 517
    /// ```
    #[macro_export]
518 519 520
    macro_rules! concat_idents {
        ($($e:ident),*) => ({ /* compiler built-in */ })
    }
521 522 523 524 525 526 527 528 529 530 531 532 533

    /// Concatenates literals into a static string slice.
    ///
    /// This macro takes any number of comma-separated literals, yielding an
    /// expression of type `&'static str` which represents all of the literals
    /// concatenated left-to-right.
    ///
    /// Integer and floating point literals are stringified in order to be
    /// concatenated.
    ///
    /// # Example
    ///
    /// ```
534
    /// let s = concat!("test", 10i, 'b', true);
535 536 537
    /// assert_eq!(s, "test10btrue");
    /// ```
    #[macro_export]
538
    macro_rules! concat { ($($e:expr),*) => ({ /* compiler built-in */ }) }
539 540 541 542 543 544 545 546 547 548 549 550 551 552

    /// A macro which expands to the line number on which it was invoked.
    ///
    /// The expanded expression has type `uint`, and the returned line is not
    /// the invocation of the `line!()` macro itself, but rather the first macro
    /// invocation leading up to the invocation of the `line!()` macro.
    ///
    /// # Example
    ///
    /// ```
    /// let current_line = line!();
    /// println!("defined on line: {}", current_line);
    /// ```
    #[macro_export]
553
    macro_rules! line { () => ({ /* compiler built-in */ }) }
554 555 556 557

    /// A macro which expands to the column number on which it was invoked.
    ///
    /// The expanded expression has type `uint`, and the returned column is not
H
Huon Wilson 已提交
558 559
    /// the invocation of the `column!()` macro itself, but rather the first macro
    /// invocation leading up to the invocation of the `column!()` macro.
560 561 562 563
    ///
    /// # Example
    ///
    /// ```
H
Huon Wilson 已提交
564
    /// let current_col = column!();
565 566 567
    /// println!("defined on column: {}", current_col);
    /// ```
    #[macro_export]
568
    macro_rules! column { () => ({ /* compiler built-in */ }) }
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

    /// A macro which expands to the file name from which it was invoked.
    ///
    /// The expanded expression has type `&'static str`, and the returned file
    /// is not the invocation of the `file!()` macro itself, but rather the
    /// first macro invocation leading up to the invocation of the `file!()`
    /// macro.
    ///
    /// # Example
    ///
    /// ```
    /// let this_file = file!();
    /// println!("defined in file: {}", this_file);
    /// ```
    #[macro_export]
584
    macro_rules! file { () => ({ /* compiler built-in */ }) }
585 586 587 588 589 590 591 592 593 594 595 596 597 598

    /// A macro which stringifies its argument.
    ///
    /// This macro will yield an expression of type `&'static str` which is the
    /// stringification of all the tokens passed to the macro. No restrictions
    /// are placed on the syntax of the macro invocation itself.
    ///
    /// # Example
    ///
    /// ```
    /// let one_plus_one = stringify!(1 + 1);
    /// assert_eq!(one_plus_one, "1 + 1");
    /// ```
    #[macro_export]
599
    macro_rules! stringify { ($t:tt) => ({ /* compiler built-in */ }) }
600 601 602 603 604 605 606 607 608 609 610 611 612

    /// Includes a utf8-encoded file as a string.
    ///
    /// This macro will yield an expression of type `&'static str` which is the
    /// contents of the filename specified. The file is located relative to the
    /// current file (similarly to how modules are found),
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let secret_key = include_str!("secret-key.ascii");
    /// ```
    #[macro_export]
613
    macro_rules! include_str { ($file:expr) => ({ /* compiler built-in */ }) }
614 615 616 617 618 619 620 621 622 623

    /// Includes a file as a byte slice.
    ///
    /// This macro will yield an expression of type `&'static [u8]` which is
    /// the contents of the filename specified. The file is located relative to
    /// the current file (similarly to how modules are found),
    ///
    /// # Example
    ///
    /// ```rust,ignore
C
Chris Wong 已提交
624
    /// let secret_key = include_bytes!("secret-key.bin");
625 626
    /// ```
    #[macro_export]
C
Chris Wong 已提交
627 628 629 630 631
    macro_rules! include_bytes { ($file:expr) => ({ /* compiler built-in */ }) }

    /// Deprecated alias for `include_bytes!()`.
    #[macro_export]
    macro_rules! include_bin { ($file:expr) => ({ /* compiler built-in */}) }
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

    /// Expands to a string that represents the current module path.
    ///
    /// The current module path can be thought of as the hierarchy of modules
    /// leading back up to the crate root. The first component of the path
    /// returned is the name of the crate currently being compiled.
    ///
    /// # Example
    ///
    /// ```rust
    /// mod test {
    ///     pub fn foo() {
    ///         assert!(module_path!().ends_with("test"));
    ///     }
    /// }
    ///
    /// test::foo();
    /// ```
    #[macro_export]
651
    macro_rules! module_path { () => ({ /* compiler built-in */ }) }
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671

    /// Boolean evaluation of configuration flags.
    ///
    /// In addition to the `#[cfg]` attribute, this macro is provided to allow
    /// boolean expression evaluation of configuration flags. This frequently
    /// leads to less duplicated code.
    ///
    /// The syntax given to this macro is the same syntax as the `cfg`
    /// attribute.
    ///
    /// # Example
    ///
    /// ```rust
    /// let my_directory = if cfg!(windows) {
    ///     "windows-specific-directory"
    /// } else {
    ///     "unix-directory"
    /// };
    /// ```
    #[macro_export]
672
    macro_rules! cfg { ($cfg:tt) => ({ /* compiler built-in */ }) }
673
}