Skip to content

Commit

Permalink
Rollup merge of rust-lang#87554 - sexxi-goose:fix-issue-87426, r=niko…
Browse files Browse the repository at this point in the history
…matsakis

2229: Discr should be read when PatKind is Range

This PR fixes an issue related to pattern matching in closures when Edition 2021 is enabled.

- If any of the patterns the discr is being matched on is `PatKind::Range` then the discr should be read

r? `@nikomatsakis`

Closes rust-lang#87426
  • Loading branch information
GuillaumeGomez committed Jul 29, 2021
2 parents 1e2eb9f + d380ed1 commit 1a2f4bd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
15 changes: 12 additions & 3 deletions compiler/rustc_typeck/src/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,21 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
}
}
}
PatKind::Lit(_) => {
// If the PatKind is a Lit then we want
PatKind::Lit(_) | PatKind::Range(..) => {
// If the PatKind is a Lit or a Range then we want
// to borrow discr.
needs_to_be_read = true;
}
_ => {}
PatKind::Or(_)
| PatKind::Box(_)
| PatKind::Slice(..)
| PatKind::Ref(..)
| PatKind::Wild => {
// If the PatKind is Or, Box, Slice or Ref, the decision is made later
// as these patterns contains subpatterns
// If the PatKind is Wild, the decision is made based on the other patterns being
// examined
}
}
}));
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/closures/2229_closure_analysis/issue-87426.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// run-pass
// edition:2021

pub fn foo() {
let ref_x_ck = 123;
let _y = || match ref_x_ck {
2_000_000..=3_999_999 => { println!("A")}
_ => { println!("B")}
};
}

fn main() {
foo();
}

0 comments on commit 1a2f4bd

Please sign in to comment.