NLL lacks various special case handling of closures#54976
NLL lacks various special case handling of closures#54976bors merged 3 commits intorust-lang:masterfrom
Conversation
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
nikomatsakis
left a comment
There was a problem hiding this comment.
Looks good but the labeling of freevars is too eager.
There was a problem hiding this comment.
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:
rust/src/librustc_borrowck/borrowck/mod.rs
Lines 704 to 713 in 5ea8eb5
(I think that in the case of issue-12127, there will be no entry in the map at all.)
There was a problem hiding this comment.
Pushed a fix for this.
|
@bors r+ |
This comment has been minimized.
This comment has been minimized.
|
@bors p=26 rollup fairness |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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.
270f802 to
d088edc
Compare
|
@bors r=nikomatsakis |
|
📌 Commit d088edc has been approved by |
…=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
|
☀️ Test successful - status-appveyor, status-travis |
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:
r? @nikomatsakis
cc @pnkfelix