From 9829efb892f7c249f2c889722a098e6a9b34f3fb Mon Sep 17 00:00:00 2001 From: Roxane Date: Wed, 28 Jul 2021 10:35:48 -0400 Subject: [PATCH] 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 6c76a8960bb..201804f9fc4 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 00000000000..4a6eb5154c1 --- /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