Skip to main content

ide_diagnostics/handlers/
non_exhaustive_record_expr.rs

1use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
2
3// Diagnostic: non-exhaustive-record-expr
4//
5// This diagnostic is triggered if a struct expression constructs a `#[non_exhaustive]`
6// struct from another crate.
7pub(crate) fn non_exhaustive_record_expr(
8    ctx: &DiagnosticsContext<'_, '_>,
9    d: &hir::NonExhaustiveRecordExpr,
10) -> Diagnostic {
11    Diagnostic::new_with_syntax_node_ptr(
12        ctx,
13        DiagnosticCode::RustcHardError("E0639"),
14        "cannot create non-exhaustive struct using struct expression",
15        d.expr.map(|it| it.into()),
16    )
17    .stable()
18}
19
20#[cfg(test)]
21mod tests {
22    use crate::tests::check_diagnostics;
23
24    #[test]
25    fn reports_external_non_exhaustive_struct_literal() {
26        check_diagnostics(
27            r#"
28//- /lib.rs crate:lib
29#[non_exhaustive]
30pub struct S {
31    pub field: u32,
32}
33
34fn local_ok() {
35    let _ = S { field: 0 };
36}
37
38//- /main.rs crate:main deps:lib
39fn main() {
40    let _ = lib::S { field: 0 };
41          //^^^^^^^^^^^^^^^^^^^ error: cannot create non-exhaustive struct using struct expression
42}
43"#,
44        );
45    }
46}