Skip to content

Commit 6fa369d

Browse files
authored
Rollup merge of rust-lang#118234 - tgross35:type_name_of_value, r=dtolnay
Stabilize `type_name_of_val` Make the following API stable: ```rust // in core::any pub fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str ``` This is a convenience method to get the type name of a value, as opposed to `type_name` that takes a type as a generic. Const stability is not added because this relies on `type_name` which is also not const. That has a blocking issue rust-lang#97156. Wording was also changed to direct most of the details to `type_name` so we don't have as much duplicated documentation. Fixes tracking issue rust-lang#66359. There were two main concerns in the tracking issue: 1. Naming: `type_name_of` and `type_name_of_val` seem like the only mentioned options. Differences in opinion here come from `std::mem::{size_of, align_of, size_of_val, align_of_val}`. This PR leaves the name as `type_name_of_val`, but I can change if desired since it is pretty verbose. 2. What this displays for `&dyn`: I don't think that having `type_name_of_val` function resolve those is worth the headache it would be, see rust-lang#66359 (comment) for some workarounds. I also amended the docs wording to leave it open-ended, in case we have means to change that behavior in the future. `@rustbot` label -T-libs +T-libs-api +needs-fcp r? libs-api
2 parents 9c0083c + b225aab commit 6fa369d

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
@@ -695,44 +695,41 @@ pub const fn type_name<T: ?Sized>() -> &'static str {
695695
intrinsics::type_name::<T>()
696696
}
697697

698-
/// Returns the name of the type of the pointed-to value as a string slice.
698+
/// Returns the type name of the pointed-to value as a string slice.
699+
///
699700
/// This is the same as `type_name::<T>()`, but can be used where the type of a
700701
/// variable is not easily available.
701702
///
702703
/// # Note
703704
///
704-
/// This is intended for diagnostic use. The exact contents and format of the
705-
/// string are not specified, other than being a best-effort description of the
706-
/// type. For example, `type_name_of_val::<Option<String>>(None)` could return
707-
/// `"Option<String>"` or `"std::option::Option<std::string::String>"`, but not
708-
/// `"foobar"`. In addition, the output may change between versions of the
709-
/// compiler.
710-
///
711-
/// This function does not resolve trait objects,
712-
/// meaning that `type_name_of_val(&7u32 as &dyn Debug)`
713-
/// may return `"dyn Debug"`, but not `"u32"`.
705+
/// Like [`type_name`], this is intended for diagnostic use and the exact output is not
706+
/// guaranteed. It provides a best-effort description, but the output may change between
707+
/// versions of the compiler.
714708
///
715-
/// The type name should not be considered a unique identifier of a type;
716-
/// multiple types may share the same type name.
709+
/// In short: use this for debugging, avoid using the output to affect program behavior. More
710+
/// information is available at [`type_name`].
717711
///
718-
/// The current implementation uses the same infrastructure as compiler
719-
/// diagnostics and debuginfo, but this is not guaranteed.
712+
/// Additionally, this function does not resolve trait objects. This means that
713+
/// `type_name_of_val(&7u32 as &dyn Debug)` may return `"dyn Debug"`, but will not return `"u32"`
714+
/// at this time.
720715
///
721716
/// # Examples
722717
///
723718
/// Prints the default integer and float types.
724719
///
725720
/// ```rust
726-
/// #![feature(type_name_of_val)]
727721
/// use std::any::type_name_of_val;
728722
///
729-
/// let x = 1;
730-
/// println!("{}", type_name_of_val(&x));
731-
/// let y = 1.0;
732-
/// println!("{}", type_name_of_val(&y));
723+
/// let s = "foo";
724+
/// let x: i32 = 1;
725+
/// let y: f32 = 1.0;
726+
///
727+
/// assert!(type_name_of_val(&s).contains("str"));
728+
/// assert!(type_name_of_val(&x).contains("i32"));
729+
/// assert!(type_name_of_val(&y).contains("f32"));
733730
/// ```
734731
#[must_use]
735-
#[unstable(feature = "type_name_of_val", issue = "66359")]
732+
#[stable(feature = "type_name_of_val", since = "CURRENT_RUSTC_VERSION")]
736733
#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
737734
pub const fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str {
738735
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)