From 7ba2237feeffa793a3fe19a39a1e1ec4a0617b63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Fri, 11 Apr 2025 12:23:29 +0200 Subject: [PATCH] Revert breaking changes * Revert "`askama_derive` accidentally exposed as a feature" * This reverts commit 68fbf93d99207835d9271cdcb447c281a15c2352. * Revert "If using local variable as value when creating a new variable, do not put it behind a reference" * This reverts commit 8081d717d351085cfbb5160dd4e58ad8adc8453b. * Revert "Describe declaration/ownership when creating a new variable" * This reverts commit c76d1a256314d95a2cf644dfa77f9aa26e98d6bd. --- askama/Cargo.toml | 2 +- askama_derive/src/generator/node.rs | 6 +--- book/src/template_syntax.md | 10 ------- testing/tests/vars.rs | 45 ----------------------------- 4 files changed, 2 insertions(+), 61 deletions(-) diff --git a/askama/Cargo.toml b/askama/Cargo.toml index ba0b41dd..5329f449 100644 --- a/askama/Cargo.toml +++ b/askama/Cargo.toml @@ -56,7 +56,7 @@ alloc = [ blocks = ["askama_derive?/blocks"] code-in-doc = ["askama_derive?/code-in-doc"] config = ["askama_derive?/config"] -derive = ["dep:askama_derive"] +derive = ["askama_derive"] serde_json = ["std", "askama_derive?/serde_json", "dep:serde", "dep:serde_json"] std = [ "alloc", diff --git a/askama_derive/src/generator/node.rs b/askama_derive/src/generator/node.rs index 7c4eea98..06a92918 100644 --- a/askama_derive/src/generator/node.rs +++ b/askama_derive/src/generator/node.rs @@ -965,11 +965,7 @@ impl<'a> Generator<'a, '_> { } self.visit_target(buf, true, true, &l.var); - // If it's not taking the ownership of a local variable or copyable, then we need to add - // a reference. - let (before, after) = if !matches!(**val, Expr::Var(name) if self.locals.get(name).is_some()) - && !is_copyable(val) - { + let (before, after) = if !is_copyable(val) { ("&(", ")") } else { ("", "") diff --git a/book/src/template_syntax.md b/book/src/template_syntax.md index 7da46698..2a809914 100644 --- a/book/src/template_syntax.md +++ b/book/src/template_syntax.md @@ -65,16 +65,6 @@ Like Rust, Askama also supports shadowing variables. For compatibility with Jinja, `set` can be used in place of `let`. -### Borrow rules - -In some cases, the value of a variable initialization will be put behind a reference -to prevent changing ownership. The rules are as follows: - - * If the value is an expression of more than one element (like `x + 2`), it WILL NOT BE put behind a reference. - * If the value is a variable defined in the templates, it WILL NOT BE put behind a reference. - * If the value has a filter applied to it (`x|capitalize`), it WILL NOT BE put behind a reference. - * If the value is a field (`x.y`), it WILL BE put behind a reference. - ## Filters Values such as those obtained from variables can be post-processed diff --git a/testing/tests/vars.rs b/testing/tests/vars.rs index 5e3b4325..aac32042 100644 --- a/testing/tests/vars.rs +++ b/testing/tests/vars.rs @@ -151,48 +151,3 @@ fn test_not_moving_fields_in_var() { }; assert_eq!(x.render().unwrap(), "a/a"); } - -// Ensure that using a local variable as value when creating -// another variable will not be behind a reference. -#[test] -fn test_moving_local_vars() { - #[derive(Template)] - #[template( - source = r#" -{%- let a = 1 -%} -{%- if a == 1 %}A{% endif -%} -{%- let b = a -%} -{%- if b == 1 %}B{% endif -%} -{%- let c = a + 0 -%} -{%- if c == 1 %}C{% endif -%} -{% let d = x %} -{%- if *d == 1 %}D{% endif -%} -{%- let e = y -%} -{%- if e == "a" %}E{% endif -%} -{%- let f = Bla::new() -%} -{%- if f.x == 0 %}F{% endif -%} -{%- let g = f.x -%} -{%- if *g == 0 %}G{% endif -%} - "#, - ext = "txt" - )] - struct X { - x: u32, - y: String, - } - struct Bla { - x: u32, - } - - impl Bla { - fn new() -> Self { - Self { x: 0 } - } - } - - let x = X { - x: 1, - y: "a".to_owned(), - }; - assert_eq!(x.render().unwrap(), "ABCDEFG"); -}