diff options
| author | Badel2 <2badel2@gmail.com> | 2018-01-30 21:33:33 +0100 |
|---|---|---|
| committer | Badel2 <2badel2@gmail.com> | 2018-02-01 23:03:19 +0100 |
| commit | 196fad0d00bddd11074b5da32af2393abaac9a26 (patch) | |
| tree | 39a5a33c03509494bb8a14194e0923109dc26076 /src/test | |
| parent | def3269a71be2e737cad27418a3dad9f5bd6cd32 (diff) | |
Turn `type_id` into a constant intrinsic
Add rustc_const_unstable attribute for `any::TypeId::of` Add test for `const fn TypeId::of`
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/compile-fail/const-typeid-of.rs | 18 | ||||
| -rw-r--r-- | src/test/run-pass/const-typeid-of.rs | 43 |
2 files changed, 61 insertions, 0 deletions
diff --git a/src/test/compile-fail/const-typeid-of.rs b/src/test/compile-fail/const-typeid-of.rs new file mode 100644 index 00000000000..401125cef09 --- /dev/null +++ b/src/test/compile-fail/const-typeid-of.rs @@ -0,0 +1,18 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::any::TypeId; + +struct A; + +fn main() { + const A_ID: TypeId = TypeId::of::<A>(); + //~^ ERROR `std::any::TypeId::of` is not yet stable as a const fn +} diff --git a/src/test/run-pass/const-typeid-of.rs b/src/test/run-pass/const-typeid-of.rs new file mode 100644 index 00000000000..ce29e55c6d7 --- /dev/null +++ b/src/test/run-pass/const-typeid-of.rs @@ -0,0 +1,43 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(core_intrinsics)] +#![feature(const_type_id)] + +use std::any::TypeId; + +struct A; + +static ID_ISIZE: TypeId = TypeId::of::<isize>(); + +pub fn main() { + assert_eq!(ID_ISIZE, TypeId::of::<isize>()); + + // sanity test of TypeId + const T: (TypeId, TypeId, TypeId) = (TypeId::of::<usize>(), + TypeId::of::<&'static str>(), + TypeId::of::<A>()); + let (d, e, f) = (TypeId::of::<usize>(), TypeId::of::<&'static str>(), + TypeId::of::<A>()); + + assert!(T.0 != T.1); + assert!(T.0 != T.2); + assert!(T.1 != T.2); + + assert_eq!(T.0, d); + assert_eq!(T.1, e); + assert_eq!(T.2, f); + + // Check fn pointer against collisions + const F: (TypeId, TypeId) = (TypeId::of::<fn(fn(A) -> A) -> A>(), + TypeId::of::<fn(fn() -> A, A) -> A>()); + + assert!(F.0 != F.1); +} |
