Skip to content

Commit 2a4fd9f

Browse files
Merge pull request #686 from GuillaumeGomez/temporary-refs
Correctly handle temporary references
2 parents d901581 + 03a01c2 commit 2a4fd9f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

askama_derive/src/generator/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ impl<'a> Generator<'a, '_> {
409409
Expr::Call(ref v) if !matches!(**v.path, Expr::Path(_)) => {
410410
self.visit_expr(ctx, &mut buf, arg)?;
411411
let buf = buf.into_token_stream();
412-
quote_spanned!(span=> { #buf })
412+
quote_spanned!(span=> #buf)
413413
}
414414
_ => {
415415
self.visit_expr(ctx, &mut buf, arg)?;

testing/tests/calls.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,33 @@ mod test_deref_method_arg {
407407
assert_eq!(x.render().unwrap(), "3322 6677");
408408
}
409409
}
410+
411+
mod sub_test {
412+
use std::borrow::Borrow;
413+
414+
use askama::Template;
415+
416+
pub fn bar(x: &String) -> String {
417+
format!("bar: {x}")
418+
}
419+
420+
// This test ensures that temporary variables behind references (like one generated
421+
// from `.borrow()`) still work.
422+
//
423+
// Regression test for <https://github.com/askama-rs/askama/issues/683>.
424+
#[test]
425+
fn test_borrow() {
426+
#[derive(Template)]
427+
#[template(
428+
source = r#"
429+
{{- crate::sub_test::bar(b.to_string().borrow()) -}}
430+
"#,
431+
ext = "txt"
432+
)]
433+
struct Foo {
434+
b: u32,
435+
}
436+
437+
assert_eq!(Foo { b: 0 }.render().unwrap(), "bar: 0");
438+
}
439+
}

0 commit comments

Comments
 (0)