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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

// Diagnostic: invalid-lhs-of-assignment
//
// This diagnostic is triggered if the left-hand side of an assignment can't be assigned to.
pub(crate) fn invalid_lhs_of_assignment(
    ctx: &DiagnosticsContext<'_, '_>,
    d: &hir::InvalidLhsOfAssignment,
) -> Diagnostic {
    Diagnostic::new_with_syntax_node_ptr(
        ctx,
        DiagnosticCode::RustcHardError("E0067"),
        "invalid left-hand side of assignment",
        d.lhs.map(Into::into),
    )
    .stable()
}

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

    #[test]
    fn unit_struct_literal() {
        check_diagnostics(
            r#"
//- minicore: add
struct Struct;
impl core::ops::AddAssign for Struct {
    fn add_assign(&mut self, _other: Self) {}
}
fn test() {
    Struct += Struct;
 // ^^^^^^ error: invalid left-hand side of assignment
}
        "#,
        );
    }

    #[test]
    fn struct_literal() {
        check_diagnostics(
            r#"
//- minicore: add
struct Struct { foo: i32, bar: i32 }
impl core::ops::AddAssign for Struct {
    fn add_assign(&mut self, _other: Self) {}
}
fn test() {
    Struct { foo: 0, bar: 0 } += Struct { foo: 1, bar: 2 };
 // ^^^^^^^^^^^^^^^^^^^^^^^^^ error: invalid left-hand side of assignment
}
        "#,
        );
    }

    #[test]
    fn destructuring_assignment() {
        // no diagnostic, as `=` is not a _compound_ assignment
        check_diagnostics(
            r#"
//- minicore: add
struct Struct { foo: i32, bar: i32 }
impl core::ops::AddAssign for Struct {
    fn add_assign(&mut self, _other: Self) {}
}
fn test(mut foo: i32, mut bar: i32) {
    Struct { foo, bar } = Struct { foo: 1, bar: 2 };
}
        "#,
        );
    }

    #[test]
    fn destructuring_compound_assignment() {
        check_diagnostics(
            r#"
//- minicore: add
struct Struct { foo: i32, bar: i32 }
impl core::ops::AddAssign for Struct {
    fn add_assign(&mut self, _other: Self) {}
}
fn test(foo: i32, bar: i32) {
    Struct { foo, bar } += Struct { foo: 1, bar: 2 };
 // ^^^^^^^^^^^^^^^^^^^ error: invalid left-hand side of assignment
}
        "#,
        );
    }
}