Skip to content

Commit 91fa898

Browse files
committed
Stabilize the type_name intrinsic in core::any
Closes rust-lang/rfcs#1428
1 parent 03f19f7 commit 91fa898

File tree

14 files changed

+46
-25
lines changed

14 files changed

+46
-25
lines changed

src/libcore/any.rs

+26
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,29 @@ impl TypeId {
450450
}
451451
}
452452
}
453+
454+
/// Returns the name of a type as a string slice.
455+
///
456+
/// # Note
457+
///
458+
/// This is intended for diagnostic use. The exact contents and format of the
459+
/// string are not specified, other than being a best-effort description of the
460+
/// type. For example, `type_name::<Option<String>>()` could return the
461+
/// `"Option<String>"` or `"std::option::Option<std::string::String>"`, but not
462+
/// `"foobar"`. In addition, the output may change between versions of the
463+
/// compiler.
464+
///
465+
/// The type name should not be considered a unique identifier of a type;
466+
/// multiple types may share the same type name.
467+
///
468+
/// The current implementation uses the same infrastructure as compiler
469+
/// diagnostics and debuginfo, but this is not guaranteed.
470+
#[stable(feature = "type_name", since = "1.38.0")]
471+
pub fn type_name<T: ?Sized>() -> &'static str {
472+
#[cfg(bootstrap)]
473+
unsafe {
474+
intrinsics::type_name::<T>()
475+
}
476+
#[cfg(not(bootstrap))]
477+
intrinsics::type_name::<T>()
478+
}

