Skip to content

Commit 5c92da4

Browse files
committed
Improve invalid assignment error
1 parent bbcf9b1 commit 5c92da4

15 files changed

+94
-51
lines changed

src/librustc_typeck/check/expr.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -791,10 +791,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
791791
}
792792
err.emit();
793793
} else if !lhs.is_syntactic_place_expr() {
794-
struct_span_err!(self.tcx.sess, expr.span, E0070,
795-
"invalid left-hand side expression")
796-
.span_label(expr.span, "left-hand of expression not valid")
797-
.emit();
794+
struct_span_err!(
795+
self.tcx.sess,
796+
expr.span,
797+
E0070,
798+
"invalid left-hand side of assignment",
799+
).span_label(lhs.span, "cannot assign to this expression").emit();
798800
}
799801

800802
self.require_type_is_sized(lhs_ty, lhs.span, traits::AssignmentLhsSized);

src/librustc_typeck/check/op.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3535

3636
if !lhs_expr.is_syntactic_place_expr() {
3737
struct_span_err!(
38-
self.tcx.sess, lhs_expr.span,
39-
E0067, "invalid left-hand side expression")
40-
.span_label(
41-
lhs_expr.span,
42-
"invalid expression for left-hand side")
43-
.emit();
38+
self.tcx.sess,
39+
op.span,
40+
E0067,
41+
"invalid left-hand side of assignment",
42+
).span_label(lhs_expr.span, "cannot assign to this expression").emit();
4443
}
4544
ty
4645
}

src/test/ui/bad/bad-expr-lhs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
fn main() {
2-
1 = 2; //~ ERROR invalid left-hand side expression
3-
1 += 2; //~ ERROR invalid left-hand side expression
4-
(1, 2) = (3, 4); //~ ERROR invalid left-hand side expression
2+
1 = 2; //~ ERROR invalid left-hand side of assignment
3+
1 += 2; //~ ERROR invalid left-hand side of assignment
4+
(1, 2) = (3, 4); //~ ERROR invalid left-hand side of assignment
55

66
let (a, b) = (1, 2);
7-
(a, b) = (3, 4); //~ ERROR invalid left-hand side expression
7+
(a, b) = (3, 4); //~ ERROR invalid left-hand side of assignment
88

9-
None = Some(3); //~ ERROR invalid left-hand side expression
9+
None = Some(3); //~ ERROR invalid left-hand side of assignment
1010
}

src/test/ui/bad/bad-expr-lhs.stderr

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
1-
error[E0070]: invalid left-hand side expression
1+
error[E0070]: invalid left-hand side of assignment
22
--> $DIR/bad-expr-lhs.rs:2:5
33
|
44
LL | 1 = 2;
5-
| ^^^^^ left-hand of expression not valid
5+
| -^^^^
6+
| |
7+
| cannot assign to this expression
68

7-
error[E0067]: invalid left-hand side expression
8-
--> $DIR/bad-expr-lhs.rs:3:5
9+
error[E0067]: invalid left-hand side of assignment
10+
--> $DIR/bad-expr-lhs.rs:3:7
911
|
1012
LL | 1 += 2;
11-
| ^ invalid expression for left-hand side
13+
| - ^^
14+
| |
15+
| cannot assign to this expression
1216

13-
error[E0070]: invalid left-hand side expression
17+
error[E0070]: invalid left-hand side of assignment
1418
--> $DIR/bad-expr-lhs.rs:4:5
1519
|
1620
LL | (1, 2) = (3, 4);
17-
| ^^^^^^^^^^^^^^^ left-hand of expression not valid
21+
| ------^^^^^^^^^
22+
| |
23+
| cannot assign to this expression
1824

19-
error[E0070]: invalid left-hand side expression
25+
error[E0070]: invalid left-hand side of assignment
2026
--> $DIR/bad-expr-lhs.rs:7:5
2127
|
2228
LL | (a, b) = (3, 4);
23-
| ^^^^^^^^^^^^^^^ left-hand of expression not valid
29+
| ------^^^^^^^^^
30+
| |
31+
| cannot assign to this expression
2432

25-
error[E0070]: invalid left-hand side expression
33+
error[E0070]: invalid left-hand side of assignment
2634
--> $DIR/bad-expr-lhs.rs:9:5
2735
|
2836
LL | None = Some(3);
29-
| ^^^^^^^^^^^^^^ left-hand of expression not valid
37+
| ----^^^^^^^^^^
38+
| |
39+
| cannot assign to this expression
3040

3141
error: aborting due to 5 previous errors
3242

