implement slice patterns

This commit is contained in:
Oliver Schneider 2016-09-28 18:22:09 +02:00
parent 73f6d6e418
commit 1c18f6ddfa
No known key found for this signature in database
GPG Key ID: 56D6EEA0FC67AC46
4 changed files with 150 additions and 16 deletions

View File

@ -576,15 +576,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
Len(ref lvalue) => {
let src = self.eval_lvalue(lvalue)?;
let ty = self.lvalue_ty(lvalue);
match ty.sty {
ty::TyArray(_, n) => self.memory.write_usize(dest, n as u64)?,
ty::TySlice(_) => if let LvalueExtra::Length(len) = src.extra {
self.memory.write_usize(dest, len)?;
} else {
bug!("Rvalue::Len of a slice given non-slice pointer: {:?}", src);
},
_ => bug!("Rvalue::Len expected array or slice, got {:?}", ty),
}
let (_, len) = src.elem_ty_and_len(ty);
self.memory.write_usize(dest, len)?;
}
Ref(_, _, ref lvalue) => {
@ -889,19 +882,34 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
}
Index(ref operand) => {
let elem_size = match base_ty.sty {
ty::TyArray(elem_ty, _) |
ty::TySlice(elem_ty) => self.type_size(elem_ty),
_ => bug!("indexing expected an array or slice, got {:?}", base_ty),
};
let (elem_ty, len) = base.elem_ty_and_len(base_ty);
let elem_size = self.type_size(elem_ty);
let n_ptr = self.eval_operand(operand)?;
let usize = self.tcx.types.usize;
let n = self.value_to_primval(n_ptr, usize)?.expect_uint("Projection::Index expected usize");
assert!(n < len);
base.ptr.offset(n as isize * elem_size as isize)
}
ConstantIndex { .. } => unimplemented!(),
Subslice { .. } => unimplemented!(),
ConstantIndex { offset, min_length, from_end } => {
let (elem_ty, n) = base.elem_ty_and_len(base_ty);
let elem_size = self.type_size(elem_ty);
assert!(n >= min_length as u64);
if from_end {
base.ptr.offset((n as isize - offset as isize) * elem_size as isize)
} else {
base.ptr.offset(offset as isize * elem_size as isize)
}
},
Subslice { from, to } => {
let (elem_ty, n) = base.elem_ty_and_len(base_ty);
let elem_size = self.type_size(elem_ty);
assert!((from as u64) <= n - (to as u64));
return Ok(Lvalue {
ptr: base.ptr.offset(from as isize * elem_size as isize),
extra: LvalueExtra::Length(n - to as u64 - from as u64),
})
},
}
}
};
@ -1172,6 +1180,17 @@ impl Lvalue {
assert_eq!(self.extra, LvalueExtra::None);
self.ptr
}
fn elem_ty_and_len<'tcx>(self, ty: Ty<'tcx>) -> (Ty<'tcx>, u64) {
match ty.sty {
ty::TyArray(elem, n) => (elem, n as u64),
ty::TySlice(elem) => if let LvalueExtra::Length(len) = self.extra {
(elem, len)
} else {
bug!("elem_ty_and_len called on a slice given non-slice lvalue: {:?}", self);
},
_ => bug!("elem_ty_and_len expected array or slice, got {:?}", ty),
}
}
}
impl<'mir, 'tcx: 'mir> Deref for CachedMir<'mir, 'tcx> {

View File

@ -0,0 +1,33 @@
// Copyright 2014 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.
#![feature(slice_patterns)]
fn main() {
let mut x: &[_] = &[1, 2, 3, 4];
let mut result = vec!();
loop {
x = match *x {
[1, n, 3, ref rest..] => {
result.push(n);
rest
}
[n, ref rest..] => {
result.push(n);
rest
}
[] =>
break
}
}
assert_eq!(result, [2, 4]);
}

View File

@ -0,0 +1,24 @@
// Copyright 2014 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.
#![feature(slice_patterns)]
fn main() {
assert_eq!(match [0u8; 1024] {
_ => 42_usize,
}, 42_usize);
assert_eq!(match [0u8; 1024] {
[1, _..] => 0_usize,
[0, _..] => 1_usize,
_ => 2_usize
}, 1_usize);
}

View File

@ -0,0 +1,58 @@
// Copyright 2014 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.
#![feature(advanced_slice_patterns)]
#![feature(slice_patterns)]
use std::fmt::Debug;
fn foldl<T, U, F>(values: &[T],
initial: U,
mut function: F)
-> U where
U: Clone+Debug, T:Debug,
F: FnMut(U, &T) -> U,
{ match values {
&[ref head, ref tail..] =>
foldl(tail, function(initial, head), function),
&[] => {
// FIXME: call guards
let res = initial.clone(); res
}
}
}
fn foldr<T, U, F>(values: &[T],
initial: U,
mut function: F)
-> U where
U: Clone,
F: FnMut(&T, U) -> U,
{
match values {
&[ref head.., ref tail] =>
foldr(head, function(tail, initial), function),
&[] => {
// FIXME: call guards
let res = initial.clone(); res
}
}
}
pub fn main() {
let x = &[1, 2, 3, 4, 5];
let product = foldl(x, 1, |a, b| a * *b);
assert_eq!(product, 120);
let sum = foldr(x, 0, |a, b| *a + b);
assert_eq!(sum, 15);
}