Skip to main content

ide_diagnostics/handlers/
functional_record_update_on_non_struct.rs

1use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
2
3// Diagnostic: functional-record-update-on-non-struct
4//
5// This diagnostic is triggered when functional record update syntax is used on
6// something other than a struct.
7pub(crate) fn functional_record_update_on_non_struct(
8    ctx: &DiagnosticsContext<'_, '_>,
9    d: &hir::FunctionalRecordUpdateOnNonStruct,
10) -> Diagnostic {
11    Diagnostic::new_with_syntax_node_ptr(
12        ctx,
13        DiagnosticCode::RustcHardError("E0436"),
14        "functional record update syntax requires a struct",
15        d.base_expr.map(Into::into),
16    )
17    .stable()
18}
19
20#[cfg(test)]
21mod tests {
22    use crate::tests::check_diagnostics;
23
24    #[test]
25    fn enum_variant_record_update() {
26        check_diagnostics(
27            r#"
28enum E {
29    V { x: i32, y: i32 },
30}
31
32fn f(e: E) {
33    let _ = E::V { x: 0, ..e };
34                         //^ error: functional record update syntax requires a struct
35}
36"#,
37        );
38    }
39
40    #[test]
41    fn struct_record_update() {
42        check_diagnostics(
43            r#"
44struct S {
45    x: i32,
46    y: i32,
47}
48
49fn f(s: S) {
50    let _ = S { x: 0, ..s };
51}
52"#,
53        );
54    }
55}