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 detailed error explanation for E0389 #33412

Merged
merged 1 commit into from
May 7, 2016
Merged

Conversation

cramertj
Copy link
Member

@cramertj cramertj commented May 4, 2016

Part of #32777

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Aatch (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@@ -286,6 +286,54 @@ You can read more about cell types in the API documentation:
https://doc.rust-lang.org/std/cell/
"##,

E0389: r##"
An attempt was made to assign to data in a non-mutable reference. Example:
Copy link
Member

Choose a reason for hiding this comment

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

Not very clear. Also, you have to write "Example of erroneous code" and not just "Example" as stated by the RFC. It's to keep error explanations homogeneous.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, okay. The RFC doesn't seem particularly consistent-- it uses "Example of erroneous code:", "Erroneous code example:", and other errors currently in the same file as E0389 & co. use "For example:", and "Example:". It makes sense to have a standard, though, so perhaps there should be a PR changing the old ones. I can take a look at that after this one if you'd like.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, but before I'd prefer this RFC to be definitely accepted. Once done, you can do it if you want (it'd be very appreciated actually!).

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay. In that case, do you want to modify the RFC to change line 86 to say "Example of erroneous code:"?

@GuillaumeGomez
Copy link
Member

Thanks for your work! Please fix the errors and then it should be good to get merged.

@cramertj
Copy link
Member Author

cramertj commented May 4, 2016

@GuillaumeGomez Done. Squash?

@@ -286,6 +286,66 @@ You can read more about cell types in the API documentation:
https://doc.rust-lang.org/std/cell/
"##,

E0389: r##"
An attempt was made to mutate data using a non-mutable reference. This
commonly occurs when attempting to assign to a reference of type
Copy link
Member

Choose a reason for hiding this comment

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

I think your second sentence is a bit unwieldly. What do you think of: "This commonly occurs when attempting to assign to a non-mutable reference of a mutable reference"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Haha that sounds crazy to me-- that's why I just wrote the type out directly ("a non-mutable reference of a mutable reference" vs. &(&mut T)). But if you prefer, I'm happy to change it as I don't think it's going to greatly confuse anyone either way.

Copy link
Member

Choose a reason for hiding this comment

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

Typecit out and keep the code in a parenthetical 😄

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 love it when we can get the best of both worlds 😄

Copy link
Member

Choose a reason for hiding this comment

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

You're demons! 😨

@GuillaumeGomez
Copy link
Member

Yes please.

@cramertj cramertj force-pushed the E0389 branch 2 times, most recently from 868dfe7 to 04b45ad Compare May 4, 2016 23:46
}
```

Here, `&mut fancy` is mutable, but `&(&mut fancy)` is not. To fix this, either
Copy link
Member

Choose a reason for hiding this comment

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

Could you provide a more in-depth explanation as to why &&mut is immutable? This can trip up newcomers who come across it, since it looks like the inner value should be mutable.

(It isn't because &&mut can be aliased, which means that &T transitively makes all mutable references inside it immutable)

Copy link
Member Author

Choose a reason for hiding this comment

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

How about, "Creating an immutable reference to a value borrows that value immutably, so neither it nor its contents can be modified." (Should I mention interior mutability?)

Copy link
Member

Choose a reason for hiding this comment

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

"Creating an immutable reference to a value borrows it immutably, so neither it nor its contents can be modified." (a little better from my point of view 😉 )

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure.

@GuillaumeGomez
Copy link
Member

GuillaumeGomez commented May 6, 2016

@Manishearth: Seems good for me now.

@Manishearth
Copy link
Member

@GuillaumeGomez where's the in-depth explanation?

@Manishearth
Copy link
Member

Manishearth commented May 6, 2016

The point here is that &&mut at first glance for many people sounds like it should be possible to mutate the inner value, just not change the reference itself.

The reason that it's not allowed is a bit nuanced. &T can alias, so &&mut T can also alias. If it can be aliased, we should not allow mutation, so &T freezes the inner value from mutation even via internal &mut references. We should explain this reason.

@GuillaumeGomez
Copy link
Member

@Manishearth: I thought "Here, &mut fancy is mutable, but &(&mut fancy) is not. Creating an immutable reference to a value borrows it immutably, so neither it nor its contents can be modified. To fix this, either remove the outer reference:" explained it. Kinda tricky :-/

@cramertj
Copy link
Member Author

cramertj commented May 6, 2016

@Manishearth I don't think the "alias" explanation is particularly transparent. I personally prefer the explanation that I provided (the one @GuillaumeGomez referenced). If anything, I think a further clarification might include that there can be multiple references of type &(&mut T), so it must be immutable to prevent multiple mutable references to the same value (equivalent to aliasing, but less confusing terminology IMO).

@Manishearth
Copy link
Member

Yes, that further clarification is what I was looking for. I used alias whilst explaining the concept I felt should be included to you, not intending it to be included in the text 😄rust-lang/rust wrote:@Manishearth I don't think the "alias" explanation is particularly transparent. I personally prefer the explanation that I provided (the one @GuillaumeGomez referenced). If anything, I think a further clarification might include that there can be multiple references of type &(&mut T), so it must be immutable to prevent multiple mutable references to the same value (equivalent to aliasing, but less confusing terminology IMO).

—You are receiving this because you were mentioned.Reply to this email directly or view it on GitHub

@cramertj
Copy link
Member Author

cramertj commented May 6, 2016

Ah, okay. I'll add that then.

@Manishearth
Copy link
Member

@GuillaumeGomez that doesn't explain why *(&&mut x) is still immutable ;)

Cleanup of E0389

Added type-d out version of type in E0389 description
@cramertj
Copy link
Member Author

cramertj commented May 6, 2016

Change made.

@Manishearth
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented May 7, 2016

📌 Commit f25cbe6 has been approved by Manishearth

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 7, 2016
Add detailed error explanation for E0389

Part of rust-lang#32777
steveklabnik added a commit to steveklabnik/rust that referenced this pull request May 7, 2016
Add detailed error explanation for E0389

Part of rust-lang#32777
steveklabnik added a commit to steveklabnik/rust that referenced this pull request May 7, 2016
Add detailed error explanation for E0389

Part of rust-lang#32777
bors added a commit that referenced this pull request May 7, 2016
@bors bors merged commit f25cbe6 into rust-lang:master May 7, 2016
@cramertj cramertj deleted the E0389 branch May 25, 2016 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants