Skip to content

Commit 0a12431

Browse files
authored
Rollup merge of #85963 - m-ou-se:constructor-type-name, r=yaahc
Show `::{{constructor}}` in std::any::type_name(). Fix #84666 Before: ``` [src/main.rs:6] type_name::<T>() = "playground::Velocity" [src/main.rs:6] type_name::<T>() = "playground::Velocity" ``` After: ``` [src/main.rs:6] type_name::<T>() = "scratchpad::Velocity::{{constructor}}" [src/main.rs:6] type_name::<T>() = "scratchpad::Velocity" ``` cc ``@scottmcm``
2 parents 99fc56b + e3b19e5 commit 0a12431

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

compiler/rustc_mir/src/interpret/intrinsics/type_name.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_hir::def_id::CrateNum;
2-
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
2+
use rustc_hir::definitions::DisambiguatedDefPathData;
33
use rustc_middle::mir::interpret::Allocation;
44
use rustc_middle::ty::{
55
self,
@@ -127,11 +127,6 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
127127
) -> Result<Self::Path, Self::Error> {
128128
self = print_prefix(self)?;
129129

130-
// Skip `::{{constructor}}` on tuple/unit structs.
131-
if disambiguated_data.data == DefPathData::Ctor {
132-
return Ok(self);
133-
}
134-
135130
write!(self.path, "::{}", disambiguated_data.data).unwrap();
136131

137132
Ok(self)

library/core/tests/any.rs

+13
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,16 @@ fn any_unsized() {
114114
fn is_any<T: Any + ?Sized>() {}
115115
is_any::<[i32]>();
116116
}
117+
118+
#[test]
119+
fn distinct_type_names() {
120+
// https://github.com/rust-lang/rust/issues/84666
121+
122+
struct Velocity(f32, f32);
123+
124+
fn type_name_of_val<T>(_: T) -> &'static str {
125+
type_name::<T>()
126+
}
127+
128+
assert_ne!(type_name_of_val(Velocity), type_name_of_val(Velocity(0.0, -9.8)),);
129+
}

0 commit comments

Comments
 (0)