Skip to content

Commit c088890

Browse files
authored
Rollup merge of rust-lang#63123 - TankhouseAle:const-fn-type-name-any, r=oli-obk
`const fn`-ify `std::any::type_name` as laid out in rust-lang#63084 A test, based on the one I added when I implemented support for the underlying `core::intrinsics::type_name` being allowed in `const fn` contexts, is included.
2 parents cdf9758 + 4a3d41d commit c088890

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/libcore/any.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ impl TypeId {
468468
/// The current implementation uses the same infrastructure as compiler
469469
/// diagnostics and debuginfo, but this is not guaranteed.
470470
#[stable(feature = "type_name", since = "1.38.0")]
471-
pub fn type_name<T: ?Sized>() -> &'static str {
471+
#[rustc_const_unstable(feature = "const_type_name")]
472+
pub const fn type_name<T: ?Sized>() -> &'static str {
472473
#[cfg(bootstrap)]
473474
unsafe {
474475
intrinsics::type_name::<T>()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// run-pass
2+
3+
#![feature(const_fn)]
4+
#![feature(const_type_name)]
5+
#![allow(dead_code)]
6+
7+
const fn type_name_wrapper<T>(_: &T) -> &'static str {
8+
std::any::type_name::<T>()
9+
}
10+
11+
struct Struct<TA, TB, TC> {
12+
a: TA,
13+
b: TB,
14+
c: TC,
15+
}
16+
17+
type StructInstantiation = Struct<i8, f64, bool>;
18+
19+
const CONST_STRUCT: StructInstantiation = StructInstantiation { a: 12, b: 13.7, c: false };
20+
21+
const CONST_STRUCT_NAME: &'static str = type_name_wrapper(&CONST_STRUCT);
22+
23+
fn main() {
24+
let non_const_struct = StructInstantiation { a: 87, b: 65.99, c: true };
25+
26+
let non_const_struct_name = type_name_wrapper(&non_const_struct);
27+
28+
assert_eq!(CONST_STRUCT_NAME, non_const_struct_name);
29+
}

0 commit comments

Comments
 (0)