Skip to content

Commit

Permalink
Rollup merge of #85963 - m-ou-se:constructor-type-name, r=yaahc
Browse files Browse the repository at this point in the history
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``
  • Loading branch information
JohnTitor committed Jun 4, 2021
2 parents 99fc56b + e3b19e5 commit 0a12431
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
7 changes: 1 addition & 6 deletions compiler/rustc_mir/src/interpret/intrinsics/type_name.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_hir::def_id::CrateNum;
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
use rustc_hir::definitions::DisambiguatedDefPathData;
use rustc_middle::mir::interpret::Allocation;
use rustc_middle::ty::{
self,
Expand Down Expand Up @@ -127,11 +127,6 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
) -> Result<Self::Path, Self::Error> {
self = print_prefix(self)?;

// Skip `::{{constructor}}` on tuple/unit structs.
if disambiguated_data.data == DefPathData::Ctor {
return Ok(self);
}

write!(self.path, "::{}", disambiguated_data.data).unwrap();

Ok(self)
Expand Down
13 changes: 13 additions & 0 deletions library/core/tests/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,16 @@ fn any_unsized() {
fn is_any<T: Any + ?Sized>() {}
is_any::<[i32]>();
}

#[test]
fn distinct_type_names() {
// https://github.com/rust-lang/rust/issues/84666

struct Velocity(f32, f32);

fn type_name_of_val<T>(_: T) -> &'static str {
type_name::<T>()
}

assert_ne!(type_name_of_val(Velocity), type_name_of_val(Velocity(0.0, -9.8)),);
}

0 comments on commit 0a12431

Please sign in to comment.