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.
This commit is contained in:
René Kijewski 2025-04-11 12:23:29 +02:00
parent 7727d4032b
commit 7ba2237fee
4 changed files with 2 additions and 61 deletions

View File

@ -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",

View File

@ -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 {
("", "")

View File

@ -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

View File

@ -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");
}