|
| 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)] |
| 15 | + |
| 16 | +use std::intrinsics::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 | + // Stack closures |
| 38 | + { |
| 39 | + let a = TypeId::of::<|&'static int, &'static int|>(); |
| 40 | + let b = TypeId::of::<for<'a> |&'static int, &'a int|>(); |
| 41 | + let c = TypeId::of::<for<'a, 'b> |&'a int, &'b int|>(); |
| 42 | + let d = TypeId::of::<for<'a, 'b> |&'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::<for<'a> |(|&'a int| -> &'a int)|>(); |
| 52 | + let f = TypeId::of::<|for<'a> |&'a int| -> &'a int|>(); |
| 53 | + assert!(e != f); |
| 54 | + } |
| 55 | + // Boxed unboxed closures |
| 56 | + { |
| 57 | + let a = TypeId::of::<Box<Fn(&'static int, &'static int)>>(); |
| 58 | + let b = TypeId::of::<Box<for<'a> Fn(&'static int, &'a int)>>(); |
| 59 | + let c = TypeId::of::<Box<for<'a, 'b> Fn(&'a int, &'b int)>>(); |
| 60 | + let d = TypeId::of::<Box<for<'a, 'b> Fn(&'b int, &'a int)>>(); |
| 61 | + assert!(a != b); |
| 62 | + assert!(a != c); |
| 63 | + assert!(a != d); |
| 64 | + assert!(b != c); |
| 65 | + assert!(b != d); |
| 66 | + assert_eq!(c, d); |
| 67 | + |
| 68 | + // Make sure De Bruijn indices are handled correctly |
| 69 | + let e = TypeId::of::<Box<for<'a> Fn(Box<Fn(&'a int) -> &'a int>)>>(); |
| 70 | + let f = TypeId::of::<Box<Fn(Box<for<'a> Fn(&'a int) -> &'a int>)>>(); |
| 71 | + assert!(e != f); |
| 72 | + } |
| 73 | + // Raw unboxed closures |
| 74 | + // Note that every unboxed closure has its own anonymous type, |
| 75 | + // so no two IDs should equal each other, even when compatible |
| 76 | + { |
| 77 | + let a = id(|&: _: &int, _: &int| {}); |
| 78 | + let b = id(|&: _: &int, _: &int| {}); |
| 79 | + assert!(a != b); |
| 80 | + } |
| 81 | + |
| 82 | + fn id<T:'static>(_: T) -> TypeId { |
| 83 | + TypeId::of::<T>() |
| 84 | + } |
| 85 | +} |
0 commit comments