Skip to content

Commit aaef1a1

Browse files
authored
Rollup merge of #87554 - sexxi-goose:fix-issue-87426, r=nikomatsakis
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 #87426
2 parents 7e4b173 + d380ed1 commit aaef1a1

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

compiler/rustc_typeck/src/expr_use_visitor.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,21 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
267267
}
268268
}
269269
}
270-
PatKind::Lit(_) => {
271-
// If the PatKind is a Lit then we want
270+
PatKind::Lit(_) | PatKind::Range(..) => {
271+
// If the PatKind is a Lit or a Range then we want
272272
// to borrow discr.
273273
needs_to_be_read = true;
274274
}
275-
_ => {}
275+
PatKind::Or(_)
276+
| PatKind::Box(_)
277+
| PatKind::Slice(..)
278+
| PatKind::Ref(..)
279+
| PatKind::Wild => {
280+
// If the PatKind is Or, Box, Slice or Ref, the decision is made later
281+
// as these patterns contains subpatterns
282+
// If the PatKind is Wild, the decision is made based on the other patterns being
283+
// examined
284+
}
276285
}
277286
}));
278287
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-pass
2+
// edition:2021
3+
4+
pub fn foo() {
5+
let ref_x_ck = 123;
6+
let _y = || match ref_x_ck {
7+
2_000_000..=3_999_999 => { println!("A")}
8+
_ => { println!("B")}
9+
};
10+
}
11+
12+
fn main() {
13+
foo();
14+
}

0 commit comments

Comments
 (0)