Skip to content
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

format_args: allow omission of format string #12384

Closed
Closed
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
34 changes: 28 additions & 6 deletions src/libsyntax/ext/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,20 @@ pub fn expand_args(ecx: &mut ExtCtxt, sp: Span,
}
}

/// Try to match a string literal, and if not found, emit a note. No big deal.
fn try_expr_literal(cx: &ExtCtxt, expr: @ast::Expr, note_msg: &str)
-> Option<~str> {

match expr.node {
ast::ExprLit(l) => match l.node {
ast::LitStr(ref s, _) => return Some(s.get().to_owned()),
_ => cx.span_note(l.span, note_msg)
Copy link
Member

Choose a reason for hiding this comment

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

@kballard is talking about this line...

},
_ => cx.span_note(expr.span, note_msg)
Copy link
Member

Choose a reason for hiding this comment

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

... and this one.

}
None
}

/// Take the various parts of `format_args!(extra, efmt, args...,
/// name=names...)` and construct the appropriate formatting
/// expression.
Expand All @@ -807,14 +821,22 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: 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,
"format argument must be a string literal.") {
Some((fmt, _)) => fmt,
None => return efmt
let fmt = match try_expr_literal(cx.ecx,
expr,
"format string literal missing, using default") {
Some(fmt) => fmt,
None => {
// push arg back into args lists
cx.args.unshift(expr);
cx.arg_types.unshift(None);
let mut parts = ~[];
let form = ~"{:?}";
parts.grow(cx.args.len(), &form);
parts.connect(" ")
}
};

let mut parser = parse::Parser::new(fmt.get());
let mut parser = parse::Parser::new(fmt);
loop {
match parser.next() {
Some(piece) => {
Expand Down
5 changes: 5 additions & 0 deletions src/test/run-pass/ifmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ pub fn main() {
t!(format!("hello"), "hello");
t!(format!("hello \\{"), "hello {");

// No format string uses default poly formatters
t!(format!(1), "1");
t!(format!(1, 2), "1 2");
t!(format!(1, "foo", 2, true), "1 \"foo\" 2 true");

// default formatters should work
t!(format!("{}", 1i), "1");
t!(format!("{}", 1i8), "1");
Expand Down