auto merge of #7203 : msullivan/rust/default-methods, r=graydon

This fixes the large number of problems that prevented cross crate
methods from ever working. It also fixes a couple lingering bugs with
polymorphic default methods and cleans up some of the code paths.

Closes #4102. Closes #4103.

r? nikomatsakis
This commit is contained in:
bors 2013-06-20 22:28:52 -07:00
commit ba05af7b1c
10 changed files with 254 additions and 216 deletions

View file

@ -0,0 +1,34 @@
#[allow(default_methods)];
pub trait A {
fn f(&self) -> int;
fn g(&self) -> int { 10 }
fn h(&self) -> int { 10 }
}
impl A for int {
fn f(&self) -> int { 10 }
}
trait B<T> {
fn thing<U>(&self, x: T, y: U) -> (T, U) { (x, y) }
}
impl<T> B<T> for int { }
impl B<float> for bool { }
pub trait TestEquality {
fn test_eq(&self, rhs: &Self) -> bool;
fn test_neq(&self, rhs: &Self) -> bool {
!self.test_eq(rhs)
}
}
impl TestEquality for int {
fn test_eq(&self, rhs: &int) -> bool {
*self == *rhs
}
}

View file

@ -15,6 +15,7 @@ trait A<T> {
}
impl A<int> for int { }
impl<T> A<T> for uint { }
fn f<T, U, V: A<T>>(i: V, j: T, k: U) -> (T, U) {
i.g(j, k)
@ -22,4 +23,5 @@ fn f<T, U, V: A<T>>(i: V, j: T, k: U) -> (T, U) {
pub fn main () {
assert_eq!(f(0, 1, 2), (1, 2));
assert_eq!(f(0u, 1, 2), (1, 2));
}

View file

@ -0,0 +1,71 @@
// xfail-fast
// aux-build:trait_default_method_xc_aux.rs
#[allow(default_methods)];
extern mod aux(name = "trait_default_method_xc_aux");
use aux::{A, B, TestEquality};
fn f<T: aux::A>(i: T) {
assert_eq!(i.g(), 10);
}
pub struct thing { x: int }
impl A for thing {
fn f(&self) -> int { 10 }
}
fn g<T, U, V: B<T>>(i: V, j: T, k: U) -> (T, U) {
i.thing(j, k)
}
fn eq<T: TestEquality>(lhs: &T, rhs: &T) -> bool {
lhs.test_eq(rhs)
}
fn neq<T: TestEquality>(lhs: &T, rhs: &T) -> bool {
lhs.test_neq(rhs)
}
impl TestEquality for thing {
fn test_eq(&self, rhs: &thing) -> bool {
//self.x.test_eq(&rhs.x)
eq(&self.x, &rhs.x)
}
}
fn main () {
// Some tests of random things
f(0);
let a = thing { x: 0 };
let b = thing { x: 1 };
assert_eq!(0i.g(), 10);
assert_eq!(a.g(), 10);
assert_eq!(a.h(), 10);
assert_eq!(0i.thing(3.14, 1), (3.14, 1));
assert_eq!(g(0i, 3.14, 1), (3.14, 1));
assert_eq!(g(false, 3.14, 1), (3.14, 1));
let obj = @0i as @A;
assert_eq!(obj.h(), 10);
// Trying out a real one
assert!(12.test_neq(&10));
assert!(!10.test_neq(&10));
assert!(a.test_neq(&b));
assert!(!a.test_neq(&a));
assert!(neq(&12, &10));
assert!(!neq(&10, &10));
assert!(neq(&a, &b));
assert!(!neq(&a, &a));
}