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

NLL lacks various special case handling of closures #54976

Merged
merged 3 commits into from
Oct 18, 2018

Conversation

davidtwco
Copy link
Member

Part of #52663.

Firstly, this PR extends existing handling of closures to also support generators.

Second, this PR adds the note found in the AST when a closure is invoked twice and captures a variable by-value:

note: closure cannot be invoked more than once because it moves the variable `dict` out of its environment
  --> $DIR/issue-42065.rs:16:29
   |
LL |         for (key, value) in dict {
   |                             ^^^^

r? @nikomatsakis
cc @pnkfelix

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 10, 2018
--> $DIR/issue-12127.rs:18:39
|
LL | let f = to_fn_once(move|| do_it(&*x));
| ^
Copy link
Member Author

Choose a reason for hiding this comment

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

This test doesn't have this note in the AST borrow checker. A brief glance made me think it was right but I'm not too sure.

Copy link
Contributor

Choose a reason for hiding this comment

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

The closure only implements FnOnce because of to_fn_once, otherwise it would be a Fn closure (it doesn't drop/move x when called).

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

Looks good but the labeling of freevars is too eager.

freevar={:?} upvar_ty={:?}",
freevar, upvar_ty,
);
if !upvar_ty.is_region_ptr() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is too broad -- the code is only looking at the type of the value that was captured (and crudely at that), but it needs to take into consideration how the value is used (e.g., the &*x in the issue-12127.rs does not require moving x).

There are a few ways to handle this.

One way would be to go through the point where the closure is created and look at whether it "moves" its arguments. That seems a bit complex though.

Another way would be to consult the closure_kind_origins map in the typeck-tables. This map indicates why a given closure has the kind it does (and in particular it will have an entry if this is due to a user of some freevar).

See the code in librustc_borrowck for an example:

if let Some((span, name)) = self.tables.closure_kind_origins().get(hir_id) {
err.span_note(*span, &format!(
"closure cannot be invoked more than once because \
it moves the variable `{}` out of its environment",
name
));
false
} else {
true
}

(I think that in the case of issue-12127, there will be no entry in the map at all.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Pushed a fix for this.

@nikomatsakis nikomatsakis added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 16, 2018
@davidtwco davidtwco added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 16, 2018
@nikomatsakis
Copy link
Contributor

@bors r+

@bors

This comment has been minimized.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 17, 2018
@kennytm
Copy link
Member

kennytm commented Oct 18, 2018

@bors p=26

rollup fairness

@bors

This comment has been minimized.

@bors

This comment has been minimized.

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 18, 2018
@rust-highfive

This comment has been minimized.

This commit extends existing special-casing of closures to highlight the
use of variables within generators that are causing the generator to
borrow them.
This commit adds a note that was present in the AST borrow checker when
closures are invoked more than once and have captured variables
by-value.
@davidtwco davidtwco force-pushed the issue-52663-special-case-closures branch from 270f802 to d088edc Compare October 18, 2018 16:24
@davidtwco
Copy link
Member Author

@bors r=nikomatsakis

@bors
Copy link
Contributor

bors commented Oct 18, 2018

📌 Commit d088edc has been approved by nikomatsakis

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 18, 2018
@bors
Copy link
Contributor

bors commented Oct 18, 2018

⌛ Testing commit d088edc with merge e7f5d48...

bors added a commit that referenced this pull request Oct 18, 2018
…=nikomatsakis

NLL lacks various special case handling of closures

Part of #52663.

Firstly, this PR extends existing handling of closures to also support generators.

Second, this PR adds the note found in the AST when a closure is invoked twice and captures a variable by-value:

```text
note: closure cannot be invoked more than once because it moves the variable `dict` out of its environment
  --> $DIR/issue-42065.rs:16:29
   |
LL |         for (key, value) in dict {
   |                             ^^^^
```

r? @nikomatsakis
cc @pnkfelix
@bors
Copy link
Contributor

bors commented Oct 18, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: nikomatsakis
Pushing e7f5d48 to master...

@bors bors merged commit d088edc into rust-lang:master Oct 18, 2018
@davidtwco davidtwco deleted the issue-52663-special-case-closures branch October 18, 2018 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-NLL Area: Non-lexical lifetimes (NLL) S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants