Skip to content

Expand string literals and exprs inside of macros #12658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,13 @@ impl<'a> ExtCtxt<'a> {
}
}

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

/// Extract comma-separated expressions from `tts`. If there is a
/// parsing error, emit a non-fatal error and return None.
pub fn get_exprs_from_tts(cx: &ExtCtxt,
pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
sp: Span,
tts: &[ast::TokenTree]) -> Option<Vec<@ast::Expr> > {
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
Expand All @@ -471,7 +473,7 @@ pub fn get_exprs_from_tts(cx: &ExtCtxt,
cx.span_err(sp, "expected token: `,`");
return None;
}
es.push(p.parse_expr());
es.push(cx.expand_expr(p.parse_expr()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for this change?

}
Some(es)
}
Expand All @@ -482,9 +484,6 @@ pub fn get_exprs_from_tts(cx: &ExtCtxt,

// This environment maps Names to SyntaxExtensions.

// Actually, the following implementation is parameterized
// by both key and value types.

//impl question: how to implement it? Initially, the
// env will contain only macros, so it might be painful
// to add an empty frame for every context. Let's just
Expand All @@ -500,14 +499,6 @@ struct MapChainFrame {
map: HashMap<Name, SyntaxExtension>,
}

#[unsafe_destructor]
impl Drop for MapChainFrame {
fn drop(&mut self) {
// make sure that syntax extension dtors run before we drop the libs
self.map.clear();
}
}

// Only generic to make it easy to test
pub struct SyntaxEnv {
priv chain: Vec<MapChainFrame> ,
Expand Down
1 change: 0 additions & 1 deletion src/libsyntax/ext/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
};
let mut accumulator = ~"";
for e in es.move_iter() {
let e = cx.expand_expr(e);
match e.node {
ast::ExprLit(lit) => {
match lit.node {
Expand Down
5 changes: 1 addition & 4 deletions src/libsyntax/ext/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,11 +843,8 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: Span,
fmtsp: sp,
};
cx.fmtsp = efmt.span;
// Be sure to recursively expand macros just in case the format string uses
// a macro to build the format expression.
let expr = cx.ecx.expand_expr(efmt);
let fmt = match expr_to_str(cx.ecx,
expr,
efmt,
"format argument must be a string literal.") {
Some((fmt, _)) => fmt,
None => return MacResult::raw_dummy_expr(sp)
Expand Down
16 changes: 16 additions & 0 deletions src/test/run-pass/asm-concat-src.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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.

// ignore-fast #[feature] doesn't work with check-fast
#[feature(asm)];

pub fn main() {
unsafe { asm!(concat!("", "")) };
}
2 changes: 1 addition & 1 deletion src/test/run-pass/asm-out-assign.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2013-2014 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down
15 changes: 15 additions & 0 deletions src/test/run-pass/ext-expand-inner-exprs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.

static FOO : &'static [u8] = bytes!(concat!(concat!("hel", "lo"), "world"));

pub fn main() {
assert_eq!(FOO, "helloworld".as_bytes());
}