Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

// Diagnostic: array-pattern-without-fixed-length
//
// This diagnostic is triggered when a rest array pattern is matched against an
// array with a non-constant length.
pub(crate) fn array_pattern_without_fixed_length(
    ctx: &DiagnosticsContext<'_, '_>,
    d: &hir::ArrayPatternWithoutFixedLength,
) -> Diagnostic {
    Diagnostic::new_with_syntax_node_ptr(
        ctx,
        DiagnosticCode::RustcHardError("E0730"),
        "cannot pattern-match on an array without a fixed length",
        d.pat.map(Into::into),
    )
    .stable()
}

#[cfg(test)]
mod tests {
    use crate::tests::check_diagnostics;

    #[test]
    fn array_pattern_without_fixed_length() {
        check_diagnostics(
            r#"
fn f<const N: usize>(arr: [u8; N]) {
    let [_head, _tail @ ..] = arr;
      //^^^^^^^^^^^^^^^^^^^ error: cannot pattern-match on an array without a fixed length
}
"#,
        );
    }

    #[test]
    fn fixed_length_array_pattern_is_ok() {
        check_diagnostics(
            r#"
fn f(arr: [u8; 3]) {
    let [_head, _tail @ ..] = arr;
}
"#,
        );
    }
}