From c795406e1917c99490fd68d19d10f577fb01a607 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 17 May 2015 23:16:39 +0200 Subject: [PATCH 1/2] Add error explanation for E0070 --- src/librustc_typeck/diagnostics.rs | 55 +++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index bb60de955f0..7b48beae572 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -124,8 +124,8 @@ be specified exactly one time. E0063: r##" This error indicates that during an attempt to build a struct or struct-like -enum variant, one of the fields was not provided. Each field should be specified -exactly once. +enum variant, one of the fields was not provided. Each field should be +specified exactly once. "##, E0066: r##" @@ -141,7 +141,8 @@ and [RFC 809] for more details. E0067: r##" The left-hand side of an assignment operator must be an lvalue expression. An lvalue expression represents a memory location and includes item paths (ie, -namespaced variables), dereferences, indexing expressions, and field references. +namespaced variables), dereferences, indexing expressions, and field +references. ``` use std::collections::LinkedList; @@ -170,6 +171,53 @@ Since `return;` is just like `return ();`, there is a mismatch between the function's return type and the value being returned. "##, +E0070: r##" +The left-hand side of an assignment operator must be an lvalue expression. An +lvalue expression represents a memory location and can be a variable (with +optional namespacing), a dereference, an indexing expression or a field +reference. + +More details can be found here: +https://doc.rust-lang.org/reference.html#lvalues,-rvalues-and-temporaries + +Now, we can go further. Here are some bad examples: +``` +struct SomeStruct { + x: i32, + y: i32 +} +const SOME_CONST : i32 = 12; + +fn some_other_func() {} + +fn some_function() { + SOME_CONST = 14; // error : a constant value cannot be changed! + 1 = 3; // error : 1 isn't a valid lvalue! + some_other_func() = 4; // error : we can't assign value to a function! + SomeStruct.x = 12; // error : SomeStruct a structure name but it is used + // like a variable! +} +``` + +And now let's give good examples: + +``` +struct SomeStruct { + x: i32, + y: i32 +} +let mut s = SomeStruct {x: 0, y: 0}; + +s.x = 3; // that's good ! + +// ... + +fn some_func(x: &mut i32) { + *x = 12; // that's good ! +} +``` +"##, + E0072: r##" When defining a recursive struct or enum, any use of the type being defined from inside the definition must occur behind a pointer (like `Box` or `&`). @@ -779,7 +827,6 @@ register_diagnostics! { E0060, E0061, E0068, - E0070, E0071, E0074, E0075, From db9b4357495b9d64d2ce88f62f2db078f4aadfbc Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 20 May 2015 18:59:12 +0200 Subject: [PATCH 2/2] Typo fix on E0067 --- src/librustc_typeck/diagnostics.rs | 32 ++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 7b48beae572..3c362285e0e 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -139,20 +139,36 @@ and [RFC 809] for more details. "##, E0067: r##" -The left-hand side of an assignment operator must be an lvalue expression. An -lvalue expression represents a memory location and includes item paths (ie, -namespaced variables), dereferences, indexing expressions, and field -references. +The left-hand side of a compound assignment expression must be an lvalue +expression. An lvalue expression represents a memory location and includes +item paths (ie, namespaced variables), dereferences, indexing expressions, +and field references. +Let's start with some bad examples: ``` use std::collections::LinkedList; -// Good -let mut list = LinkedList::new(); - - // Bad: assignment to non-lvalue expression LinkedList::new() += 1; + +// ... + +fn some_func(i: &mut i32) { + i += 12; // Error : '+=' operation cannot be applied on a reference ! +} + +And now some good examples: +``` +let mut i : i32 = 0; + +i += 12; // Good ! + +// ... + +fn some_func(i: &mut i32) { + *i += 12; // Good ! +} + ``` "##,