提交 247835aa 编写于 作者: B bors

Auto merge of #47374 - topecongiro:issue-47096, r=nikomatsakis

Simplify irrefutable slice patterns

Closes #47096.
......@@ -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 @@ fn simplify_match_pair<'pat>(&mut self,
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 || {
......
// 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);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册