diff options
| author | Malo <57839069+MDLC01@users.noreply.github.com> | 2024-06-16 11:35:18 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-16 09:35:18 +0000 |
| commit | 34550220aee087271769fb9e5d94d1faebe243c1 (patch) | |
| tree | ec2b737f906d8d1e1429471bfadf54fb18fc02bf /crates | |
| parent | f25308d1ebd6554d35d512584ca4c3034f230240 (diff) | |
Add hints to array destructuring error messages (#4400)
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/typst/src/eval/binding.rs | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/crates/typst/src/eval/binding.rs b/crates/typst/src/eval/binding.rs index 8a2d91ee..4201faed 100644 --- a/crates/typst/src/eval/binding.rs +++ b/crates/typst/src/eval/binding.rs @@ -96,7 +96,10 @@ where match p { ast::DestructuringItem::Pattern(pattern) => { let Ok(v) = value.at(i as i64, None) else { - bail!(pattern.span(), "not enough elements to destructure"); + bail!( + pattern.span(), "not enough elements to destructure"; + hint: "the provided array has a length of {len}", + ); }; destructure_impl(vm, pattern, v, f)?; i += 1; @@ -105,7 +108,10 @@ where let sink_size = (1 + len).checked_sub(destruct.items().count()); let sink = sink_size.and_then(|s| value.as_slice().get(i..i + s)); let (Some(sink_size), Some(sink)) = (sink_size, sink) else { - bail!(spread.span(), "not enough elements to destructure"); + bail!( + spread.span(), "not enough elements to destructure"; + hint: "the provided array has a length of {len}", + ); }; if let Some(expr) = spread.sink_expr() { f(vm, expr, Value::Array(sink.into()))?; @@ -119,7 +125,22 @@ where } if i < len { - bail!(destruct.span(), "too many elements to destructure"); + if i == 0 { + bail!( + destruct.span(), "too many elements to destructure"; + hint: "the provided array has a length of {len}, but the pattern expects an empty array", + ) + } else if i == 1 { + bail!( + destruct.span(), "too many elements to destructure"; + hint: "the provided array has a length of {len}, but the pattern expects a single element", + ); + } else { + bail!( + destruct.span(), "too many elements to destructure"; + hint: "the provided array has a length of {len}, but the pattern expects {i} elements", + ); + } } Ok(()) |
