]> git.lizzy.rs Git - rust.git/blob - src/test/ui/type-id-higher-rank.rs
Rollup merge of #61207 - taiki-e:arbitrary_self_types-lifetime-elision-2, r=Centril
[rust.git] / src / test / ui / type-id-higher-rank.rs
1 // run-pass
2 // Test that type IDs correctly account for higher-rank lifetimes
3 // Also acts as a regression test for an ICE (issue #19791)
4
5 use std::any::{Any, TypeId};
6
7 struct Struct<'a>(&'a ());
8 trait Trait<'a> {}
9
10 fn main() {
11     // Bare fns
12     {
13         let a = TypeId::of::<fn(&'static isize, &'static isize)>();
14         let b = TypeId::of::<for<'a> fn(&'static isize, &'a isize)>();
15         let c = TypeId::of::<for<'a, 'b> fn(&'a isize, &'b isize)>();
16         let d = TypeId::of::<for<'a, 'b> fn(&'b isize, &'a isize)>();
17         assert!(a != b);
18         assert!(a != c);
19         assert!(a != d);
20         assert!(b != c);
21         assert!(b != d);
22         assert_eq!(c, d);
23
24         // Make sure De Bruijn indices are handled correctly
25         let e = TypeId::of::<for<'a> fn(fn(&'a isize) -> &'a isize)>();
26         let f = TypeId::of::<fn(for<'a> fn(&'a isize) -> &'a isize)>();
27         assert!(e != f);
28
29         // Make sure lifetime parameters of items are not ignored.
30         let g = TypeId::of::<for<'a> fn(&'a dyn Trait<'a>) -> Struct<'a>>();
31         let h = TypeId::of::<for<'a> fn(&'a dyn Trait<'a>) -> Struct<'static>>();
32         let i = TypeId::of::<for<'a, 'b> fn(&'a dyn Trait<'b>) -> Struct<'b>>();
33         assert!(g != h);
34         assert!(g != i);
35         assert!(h != i);
36
37         // Make sure lifetime anonymization handles nesting correctly
38         let j = TypeId::of::<fn(for<'a> fn(&'a isize) -> &'a usize)>();
39         let k = TypeId::of::<fn(for<'b> fn(&'b isize) -> &'b usize)>();
40         assert_eq!(j, k);
41     }
42     // Boxed unboxed closures
43     {
44         let a = TypeId::of::<Box<dyn Fn(&'static isize, &'static isize)>>();
45         let b = TypeId::of::<Box<dyn for<'a> Fn(&'static isize, &'a isize)>>();
46         let c = TypeId::of::<Box<dyn for<'a, 'b> Fn(&'a isize, &'b isize)>>();
47         let d = TypeId::of::<Box<dyn for<'a, 'b> Fn(&'b isize, &'a isize)>>();
48         assert!(a != b);
49         assert!(a != c);
50         assert!(a != d);
51         assert!(b != c);
52         assert!(b != d);
53         assert_eq!(c, d);
54
55         // Make sure De Bruijn indices are handled correctly
56         let e = TypeId::of::<Box<dyn for<'a> Fn(Box<dyn Fn(&'a isize) -> &'a isize>)>>();
57         let f = TypeId::of::<Box<dyn Fn(Box<dyn for<'a> Fn(&'a isize) -> &'a isize>)>>();
58         assert!(e != f);
59     }
60     // Raw unboxed closures
61     // Note that every unboxed closure has its own anonymous type,
62     // so no two IDs should equal each other, even when compatible
63     {
64         let a = id(|_: &isize, _: &isize| {});
65         let b = id(|_: &isize, _: &isize| {});
66         assert!(a != b);
67     }
68
69     fn id<T:Any>(_: T) -> TypeId {
70         TypeId::of::<T>()
71     }
72 }