Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro doesn't resolve argument #812

Open
DaniPopes opened this issue Apr 5, 2023 · 1 comment
Open

Macro doesn't resolve argument #812

DaniPopes opened this issue Apr 5, 2023 · 1 comment

Comments

@DaniPopes
Copy link

DaniPopes commented Apr 5, 2023

Only happens when shadowing the argument in a block and a for loop is present. More details in the repro comments.

This can be easily avoided by renaming either the parameter or the assigned variable, but this error was very confusing when I first encountered it.

Repro:

#[derive(askama::Template)]
//~^ error[E0425]: cannot find value `string` in this scope
#[template(
    source = r#"
{% macro one(string) %}
    {# a block #}
    {% if false %}
        {# assignment with same name as param #}
        {% let string = string.trim_start() %}

        {# any `for` statement #}
        {% for _ in std::iter::once(()) %} {% endfor %}
    {% endif %}

    {# `string` doesn't correctly expand to `self.field` making compilation fail with E0425 #}
    {{ string.trim() }}
{% endmacro %}

{% call one(field) %}
"#,
    ext = "txt",
    whitespace = "suppress",
    print = "code"
)]
struct S {
    field: &'static str,
}
@Kijewski
Copy link
Collaborator

Kijewski commented Apr 5, 2023

Ah, thank you for finding the issued and adding an example to reproduce it!

The generator for for-loops only calls self.locals.push() if it is used with for … in … if …, but the method should always be called, because the loop body is a new scope. I haven't looked into it, but I wager that if-else-blocks and match-with-blocks have the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants