From c8d3814f9b9e6ddff2e2b4714b701926c0e7e78a Mon Sep 17 00:00:00 2001 From: arthurprs Date: Tue, 24 Oct 2017 22:25:25 +0200 Subject: [PATCH 1/2] Hint slice.{r}position is within slice bounds --- src/libcore/slice/mod.rs | 4 ++++ src/libcore/tests/slice.rs | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index 5039bef631e51..33fac50658ff3 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -1199,8 +1199,10 @@ macro_rules! iterator { fn position(&mut self, mut predicate: F) -> Option where F: FnMut(Self::Item) -> bool, { + let len = self.len(); let mut index = 0; self.search_while(None, move |elt| { + unsafe { assume(index < len); } if predicate(elt) { SearchWhile::Done(Some(index)) } else { @@ -1213,9 +1215,11 @@ macro_rules! iterator { fn rposition(&mut self, mut predicate: F) -> Option where F: FnMut(Self::Item) -> bool, { + let len = self.len(); let mut index = self.len(); self.rsearch_while(None, move |elt| { index -= 1; + unsafe { assume(index < len); } if predicate(elt) { SearchWhile::Done(Some(index)) } else { diff --git a/src/libcore/tests/slice.rs b/src/libcore/tests/slice.rs index 8c31d2e83d352..7a463ce9af042 100644 --- a/src/libcore/tests/slice.rs +++ b/src/libcore/tests/slice.rs @@ -13,6 +13,25 @@ use core::slice::heapsort; use core::result::Result::{Ok, Err}; use rand::{Rng, XorShiftRng}; + +#[test] +fn test_position() { + let b = [1, 2, 3, 5, 5]; + assert!(b.iter().position(|&v| v == 9) == None); + assert!(b.iter().position(|&v| v == 5) == Some(3)); + assert!(b.iter().position(|&v| v == 3) == Some(2)); + assert!(b.iter().position(|&v| v == 0) == None); +} + +#[test] +fn test_rposition() { + let b = [1, 2, 3, 5, 5]; + assert!(b.iter().rposition(|&v| v == 9) == None); + assert!(b.iter().rposition(|&v| v == 5) == Some(4)); + assert!(b.iter().rposition(|&v| v == 3) == Some(2)); + assert!(b.iter().rposition(|&v| v == 0) == None); +} + #[test] fn test_binary_search() { let b = [1, 2, 4, 6, 8, 9]; From 204d6409cfe61281b2a1b76c0f010e692242aee5 Mon Sep 17 00:00:00 2001 From: arthurprs Date: Mon, 30 Oct 2017 10:52:18 +0100 Subject: [PATCH 2/2] run more tests in travis --- .travis.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index db34f1404481b..ec782b6b55184 100644 --- a/.travis.yml +++ b/.travis.yml @@ -128,11 +128,9 @@ matrix: - env: IMAGE=dist-armv7-linux DEPLOY=1 if: branch = auto - env: IMAGE=dist-i586-gnu-i686-musl DEPLOY=1 - if: branch = auto - env: IMAGE=dist-i686-freebsd DEPLOY=1 if: branch = auto - env: IMAGE=dist-i686-linux DEPLOY=1 - if: branch = auto - env: IMAGE=dist-mips-linux DEPLOY=1 if: branch = auto - env: IMAGE=dist-mips64-linux DEPLOY=1 @@ -152,7 +150,6 @@ matrix: - env: IMAGE=dist-x86_64-freebsd DEPLOY=1 if: branch = auto - env: IMAGE=dist-x86_64-musl DEPLOY=1 - if: branch = auto - env: IMAGE=dist-x86_64-netbsd DEPLOY=1 if: branch = auto - env: IMAGE=asmjs