Skip to content

Commit 7bd3bbd

Browse files
committed
Auto merge of #25552 - GuillaumeGomez:left-hand-error, r=pnkfelix
Part of #24407.
2 parents d354309 + db9b435 commit 7bd3bbd

File tree

1 file changed

+73
-10
lines changed

1 file changed

+73
-10
lines changed

src/librustc_typeck/diagnostics.rs

+73-10
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ be specified exactly one time.
282282

283283
E0063: r##"
284284
This error indicates that during an attempt to build a struct or struct-like
285-
enum variant, one of the fields was not provided. Each field should be specified
286-
exactly once.
285+
enum variant, one of the fields was not provided. Each field should be
286+
specified exactly once.
287287
"##,
288288

289289
E0066: r##"
@@ -297,19 +297,36 @@ and [RFC 809] for more details.
297297
"##,
298298

299299
E0067: r##"
300-
The left-hand side of an assignment operator must be an lvalue expression. An
301-
lvalue expression represents a memory location and includes item paths (ie,
302-
namespaced variables), dereferences, indexing expressions, and field references.
300+
The left-hand side of a compound assignment expression must be an lvalue
301+
expression. An lvalue expression represents a memory location and includes
302+
item paths (ie, namespaced variables), dereferences, indexing expressions,
303+
and field references.
303304
305+
Let's start with some bad examples:
304306
```
305307
use std::collections::LinkedList;
306308
307-
// Good
308-
let mut list = LinkedList::new();
309-
310-
311309
// Bad: assignment to non-lvalue expression
312310
LinkedList::new() += 1;
311+
312+
// ...
313+
314+
fn some_func(i: &mut i32) {
315+
i += 12; // Error : '+=' operation cannot be applied on a reference !
316+
}
317+
318+
And now some good examples:
319+
```
320+
let mut i : i32 = 0;
321+
322+
i += 12; // Good !
323+
324+
// ...
325+
326+
fn some_func(i: &mut i32) {
327+
*i += 12; // Good !
328+
}
329+
313330
```
314331
"##,
315332

@@ -328,6 +345,53 @@ Since `return;` is just like `return ();`, there is a mismatch between the
328345
function's return type and the value being returned.
329346
"##,
330347

348+
E0070: r##"
349+
The left-hand side of an assignment operator must be an lvalue expression. An
350+
lvalue expression represents a memory location and can be a variable (with
351+
optional namespacing), a dereference, an indexing expression or a field
352+
reference.
353+
354+
More details can be found here:
355+
https://doc.rust-lang.org/reference.html#lvalues,-rvalues-and-temporaries
356+
357+
Now, we can go further. Here are some bad examples:
358+
```
359+
struct SomeStruct {
360+
x: i32,
361+
y: i32
362+
}
363+
const SOME_CONST : i32 = 12;
364+
365+
fn some_other_func() {}
366+
367+
fn some_function() {
368+
SOME_CONST = 14; // error : a constant value cannot be changed!
369+
1 = 3; // error : 1 isn't a valid lvalue!
370+
some_other_func() = 4; // error : we can't assign value to a function!
371+
SomeStruct.x = 12; // error : SomeStruct a structure name but it is used
372+
// like a variable!
373+
}
374+
```
375+
376+
And now let's give good examples:
377+
378+
```
379+
struct SomeStruct {
380+
x: i32,
381+
y: i32
382+
}
383+
let mut s = SomeStruct {x: 0, y: 0};
384+
385+
s.x = 3; // that's good !
386+
387+
// ...
388+
389+
fn some_func(x: &mut i32) {
390+
*x = 12; // that's good !
391+
}
392+
```
393+
"##,
394+
331395
E0072: r##"
332396
When defining a recursive struct or enum, any use of the type being defined
333397
from inside the definition must occur behind a pointer (like `Box` or `&`).
@@ -931,7 +995,6 @@ register_diagnostics! {
931995
E0060,
932996
E0061,
933997
E0068,
934-
E0070,
935998
E0071,
936999
E0074,
9371000
E0075,

0 commit comments

Comments
 (0)