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

+6-4
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

+5-6
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

+5-5
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

+21-11
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

+5-3
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

+12-6
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

+1-1
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

+4-2
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

+3-1
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

+21-5
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`.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ fn main () {
33
//~^ ERROR expected one of `,` or `>`, found `=`
44
//~| ERROR expected value, found struct `Vec`
55
//~| ERROR mismatched types
6-
//~| ERROR invalid left-hand side expression
6+
//~| ERROR invalid left-hand side of assignment
77
//~| ERROR expected expression, found reserved identifier `_`
88
//~| ERROR expected expression, found reserved identifier `_`
99
let sr2: Vec<(u32, _, _)> = sr.iter().map(|(faction, th_sender, th_receiver)| {}).collect();

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ LL | let sr: Vec<(u32, _, _) = vec![];
3535
found struct `std::vec::Vec<_>`
3636
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
3737

38-
error[E0070]: invalid left-hand side expression
38+
error[E0070]: invalid left-hand side of assignment
3939
--> $DIR/issue-34334.rs:2:13
4040
|
4141
LL | let sr: Vec<(u32, _, _) = vec![];
42-
| ^^^^^^^^^^^^^^^^^^^^^^^^ left-hand of expression not valid
42+
| ---------------^^^^^^^^^
43+
| |
44+
| cannot assign to this expression
4345

4446
error[E0599]: no method named `iter` found for type `()` in the current scope
4547
--> $DIR/issue-34334.rs:9:36

src/test/ui/type/type-check/assignment-expected-bool.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ fn main() {
3030
// A test to check that not expecting `bool` behaves well:
3131
let _: usize = 0 = 0;
3232
//~^ ERROR mismatched types [E0308]
33-
//~| ERROR invalid left-hand side expression [E0070]
33+
//~| ERROR invalid left-hand side of assignment [E0070]
3434
}

src/test/ui/type/type-check/assignment-expected-bool.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ LL | || (0 = 0);
9797
| expected `bool`, found `()`
9898
| help: try comparing for equality: `0 == 0`
9999

100-
error[E0070]: invalid left-hand side expression
100+
error[E0070]: invalid left-hand side of assignment
101101
--> $DIR/assignment-expected-bool.rs:31:20
102102
|
103103
LL | let _: usize = 0 = 0;
104-
| ^^^^^ left-hand of expression not valid
104+
| -^^^^
105+
| |
106+
| cannot assign to this expression
105107

106108
error[E0308]: mismatched types
107109
--> $DIR/assignment-expected-bool.rs:31:20

src/test/ui/type/type-check/assignment-in-if.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626
//~^ ERROR mismatched types
2727
println!("{}", x);
2828
}
29-
// "invalid left-hand side expression" error is suppresed
29+
// "invalid left-hand side of assignment" error is suppresed
3030
if 3 = x {
3131
//~^ ERROR mismatched types
3232
println!("{}", x);

0 commit comments

Comments
 (0)