summaryrefslogtreecommitdiffstats
path: root/tests/ui/typeid-intrinsic.rs
blob: 5bc4e0c217f40894c0d406cce38ceeb41cc95f8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// run-pass

#![allow(deprecated)]
// aux-build:typeid-intrinsic-aux1.rs
// aux-build:typeid-intrinsic-aux2.rs

#![feature(core_intrinsics)]

extern crate typeid_intrinsic_aux1 as other1;
extern crate typeid_intrinsic_aux2 as other2;

use std::hash::{SipHasher, Hasher, Hash};
use std::any::TypeId;

struct A;
struct Test;

pub fn main() {
    assert_eq!(TypeId::of::<other1::A>(), other1::id_A());
    assert_eq!(TypeId::of::<other1::B>(), other1::id_B());
    assert_eq!(TypeId::of::<other1::C>(), other1::id_C());
    assert_eq!(TypeId::of::<other1::D>(), other1::id_D());
    assert_eq!(TypeId::of::<other1::E>(), other1::id_E());
    assert_eq!(TypeId::of::<other1::F>(), other1::id_F());
    assert_eq!(TypeId::of::<other1::G>(), other1::id_G());
    assert_eq!(TypeId::of::<other1::H>(), other1::id_H());
    assert_eq!(TypeId::of::<other1::I>(), other1::id_I());

    assert_eq!(TypeId::of::<other2::A>(), other2::id_A());
    assert_eq!(TypeId::of::<other2::B>(), other2::id_B());
    assert_eq!(TypeId::of::<other2::C>(), other2::id_C());
    assert_eq!(TypeId::of::<other2::D>(), other2::id_D());
    assert_eq!(TypeId::of::<other2::E>(), other2::id_E());
    assert_eq!(TypeId::of::<other2::F>(), other2::id_F());
    assert_eq!(TypeId::of::<other2::G>(), other2::id_G());
    assert_eq!(TypeId::of::<other2::H>(), other2::id_H());
    assert_eq!(TypeId::of::<other1::I>(), other2::id_I());

    assert_eq!(other1::id_F(), other2::id_F());
    assert_eq!(other1::id_G(), other2::id_G());
    assert_eq!(other1::id_H(), other2::id_H());
    assert_eq!(other1::id_I(), other2::id_I());

    assert_eq!(TypeId::of::<isize>(), other2::foo::<isize>());
    assert_eq!(TypeId::of::<isize>(), other1::foo::<isize>());
    assert_eq!(other2::foo::<isize>(), other1::foo::<isize>());
    assert_eq!(TypeId::of::<A>(), other2::foo::<A>());
    assert_eq!(TypeId::of::<A>(), other1::foo::<A>());
    assert_eq!(other2::foo::<A>(), other1::foo::<A>());

    // sanity test of TypeId
    let (a, b, c) = (TypeId::of::<usize>(), TypeId::of::<&'static str>(),
                     TypeId::of::<Test>());
    let (d, e, f) = (TypeId::of::<usize>(), TypeId::of::<&'static str>(),
                     TypeId::of::<Test>());

    assert!(a != b);
    assert!(a != c);
    assert!(b != c);

    assert_eq!(a, d);
    assert_eq!(b, e);
    assert_eq!(c, f);

    // check it has a hash
    let (a, b) = (TypeId::of::<usize>(), TypeId::of::<usize>());

    let mut s1 = SipHasher::new();
    a.hash(&mut s1);
    let mut s2 = SipHasher::new();
    b.hash(&mut s2);

    assert_eq!(s1.finish(), s2.finish());

    // Check projections

    assert_eq!(TypeId::of::<other1::I32Iterator>(), other1::id_i32_iterator());
    assert_eq!(TypeId::of::<other1::U32Iterator>(), other1::id_u32_iterator());
    assert_eq!(other1::id_i32_iterator(), other2::id_i32_iterator());
    assert_eq!(other1::id_u32_iterator(), other2::id_u32_iterator());
    assert_ne!(other1::id_i32_iterator(), other1::id_u32_iterator());
    assert_ne!(TypeId::of::<other1::I32Iterator>(), TypeId::of::<other1::U32Iterator>());

    // Check fn pointer against collisions
    assert_ne!(
        TypeId::of::<fn(fn(A) -> A) -> A>(),
        TypeId::of::<fn(fn() -> A, A) -> A>()
    );
    assert_ne!(
        TypeId::of::<for<'a> fn(&'a i32) -> &'a i32>(),
        TypeId::of::<for<'a> fn(&'a i32) -> &'static i32>()
    );
    assert_ne!(
        TypeId::of::<for<'a, 'b> fn(&'a i32, &'b i32) -> &'a i32>(),
        TypeId::of::<for<'a, 'b> fn(&'b i32, &'a i32) -> &'a i32>()
    );
}