Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

matching is always considered a use of the place, even with _ patterns #27

Closed
nikomatsakis opened this issue Nov 19, 2020 · 1 comment · Fixed by rust-lang/rust#82536
Assignees
Labels
edge-cases Things we should remember when documenting

Comments

@nikomatsakis
Copy link
Contributor

In this example playground:

    let foo = [1, 2, 3];
    let c = || match foo { _ => () };

the match foo winds up registering a read of foo in the ExprUseVisitor. Note that the behavior here is different from let _ = foo. This is also somewhat inconsistent with the borrow checker, which considers match foo { _ => () } to not be a use (i.e., this code compiles, playground):

let mut x = 1;
let y = &x;
match x { _ => () }
drop(y);
@nikomatsakis nikomatsakis added the edge-cases Things we should remember when documenting label Nov 19, 2020
@arora-aman
Copy link
Member

arora-aman commented Nov 21, 2020

  • From a back-compat perspective, inside closures do we want to stop capturing the place that is being matched on?
  • If yes, the fix is kind of simple for capture analysis. Clippy uses ExprUseVisitor which makes this a little tricky.
  • For capture analysis, we just remove this line
  • The reason it works is:
    • When capture_disjoint_fields is enabled, the case described above falls under Handle patterns within closures with the feature gate enabled #24
    • When it's not enabled, since we capture everything that is mentioned inside the closure, we will end up capturing
      the discriminant as well, similar to how we have been doing things so far.

What remains is to see if this breaks clippy. If it does, how they use it will define how we end up handling the problem.

@nikomatsakis nikomatsakis added this to the Feature complete milestone Feb 17, 2021
@roxelo roxelo self-assigned this Feb 19, 2021
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 16, 2021
…=nikomatsakis

2229: Handle patterns within closures correctly when `capture_disjoint_fields` is enabled

This PR fixes several issues related to handling patterns within closures when `capture_disjoint_fields` is enabled.
1. Matching is always considered a use of the place, even with `_` patterns
2. Compiler ICE when capturing fields in closures through `let` assignments

To do so, we

- Introduced new Fake Reads
- Delayed use of `Place` in favor of `PlaceBuilder`
- Ensured that `PlaceBuilder` can be resolved before attempting to extract `Place` in any of the pattern matching code

Closes rust-lang/project-rfc-2229/issues/27
Closes rust-lang/project-rfc-2229/issues/24
r? `@nikomatsakis`
flip1995 pushed a commit to flip1995/rust-clippy that referenced this issue Mar 25, 2021
…akis

2229: Handle patterns within closures correctly when `capture_disjoint_fields` is enabled

This PR fixes several issues related to handling patterns within closures when `capture_disjoint_fields` is enabled.
1. Matching is always considered a use of the place, even with `_` patterns
2. Compiler ICE when capturing fields in closures through `let` assignments

To do so, we

- Introduced new Fake Reads
- Delayed use of `Place` in favor of `PlaceBuilder`
- Ensured that `PlaceBuilder` can be resolved before attempting to extract `Place` in any of the pattern matching code

Closes rust-lang/project-rfc-2229/issues/27
Closes rust-lang/project-rfc-2229/issues/24
r? `@nikomatsakis`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
edge-cases Things we should remember when documenting
Projects
None yet
3 participants