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

Projects
None yet
7 participants
@davidtwco
Member

davidtwco commented Oct 10, 2018

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

--> $DIR/issue-12127.rs:18:39
|
LL | let f = to_fn_once(move|| do_it(&*x));
| ^

This comment has been minimized.

@davidtwco

davidtwco Oct 10, 2018

Member

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.

This comment has been minimized.

@matthewjasper

matthewjasper Oct 11, 2018

Contributor

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

Looks good but the labeling of freevars is too eager.

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

This comment has been minimized.

@nikomatsakis

nikomatsakis Oct 16, 2018

Contributor

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.)

This comment has been minimized.

@davidtwco

davidtwco Oct 16, 2018

Member

Pushed a fix for this.

@nikomatsakis

This comment has been minimized.

Contributor

nikomatsakis commented Oct 17, 2018

@bors r+

@bors

This comment was marked as outdated.

Contributor

bors commented Oct 17, 2018

📌 Commit 270f802 has been approved by nikomatsakis

@kennytm

This comment has been minimized.

Member

kennytm commented Oct 18, 2018

@bors p=26

rollup fairness

@bors

This comment was marked as outdated.

Contributor

bors commented Oct 18, 2018

⌛️ Testing commit 270f802 with merge 0f6d1c0...

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

Auto merge of #54976 - davidtwco:issue-52663-special-case-closures, r…
…=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

This comment was marked as outdated.

Contributor

bors commented Oct 18, 2018

💔 Test failed - status-travis

@rust-highfive

This comment was marked as outdated.

Collaborator

rust-highfive commented Oct 18, 2018

The job x86_64-gnu-debug of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
[00:06:23] For more information about this error, try `rustc --explain E0425`.
[00:06:23] error: Could not compile `rustc_mir`.
[00:06:23] warning: build failed, waiting for other jobs to finish...
[00:06:28] error: build failed
[00:06:28] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "check" "--target" "x86_64-unknown-linux-gnu" "-j" "4" "--release" "--color" "always" "--features" " jemalloc" "--manifest-path" "/checkout/src/rustc/Cargo.toml" "--message-format" "json"
[00:06:28] expected success, got: exit code: 101
[00:06:28] thread 'main' panicked at 'cargo must succeed', bootstrap/compile.rs:1115:9
[00:06:28] travis_fold:end:stage0-rustc

[00:06:28] travis_time:end:stage0-rustc:start=1539877283060719757,finish=1539877418125808465,duration=135065088708

---
travis_time:end:0d17b91c:start=1539877418834613463,finish=1539877418841667840,duration=7054377
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:2631c71a
$ ln -s . checkout && for CORE in obj/cores/core.*; do EXE=$(echo $CORE | sed 's|obj/cores/core\.[0-9]*\.!checkout!\(.*\)|\1|;y|!|/|'); if [ -f "$EXE" ]; then printf travis_fold":start:crashlog\n\033[31;1m%s\033[0m\n" "$CORE"; gdb --batch -q -c "$CORE" "$EXE" -iex 'set auto-load off' -iex 'dir src/' -iex 'set sysroot .' -ex bt -ex q; echo travis_fold":"end:crashlog; fi; done || true
travis_fold:end:after_failure.4
travis_fold:start:after_failure.5
travis_time:start:1d4f1a15
travis_time:start:1d4f1a15
$ cat ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true
cat: ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers: No such file or directory
travis_fold:end:after_failure.5
travis_fold:start:after_failure.6
travis_time:start:023e7a7c
$ dmesg | grep -i kill

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

davidtwco added some commits Oct 10, 2018

Extend closure special-casing for generators.
This commit extends existing special-casing of closures to highlight the
use of variables within generators that are causing the generator to
borrow them.
Add by-value captured variable note on second use.
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

This comment has been minimized.

Member

davidtwco commented Oct 18, 2018

@bors r=nikomatsakis

@bors

This comment has been minimized.

Contributor

bors commented Oct 18, 2018

📌 Commit d088edc has been approved by nikomatsakis

@bors

This comment has been minimized.

Contributor

bors commented Oct 18, 2018

⌛️ Testing commit d088edc with merge e7f5d48...

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

Auto merge of #54976 - davidtwco:issue-52663-special-case-closures, r…
…=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

This comment has been minimized.

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

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details

@davidtwco davidtwco deleted the davidtwco:issue-52663-special-case-closures branch Oct 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment