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

Provide more information about disjoint capture in closures and migration instructions #246

Merged
merged 7 commits into from
Jul 7, 2021

Conversation

roxelo
Copy link
Member

@roxelo roxelo commented Jul 1, 2021

This PR adds additional information about disjoint capture in closures such as changes in semantics and migration instructions.

r? @nikomatsakis

Closes #241

src/rust-2021/disjoint-capture-in-closures.md Outdated Show resolved Hide resolved
src/rust-2021/disjoint-capture-in-closures.md Outdated Show resolved Hide resolved
src/rust-2021/disjoint-capture-in-closures.md Outdated Show resolved Hide resolved
src/rust-2021/disjoint-capture-in-closures.md Outdated Show resolved Hide resolved
src/rust-2021/disjoint-capture-in-closures.md Outdated Show resolved Hide resolved
src/rust-2021/disjoint-capture-in-closures.md Outdated Show resolved Hide resolved
src/rust-2021/disjoint-capture-in-closures.md Outdated Show resolved Hide resolved
src/rust-2021/disjoint-capture-in-closures.md Outdated Show resolved Hide resolved
*(px.0) += 10;
}); // Closure captured px.0 which is not Send
```

Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like it would be nice to have a discussion in these sections about what the idiomatic code would look like in 2021. Are people expected to write let _ = &px; under normal circumstances? Or is there another way this example could be cleanly written in 2021?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the expectation is that they would write let _ = &px;.

Any thoughts @arora-aman?

Copy link
Member

@arora-aman arora-aman Jul 2, 2021

Choose a reason for hiding this comment

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

We don't usually expect users needing to use let _ = &px in a usual case.

However we should probably add a section saying that this feature/edition 2021 might increase the sizes of their closure and if they are working in a memory critical codebase they can use -Zprofile-closures to get an idea of the effect on the size of closures. The profile dumped by -Zprofile-closures contains the size of the closures before and after the edition/feature along with where in the sourcebase the closure is located. In the case where a closure isn't benefiting from disjoint capture they can force the variables to be completely captured similar to technique used for migration within the closure body.

let _ = &var;
let _ = (&var1, &var2, &var3);

src/rust-2021/disjoint-capture-in-closures.md Outdated Show resolved Hide resolved
roxelo and others added 2 commits July 2, 2021 09:33
Co-authored-by: Ryan Levick <rylev@users.noreply.github.com>
Co-authored-by: Eric Huss <eric@huss.org>
@nikomatsakis nikomatsakis self-assigned this Jul 6, 2021
@nikomatsakis
Copy link
Contributor

I opened sexxi-goose#2 with some suggested edits

@@ -3,7 +3,7 @@
## Summary

- `|| a.x + 1` now captures only `a.x` instead of `a`.
- This can subtly change the drop order of things.
- This can subtly change the drop order of things and whether auto traits are applied to closures or not.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- This can subtly change the drop order of things and whether auto traits are applied to closures or not.
- This can cause subtle changes to the timing when variables are dropped the drop order as well as to whether the closure implements auto traits like `Send`.
- In most cases, these changes are not harmful, but the migration lint will nonetheless avoid them by inserting statements like `let _ = &x` to force an entire variable to be captured.

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.

Make tests pass

src/rust-2021/disjoint-capture-in-closures.md Outdated Show resolved Hide resolved
@nikomatsakis nikomatsakis merged commit 76c51bd into rust-lang:master Jul 7, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 20, 2021
Update books

## reference

6 commits in ab60513a3a5a0591e237fddff5d027a982648392..82d75cf423e4a7824fb36e73ccb18519d6900610
2021-07-05 08:27:31 -0700 to 2021-07-15 06:49:08 -0700
- fix wording/punctuation in "Lifetime bounds" (rust-lang/reference#1062)
- mention implicit `Sized` bound in more places (rust-lang/reference#1053)
-  (rust-lang/reference#1060)
- Remove inner attributes from non-block expressions. (rust-lang/reference#1051)
- Add note about the sign of the remainder (rust-lang/reference#1073)
- Clarify closure capture modes slightly (rust-lang/reference#1064)

## book

21 commits in a90f07f1e9a7fc75dc9105a6c6f16d5c13edceb0..eac55314210519238652f12b30fec9daea61f7fe
2021-07-05 14:43:12 -0400 to 2021-07-19 11:08:01 -0400
-  (rust-lang/book#2791)
- Add Deref trait to overload the deref operator in appendix table
- Revise linker installation instructions. Fixes rust-lang/book#2151.
- Clarify number literal type suffixes. Fixes rust-lang/book#1979.
- Add a note that lifetimes are coming up but aren't required
- Zip creates an iterator, not a vector. Fixes rust-lang/book#2762.
- Add Thai translation to the appendix. Connects to rust-lang/book#2636
- Derive Copy and Clone for Point to match stdlib docs example; fixes rust-lang/book#2657
- Make the definition of Option match the stdlib's, fixes rust-lang/book#2634
- Explain double quote backslash; closes rust-lang/book#2597
- Don't use the term 'anonymous struct' to fix rust-lang/book#2584
- Reword a sentence to fix rust-lang/book#2525
- Add Hindi translation. Connects to rust-lang/book#2496
- Remove unsafe Ferris designations from code. Fixes rust-lang/book#2555
- Add Traditional Chinese translation. Connects to rust-lang/book#2470
- Remove some more references to the authors field
- Make Appendix E be a link
- fix line wrapping
- remove description about the authors field
- remove the authors field
- remove the authors field

## rust-by-example

6 commits in 028f93a61500fe8f746ee7cc6b204ea6c9f42935..1db6bb483cc87ad3b424d9aba764fe622960a1be
2021-07-06 06:28:53 -0300 to 2021-07-15 06:17:42 -0300
- Updated the file destructure_tuple.md (rust-lang/rust-by-example#1452)
- Fix nomenclature around methods (rust-lang/rust-by-example#1450)
- std_misc/path.md: update `Show` to `Display` (rust-lang/rust-by-example#1449)
- Fix up github action (rust-lang/rust-by-example#1451)
- Remove github pages
- Support GitHub Actions (rust-lang/rust-by-example#1330)

## rustc-dev-guide

2 commits in 60e2825..93422c2
2021-07-05 11:21:03 -0400 to 2021-07-13 12:45:58 -0400
- Update for merge of CrateDisambiguator into StableCrateId
- Minor capitalization fix (rust-lang/rustc-dev-guide#1170)

## edition-guide

8 commits in 5d57b3832f8d308a9f478ce0a69799548f27ad4d..af696ce8ea526445590ae0ca66a8128d2a95a69a
2021-07-05 10:33:32 +0200 to 2021-07-20 11:38:03 -0400
- Add migration section for panic-macro-consistency (rust-lang/edition-guide#258)
- Update transitioning chapter. (rust-lang/edition-guide#255)
- Add details on migrating the Cargo feature resolver. (rust-lang/edition-guide#259)
- Improve panic macro consistency summary (rust-lang/edition-guide#256)
- Add migration details to or-patterns guide (rust-lang/edition-guide#252)
- Add migration section to into-iterator (rust-lang/edition-guide#253)
- Add Rust 2021 prelude migration details (rust-lang/edition-guide#251)
- Provide more information about disjoint capture in closures and migration instructions (rust-lang/edition-guide#246)

## embedded-book

1 commits in 506840eb73b0749336e1d5274e16d6393892ee82..09986cd352404eb4659db44613b27cac9aa652fc
2021-06-24 00:01:32 +0000 to 2021-07-18 19:26:46 +0000
- Stop referring to the alloc crate as unstable  (rust-embedded/book#297)
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.

Rust 2021 documentation for: Disjoint capture in closures (RFC 2229)
5 participants