Skip to content

Commit

Permalink
Test generic methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nagisa committed Dec 17, 2015
1 parent b3720ea commit 7dd9579
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/librustc_trans/trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub fn trans_constval<'blk, 'tcx>(bcx: common::Block<'blk, 'tcx>,
}
},
ConstVal::Array(id, _) | ConstVal::Repeat(id, _) => {
let expr = ccx.tcx().map.expect_expr(id);
let expr = bcx.tcx().map.expect_expr(id);
expr::trans(bcx, expr).datum.val
},
ConstVal::Function(_) => {
Expand Down
63 changes: 63 additions & 0 deletions src/test/run-pass/mir_refs_correct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@ trait X {
fn hoy2() -> u8 { 45 }
}

trait F<U> {
fn f(self, other: U) -> u64;
}

impl F<u32> for u32 {
fn f(self, other: u32) -> u64 { self as u64 + other as u64 }
}

impl F<u64> for u32 {
fn f(self, other: u64) -> u64 { self as u64 - other }
}

impl F<u64> for u64 {
fn f(self, other: u64) -> u64 { self * other }
}

impl F<u32> for u64 {
fn f(self, other: u32) -> u64 { self ^ other as u64 }
}

trait T<I, O> {
fn staticmeth(i: I, o: O) -> (I, O) { (i, o) }
}

impl<I, O> T<I, O> for O {}

impl X for S {}

enum E {
Expand All @@ -33,6 +59,7 @@ enum E {

const C: u8 = 84;
const C2: [u8; 5] = [42; 5];
const C3: [u8; 3] = [42, 41, 40];

fn regular() -> u8 {
21
Expand Down Expand Up @@ -108,6 +135,11 @@ fn t13() -> [u8; 5] {
C2
}

#[rustc_mir]
fn t13_2() -> [u8; 3] {
C3
}

#[rustc_mir]
fn t14() -> fn()-> u8 {
<S as X>::hoy2
Expand All @@ -118,6 +150,31 @@ fn t15() -> fn(&S)-> u8 {
S::hey2
}

#[rustc_mir]
fn t16() -> fn(u32, u32)->u64 {
F::f
}

#[rustc_mir]
fn t17() -> fn(u32, u64)->u64 {
F::f
}

#[rustc_mir]
fn t18() -> fn(u64, u64)->u64 {
F::f
}

#[rustc_mir]
fn t19() -> fn(u64, u32)->u64 {
F::f
}

#[rustc_mir]
fn t20() -> fn(u64, u32)->(u64, u32) {
<u32 as T<_, _>>::staticmeth
}

fn main(){
unsafe {
assert_eq!(t1()(), regular());
Expand Down Expand Up @@ -148,8 +205,14 @@ fn main(){

assert_eq!(t12(), C);
assert_eq!(t13(), C2);
assert_eq!(t13_2(), C3);

assert_eq!(t14()(), <S as X>::hoy2());
assert_eq!(t15()(&s), S::hey2(&s));
assert_eq!(t16()(10u32, 20u32), F::f(10u32, 20u32));
assert_eq!(t17()(30u32, 10u64), F::f(30u32, 10u64));
assert_eq!(t18()(50u64, 5u64), F::f(50u64, 5u64));
assert_eq!(t19()(322u64, 2u32), F::f(322u64, 2u32));
assert_eq!(t20()(123u64, 38u32), <u32 as T<_, _>>::staticmeth(123, 38));
}
}

0 comments on commit 7dd9579

Please sign in to comment.