Skip to main content

ide_diagnostics/handlers/
pattern_arg_in_extern_fn.rs

1use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
2
3// Diagnostic: pattern-arg-in-extern-fn
4//
5// This diagnostic is triggered if a pattern was declared as an argument in a foreign function declaration.
6pub(crate) fn pattern_arg_in_extern_fn(
7    ctx: &DiagnosticsContext<'_, '_>,
8    d: &hir::PatternArgInExternFn,
9) -> Diagnostic {
10    Diagnostic::new_with_syntax_node_ptr(
11        ctx,
12        DiagnosticCode::RustcHardError("E0130"),
13        "patterns aren't allowed in foreign function declarations",
14        d.node.map(Into::into),
15    )
16    .stable()
17}
18
19#[cfg(test)]
20mod tests {
21    use crate::tests::check_diagnostics;
22
23    #[test]
24    fn tuple_pattern() {
25        check_diagnostics(
26            r#"
27unsafe extern { fn foo((a, b): (u32, u32)); }
28                    // ^^^^^^ error: patterns aren't allowed in foreign function declarations
29            "#,
30        );
31    }
32
33    #[test]
34    fn struct_pattern() {
35        check_diagnostics(
36            r#"
37struct Foo(u32, u32);
38unsafe extern { fn foo(Foo(a, b): Foo); }
39                    // ^^^^^^^^^ error: patterns aren't allowed in foreign function declarations
40            "#,
41        );
42
43        check_diagnostics(
44            r#"
45struct Foo{ bar: u32, baz: u32 }
46unsafe extern { fn foo(Foo { bar, baz }: Foo); }
47                    // ^^^^^^^^^^^^^^^^ error: patterns aren't allowed in foreign function declarations
48            "#,
49        );
50    }
51
52    #[test]
53    fn pattern_is_second_arg() {
54        check_diagnostics(
55            r#"
56struct Foo(u32, u32);
57unsafe extern { fn foo(okay: u32, Foo(a, b): Foo); }
58                               // ^^^^^^^^^ error: patterns aren't allowed in foreign function declarations
59            "#,
60        );
61    }
62
63    #[test]
64    fn non_simple_ident() {
65        check_diagnostics(
66            r#"
67unsafe extern { fn foo(ref a: u32); }
68                    // ^^^^^ error: patterns aren't allowed in foreign function declarations
69            "#,
70        );
71
72        check_diagnostics(
73            r#"
74unsafe extern { fn foo(mut a: u32); }
75                    // ^^^^^ error: patterns aren't allowed in foreign function declarations
76            "#,
77        );
78
79        check_diagnostics(
80            r#"
81unsafe extern { fn foo(a @ _: u32); }
82                    // ^^^^^ error: patterns aren't allowed in foreign function declarations
83            "#,
84        );
85    }
86}