askama icon indicating copy to clipboard operation
askama copied to clipboard

Askama's let operator tries to move out of self

Open msrd0 opened this issue 4 years ago • 1 comments

For code like {% let foo = bar %}, askama emits let foo = self.bar in rust code, which only works if bar is of a type that implements Copy. Error message looks similar to this:

 --> tests/dashboard.rs:8:10
  |
8 | #[derive(Template)]
  |          ^^^^^^^^
  |          |
  |          move occurs because `self.style.dashboard` has type `std::string::String`, which does not implement the `Copy` trait
  |          help: consider borrowing here: `&Template`
  |
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

msrd0 avatar Nov 10 '20 14:11 msrd0

I considered this issue while working on #396. I ended up not implicitly borrowing for let declarations. The reason being that it semantically mimics Rust more closure. I felt it would be more confusing to have to call clone() when self.bar is already copyable. Thereby it would be more natural to call e.g. as_str() or as_ref() when knowing that it otherwise would get moved.

Ideas are welcome. The main issue in relation to this, is that the generator does not have any type information available.

vallentin avatar Dec 10 '20 21:12 vallentin