diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2020-11-13 22:08:17 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2020-11-13 22:13:13 +0100 |
commit | 403a0b028d02ec72a637ed4a7bdda223ab5966f7 (patch) | |
tree | d00ae603c73c853a41db5bd636da6e038c2c0619 /askama_shared/src/generator.rs | |
parent | f23162a2080a3fb3fe2b8ba7eba4d61da5737dfe (diff) | |
download | askama-403a0b028d02ec72a637ed4a7bdda223ab5966f7.tar.gz askama-403a0b028d02ec72a637ed4a7bdda223ab5966f7.tar.bz2 askama-403a0b028d02ec72a637ed4a7bdda223ab5966f7.zip |
Correctly scope macro argument name bindings (fixes #382)
Diffstat (limited to 'askama_shared/src/generator.rs')
-rw-r--r-- | askama_shared/src/generator.rs | 15 |
1 files changed, 12 insertions, 3 deletions
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); |