diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index acd716b38..daf22aee1 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -1328,7 +1328,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { "cycle" => match args { [arg] => { if matches!(arg, Expr::Array(arr) if arr.is_empty()) { - panic!("loop.cycle(…) cannot use an empty array."); + return Err("loop.cycle(…) cannot use an empty array".into()); } buf.write("({"); buf.write("let _cycle = &("); diff --git a/testing/tests/ui/loop_cycle_empty.rs b/testing/tests/ui/loop_cycle_empty.rs new file mode 100644 index 000000000..4aa601072 --- /dev/null +++ b/testing/tests/ui/loop_cycle_empty.rs @@ -0,0 +1,14 @@ +// Nb. this test fails because currently an empty array "[]" is always a syntax error in askama, +// but even if this changes, this test should keep failing, but possibly with another error message + +use askama::Template; + +#[derive(Template)] +#[template( + source = r#"{% for v in values %}{{ loop.cycle([]) }}{{ v }},{% endfor %}"#, + ext = "txt" +)] +struct ForCycleEmpty; + +fn main() { +} diff --git a/testing/tests/ui/loop_cycle_empty.stderr b/testing/tests/ui/loop_cycle_empty.stderr new file mode 100644 index 000000000..13beb9a82 --- /dev/null +++ b/testing/tests/ui/loop_cycle_empty.stderr @@ -0,0 +1,8 @@ +error: problems parsing template source at row 1, column 34 near: + "([]) }}{{ v }},{% endfor %}" + --> tests/ui/loop_cycle_empty.rs:6:10 + | +6 | #[derive(Template)] + | ^^^^^^^^ + | + = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)