Skip to content

Inconsistent (or incorrect) reference semantics of (probably) ? #399

@AMDmi3

Description

@AMDmi3

Thanks for fixing #386, however as it turns out, my attempt to simplify the demonstration code revealed unrelated problem, and my original problem was not fixed. It looks more related to ? now, but I'm no longer sure.

Here's more elaborate demonstration, closer to my code:

use askama::Template;
use serde_json::Value;

#[derive(Template)]
#[template(
    ext = "html",
    source = "
{%- macro p(v) %}{% if v == \"foo\" %}{% endif %}{% endmacro -%}

{%- if v.as_str().unwrap() == \"foo\" %}{% endif -%}
{%- call p(v.as_str().unwrap()) -%}

{%- if v.as_str().ok_or(\"error\")? == \"foo\" %}{% endif -%}
{%- call p(v.as_str().ok_or(\"error\")?) -%}

{%- let v1 = v.as_str().unwrap() %}
{%- if v1 == \"foo\" %}{% endif -%}
{%- call p(v1) -%}

{%- let v2 = v.as_str().ok_or(\"error\")? %}
{%- if v2 == \"foo\" %}{% endif -%}
{%- call p(v2) -%}
"
)]
struct Test {
    v: serde_json::Value,
}

fn main() {
    println!("{}", Test{v: Value::String("foo".to_string())}.render().unwrap());
}

like the previous example, this worked with askama 0.12 - all cases were treated as &str.

With askama 0.13.0 (both release and master after #386 fix), however, commented out lines do not compile, because an extra reference is added:

use askama::Template;
use serde_json::Value;

#[derive(Template)]
#[template(
    ext = "html",
    source = "
{%- macro p(v) %}{% if v == \"foo\" %}{% endif %}{% endmacro -%}

{%- if v.as_str().unwrap() == \"foo\" %}{% endif -%}
{%- call p(v.as_str().unwrap()) -%}

{%- if v.as_str().ok_or(\"error\")? == \"foo\" %}{% endif -%}
{#%- call p(v.as_str().ok_or(\"error\")?) -%#}

{%- let v1 = v.as_str().unwrap() %}
{%- if v1 == \"foo\" %}{% endif -%}
{%- call p(v1) -%}

{%- let v2 = v.as_str().ok_or(\"error\")? %}
{#%- if v2 == \"foo\" %}{% endif -%#}
{#%- call p(v2) -%#}
"
)]
struct Test {
    v: serde_json::Value,
}

fn main() {
    println!("{}", Test{v: Value::String("foo".to_string())}.render().unwrap());
}

So now it looks more like the problem is with ? operator, which, the same way as unwrap, returns &str, but it's mistreated and another reference is added both when it's passed to macro and when it's used in let expression (but not when it's used in expression directly).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions