]>
Commit | Line | Data |
---|---|---|
416331ca | 1 | // run-pass |
1a4d82fc JJ |
2 | // Test that type IDs correctly account for higher-rank lifetimes |
3 | // Also acts as a regression test for an ICE (issue #19791) | |
4 | ||
c34b1796 | 5 | use std::any::{Any, TypeId}; |
1a4d82fc | 6 | |
5bcae85e SL |
7 | struct Struct<'a>(&'a ()); |
8 | trait Trait<'a> {} | |
9 | ||
1a4d82fc JJ |
10 | fn main() { |
11 | // Bare fns | |
12 | { | |
c34b1796 AL |
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)>(); | |
1a4d82fc JJ |
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 | |
c34b1796 AL |
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)>(); | |
1a4d82fc | 27 | assert!(e != f); |
5bcae85e SL |
28 | |
29 | // Make sure lifetime parameters of items are not ignored. | |
dc9dc135 XL |
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>>(); | |
5bcae85e SL |
33 | assert!(g != h); |
34 | assert!(g != i); | |
35 | assert!(h != i); | |
3b2f2976 XL |
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); | |
1a4d82fc JJ |
41 | } |
42 | // Boxed unboxed closures | |
43 | { | |
dc9dc135 XL |
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)>>(); | |
1a4d82fc JJ |
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 | |
dc9dc135 XL |
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>)>>(); | |
1a4d82fc JJ |
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 | { | |
c34b1796 AL |
64 | let a = id(|_: &isize, _: &isize| {}); |
65 | let b = id(|_: &isize, _: &isize| {}); | |
1a4d82fc JJ |
66 | assert!(a != b); |
67 | } | |
68 | ||
c34b1796 | 69 | fn id<T:Any>(_: T) -> TypeId { |
1a4d82fc JJ |
70 | TypeId::of::<T>() |
71 | } | |
72 | } |