-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add explanations for E0503 and E0508. #34133
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -529,6 +529,62 @@ For more information on the rust ownership system, take a look at | |
https://doc.rust-lang.org/stable/book/references-and-borrowing.html. | ||
"##, | ||
|
||
E0503: r##" | ||
A value was used after it was mutably borrowed. | ||
|
||
Example of erroneous code: | ||
|
||
```compile_fail | ||
fn main() { | ||
let mut value = 3; | ||
// Create a mutable borrow of `value`. This borrow | ||
// lives until the end of this function. | ||
let _borrow = &mut value; | ||
let _sum = value + 1; // error: cannot use `value` because | ||
// it was mutably borrowed | ||
} | ||
``` | ||
|
||
In this example, `value` is mutably borrowed by `borrow` and cannot be | ||
used to calculate `sum`. This is not possible because this would violate | ||
Rust's mutability rules. | ||
|
||
You can fix this error by limiting the scope of the borrow: | ||
|
||
``` | ||
fn main() { | ||
let mut value = 3; | ||
// By creating a new block, you can limit the scope | ||
// of the reference. | ||
{ | ||
let _borrow = &mut value; // Use `_borrow` inside this block. | ||
} | ||
// The block has ended and with it the borrow. | ||
// You can now use `value` again. | ||
let _sum = value + 1; | ||
} | ||
``` | ||
|
||
Or by cloning `value` before borrowing it: | ||
|
||
``` | ||
fn main() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
let mut value = 3; | ||
// We clone `value`, creating a copy. | ||
let value_cloned = value.clone(); | ||
// The mutable borrow is a reference to `value` and | ||
// not to `value_cloned`... | ||
let _borrow = &mut value; | ||
// ... which means we can still use `value_cloned`, | ||
let _sum = value_cloned + 1; | ||
// even though the borrow only ends here. | ||
} | ||
``` | ||
|
||
You can find more information about borrowing in the rust-book: | ||
http://doc.rust-lang.org/stable/book/references-and-borrowing.html | ||
"##, | ||
|
||
E0504: r##" | ||
This error occurs when an attempt is made to move a borrowed variable into a | ||
closure. | ||
|
@@ -911,6 +967,50 @@ You can find more information about borrowing in the rust-book: | |
http://doc.rust-lang.org/stable/book/references-and-borrowing.html | ||
"##, | ||
|
||
E0508: r##" | ||
A value was moved out of a non-copy fixed-size array. | ||
|
||
Example of erroneous code: | ||
|
||
```compile_fail | ||
struct NonCopy; | ||
|
||
fn main() { | ||
let array = [NonCopy; 1]; | ||
let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`, | ||
// a non-copy fixed-size array | ||
} | ||
``` | ||
|
||
The first element was moved out of the array, but this is not | ||
possible because `NonCopy` does not implement the `Copy` trait. | ||
|
||
Consider borrowing the element instead of moving it: | ||
|
||
``` | ||
struct NonCopy; | ||
|
||
fn main() { | ||
let array = [NonCopy; 1]; | ||
let _value = &array[0]; // Borrowing is allowed, unlike moving. | ||
} | ||
``` | ||
|
||
Alternatively, if your type implements `Clone` and you need to own the value, | ||
consider borrowing and then cloning: | ||
|
||
``` | ||
#[derive(Clone)] | ||
struct NonCopy; | ||
|
||
fn main() { | ||
let array = [NonCopy; 1]; | ||
// Now you can clone the array element. | ||
let _value = array[0].clone(); | ||
} | ||
``` | ||
"##, | ||
|
||
E0509: r##" | ||
This error occurs when an attempt is made to move out of a value whose type | ||
implements the `Drop` trait. | ||
|
@@ -1011,7 +1111,5 @@ fn main() { | |
register_diagnostics! { | ||
E0385, // {} in an aliasable location | ||
E0388, // {} in a static location | ||
E0503, // cannot use `..` because it was mutably borrowed | ||
E0508, // cannot move out of type `..`, a non-copy fixed-size array | ||
E0524, // two closures require unique access to `..` at the same time | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you annotate this example with some comments to explain what you did please?