Improve weight algorithm and tests

This commit is contained in:
ggomez 2016-05-03 17:03:00 +02:00
parent b8fad79a07
commit 61e6169ffe
3 changed files with 133 additions and 59 deletions

View file

@ -31,4 +31,5 @@ impl Index<usize> for [i32] {
fn main() {
Index::<u32>::index(&[1, 2, 3] as &[i32], 2u32); //~ ERROR E0277
//~| NOTE a usize is required
//~| NOTE required by
}

View file

@ -19,15 +19,11 @@ trait Index<Idx: ?Sized> {
fn index(&self, index: Idx) -> &Self::Output;
}
struct Foo {
i: usize,
}
#[rustc_on_unimplemented = "a Foo is required to index into a slice"]
impl Index<Foo> for [i32] {
#[rustc_on_unimplemented = "a isize is required to index into a slice"]
impl Index<isize> for [i32] {
type Output = i32;
fn index(&self, index: Foo) -> &i32 {
&self[index.i]
fn index(&self, index: isize) -> &i32 {
&self[index as usize]
}
}
@ -39,8 +35,30 @@ impl Index<usize> for [i32] {
}
}
trait Foo<A, B> {
fn f(&self, a: &A, b: &B);
}
#[rustc_on_unimplemented = "two i32 Foo trait takes"]
impl Foo<i32, i32> for [i32] {
fn f(&self, a: &i32, b: &i32) {}
}
#[rustc_on_unimplemented = "two u32 Foo trait takes"]
impl Foo<u32, u32> for [i32] {
fn f(&self, a: &u32, b: &u32) {}
}
#[rustc_error]
fn main() {
Index::<u32>::index(&[1, 2, 3] as &[i32], 2u32); //~ ERROR E0277
//~| NOTE a usize is required
//~| NOTE required by
Index::<i32>::index(&[1, 2, 3] as &[i32], 2i32); //~ ERROR E0277
//~| NOTE a isize is required
//~| NOTE required by
Foo::<usize, usize>::f(&[1, 2, 3] as &[i32], &2usize, &2usize); //~ ERROR E0277
//~| NOTE two u32 Foo trait
//~| NOTE required by
}