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

ExprUseVisitor: Treat ByValue use of Copy types as ImmBorrow #87069

Merged
merged 5 commits into from
Jul 16, 2021

Conversation

arora-aman
Copy link
Member

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 11, 2021
match mode {
ConsumeMode::Move => self.delegate.consume(place_with_id, diag_expr_id, mode),
ConsumeMode::Copy => {
self.delegate.borrow(place_with_id, diag_expr_id, ty::BorrowKind::ImmBorrow)
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I had expected to make this change within the upvar delegate, and not the ExprUseVisitor. It seems potentially useful to some clients to know that this was a "by-value copy" -- I think there is other code in clippy that uses this, for example.

Copy link
Contributor

Choose a reason for hiding this comment

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

Otherwise we should just remove the ConsumeMode

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.

Suggested edit to the comment, and a simplification

Comment on lines 29 to 35
// The value found at `place` is moved, depending
// on `mode`. Where `diag_expr_id` is the id used for diagnostics for `place`.
//
// The parameter `diag_expr_id` indicates the HIR id that ought to be used for
// diagnostics. Around pattern matching such as `let pat = expr`, the diagnostic
// id will be the id of the expression `expr` but the place itself will have
// the id of the binding in the pattern `pat`.
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
// The value found at `place` is moved, depending
// on `mode`. Where `diag_expr_id` is the id used for diagnostics for `place`.
//
// The parameter `diag_expr_id` indicates the HIR id that ought to be used for
// diagnostics. Around pattern matching such as `let pat = expr`, the diagnostic
// id will be the id of the expression `expr` but the place itself will have
// the id of the binding in the pattern `pat`.
// The value found at `place` is moved, meaning that it is used by value
// and is not known to have `Copy` type.
//
// If a value is used by value but *does* have `Copy` type, that is treated as a
// "shared borrow". This corresponds to the minimal access that a closure needs
// to perform that operation (a shared reference).
//
// The parameter `diag_expr_id` indicates the HIR id that ought to be used for
// diagnostics. Around pattern matching such as `let pat = expr`, the diagnostic
// id will be the id of the expression `expr` but the place itself will have
// the id of the binding in the pattern `pat`.

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't add the detail about closures because ExprUseVisitor and therefore this delegate is used in other contexts too

Comment on lines 656 to 664
let mode = copy_or_move(mc, &place);
match mode {
ConsumeMode::Move => delegate.consume(place, discr_place.hir_id),
ConsumeMode::Copy => delegate.borrow(
place,
discr_place.hir_id,
ty::BorrowKind::ImmBorrow,
),
}
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 mode = copy_or_move(mc, &place);
match mode {
ConsumeMode::Move => delegate.consume(place, discr_place.hir_id),
ConsumeMode::Copy => delegate.borrow(
place,
discr_place.hir_id,
ty::BorrowKind::ImmBorrow,
),
}
self.delegate_consume(place, discr_place.hir_id);

Does this not work?

Copy link
Member Author

Choose a reason for hiding this comment

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

we need 2229 enabled to make this work 🙃. The closure is passed to self.mc. I'll just refactor into a helper.

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Jul 15, 2021

📌 Commit 75291ee 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 Jul 15, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jul 15, 2021
…matsakis

ExprUseVisitor: Treat ByValue use of Copy types as ImmBorrow

r? `@nikomatsakis`
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jul 15, 2021
…matsakis

ExprUseVisitor: Treat ByValue use of Copy types as ImmBorrow

r? ``@nikomatsakis``
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 16, 2021
…laumeGomez

Rollup of 7 pull requests

Successful merges:

 - rust-lang#86983 (Add or improve natvis definitions for common standard library types)
 - rust-lang#87069 (ExprUseVisitor: Treat ByValue use of Copy types as ImmBorrow)
 - rust-lang#87138 (Correct invariant documentation for `steps_between`)
 - rust-lang#87145 (Make --cap-lints and related options leave crate hash alone)
 - rust-lang#87161 (RFC2229: Use the correct place type)
 - rust-lang#87162 (Fix type decl layout "overflow")
 - rust-lang#87167 (Fix sidebar display on small devices)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit c1ffca0 into rust-lang:master Jul 16, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jul 16, 2021
flip1995 pushed a commit to flip1995/rust that referenced this pull request Jul 19, 2021
…matsakis

ExprUseVisitor: Treat ByValue use of Copy types as ImmBorrow

r? ```@nikomatsakis```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

5 participants