Skip to content

Commit 50f94f6

Browse files
committed
Avoid needless reexpansions.
1 parent f81f496 commit 50f94f6

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

src/libsyntax/ext/base.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use self::SyntaxExtension::*;
1313
use ast;
1414
use ast::{Name, PatKind};
1515
use attr::HasAttrs;
16-
use codemap::{self, CodeMap, ExpnInfo};
16+
use codemap::{self, CodeMap, ExpnInfo, Spanned, respan};
1717
use syntax_pos::{Span, ExpnId, NO_EXPANSION};
1818
use errors::DiagnosticBuilder;
1919
use ext;
@@ -805,8 +805,8 @@ impl<'a> ExtCtxt<'a> {
805805
/// Extract a string literal from the macro expanded version of `expr`,
806806
/// emitting `err_msg` if `expr` is not a string literal. This does not stop
807807
/// compilation on error, merely emits a non-fatal error and returns None.
808-
pub fn expr_to_string(cx: &mut ExtCtxt, expr: P<ast::Expr>, err_msg: &str)
809-
-> Option<(InternedString, ast::StrStyle)> {
808+
pub fn expr_to_spanned_string(cx: &mut ExtCtxt, expr: P<ast::Expr>, err_msg: &str)
809+
-> Option<Spanned<(InternedString, ast::StrStyle)>> {
810810
// Update `expr.span`'s expn_id now in case expr is an `include!` macro invocation.
811811
let expr = expr.map(|mut expr| {
812812
expr.span.expn_id = cx.backtrace;
@@ -817,14 +817,19 @@ pub fn expr_to_string(cx: &mut ExtCtxt, expr: P<ast::Expr>, err_msg: &str)
817817
let expr = cx.expander().fold_expr(expr);
818818
match expr.node {
819819
ast::ExprKind::Lit(ref l) => match l.node {
820-
ast::LitKind::Str(ref s, style) => return Some(((*s).clone(), style)),
820+
ast::LitKind::Str(ref s, style) => return Some(respan(expr.span, (s.clone(), style))),
821821
_ => cx.span_err(l.span, err_msg)
822822
},
823823
_ => cx.span_err(expr.span, err_msg)
824824
}
825825
None
826826
}
827827

828+
pub fn expr_to_string(cx: &mut ExtCtxt, expr: P<ast::Expr>, err_msg: &str)
829+
-> Option<(InternedString, ast::StrStyle)> {
830+
expr_to_spanned_string(cx, expr, err_msg).map(|s| s.node)
831+
}
832+
828833
/// Non-fatally assert that `tts` is empty. Note that this function
829834
/// returns even when `tts` is non-empty, macros that *need* to stop
830835
/// compilation should call
@@ -851,7 +856,7 @@ pub fn get_single_str_from_tts(cx: &mut ExtCtxt,
851856
cx.span_err(sp, &format!("{} takes 1 argument", name));
852857
return None
853858
}
854-
let ret = cx.expander().fold_expr(panictry!(p.parse_expr()));
859+
let ret = panictry!(p.parse_expr());
855860
if p.token != token::Eof {
856861
cx.span_err(sp, &format!("{} takes 1 argument", name));
857862
}

src/libsyntax_ext/format.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use syntax::ast;
1717
use syntax::ext::base::*;
1818
use syntax::ext::base;
1919
use syntax::ext::build::AstBuilder;
20-
use syntax::fold::Folder;
2120
use syntax::parse::token::{self, keywords};
2221
use syntax::ptr::P;
2322
use syntax_pos::{Span, DUMMY_SP};
@@ -702,10 +701,12 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
702701
let arg_types: Vec<_> = (0..args.len()).map(|_| Vec::new()).collect();
703702
let arg_unique_types: Vec<_> = (0..args.len()).map(|_| Vec::new()).collect();
704703
let macsp = ecx.call_site();
705-
// Expand the format literal so that efmt.span will have a backtrace. This
706-
// is essential for locating a bug when the format literal is generated in
707-
// a macro. (e.g. println!("{}"), which uses concat!($fmt, "\n")).
708-
let efmt = ecx.expander().fold_expr(efmt);
704+
let msg = "format argument must be a string literal.";
705+
let fmt = match expr_to_spanned_string(ecx, efmt, msg) {
706+
Some(fmt) => fmt,
707+
None => return DummyResult::raw_expr(sp),
708+
};
709+
709710
let mut cx = Context {
710711
ecx: ecx,
711712
args: args,
@@ -723,14 +724,10 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
723724
str_pieces: Vec::new(),
724725
all_pieces_simple: true,
725726
macsp: macsp,
726-
fmtsp: efmt.span,
727-
};
728-
let fmt = match expr_to_string(cx.ecx, efmt, "format argument must be a string literal.") {
729-
Some((fmt, _)) => fmt,
730-
None => return DummyResult::raw_expr(sp),
727+
fmtsp: fmt.span,
731728
};
732729

733-
let mut parser = parse::Parser::new(&fmt);
730+
let mut parser = parse::Parser::new(&fmt.node.0);
734731
let mut pieces = vec![];
735732

736733
loop {

0 commit comments

Comments
 (0)