From 403a0b028d02ec72a637ed4a7bdda223ab5966f7 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 13 Nov 2020 22:08:17 +0100 Subject: Correctly scope macro argument name bindings (fixes #382) --- askama_shared/src/generator.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'askama_shared') diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index cbf0d1a..a37ca7c 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -708,14 +708,23 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { buf.writeln("{")?; self.prepare_ws(def.ws1); + let mut names = Buffer::new(0); + let mut values = Buffer::new(0); for (i, arg) in def.args.iter().enumerate() { - let expr_code = self.visit_expr_root(args.get(i).ok_or_else(|| { + if i > 0 { + names.write(", "); + values.write(", "); + } + names.write(arg); + + values.write("&"); + values.write(&self.visit_expr_root(args.get(i).ok_or_else(|| { CompileError::String(format!("macro '{}' takes more than {} arguments", name, i)) - })?)?; - buf.writeln(&format!("let {} = &{};", arg, expr_code))?; + })?)?); self.locals.insert(arg); } + buf.writeln(&format!("let ({}) = ({});", names.buf, values.buf))?; let mut size_hint = self.handle(own_ctx, &def.nodes, buf, AstLevel::Nested)?; self.flush_ws(def.ws2); -- cgit