Skip to content

[WIP] Don't rely on the type of lhs when type checking lhs OP rhs #19434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
@@ -945,15 +945,15 @@ mod tests {
let mut m = list_from(v.as_slice());
m.rotate_backward(); check_links(&m);
m.rotate_forward(); check_links(&m);
assert_eq!(v.iter().collect::<Vec<&int>>(), m.iter().collect());
assert_eq!(v.iter().collect::<Vec<&int>>(), m.iter().collect::<Vec<_>>());
m.rotate_forward(); check_links(&m);
m.rotate_forward(); check_links(&m);
m.pop_front(); check_links(&m);
m.rotate_forward(); check_links(&m);
m.rotate_backward(); check_links(&m);
m.push_front(9); check_links(&m);
m.rotate_forward(); check_links(&m);
assert_eq!(vec![3i,9,5,1,2], m.into_iter().collect());
assert_eq!(vec![3i,9,5,1,2], m.into_iter().collect::<Vec<_>>());
}

#[test]
22 changes: 11 additions & 11 deletions src/libcollections/enum_set.rs
Original file line number Diff line number Diff line change
@@ -401,19 +401,19 @@ mod test {
assert!(elems.is_empty())

e1.insert(A);
let elems = e1.iter().collect();
let elems: Vec<_> = e1.iter().collect();
assert_eq!(vec![A], elems)

e1.insert(C);
let elems = e1.iter().collect();
let elems: Vec<_> = e1.iter().collect();
assert_eq!(vec![A,C], elems)

e1.insert(C);
let elems = e1.iter().collect();
let elems: Vec<_> = e1.iter().collect();
assert_eq!(vec![A,C], elems)

e1.insert(B);
let elems = e1.iter().collect();
let elems: Vec<_> = e1.iter().collect();
assert_eq!(vec![A,B,C], elems)
}

@@ -431,35 +431,35 @@ mod test {
e2.insert(C);

let e_union = e1 | e2;
let elems = e_union.iter().collect();
let elems: Vec<_> = e_union.iter().collect();
assert_eq!(vec![A,B,C], elems)

let e_intersection = e1 & e2;
let elems = e_intersection.iter().collect();
let elems: Vec<_> = e_intersection.iter().collect();
assert_eq!(vec![C], elems)

// Another way to express intersection
let e_intersection = e1 - (e1 - e2);
let elems = e_intersection.iter().collect();
let elems: Vec<_> = e_intersection.iter().collect();
assert_eq!(vec![C], elems)

let e_subtract = e1 - e2;
let elems = e_subtract.iter().collect();
let elems: Vec<_> = e_subtract.iter().collect();
assert_eq!(vec![A], elems)

// Bitwise XOR of two sets, aka symmetric difference
let e_symmetric_diff = e1 ^ e2;
let elems = e_symmetric_diff.iter().collect();
let elems: Vec<_> = e_symmetric_diff.iter().collect();
assert_eq!(vec![A,B], elems)

// Another way to express symmetric difference
let e_symmetric_diff = (e1 - e2) | (e2 - e1);
let elems = e_symmetric_diff.iter().collect();
let elems: Vec<_> = e_symmetric_diff.iter().collect();
assert_eq!(vec![A,B], elems)

// Yet another way to express symmetric difference
let e_symmetric_diff = (e1 | e2) - (e1 & e2);
let elems = e_symmetric_diff.iter().collect();
let elems: Vec<_> = e_symmetric_diff.iter().collect();
assert_eq!(vec![A,B], elems)
}

1 change: 1 addition & 0 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
@@ -563,6 +563,7 @@ impl<'a> Ord for MaybeOwned<'a> {
}
}

#[allow(deprecated)]
#[deprecated = "use std::str::CowString"]
impl<'a, S: Str> Equiv<S> for MaybeOwned<'a> {
#[inline]
19 changes: 17 additions & 2 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ use str::{CharRange, CowString, FromStr, StrAllocating, Owned};
use vec::{DerefVec, Vec, as_vec};

/// A growable string stored as a UTF-8 encoded buffer.
#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord)]
#[deriving(Clone, PartialOrd, Eq, Ord)]
#[stable]
pub struct String {
vec: Vec<u8>,
@@ -738,6 +738,20 @@ impl Extend<char> for String {
}
}

impl<Rhs> PartialEq<Rhs> for String where Rhs: Deref<str> {
#[inline]
fn eq(&self, other: &Rhs) -> bool { PartialEq::eq(&**self, &**other) }
#[inline]
fn ne(&self, other: &Rhs) -> bool { PartialEq::ne(&**self, &**other) }
}

impl<'a> PartialEq<String> for &'a str {
#[inline]
fn eq(&self, other: &String) -> bool { PartialEq::eq(*self, &**other) }
#[inline]
fn ne(&self, other: &String) -> bool { PartialEq::ne(*self, &**other) }
}

#[experimental = "waiting on Str stabilization"]
impl Str for String {
#[inline]
@@ -779,7 +793,8 @@ impl<H: hash::Writer> hash::Hash<H> for String {
}
}

#[experimental = "waiting on Equiv stabilization"]
#[allow(deprecated)]
#[deprecated = "Use overloaded `core::cmp::PartialEq`"]
impl<'a, S: Str> Equiv<S> for String {
#[inline]
fn equiv(&self, other: &S) -> bool {
34 changes: 24 additions & 10 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
@@ -535,12 +535,25 @@ impl<T> Extend<T> for Vec<T> {
}
}

#[unstable = "waiting on PartialEq stability"]
impl<T: PartialEq> PartialEq for Vec<T> {
impl<A, B, Rhs> PartialEq<Rhs> for Vec<A> where A: PartialEq<B>, Rhs: Deref<[B]> {
#[inline]
fn eq(&self, other: &Vec<T>) -> bool {
self.as_slice() == other.as_slice()
}
fn eq(&self, other: &Rhs) -> bool { PartialEq::eq(&**self, &**other) }
#[inline]
fn ne(&self, other: &Rhs) -> bool { PartialEq::ne(&**self, &**other) }
}

impl<'a, A, B> PartialEq<Vec<B>> for &'a [A] where A: PartialEq<B> {
#[inline]
fn eq(&self, other: &Vec<B>) -> bool { PartialEq::eq(*self, &**other) }
#[inline]
fn ne(&self, other: &Vec<B>) -> bool { PartialEq::ne(*self, &**other) }
}

impl<'a, A, B> PartialEq<Vec<B>> for &'a mut [A] where A: PartialEq<B> {
#[inline]
fn eq(&self, other: &Vec<B>) -> bool { PartialEq::eq(&**self, &**other) }
#[inline]
fn ne(&self, other: &Vec<B>) -> bool { PartialEq::ne(&**self, &**other) }
}

#[unstable = "waiting on PartialOrd stability"]
@@ -554,7 +567,8 @@ impl<T: PartialOrd> PartialOrd for Vec<T> {
#[unstable = "waiting on Eq stability"]
impl<T: Eq> Eq for Vec<T> {}

#[experimental]
#[allow(deprecated)]
#[deprecated = "Use overloaded `core::cmp::PartialEq`"]
impl<T: PartialEq, Sized? V: AsSlice<T>> Equiv<V> for Vec<T> {
#[inline]
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
@@ -1813,27 +1827,27 @@ mod tests {
let mut values = vec![1u8,2,3,4,5];
{
let slice = values.slice_from_mut(2);
assert!(slice == &mut [3, 4, 5]);
assert!(slice == [3, 4, 5]);
for p in slice.iter_mut() {
*p += 2;
}
}

assert!(values.as_slice() == &[1, 2, 5, 6, 7]);
assert!(values.as_slice() == [1, 2, 5, 6, 7]);
}

#[test]
fn test_slice_to_mut() {
let mut values = vec![1u8,2,3,4,5];
{
let slice = values.slice_to_mut(2);
assert!(slice == &mut [1, 2]);
assert!(slice == [1, 2]);
for p in slice.iter_mut() {
*p += 1;
}
}

assert!(values.as_slice() == &[2, 3, 3, 4, 5]);
assert!(values.as_slice() == [2, 3, 3, 4, 5]);
}

#[test]
27 changes: 24 additions & 3 deletions src/libcore/array.rs
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ use clone::Clone;
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
use fmt;
use kinds::Copy;
use ops::Deref;
use option::Option;

// macro for implementing n-ary tuple functions and operations
@@ -39,17 +40,37 @@ macro_rules! array_impls {
}

#[unstable = "waiting for PartialEq to stabilize"]
impl<T:PartialEq> PartialEq for [T, ..$N] {
impl<A, B> PartialEq<[B, ..$N]> for [A, ..$N] where A: PartialEq<B> {
#[inline]
fn eq(&self, other: &[T, ..$N]) -> bool {
fn eq(&self, other: &[B, ..$N]) -> bool {
self[] == other[]
}
#[inline]
fn ne(&self, other: &[T, ..$N]) -> bool {
fn ne(&self, other: &[B, ..$N]) -> bool {
self[] != other[]
}
}

impl<'a, A, B, Rhs> PartialEq<Rhs> for [A, ..$N] where
A: PartialEq<B>,
Rhs: Deref<[B]>,
{
#[inline(always)]
fn eq(&self, other: &Rhs) -> bool { PartialEq::eq(self[], &**other) }
#[inline(always)]
fn ne(&self, other: &Rhs) -> bool { PartialEq::ne(self[], &**other) }
}

impl<'a, A, B, Lhs> PartialEq<[B, ..$N]> for Lhs where
A: PartialEq<B>,
Lhs: Deref<[A]>
{
#[inline(always)]
fn eq(&self, other: &[B, ..$N]) -> bool { PartialEq::eq(&**self, other[]) }
#[inline(always)]
fn ne(&self, other: &[B, ..$N]) -> bool { PartialEq::ne(&**self, other[]) }
}

#[unstable = "waiting for Eq to stabilize"]
impl<T:Eq> Eq for [T, ..$N] { }

Loading