Implement a concat!() format extension
This extension can be used to concatenate string literals at compile time. C has
this useful ability when placing string literals lexically next to one another,
but this needs to be handled at the syntax extension level to recursively expand
macros.
The major use case for this is something like:
macro_rules! mylog( ($fmt:expr $($arg:tt)*) => {
error2!(concat!(file!(), ":", line!(), " - ", $fmt) $($arg)*);
})
Where the mylog macro will automatically prepend the filename/line number to the
beginning of every log message.
This commit is contained in:
parent
e976de32dc
commit
a49e65c2ed
8 changed files with 118 additions and 3 deletions
14
src/test/compile-fail/concat.rs
Normal file
14
src/test/compile-fail/concat.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// Copyright 2013 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.
|
||||
|
||||
fn main() {
|
||||
concat!(foo); //~ ERROR: expected a literal
|
||||
concat!(foo()); //~ ERROR: expected a literal
|
||||
}
|
||||
19
src/test/run-pass/concat.rs
Normal file
19
src/test/run-pass/concat.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright 2013 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.
|
||||
|
||||
pub fn main() {
|
||||
assert_eq!(format!(concat!("foo", "bar", "{}"), "baz"), ~"foobarbaz");
|
||||
assert_eq!(format!(concat!()), ~"");
|
||||
|
||||
assert_eq!(
|
||||
concat!(1, 2i, 3u, 4f32, 4.0, 'a', true, ()),
|
||||
"12344.0atrue"
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue