]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/type-id-higher-rank.rs
e4d7d2920566915707958f8e04b082cc78b60407
[rust.git] / src / test / run-pass / type-id-higher-rank.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test that type IDs correctly account for higher-rank lifetimes
12 // Also acts as a regression test for an ICE (issue #19791)
13
14 #![feature(unboxed_closures, core)]
15
16 use std::any::TypeId;
17
18 fn main() {
19     // Bare fns
20     {
21         let a = TypeId::of::<fn(&'static int, &'static int)>();
22         let b = TypeId::of::<for<'a> fn(&'static int, &'a int)>();
23         let c = TypeId::of::<for<'a, 'b> fn(&'a int, &'b int)>();
24         let d = TypeId::of::<for<'a, 'b> fn(&'b int, &'a int)>();
25         assert!(a != b);
26         assert!(a != c);
27         assert!(a != d);
28         assert!(b != c);
29         assert!(b != d);
30         assert_eq!(c, d);
31
32         // Make sure De Bruijn indices are handled correctly
33         let e = TypeId::of::<for<'a> fn(fn(&'a int) -> &'a int)>();
34         let f = TypeId::of::<fn(for<'a> fn(&'a int) -> &'a int)>();
35         assert!(e != f);
36     }
37     // Boxed unboxed closures
38     {
39         let a = TypeId::of::<Box<Fn(&'static int, &'static int)>>();
40         let b = TypeId::of::<Box<for<'a> Fn(&'static int, &'a int)>>();
41         let c = TypeId::of::<Box<for<'a, 'b> Fn(&'a int, &'b int)>>();
42         let d = TypeId::of::<Box<for<'a, 'b> Fn(&'b int, &'a int)>>();
43         assert!(a != b);
44         assert!(a != c);
45         assert!(a != d);
46         assert!(b != c);
47         assert!(b != d);
48         assert_eq!(c, d);
49
50         // Make sure De Bruijn indices are handled correctly
51         let e = TypeId::of::<Box<for<'a> Fn(Box<Fn(&'a int) -> &'a int>)>>();
52         let f = TypeId::of::<Box<Fn(Box<for<'a> Fn(&'a int) -> &'a int>)>>();
53         assert!(e != f);
54     }
55     // Raw unboxed closures
56     // Note that every unboxed closure has its own anonymous type,
57     // so no two IDs should equal each other, even when compatible
58     {
59         let a = id(|_: &int, _: &int| {});
60         let b = id(|_: &int, _: &int| {});
61         assert!(a != b);
62     }
63
64     fn id<T:'static>(_: T) -> TypeId {
65         TypeId::of::<T>()
66     }
67 }