Skip to content

Commit

Permalink
Merge pull request #171 from GuillaumeGomez/fix-invalid-cond-opt
Browse files Browse the repository at this point in the history
Fix invalid condition optimization
  • Loading branch information
Kijewski authored Sep 11, 2024
2 parents 7ff1629 + d85a3cb commit 548036f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion rinja_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,10 @@ impl<'a> Generator<'a> {
EvaluatedResult::AlwaysTrue,
WithSpan::new(Expr::BoolLit(true), ""),
),
EvaluatedResult::Unknown => (EvaluatedResult::Unknown, expr),
EvaluatedResult::Unknown => (
EvaluatedResult::Unknown,
WithSpan::new(Expr::Unary("!", Box::new(expr)), span),
),
}
}
Expr::Unary(_, _) => (EvaluatedResult::Unknown, WithSpan::new(expr, span)),
Expand Down
18 changes: 18 additions & 0 deletions rinja_derive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,24 @@ match (
&[("y", "u32")],
3,
);

// Ensure that the `!` is kept .
compare(
"{% if y is defined && !y %}bla{% endif %}",
r#"if *(&(!self.y) as &::core::primitive::bool) {
writer.write_str("bla")?;
}"#,
&[("y", "bool")],
3,
);
compare(
"{% if y is defined && !(y) %}bla{% endif %}",
r#"if *(&(!(self.y)) as &::core::primitive::bool) {
writer.write_str("bla")?;
}"#,
&[("y", "bool")],
3,
);
}

#[test]
Expand Down

0 comments on commit 548036f

Please sign in to comment.