From a199defeca2dfc6aa3e972acca82c96db07f99e9 Mon Sep 17 00:00:00 2001 From: Christian Vallentin Date: Tue, 1 Dec 2020 17:01:05 +0100 Subject: Improved if statement generation to avoid issues with implicit borrows (#392) * Changed to automatically coerce to bool * Added new test case * Updated test case to include else if --- askama_shared/src/generator.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'askama_shared') diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index 3a91e3c..ad086a3 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -518,8 +518,15 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { buf.dedent()?; buf.write("} else if "); } + // The following syntax `*(&(...) as &bool)` is used to + // trigger Rust's automatic dereferencing, to coerce + // e.g. `&&&&&bool` to `bool`. First `&(...) as &bool` + // coerces e.g. `&&&bool` to `&bool`. Then `*(&bool)` + // finally dereferences it to `bool`. + buf.write("*(&("); let expr_code = self.visit_expr_root(expr)?; buf.write(&expr_code); + buf.write(") as &bool)"); } None => { buf.dedent()?; -- cgit