From 9829efb892f7c249f2c889722a098e6a9b34f3fb Mon Sep 17 00:00:00 2001 From: Roxane Date: Wed, 28 Jul 2021 10:35:48 -0400 Subject: [PATCH 1/2] Range PatKind implies discr should be read --- compiler/rustc_typeck/src/expr_use_visitor.rs | 9 ++++++--- .../closures/2229_closure_analysis/issue-87426.rs | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/closures/2229_closure_analysis/issue-87426.rs diff --git a/compiler/rustc_typeck/src/expr_use_visitor.rs b/compiler/rustc_typeck/src/expr_use_visitor.rs index 6c76a8960bbe1..201804f9fc40b 100644 --- a/compiler/rustc_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_typeck/src/expr_use_visitor.rs @@ -267,12 +267,15 @@ 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; } - _ => {} + _ => { + // If the PatKind is Or, Box, Slice or Ref, the decision is made later + // as these patterns contains subpatterns + } } })); } diff --git a/src/test/ui/closures/2229_closure_analysis/issue-87426.rs b/src/test/ui/closures/2229_closure_analysis/issue-87426.rs new file mode 100644 index 0000000000000..4a6eb5154c136 --- /dev/null +++ b/src/test/ui/closures/2229_closure_analysis/issue-87426.rs @@ -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(); +} \ No newline at end of file From d380ed13043d52139b3c766a07edb93b891e2be6 Mon Sep 17 00:00:00 2001 From: Roxane Date: Wed, 28 Jul 2021 12:27:57 -0400 Subject: [PATCH 2/2] fix nit --- compiler/rustc_typeck/src/expr_use_visitor.rs | 8 +++++++- src/test/ui/closures/2229_closure_analysis/issue-87426.rs | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_typeck/src/expr_use_visitor.rs b/compiler/rustc_typeck/src/expr_use_visitor.rs index 201804f9fc40b..1d7852d964c1d 100644 --- a/compiler/rustc_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_typeck/src/expr_use_visitor.rs @@ -272,9 +272,15 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { // 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 } } })); diff --git a/src/test/ui/closures/2229_closure_analysis/issue-87426.rs b/src/test/ui/closures/2229_closure_analysis/issue-87426.rs index 4a6eb5154c136..74506979a28c5 100644 --- a/src/test/ui/closures/2229_closure_analysis/issue-87426.rs +++ b/src/test/ui/closures/2229_closure_analysis/issue-87426.rs @@ -11,4 +11,4 @@ pub fn foo() { fn main() { foo(); -} \ No newline at end of file +}