Skip to content

Commit

Permalink
Auto merge of #47374 - topecongiro:issue-47096, r=nikomatsakis
Browse files Browse the repository at this point in the history
Simplify irrefutable slice patterns

Closes #47096.
  • Loading branch information
bors committed Jan 25, 2018
2 parents a0a9007 + 604a54f commit 247835a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/librustc_const_eval/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
Expand Down
17 changes: 15 additions & 2 deletions src/librustc_mir/build/matches/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {
Expand Down
24 changes: 24 additions & 0 deletions src/test/run-pass/irrefutable-slice-patterns.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 247835a

Please sign in to comment.