src/librustc/traits/query/normalize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl<'cx, 'tcx> At<'cx, 'tcx> {
3434
{
3535
debug!(
3636
"normalize::<{}>(value={:?}, param_env={:?})",
37-
unsafe { ::std::intrinsics::type_name::<T>() },
37+
::std::any::type_name::<T>(),
3838
value,
3939
self.param_env,
4040
);

src/librustc/traits/query/normalize_erasing_regions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<'tcx> TyCtxt<'tcx> {
2222
{
2323
debug!(
2424
"normalize_erasing_regions::<{}>(value={:?}, param_env={:?})",
25-
unsafe { ::std::intrinsics::type_name::<T>() },
25+
::std::any::type_name::<T>(),
2626
value,
2727
param_env,
2828
);

src/librustc/ty/query/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'tcx, M: QueryAccessors<'tcx, Key = DefId>> QueryDescription<'tcx> for M {
6969
if !tcx.sess.verbose() {
7070
format!("processing `{}`", tcx.def_path_str(def_id)).into()
7171
} else {
72-
let name = unsafe { ::std::intrinsics::type_name::<M>() };
72+
let name = ::std::any::type_name::<M>();
7373
format!("processing {:?} with query `{}`", def_id, name).into()
7474
}
7575
}

src/librustc/ty/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use rustc_target::spec::PanicStrategy;
5454
use std::borrow::Cow;
5555
use std::ops::Deref;
5656
use std::sync::Arc;
57-
use std::intrinsics::type_name;
57+
use std::any::type_name;
5858
use syntax_pos::{Span, DUMMY_SP};
5959
use syntax_pos::symbol::InternedString;
6060
use syntax::attr;

src/librustc/ty/query/on_disk_cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ where
10601060
Q::Value: Encodable,
10611061
{
10621062
let desc = &format!("encode_query_results for {}",
1063-
unsafe { ::std::intrinsics::type_name::<Q>() });
1063+
::std::any::type_name::<Q>());
10641064

10651065
time_ext(tcx.sess.time_extended(), Some(tcx.sess), desc, || {
10661066
let map = Q::query_cache(tcx).borrow();

src/librustc/ty/query/plumbing.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,9 @@ macro_rules! define_queries_inner {
782782
#[cfg(not(debug_assertions))]
783783
cache_hits: 0,
784784
key_size: mem::size_of::<Q::Key>(),
785-
key_type: unsafe { type_name::<Q::Key>() },
785+
key_type: type_name::<Q::Key>(),
786786
value_size: mem::size_of::<Q::Value>(),
787-
value_type: unsafe { type_name::<Q::Value>() },
787+
value_type: type_name::<Q::Value>(),
788788
entry_count: map.results.len(),
789789
}
790790
}

src/librustc_mir/transform/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl<'tcx> MirSource<'tcx> {
127127
/// Generates a default name for the pass based on the name of the
128128
/// type `T`.
129129
pub fn default_name<T: ?Sized>() -> Cow<'static, str> {
130-
let name = unsafe { ::std::intrinsics::type_name::<T>() };
130+
let name = ::std::any::type_name::<T>();
131131
if let Some(tail) = name.rfind(":") {
132132
Cow::from(&name[tail+1..])
133133
} else {

src/librustc_typeck/check/intrinsic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn intrisic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
7171
"saturating_add" | "saturating_sub" |
7272
"rotate_left" | "rotate_right" |
7373
"ctpop" | "ctlz" | "cttz" | "bswap" | "bitreverse" |
74-
"minnumf32" | "minnumf64" | "maxnumf32" | "maxnumf64"
74+
"minnumf32" | "minnumf64" | "maxnumf32" | "maxnumf64" | "type_name"
7575
=> hir::Unsafety::Normal,
7676
_ => hir::Unsafety::Unsafe,
7777
}

src/libserialize/serialize.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
Core encoding and decoding interfaces.
55
*/
66

7+
use std::any;
78
use std::borrow::Cow;
8-
use std::intrinsics;
99
use std::marker::PhantomData;
1010
use std::path;
1111
use std::rc::Rc;
@@ -849,9 +849,9 @@ pub trait SpecializationError {
849849
impl<E> SpecializationError for E {
850850
default fn not_found<S, T: ?Sized>(trait_name: &'static str, method_name: &'static str) -> E {
851851
panic!("missing specialization: `<{} as {}<{}>>::{}` not overridden",
852-
unsafe { intrinsics::type_name::<S>() },
852+
any::type_name::<S>(),
853853
trait_name,
854-
unsafe { intrinsics::type_name::<T>() },
854+
any::type_name::<T>(),
855855
method_name);
856856
}
857857
}

src/test/run-pass/consts/const-fn-type-name.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![allow(dead_code)]
66

77
const fn type_name_wrapper<T>(_: &T) -> &'static str {
8-
unsafe { core::intrinsics::type_name::<T>() }
8+
core::intrinsics::type_name::<T>()
99
}
1010

1111
struct Struct<TA, TB, TC> {

src/test/run-pass/issues/issue-21058.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// run-pass
22
#![allow(dead_code)]
3-
#![feature(core_intrinsics)]
43

54
use std::fmt::Debug;
65

@@ -12,7 +11,7 @@ macro_rules! check {
1211
assert_eq!(type_name_of_val($ty_of), $expected);
1312
};
1413
($ty:ty, $expected:expr) => {
15-
assert_eq!(unsafe { std::intrinsics::type_name::<$ty>()}, $expected);
14+
assert_eq!(std::any::type_name::<$ty>(), $expected);
1615
};
1716
}
1817

@@ -50,7 +49,7 @@ fn bar<T: Trait>() {
5049
}
5150

5251
fn type_name_of_val<T>(_: T) -> &'static str {
53-
unsafe { std::intrinsics::type_name::<T>() }
52+
std::any::type_name::<T>()
5453
}
5554

5655
#[derive(Debug)]

src/test/run-pass/issues/issue-61894.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#![feature(core_intrinsics)]
22

3-
use std::intrinsics::type_name;
3+
use std::any::type_name;
44

55
struct Bar<M>(M);
66

77
impl<M> Bar<M> {
88
fn foo(&self) -> &'static str {
99
fn f() {}
1010
fn type_name_of<T>(_: T) -> &'static str {
11-
unsafe { type_name::<T>() }
11+
type_name::<T>()
1212
}
1313
type_name_of(f)
1414
}

src/test/run-pass/tydesc-name.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
#![allow(dead_code)]
22

3-
#![feature(core_intrinsics)]
4-
5-
use std::intrinsics::type_name;
3+
use std::any::type_name;
64

75
struct Foo<T> {
86
x: T
97
}
108

119
pub fn main() {
12-
unsafe {
13-
assert_eq!(type_name::<isize>(), "isize");
14-
assert_eq!(type_name::<Foo<usize>>(), "tydesc_name::Foo<usize>");
15-
}
10+
assert_eq!(type_name::<isize>(), "isize");
11+
assert_eq!(type_name::<Foo<usize>>(), "tydesc_name::Foo<usize>");
1612
}

0 commit comments

Comments
 (0)