Skip to content

Commit a0e54c7

Browse files
committed
Expand string literals and exprs inside of macros
A couple of syntax extensions manually expanded expressions, but it wasn't done universally, most noticably inside of asm!(). There's also a bit of random cleanup.
1 parent 2543177 commit a0e54c7

File tree

7 files changed

+41
-23
lines changed

7 files changed

+41
-23
lines changed

src/libsyntax/ext/base.rs

+8-17
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,13 @@ impl<'a> ExtCtxt<'a> {
405405
}
406406
}
407407

408-
/// Extract a string literal from `expr`, emitting `err_msg` if `expr`
409-
/// is not a string literal. This does not stop compilation on error,
410-
/// merely emits a non-fatal error and returns None.
411-
pub fn expr_to_str(cx: &ExtCtxt, expr: @ast::Expr, err_msg: &str)
408+
/// Extract a string literal from the macro expanded version of `expr`,
409+
/// emitting `err_msg` if `expr` is not a string literal. This does not stop
410+
/// compilation on error, merely emits a non-fatal error and returns None.
411+
pub fn expr_to_str(cx: &mut ExtCtxt, expr: @ast::Expr, err_msg: &str)
412412
-> Option<(InternedString, ast::StrStyle)> {
413+
// we want to be able to handle e.g. concat("foo", "bar")
414+
let expr = cx.expand_expr(expr);
413415
match expr.node {
414416
ast::ExprLit(l) => match l.node {
415417
ast::LitStr(ref s, style) => return Some(((*s).clone(), style)),
@@ -457,7 +459,7 @@ pub fn get_single_str_from_tts(cx: &ExtCtxt,
457459

458460
/// Extract comma-separated expressions from `tts`. If there is a
459461
/// parsing error, emit a non-fatal error and return None.
460-
pub fn get_exprs_from_tts(cx: &ExtCtxt,
462+
pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
461463
sp: Span,
462464
tts: &[ast::TokenTree]) -> Option<Vec<@ast::Expr> > {
463465
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
@@ -471,7 +473,7 @@ pub fn get_exprs_from_tts(cx: &ExtCtxt,
471473
cx.span_err(sp, "expected token: `,`");
472474
return None;
473475
}
474-
es.push(p.parse_expr());
476+
es.push(cx.expand_expr(p.parse_expr()));
475477
}
476478
Some(es)
477479
}
@@ -482,9 +484,6 @@ pub fn get_exprs_from_tts(cx: &ExtCtxt,
482484

483485
// This environment maps Names to SyntaxExtensions.
484486

485-
// Actually, the following implementation is parameterized
486-
// by both key and value types.
487-
488487
//impl question: how to implement it? Initially, the
489488
// env will contain only macros, so it might be painful
490489
// to add an empty frame for every context. Let's just
@@ -500,14 +499,6 @@ struct MapChainFrame {
500499
map: HashMap<Name, SyntaxExtension>,
501500
}
502501

503-
#[unsafe_destructor]
504-
impl Drop for MapChainFrame {
505-
fn drop(&mut self) {
506-
// make sure that syntax extension dtors run before we drop the libs
507-
self.map.clear();
508-
}
509-
}
510-
511502
// Only generic to make it easy to test
512503
pub struct SyntaxEnv {
513504
priv chain: Vec<MapChainFrame> ,

src/libsyntax/ext/concat.rs

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
2525
};
2626
let mut accumulator = ~"";
2727
for e in es.move_iter() {
28-
let e = cx.expand_expr(e);
2928
match e.node {
3029
ast::ExprLit(lit) => {
3130
match lit.node {

src/libsyntax/ext/format.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -843,11 +843,8 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: Span,
843843
fmtsp: sp,
844844
};
845845
cx.fmtsp = efmt.span;
846-
// Be sure to recursively expand macros just in case the format string uses
847-
// a macro to build the format expression.
848-
let expr = cx.ecx.expand_expr(efmt);
849846
let fmt = match expr_to_str(cx.ecx,
850-
expr,
847+
efmt,
851848
"format argument must be a string literal.") {
852849
Some((fmt, _)) => fmt,
853850
None => return MacResult::raw_dummy_expr(sp)

src/test/run-pass/asm-concat-src.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
// ignore-fast #[feature] doesn't work with check-fast
12+
#[feature(asm)];
13+
14+
pub fn main() {
15+
unsafe { asm!(concat!("", "")) };
16+
}

src/test/run-pass/asm-out-assign.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2013-2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
static FOO : &'static [u8] = bytes!(concat!(concat!("hel", "lo"), "world"));
12+
13+
pub fn main() {
14+
assert_eq!(FOO, "helloworld".as_bytes());
15+
}

0 commit comments

Comments
 (0)