diff --git a/src/libcollections/macros.rs b/src/libcollections/macros.rs index 79c86a846f1b9..9788a586083e2 100644 --- a/src/libcollections/macros.rs +++ b/src/libcollections/macros.rs @@ -33,8 +33,21 @@ macro_rules! vec { /// format!("hello {}", "world!"); /// format!("x = {}, y = {y}", 10, y = 30); /// ``` +#[cfg(stage0)] #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] macro_rules! format { ($($arg:tt)*) => ($crate::fmt::format(format_args!($($arg)*))) } + +#[cfg(not(stage0))] +#[macro_export] +#[stable(feature = "rust1", since = "1.0.0")] +macro_rules! format { + ($fmt:expr) => { + format_arg!($fmt).to_string() + }; + ($fmt:expr, $($arg:tt)+) => { + $crate::fmt::format(format_args!($fmt, $($arg)*)) + } +} diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 943365d8454d4..8af68f609f3ba 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -177,11 +177,23 @@ macro_rules! try { /// write!(&mut w, "test"); /// write!(&mut w, "formatted {}", "arguments"); /// ``` +#[cfg(stage0)] #[macro_export] macro_rules! write { ($dst:expr, $($arg:tt)*) => ((&mut *$dst).write_fmt(format_args!($($arg)*))) } +#[cfg(not(stage0))] +#[macro_export] +macro_rules! write { + ($dst:expr, $fmt:expr) => { + (&mut *$dst).write_str(format_arg!($fmt)) + }; + ($dst:expr, $fmt:expr, $($arg:tt)+) => { + (&mut *$dst).write_fmt(format_args!($fmt, $($arg)*)) + }; +} + /// Equivalent to the `write!` macro, except that a newline is appended after /// the message is written. #[macro_export] diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 6a2aafcf8f396..9f6fe10fd6ec1 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -79,12 +79,25 @@ macro_rules! format { /// Equivalent to the `println!` macro except that a newline is not printed at /// the end of the message. +#[cfg(stage0)] #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] macro_rules! print { ($($arg:tt)*) => ($crate::old_io::stdio::print_args(format_args!($($arg)*))) } +#[cfg(not(stage0))] +#[macro_export] +#[stable(feature = "rust1", since = "1.0.0")] +macro_rules! print { + ($fmt:expr) => { + $crate::old_io::stdio::print(format_arg!($fmt)) + }; + ($fmt:expr, $($arg:tt)+) => { + $crate::old_io::stdio::print_args(format_args!($fmt, $($arg)*)) + }; +} + /// Macro for printing to a task's stdout handle. /// /// Each task can override its stdout handle via `std::old_io::stdio::set_stdout`. @@ -97,12 +110,25 @@ macro_rules! print { /// println!("hello there!"); /// println!("format {} arguments", "some"); /// ``` +#[cfg(stage0)] #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] macro_rules! println { ($($arg:tt)*) => ($crate::old_io::stdio::println_args(format_args!($($arg)*))) } +#[cfg(not(stage0))] +#[macro_export] +#[stable(feature = "rust1", since = "1.0.0")] +macro_rules! println { + ($fmt:expr) => { + $crate::old_io::stdio::println(format_arg!($fmt)) + }; + ($fmt:expr, $($arg:tt)*) => { + $crate::old_io::stdio::println_args(format_args!($fmt, $($arg)*)) + }; +} + /// 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`. @@ -185,6 +211,28 @@ macro_rules! log { /// into libsyntax itself. #[cfg(dox)] pub mod builtin { + /// Parse a `&'static str` with a compatible format to `format_args!()`. + /// + /// This macro produces a value of type `&'static str`, and is intended to + /// be used by the other formatting macros (`format!`, `write!`, `println!`) + /// are proxied through this one. + /// + /// For more information, see the documentation in `std::fmt`. + /// + /// # Example + /// + /// ```rust + /// use std::fmt; + /// + /// let s = format_arg!("hello"); + /// assert_eq!(s, format!("hello")); + /// + /// ``` + #[macro_export] + macro_rules! format_arg { ($fmt:expr) => ({ + /* compiler built-in */ + }) } + /// The core macro for formatted string creation & output. /// /// This macro produces a value of type `fmt::Arguments`. This value can be diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 64ae6162ef4e5..4af1de6b36fdd 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -447,6 +447,9 @@ fn initial_syntax_expander_table(ecfg: &expand::ExpansionConfig) -> SyntaxEnv { let mut syntax_expanders = SyntaxEnv::new(); syntax_expanders.insert(intern("macro_rules"), MacroRulesTT); + syntax_expanders.insert(intern("format_arg"), + builtin_normal_expander( + ext::format::expand_format_arg)); syntax_expanders.insert(intern("format_args"), builtin_normal_expander( ext::format::expand_format_args)); diff --git a/src/libsyntax/ext/format.rs b/src/libsyntax/ext/format.rs index 170a455a91326..074fb19711875 100644 --- a/src/libsyntax/ext/format.rs +++ b/src/libsyntax/ext/format.rs @@ -57,7 +57,7 @@ struct Context<'a, 'b:'a> { /// Collection of the compiled `rt::Argument` structures pieces: Vec
>, /// Collection of string literals - str_pieces: Vec
>,
+ str_pieces: Vec<(Span, token::InternedString)>,
/// Stays `true` if all formatting parameters are default (as in "{}{}").
all_pieces_simple: bool,
@@ -335,11 +335,11 @@ impl<'a, 'b> Context<'a, 'b> {
}
/// Translate the accumulated string literals to a literal expression
- fn trans_literal_string(&mut self) -> P >,
+ name_ordering: Vec