-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #47892 - Badel2:const_type_id_of, r=oli-obk
Turn `type_id` into a constant intrinsic #27745 The method `get_type_id` in `Any` is intended to support reflection. It's currently unstable in favor of using an associated constant instead. This PR makes the `type_id` intrinsic a constant intrinsic, the same as `size_of` and `align_of`, allowing `TypeId::of` to be a `const fn`, which will allow using an associated constant in `Any`.
- Loading branch information
Showing
8 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |