Skip to content

new filter_fn breaks trait bounds #671

@martinetd

Description

@martinetd

Describe the bug

After updating to 0.15 I can no longer get my filter requiring an argument to be AsRef to build (see reproducer below)

(I'm not convinced I actually need AsRef here, but I couldn't figure any workaround either...)
Anyway I think it's similar to #641 and the macro would need to actively keep track of passed bounds somehow?
I don't have time to look right now, but happy to discuss.

Thanks!

To Reproduce

(this builds with askama 0.14 after removing the filter_fn attribute)

use askama::Template;

mod filters {
    use std::convert::Infallible;

    #[askama::filter_fn]
    pub fn or_dash<'a, T>(
        value: &'a Option<T>,
        _: &'a dyn askama::Values,
    ) -> askama::Result<&'a str, Infallible>
    where
        T: AsRef<str>,
    {
        Ok(match value {
            Some(value) => value.as_ref(),
            None => "--",
        })
    }
}

fn main() {
    #[derive(Template)]
    #[template(source = r#"{{ foo | or_dash }}"#, ext = "txt")]
    struct Template {
        foo: Option<String>,
    }
    let a = Template { foo: None };
    assert_eq!(a.render().unwrap(), "--");
    let a = Template {
        foo: Some("ok".to_string()),
    };
    assert_eq!(a.render().unwrap(), "ok");
}
error[E0599]: the method `as_ref` exists for reference `&T`, but its trait bounds were not satisfied
  --> src/main.rs:15:34
   |
15 |             Some(value) => value.as_ref(),
   |                                  ^^^^^^ method cannot be called on `&T` due to unsatisfied trait bounds
   |
   = note: the following trait bounds were not satisfied:
           `T: AsRef<_>`
           which is required by `&T: AsRef<_>`
   = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `as_ref`, perhaps you need to restrict type parameter `T` with it:
   |
 6 |     #[askama::filter_fn] AsRef</* T */>
   |                          ++++++++++++++

For more information about this error, try `rustc --explain E0599`.

Askama version

0.15.1

Rust version

1.91.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions