1
Fork 0

Improve invalid assignment error

This commit is contained in:
varkor 2019-12-22 18:42:15 +00:00
parent a5991c57cf
commit b7bfdbe681
15 changed files with 92 additions and 49 deletions

View file

@ -753,9 +753,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} }
err.emit(); err.emit();
} else if !lhs.is_syntactic_place_expr() { } else if !lhs.is_syntactic_place_expr() {
struct_span_err!(self.tcx.sess, expr.span, E0070, "invalid left-hand side expression") struct_span_err!(
.span_label(expr.span, "left-hand of expression not valid") self.tcx.sess,
.emit(); expr.span,
E0070,
"invalid left-hand side of assignment",
).span_label(lhs.span, "cannot assign to this expression").emit();
} }
self.require_type_is_sized(lhs_ty, lhs.span, traits::AssignmentLhsSized); self.require_type_is_sized(lhs_ty, lhs.span, traits::AssignmentLhsSized);

View file

@ -36,12 +36,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if !lhs_expr.is_syntactic_place_expr() { if !lhs_expr.is_syntactic_place_expr() {
struct_span_err!( struct_span_err!(
self.tcx.sess, self.tcx.sess,
lhs_expr.span, op.span,
E0067, E0067,
"invalid left-hand side expression" "invalid left-hand side of assignment",
) ).span_label(lhs_expr.span, "cannot assign to this expression").emit();
.span_label(lhs_expr.span, "invalid expression for left-hand side")
.emit();
} }
ty ty
} }

View file

@ -1,10 +1,10 @@
fn main() { fn main() {
1 = 2; //~ ERROR invalid left-hand side expression 1 = 2; //~ ERROR invalid left-hand side of assignment
1 += 2; //~ ERROR invalid left-hand side expression 1 += 2; //~ ERROR invalid left-hand side of assignment
(1, 2) = (3, 4); //~ ERROR invalid left-hand side expression (1, 2) = (3, 4); //~ ERROR invalid left-hand side of assignment
let (a, b) = (1, 2); let (a, b) = (1, 2);
(a, b) = (3, 4); //~ ERROR invalid left-hand side expression (a, b) = (3, 4); //~ ERROR invalid left-hand side of assignment
None = Some(3); //~ ERROR invalid left-hand side expression None = Some(3); //~ ERROR invalid left-hand side of assignment
} }

View file

@ -1,32 +1,42 @@
error[E0070]: invalid left-hand side expression error[E0070]: invalid left-hand side of assignment
--> $DIR/bad-expr-lhs.rs:2:5 --> $DIR/bad-expr-lhs.rs:2:5
| |
LL | 1 = 2; LL | 1 = 2;
| ^^^^^ left-hand of expression not valid | -^^^^
| |
| cannot assign to this expression
error[E0067]: invalid left-hand side expression error[E0067]: invalid left-hand side of assignment
--> $DIR/bad-expr-lhs.rs:3:5 --> $DIR/bad-expr-lhs.rs:3:7
| |
LL | 1 += 2; LL | 1 += 2;
| ^ invalid expression for left-hand side | - ^^
| |
| cannot assign to this expression
error[E0070]: invalid left-hand side expression error[E0070]: invalid left-hand side of assignment
--> $DIR/bad-expr-lhs.rs:4:5 --> $DIR/bad-expr-lhs.rs:4:5
| |
LL | (1, 2) = (3, 4); LL | (1, 2) = (3, 4);
| ^^^^^^^^^^^^^^^ left-hand of expression not valid | ------^^^^^^^^^
| |
| cannot assign to this expression
error[E0070]: invalid left-hand side expression error[E0070]: invalid left-hand side of assignment
--> $DIR/bad-expr-lhs.rs:7:5 --> $DIR/bad-expr-lhs.rs:7:5
| |
LL | (a, b) = (3, 4); LL | (a, b) = (3, 4);
| ^^^^^^^^^^^^^^^ left-hand of expression not valid | ------^^^^^^^^^
| |
| cannot assign to this expression
error[E0070]: invalid left-hand side expression error[E0070]: invalid left-hand side of assignment
--> $DIR/bad-expr-lhs.rs:9:5 --> $DIR/bad-expr-lhs.rs:9:5
| |
LL | None = Some(3); LL | None = Some(3);
| ^^^^^^^^^^^^^^ left-hand of expression not valid | ----^^^^^^^^^^
| |
| cannot assign to this expression
error: aborting due to 5 previous errors error: aborting due to 5 previous errors

View file

@ -8,11 +8,13 @@ LL | LinkedList::new() += 1;
| |
= note: an implementation of `std::ops::AddAssign` might be missing for `std::collections::LinkedList<_>` = note: an implementation of `std::ops::AddAssign` might be missing for `std::collections::LinkedList<_>`
error[E0067]: invalid left-hand side expression error[E0067]: invalid left-hand side of assignment
--> $DIR/E0067.rs:4:5 --> $DIR/E0067.rs:4:23
| |
LL | LinkedList::new() += 1; LL | LinkedList::new() += 1;
| ^^^^^^^^^^^^^^^^^ invalid expression for left-hand side | ----------------- ^^
| |
| cannot assign to this expression
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -1,14 +1,18 @@
error[E0070]: invalid left-hand side expression error[E0070]: invalid left-hand side of assignment
--> $DIR/E0070.rs:6:5 --> $DIR/E0070.rs:6:5
| |
LL | SOME_CONST = 14; LL | SOME_CONST = 14;
| ^^^^^^^^^^^^^^^ left-hand of expression not valid | ----------^^^^^
| |
| cannot assign to this expression
error[E0070]: invalid left-hand side expression error[E0070]: invalid left-hand side of assignment
--> $DIR/E0070.rs:7:5 --> $DIR/E0070.rs:7:5
| |
LL | 1 = 3; LL | 1 = 3;
| ^^^^^ left-hand of expression not valid | -^^^^
| |
| cannot assign to this expression
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/E0070.rs:8:25 --> $DIR/E0070.rs:8:25
@ -16,11 +20,13 @@ error[E0308]: mismatched types
LL | some_other_func() = 4; LL | some_other_func() = 4;
| ^ expected `()`, found integer | ^ expected `()`, found integer
error[E0070]: invalid left-hand side expression error[E0070]: invalid left-hand side of assignment
--> $DIR/E0070.rs:8:5 --> $DIR/E0070.rs:8:5
| |
LL | some_other_func() = 4; LL | some_other_func() = 4;
| ^^^^^^^^^^^^^^^^^^^^^ left-hand of expression not valid | -----------------^^^^
| |
| cannot assign to this expression
error: aborting due to 4 previous errors error: aborting due to 4 previous errors

View file

@ -4,7 +4,7 @@ mod A {
fn main() { fn main() {
A::C = 1; A::C = 1;
//~^ ERROR: invalid left-hand side expression //~^ ERROR: invalid left-hand side of assignment
//~| ERROR: mismatched types //~| ERROR: mismatched types
//~| ERROR: struct `C` is private //~| ERROR: struct `C` is private
} }

View file

@ -10,11 +10,13 @@ error[E0308]: mismatched types
LL | A::C = 1; LL | A::C = 1;
| ^ expected struct `A::C`, found integer | ^ expected struct `A::C`, found integer
error[E0070]: invalid left-hand side expression error[E0070]: invalid left-hand side of assignment
--> $DIR/issue-13407.rs:6:5 --> $DIR/issue-13407.rs:6:5
| |
LL | A::C = 1; LL | A::C = 1;
| ^^^^^^^^ left-hand of expression not valid | ----^^^^
| |
| cannot assign to this expression
error: aborting due to 3 previous errors error: aborting due to 3 previous errors

View file

@ -1,7 +1,9 @@
macro_rules! not_a_place { macro_rules! not_a_place {
($thing:expr) => { ($thing:expr) => {
$thing = 42; $thing = 42;
//~^ ERROR invalid left-hand side expression //~^ ERROR invalid left-hand side of assignment
$thing += 42;
//~^ ERROR invalid left-hand side of assignment
} }
} }

View file

@ -1,12 +1,28 @@
error[E0070]: invalid left-hand side expression error[E0070]: invalid left-hand side of assignment
--> $DIR/issue-26093.rs:3:9 --> $DIR/issue-26093.rs:3:9
| |
LL | $thing = 42; LL | $thing = 42;
| ^^^^^^^^^^^ left-hand of expression not valid | ^^^^^^^^^^^
... ...
LL | not_a_place!(99); LL | not_a_place!(99);
| ----------------- in this macro invocation | -----------------
| | |
| | cannot assign to this expression
| in this macro invocation
error: aborting due to previous error error[E0067]: invalid left-hand side of assignment
--> $DIR/issue-26093.rs:5:16
|
LL | $thing += 42;
| ^^
...
LL | not_a_place!(99);
| -----------------
| | |
| | cannot assign to this expression
| in this macro invocation
For more information about this error, try `rustc --explain E0070`. error: aborting due to 2 previous errors
Some errors have detailed explanations: E0067, E0070.
For more information about an error, try `rustc --explain E0067`.

View file

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

View file

@ -35,11 +35,13 @@ LL | let sr: Vec<(u32, _, _) = vec![];
found struct `std::vec::Vec<_>` found struct `std::vec::Vec<_>`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error[E0070]: invalid left-hand side expression error[E0070]: invalid left-hand side of assignment
--> $DIR/issue-34334.rs:2:13 --> $DIR/issue-34334.rs:2:13
| |
LL | let sr: Vec<(u32, _, _) = vec![]; LL | let sr: Vec<(u32, _, _) = vec![];
| ^^^^^^^^^^^^^^^^^^^^^^^^ left-hand of expression not valid | ---------------^^^^^^^^^
| |
| cannot assign to this expression
error[E0599]: no method named `iter` found for type `()` in the current scope error[E0599]: no method named `iter` found for type `()` in the current scope
--> $DIR/issue-34334.rs:9:36 --> $DIR/issue-34334.rs:9:36

View file

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

View file

@ -97,11 +97,13 @@ LL | || (0 = 0);
| expected `bool`, found `()` | expected `bool`, found `()`
| help: try comparing for equality: `0 == 0` | help: try comparing for equality: `0 == 0`
error[E0070]: invalid left-hand side expression error[E0070]: invalid left-hand side of assignment
--> $DIR/assignment-expected-bool.rs:31:20 --> $DIR/assignment-expected-bool.rs:31:20
| |
LL | let _: usize = 0 = 0; LL | let _: usize = 0 = 0;
| ^^^^^ left-hand of expression not valid | -^^^^
| |
| cannot assign to this expression
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/assignment-expected-bool.rs:31:20 --> $DIR/assignment-expected-bool.rs:31:20

View file

@ -26,7 +26,7 @@ fn main() {
//~^ ERROR mismatched types //~^ ERROR mismatched types
println!("{}", x); println!("{}", x);
} }
// "invalid left-hand side expression" error is suppresed // "invalid left-hand side of assignment" error is suppresed
if 3 = x { if 3 = x {
//~^ ERROR mismatched types //~^ ERROR mismatched types
println!("{}", x); println!("{}", x);