librustc: Always parse macro!()/macro![] as expressions if not

followed by a semicolon.

This allows code like `vec![1i, 2, 3].len();` to work.

This breaks code that uses macros as statements without putting
semicolons after them, such as:

    fn main() {
        ...
        assert!(a == b)
        assert!(c == d)
        println(...);
    }

It also breaks code that uses macros as items without semicolons:

    local_data_key!(foo)

    fn main() {
        println("hello world")
    }

Add semicolons to fix this code. Those two examples can be fixed as
follows:

    fn main() {
        ...
        assert!(a == b);
        assert!(c == d);
        println(...);
    }

    local_data_key!(foo);

    fn main() {
        println("hello world")
    }

RFC #378.

Closes #18635.

[breaking-change]
This commit is contained in:
Patrick Walton
2014-11-14 09:18:10 -08:00
committed by Jorge Aparicio
parent c0b2885ee1
commit ddb2466f6a
222 changed files with 2330 additions and 2039 deletions

View File

@@ -12,7 +12,7 @@
/// Entry point of task panic, for details, see std::macros
#[macro_export]
macro_rules! panic(
macro_rules! panic {
() => (
panic!("{}", "explicit panic")
);
@@ -44,11 +44,11 @@ macro_rules! panic(
}
format_args!(_run_fmt, $fmt, $($arg)*)
});
)
}
/// Runtime assertion, for details see std::macros
#[macro_export]
macro_rules! assert(
macro_rules! assert {
($cond:expr) => (
if !$cond {
panic!(concat!("assertion failed: ", stringify!($cond)))
@@ -59,21 +59,21 @@ macro_rules! assert(
panic!($($arg)*)
}
);
)
}
/// Runtime assertion, only without `--cfg ndebug`
#[macro_export]
macro_rules! debug_assert(
macro_rules! debug_assert {
($(a:tt)*) => ({
if cfg!(not(ndebug)) {
assert!($($a)*);
}
})
)
}
/// Runtime assertion for equality, for details see std::macros
#[macro_export]
macro_rules! assert_eq(
macro_rules! assert_eq {
($cond1:expr, $cond2:expr) => ({
let c1 = $cond1;
let c2 = $cond2;
@@ -81,46 +81,47 @@ macro_rules! assert_eq(
panic!("expressions not equal, left: {}, right: {}", c1, c2);
}
})
)
}
/// Runtime assertion for equality, only without `--cfg ndebug`
#[macro_export]
macro_rules! debug_assert_eq(
macro_rules! debug_assert_eq {
($($a:tt)*) => ({
if cfg!(not(ndebug)) {
assert_eq!($($a)*);
}
})
)
}
/// Runtime assertion, disableable at compile time
#[macro_export]
macro_rules! debug_assert(
macro_rules! debug_assert {
($($arg:tt)*) => (if cfg!(not(ndebug)) { assert!($($arg)*); })
)
}
/// Short circuiting evaluation on Err
#[macro_export]
macro_rules! try(
macro_rules! try {
($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
)
}
/// Writing a formatted string into a writer
#[macro_export]
macro_rules! write(
macro_rules! write {
($dst:expr, $($arg:tt)*) => ({
let dst = &mut *$dst;
format_args!(|args| { dst.write_fmt(args) }, $($arg)*)
})
)
}
/// Writing a formatted string plus a newline into a writer
#[macro_export]
macro_rules! writeln(
macro_rules! writeln {
($dst:expr, $fmt:expr $($arg:tt)*) => (
write!($dst, concat!($fmt, "\n") $($arg)*)
)
)
}
#[macro_export]
macro_rules! unreachable( () => (panic!("unreachable code")) )
macro_rules! unreachable { () => (panic!("unreachable code")) }