Skip to content

Add support for macro args type annotations#722

Merged
GuillaumeGomez merged 2 commits intoaskama-rs:mainfrom
GuillaumeGomez:macro-arg-type-annotation
Apr 5, 2026
Merged

Add support for macro args type annotations#722
GuillaumeGomez merged 2 commits intoaskama-rs:mainfrom
GuillaumeGomez:macro-arg-type-annotation

Conversation

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

Fixes #514.

Does it look good to you @seijikun ?

@GuillaumeGomez GuillaumeGomez force-pushed the macro-arg-type-annotation branch from 18e1970 to 653e423 Compare March 30, 2026 14:44
@seijikun
Copy link
Copy Markdown
Contributor

seijikun commented Apr 2, 2026

Awesome, thank you for working on this!

I just tried the following:

use askama::Template;

struct ComplexStruct {
    title: String,
    icon: String,
}

#[derive(Template)]
#[template(
    source = r#"
        {%- macro test(entries: &[ComplexStruct]) -%}{% endmacro -%}
        {{ test(&entries) }}
    "#,
    ext = "txt"
)]
struct Page {
    entries: Vec<ComplexStruct>,
}

fn main() {
    let page = Page {
        entries: vec![ComplexStruct {
            title: "title".to_string(),
            icon: "icon".to_string(),
        }],
    };
    page.render().unwrap();
}

But am getting this error:

error: expected `)` to close macro argument list
         --> <source attribute>:2:22
       "(entries: &[ComplexStruct]) -%}{% endmacro -%}\n        {{ test(entries) }}\n    "
  --> src/main.rs:10:14
   |
10 |       source = r#"
   |  ______________^
11 | |         {%- macro test(entries: &[ComplexStruct]) -%}{% endmacro -%}
12 | |         {{ test(entries) }}
13 | |     "#,
   | |______^

@GuillaumeGomez GuillaumeGomez force-pushed the macro-arg-type-annotation branch from 653e423 to bd6bf17 Compare April 2, 2026 16:22
@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

Updated with new tests (including yours). Can you give it another try please?

@seijikun
Copy link
Copy Markdown
Contributor

seijikun commented Apr 4, 2026

Looks good!
I'd personally prefer the implementation to take the type verbatim instead of forcefully injecting a & - so the behavior is a bit more predictable.
But I guess you probably had a good reason for taking all arguments as reference(?)

source = r#"
{%- macro test(title: Option<u32> = None) -%}{% endmacro -%}
{%- let y = 12 -%}
{% call test(y) %}{% endcall -%}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to also test the short call syntax in the tests:

{{ test(y) }}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a test for it.

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

GuillaumeGomez commented Apr 5, 2026

I'd personally prefer the implementation to take the type verbatim instead of forcefully injecting a & - so the behavior is a bit more predictable.
But I guess you probably had a good reason for taking all arguments as reference(?)

Sadly it comes from the current limitations of the proc-macros error reporting. If we don't add a reference, it will then take ownership, leading to hard to track errors. So for now, it's simply a better user experience. Hopefully, once the proc-macro Span API evolves, we can change it.

@GuillaumeGomez GuillaumeGomez force-pushed the macro-arg-type-annotation branch from bd6bf17 to 724f05b Compare April 5, 2026 15:18
@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

@seijikun Added the test you suggested, thanks for that!

Also realized I forgot to add docs for it so added it as well. Seems like PR is ready to go. Merging once CI is happy.

@GuillaumeGomez GuillaumeGomez merged commit 393b51d into askama-rs:main Apr 5, 2026
50 checks passed
@GuillaumeGomez GuillaumeGomez deleted the macro-arg-type-annotation branch April 5, 2026 15:37
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

Successfully merging this pull request may close these issues.

Add syntax for argument type declaration for macros

2 participants