Skip to content

Don't have format! move out of local variables #8945

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

Closed
wants to merge 1 commit into from
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
8 changes: 6 additions & 2 deletions src/libsyntax/ext/ifmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,18 +588,22 @@ impl Context {
// foo(bar(&1))
// the lifetime of `1` doesn't outlast the call to `bar`, so it's not
// vald for the call to `foo`. To work around this all arguments to the
// fmt! string are shoved into locals.
// fmt! string are shoved into locals. Furthermore, we shove the address
// of each variable because we don't want to move out of the arguments
// passed to this function.
for (i, &e) in self.args.iter().enumerate() {
if self.arg_types[i].is_none() { loop } // error already generated

let name = self.ecx.ident_of(fmt!("__arg%u", i));
let e = self.ecx.expr_addr_of(e.span, e);
lets.push(self.ecx.stmt_let(e.span, false, name, e));
locals.push(self.format_arg(e.span, Left(i), name));
}
for (&name, &e) in self.names.iter() {
if !self.name_types.contains_key(&name) { loop }

let lname = self.ecx.ident_of(fmt!("__arg%s", name));
let e = self.ecx.expr_addr_of(e.span, e);
lets.push(self.ecx.stmt_let(e.span, false, lname, e));
names[*self.name_positions.get(&name)] =
Some(self.format_arg(e.span, Right(name), lname));
Expand Down Expand Up @@ -643,7 +647,7 @@ impl Context {
Right(s) => *self.name_types.get(&s)
};

let argptr = self.ecx.expr_addr_of(sp, self.ecx.expr_ident(sp, ident));
let argptr = self.ecx.expr_ident(sp, ident);
let fmt_trait = match ty {
Unknown => "Default",
Known(tyname) => {
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 @@ -215,6 +215,11 @@ pub fn main() {

test_write();
test_print();

// make sure that format! doesn't move out of local variables
let a = ~3;
format!("{:?}", a);
format!("{:?}", a);
}

// Basic test to make sure that we can invoke the `write!` macro with an
Expand Down