Skip to content

Commit 5222274

Browse files
committed
Stabilize type_name_of_val
Make the following API stable: // in core::any pub fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str Const stability is not added because this relies on `type_name` which is also not const. That has a blocking issue. Fixes rust-lang#66359
1 parent 41fe75e commit 5222274

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

library/core/src/any.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -690,44 +690,41 @@ pub const fn type_name<T: ?Sized>() -> &'static str {
690690
intrinsics::type_name::<T>()
691691
}
692692

693-
/// Returns the name of the type of the pointed-to value as a string slice.
693+
/// Returns the type name of the pointed-to value as a string slice.
694+
///
694695
/// This is the same as `type_name::<T>()`, but can be used where the type of a
695696
/// variable is not easily available.
696697
///
697698
/// # Note
698699
///
699-
/// This is intended for diagnostic use. The exact contents and format of the
700-
/// string are not specified, other than being a best-effort description of the
701-
/// type. For example, `type_name_of_val::<Option<String>>(None)` could return
702-
/// `"Option<String>"` or `"std::option::Option<std::string::String>"`, but not
703-
/// `"foobar"`. In addition, the output may change between versions of the
704-
/// compiler.
705-
///
706-
/// This function does not resolve trait objects,
707-
/// meaning that `type_name_of_val(&7u32 as &dyn Debug)`
708-
/// may return `"dyn Debug"`, but not `"u32"`.
700+
/// Like [`type_name`], this is intended for diagnostic use and the exact output is not
701+
/// guaranteed. It provides a best-effort description, but the output may change between
702+
/// versions of the compiler.
709703
///
710-
/// The type name should not be considered a unique identifier of a type;
711-
/// multiple types may share the same type name.
704+
/// In short: use this for debugging, avoid using the output to affect program behavior. More
705+
/// information is available at [`type_name`].
712706
///
713-
/// The current implementation uses the same infrastructure as compiler
714-
/// diagnostics and debuginfo, but this is not guaranteed.
707+
/// Additionally, this function does not resolve trait objects. This means that
708+
/// `type_name_of_val(&7u32 as &dyn Debug)` may return `"dyn Debug"`, but wil not return `"u32"`
709+
/// at this time.
715710
///
716711
/// # Examples
717712
///
718713
/// Prints the default integer and float types.
719714
///
720715
/// ```rust
721-
/// #![feature(type_name_of_val)]
722716
/// use std::any::type_name_of_val;
723717
///
724-
/// let x = 1;
725-
/// println!("{}", type_name_of_val(&x));
726-
/// let y = 1.0;
727-
/// println!("{}", type_name_of_val(&y));
718+
/// let s = "foo";
719+
/// let x: i32 = 1;
720+
/// let y: f32 = 1.0;
721+
///
722+
/// assert!(type_name_of_val(&s).contains("str"));
723+
/// assert!(type_name_of_val(&x).contains("i32"));
724+
/// assert!(type_name_of_val(&y).contains("f32"));
728725
/// ```
729726
#[must_use]
730-
#[unstable(feature = "type_name_of_val", issue = "66359")]
727+
#[stable(feature = "type_name_of_val", since = "CURRENT_RUSTC_VERSION")]
731728
#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
732729
pub const fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str {
733730
type_name::<T>()

tests/ui/hygiene/cross-crate-define-and-use.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// check-pass
77
// aux-build:use_by_macro.rs
88

9-
#![feature(type_name_of_val)]
109
extern crate use_by_macro;
1110

1211
use use_by_macro::*;

0 commit comments

Comments
 (0)