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

2229: Capture box completely in move closures #86445

Merged
merged 2 commits into from
Jun 27, 2021

Conversation

arora-aman
Copy link
Member

Even if the content from box is used in a sharef-ref context,
we capture the box entirerly.

This is motivated by:

  1. We only capture data that is on the stack.
  2. Capturing data from within the box might end up moving more data than
    the user anticipated.

Closes rust-lang/project-rfc-2229#50

r? @nikomatsakis

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 18, 2021
@@ -1630,7 +1630,14 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'tcx> {
self.fcx.param_env,
&place_with_id.place,
);

let place = restrict_preicision_for_box(&place, self.capture_clause);
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
let place = restrict_preicision_for_box(&place, self.capture_clause);
let place = restrict_precision_for_box(&place, self.capture_clause);

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a litle bit surprised, though, to see this call here, in borrow. Doesn't this apply no matter what type of access we have, so long as we are in a move closure? I think I would expect some common helper routine invoked by consume and borrow to do this (along with other forms of "restrict").

// 2. One motivatiton for the user to use a box might be to reduce the amount of data that gets
// moved (if size of pointer < size of data). We want to make sure that this optimization that
// the user made is respected.
fn restrict_preicision_for_box(place: &Place<'tcx>, capture_by: hir::CaptureBy) -> Place<'tcx> {
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
fn restrict_preicision_for_box(place: &Place<'tcx>, capture_by: hir::CaptureBy) -> Place<'tcx> {
fn restrict_precision_for_box(place: &Place<'tcx>, capture_by: hir::CaptureBy) -> Place<'tcx> {

@@ -1654,6 +1661,34 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'tcx> {
}
}

// In case of move closures we don't want to capture derefs on a box.
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
// In case of move closures we don't want to capture derefs on a box.
// In the case of move closures, we don't want to capture derefs on a box.

// In case of move closures we don't want to capture derefs on a box.
// This is motivated by:
// 1. We only want to capture data that is on the stack
// 2. One motivatiton for the user to use a box might be to reduce the amount of data that gets
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
// 2. One motivatiton for the user to use a box might be to reduce the amount of data that gets
// 2. One motivation for the user to use a box might be to reduce the amount of data that gets

// moved (if size of pointer < size of data). We want to make sure that this optimization that
// the user made is respected.
fn restrict_preicision_for_box(place: &Place<'tcx>, capture_by: hir::CaptureBy) -> Place<'tcx> {
let mut rv = place.clone();
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
let mut rv = place.clone();

fn restrict_preicision_for_box(place: &Place<'tcx>, capture_by: hir::CaptureBy) -> Place<'tcx> {
let mut rv = place.clone();
match capture_by {
hir::CaptureBy::Ref => rv,
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
hir::CaptureBy::Ref => rv,
hir::CaptureBy::Ref => place.clone(),

hir::CaptureBy::Ref => rv,
hir::CaptureBy::Value => {
if ty::TyS::is_box(place.base_ty) {
Place { projections: Vec::new(), ..rv }
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
Place { projections: Vec::new(), ..rv }
Place { projections: Vec::new(), ..place.clone() }

// In either case we want to stop at the box.
let pos = place.projections.iter().position(|proj| ty::TyS::is_box(proj.ty));
match pos {
None => rv,
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
None => rv,
None => place.clone(),

match pos {
None => rv,
Some(idx) => {
Place { projections: rv.projections.drain(0..=idx).collect(), ..rv }
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
Place { projections: rv.projections.drain(0..=idx).collect(), ..rv }
let mut rv = place.clone();
Place { projections: rv.projections.drain(0..=idx).collect(), ..rv }

This could be more efficient, but good enough I guess.

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Jun 23, 2021

📌 Commit 757add16b06fbd8088d8ff5f85505f780f58e4fb 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 Jun 23, 2021
@bors
Copy link
Contributor

bors commented Jun 24, 2021

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

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 24, 2021
Even if the content from box is used in a sharef-ref context,
we capture the box entirerly.

This is motivated by:
1) We only capture data that is on the stack.
2) Capturing data from within the box might end up moving more data than
the user anticipated.
fixup! 2229: Capture box completely in move closures
@arora-aman
Copy link
Member Author

@nikomatsakis udated

@m-ou-se
Copy link
Member

m-ou-se commented Jun 27, 2021

@bors r=nikomatsakis

@bors
Copy link
Contributor

bors commented Jun 27, 2021

📌 Commit d37a07f 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 27, 2021
@bors
Copy link
Contributor

bors commented Jun 27, 2021

⌛ Testing commit d37a07f with merge a4f832b...

@bors
Copy link
Contributor

bors commented Jun 27, 2021

☀️ Test successful - checks-actions
Approved by: nikomatsakis
Pushing a4f832b to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 27, 2021
@bors bors merged commit a4f832b into rust-lang:master Jun 27, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jun 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.

Truncate box deref in move closures
6 participants