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 E0504 #33386

Merged
merged 1 commit into from
May 11, 2016
Merged

Conversation

cramertj
Copy link
Member

@cramertj cramertj commented May 3, 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 @nrc (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.

This error occurs when an attempt is made to move a borrowed variable into a
closure. For example:

```compile_fail
Copy link
Member

Choose a reason for hiding this comment

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

No need to use threads here. It shadows a bit the explanation and makes the example harder than necessary. You could use:

struct FancyNum {
    num: u8
}

fn main() {
    let fancy_num = FancyNum { num: 5 };
    let fancy_ref = &fancy_num;

    let x = move || {
        println!("child thread: {}", fancy_num.num);
        // error: cannot move `fancy_num` into closure because it is borrowed
    };

    println!("main thread: {}", fancy_ref.num);
}

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 wondered about that-- I was trying to give an example that showed a common use of moved closures, but since it sounds like you think it would be clearer without I'll remove it.

Copy link
Member

Choose a reason for hiding this comment

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

I just hope it won't get in the way for readers. What do you think about this @steveklabnik?

Copy link
Member

Choose a reason for hiding this comment

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

IMO we should have both examples. This error can crop up a lot in threaded situations, and an extra example addressing it specifically would be nice.

Copy link
Member Author

Choose a reason for hiding this comment

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

What do you think about changing the Rc example into an Arc example and using thread::spawn there? It would highlight threads as well as giving a more motivating example for using reference counting (compared with the existing example, which uses reference counting for no reason).

@GuillaumeGomez
Copy link
Member

Thanks for showing Rc type. I'm just not sure yet about displaying a thread in the example or not so let's wait for @steveklabnik (or @Manishearth)'s opinion.

@bors
Copy link
Contributor

bors commented May 5, 2016

☔ The latest upstream changes (presumably #33376) made this pull request unmergeable. Please resolve the merge conflicts.

@cramertj cramertj force-pushed the E0504 branch 5 times, most recently from e33fa5a to 3371b8a Compare May 5, 2016 02:01
@cramertj
Copy link
Member Author

cramertj commented May 5, 2016

@GuillaumeGomez

@steveklabnik
Copy link
Member

@bors: r+ rollup

looks good, thanks!

@bors
Copy link
Contributor

bors commented May 5, 2016

📌 Commit 3371b8a has been approved by steveklabnik

steveklabnik added a commit to steveklabnik/rust that referenced this pull request May 5, 2016
Add detailed error explanation for E0504

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

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

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

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 E0504

Part of rust-lang#32777
@@ -386,6 +386,109 @@ fn foo(a: &mut i32) {
let bar = || {
inside_closure(a)
};
```
Copy link
Member

Choose a reason for hiding this comment

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

this is causing compilation to fail; it makes the error above invalid. you need to include a closing }

@steveklabnik
Copy link
Member

@bors: r-

see my inline comment; this was failing the rollup

@steveklabnik
Copy link
Member

ff1f450 <- something like this would fix this PR

Removed unnecessary use of threads from E0504

Cleaned up line ending on E0504

Added more examples for E0504

Changed to erroneous code wording

Switched Rc example to thread/Arc example

Added comments describing why errors no longer occur
@cramertj
Copy link
Member Author

cramertj commented May 9, 2016

@steveklabnik Fixed. Sorry about that.

@steveklabnik
Copy link
Member

No worries! Thanks 😄

@bors: r+ rollup

@bors
Copy link
Contributor

bors commented May 9, 2016

📌 Commit 38a5338 has been approved by steveklabnik

steveklabnik added a commit to steveklabnik/rust that referenced this pull request May 10, 2016
Add detailed error explanation for E0504

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

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

Part of rust-lang#32777
bors added a commit that referenced this pull request May 11, 2016
Rollup of 9 pull requests

- Successful merges: #33129, #33260, #33345, #33386, #33522, #33524, #33528, #33539, #33542
- Failed merges: #33342, #33475, #33517
@bors bors merged commit 38a5338 into rust-lang:master May 11, 2016
@cramertj cramertj deleted the E0504 branch May 25, 2016 19:55
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.

7 participants