Auto merge of #47374 - topecongiro:issue-47096, r=nikomatsakis
Simplify irrefutable slice patterns Closes #47096.
This commit is contained in:
commit
247835aacb
@ -92,7 +92,9 @@ pub enum PatternKind<'tcx> {
|
||||
end: RangeEnd,
|
||||
},
|
||||
|
||||
/// matches against a slice, checking the length and extracting elements
|
||||
/// matches against a slice, checking the length and extracting elements.
|
||||
/// irrefutable when there is a slice pattern and both `prefix` and `suffix` are empty.
|
||||
/// e.g. `&[ref xs..]`.
|
||||
Slice {
|
||||
prefix: Vec<Pattern<'tcx>>,
|
||||
slice: Option<Pattern<'tcx>>,
|
||||
|
@ -92,11 +92,24 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
|
||||
Err(match_pair)
|
||||
}
|
||||
|
||||
PatternKind::Range { .. } |
|
||||
PatternKind::Slice { .. } => {
|
||||
PatternKind::Range { .. } => {
|
||||
Err(match_pair)
|
||||
}
|
||||
|
||||
PatternKind::Slice { ref prefix, ref slice, ref suffix } => {
|
||||
if prefix.is_empty() && slice.is_some() && suffix.is_empty() {
|
||||
// irrefutable
|
||||
self.prefix_slice_suffix(&mut candidate.match_pairs,
|
||||
&match_pair.place,
|
||||
prefix,
|
||||
slice.as_ref(),
|
||||
suffix);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(match_pair)
|
||||
}
|
||||
}
|
||||
|
||||
PatternKind::Variant { adt_def, substs, variant_index, ref subpatterns } => {
|
||||
let irrefutable = adt_def.variants.iter().enumerate().all(|(i, v)| {
|
||||
i == variant_index || {
|
||||
|
24
src/test/run-pass/irrefutable-slice-patterns.rs
Normal file
24
src/test/run-pass/irrefutable-slice-patterns.rs
Normal file
@ -0,0 +1,24 @@
|
||||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// #47096
|
||||
|
||||
#![feature(slice_patterns)]
|
||||
|
||||
fn foo(s: &[i32]) -> &[i32] {
|
||||
let &[ref xs..] = s;
|
||||
xs
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = [1, 2, 3];
|
||||
let y = foo(&x);
|
||||
assert_eq!(x, y);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user