src/test/ui/error-codes/E0067.stderr

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ LL | LinkedList::new() += 1;
88
|
99
= note: an implementation of `std::ops::AddAssign` might be missing for `std::collections::LinkedList<_>`
1010

11-
error[E0067]: invalid left-hand side expression
12-
--> $DIR/E0067.rs:4:5
11+
error[E0067]: invalid left-hand side of assignment
12+
--> $DIR/E0067.rs:4:23
1313
|
1414
LL | LinkedList::new() += 1;
15-
| ^^^^^^^^^^^^^^^^^ invalid expression for left-hand side
15+
| ----------------- ^^
16+
| |
17+
| cannot assign to this expression
1618

1719
error: aborting due to 2 previous errors
1820

src/test/ui/error-codes/E0070.stderr

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
error[E0070]: invalid left-hand side expression
1+
error[E0070]: invalid left-hand side of assignment
22
--> $DIR/E0070.rs:6:5
33
|
44
LL | SOME_CONST = 14;
5-
| ^^^^^^^^^^^^^^^ left-hand of expression not valid
5+
| ----------^^^^^
6+
| |
7+
| cannot assign to this expression
68

7-
error[E0070]: invalid left-hand side expression
9+
error[E0070]: invalid left-hand side of assignment
810
--> $DIR/E0070.rs:7:5
911
|
1012
LL | 1 = 3;
11-
| ^^^^^ left-hand of expression not valid
13+
| -^^^^
14+
| |
15+
| cannot assign to this expression
1216

1317
error[E0308]: mismatched types
1418
--> $DIR/E0070.rs:8:25
1519
|
1620
LL | some_other_func() = 4;
1721
| ^ expected `()`, found integer
1822

19-
error[E0070]: invalid left-hand side expression
23+
error[E0070]: invalid left-hand side of assignment
2024
--> $DIR/E0070.rs:8:5
2125
|
2226
LL | some_other_func() = 4;
23-
| ^^^^^^^^^^^^^^^^^^^^^ left-hand of expression not valid
27+
| -----------------^^^^
28+
| |
29+
| cannot assign to this expression
2430

2531
error: aborting due to 4 previous errors
2632

src/test/ui/issues/issue-13407.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod A {
44

55
fn main() {
66
A::C = 1;
7-
//~^ ERROR: invalid left-hand side expression
7+
//~^ ERROR: invalid left-hand side of assignment
88
//~| ERROR: mismatched types
99
//~| ERROR: struct `C` is private
1010
}

src/test/ui/issues/issue-13407.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ error[E0308]: mismatched types
1010
LL | A::C = 1;
1111
| ^ expected struct `A::C`, found integer
1212

13-
error[E0070]: invalid left-hand side expression
13+
error[E0070]: invalid left-hand side of assignment
1414
--> $DIR/issue-13407.rs:6:5
1515
|
1616
LL | A::C = 1;
17-
| ^^^^^^^^ left-hand of expression not valid
17+
| ----^^^^
18+
| |
19+
| cannot assign to this expression
1820

1921
error: aborting due to 3 previous errors
2022

src/test/ui/issues/issue-26093.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
macro_rules! not_a_place {
22
($thing:expr) => {
33
$thing = 42;
4-
//~^ ERROR invalid left-hand side expression
4+
//~^ ERROR invalid left-hand side of assignment
5+
$thing += 42;
6+
//~^ ERROR invalid left-hand side of assignment
57
}
68
}
79

src/test/ui/issues/issue-26093.stderr

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1-
error[E0070]: invalid left-hand side expression
1+
error[E0070]: invalid left-hand side of assignment
22
--> $DIR/issue-26093.rs:3:9
33
|
44
LL | $thing = 42;
5-
| ^^^^^^^^^^^ left-hand of expression not valid
5+
| ^^^^^^^^^^^
66
...
77
LL | not_a_place!(99);
8-
| ----------------- in this macro invocation
8+
| -----------------
9+
| | |
10+
| | cannot assign to this expression
11+
| in this macro invocation
912

10-
error: aborting due to previous error
13+
error[E0067]: invalid left-hand side of assignment
14+
--> $DIR/issue-26093.rs:5:16
15+
|
16+
LL | $thing += 42;
17+
| ^^
18+
...
19+
LL | not_a_place!(99);
20+
| -----------------
21+
| | |
22+
| | cannot assign to this expression
23+
| in this macro invocation
24+
25+
error: aborting due to 2 previous errors
1126

12-
For more information about this error, try `rustc --explain E0070`.
27+
Some errors have detailed explanations: E0067, E0070.
28+
For more information about an error, try `rustc --explain E0067`.

0 commit comments

Comments
 (0)