Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error explanation for E0070 #25552

Merged
merged 2 commits into from
May 21, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 73 additions & 10 deletions src/librustc_typeck/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect your text editor has done the wrapping here, but these lines of 80 chars are actually fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't even see that. I must have failed my wrapping somewhere... Well, if it is needed, I can undo this. ^^

"##,

E0066: r##"
Expand All @@ -139,19 +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 !
}

```
"##,

Expand All @@ -170,6 +187,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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some explanation about why using a static mut instead of a const would be helpful here. Perhaps this can be something like "The assignment to a const above is incorrect because a const is a constant value and is not associated with a memory location. You may want a static mut instead, which looks like this:"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum... Well, I guess it cannot hurt.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be too much explanation, I'm not sure! Perhaps it is obvious that you cannot assign to a constant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, since we give an example above, it cannot hurt. The goal is to help developers as much as possible after all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I don't know if pointing people at static mut is necessarily what we want to do here, as opposed to a non-mut static that holds an appropriate atomic data type... we can point people at static mut, but then they'll have to use unsafe code to access it (for good reason) ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the best would be to just avoid static references, it's not the topic after all.


```
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 `&`).
Expand Down Expand Up @@ -779,7 +843,6 @@ register_diagnostics! {
E0060,
E0061,
E0068,
E0070,
E0071,
E0074,
E0075,
Expand Down