Skip to content

Commit 247835a

Browse files
committed
Auto merge of #47374 - topecongiro:issue-47096, r=nikomatsakis
Simplify irrefutable slice patterns Closes #47096.
2 parents a0a9007 + 604a54f commit 247835a

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/librustc_const_eval/pattern.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ pub enum PatternKind<'tcx> {
9292
end: RangeEnd,
9393
},
9494

95-
/// matches against a slice, checking the length and extracting elements
95+
/// matches against a slice, checking the length and extracting elements.
96+
/// irrefutable when there is a slice pattern and both `prefix` and `suffix` are empty.
97+
/// e.g. `&[ref xs..]`.
9698
Slice {
9799
prefix: Vec<Pattern<'tcx>>,
98100
slice: Option<Pattern<'tcx>>,

src/librustc_mir/build/matches/simplify.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,24 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
9292
Err(match_pair)
9393
}
9494

95-
PatternKind::Range { .. } |
96-
PatternKind::Slice { .. } => {
95+
PatternKind::Range { .. } => {
9796
Err(match_pair)
9897
}
9998

99+
PatternKind::Slice { ref prefix, ref slice, ref suffix } => {
100+
if prefix.is_empty() && slice.is_some() && suffix.is_empty() {
101+
// irrefutable
102+
self.prefix_slice_suffix(&mut candidate.match_pairs,
103+
&match_pair.place,
104+
prefix,
105+
slice.as_ref(),
106+
suffix);
107+
Ok(())
108+
} else {
109+
Err(match_pair)
110+
}
111+
}
112+
100113
PatternKind::Variant { adt_def, substs, variant_index, ref subpatterns } => {
101114
let irrefutable = adt_def.variants.iter().enumerate().all(|(i, v)| {
102115
i == variant_index || {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// #47096
12+
13+
#![feature(slice_patterns)]
14+
15+
fn foo(s: &[i32]) -> &[i32] {
16+
let &[ref xs..] = s;
17+
xs
18+
}
19+
20+
fn main() {
21+
let x = [1, 2, 3];
22+
let y = foo(&x);
23+
assert_eq!(x, y);
24+
}

0 commit comments

Comments
 (0)