Skip to content

Commit e24df27

Browse files
Format dyn Trait better in type_name intrinsic
1 parent e70cbef commit e24df27

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

compiler/rustc_const_eval/src/util/type_name.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,10 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
7373
}
7474

7575
fn print_dyn_existential(
76-
mut self,
76+
self,
7777
predicates: &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
7878
) -> Result<Self::DynExistential, Self::Error> {
79-
let mut first = true;
80-
for p in predicates {
81-
if !first {
82-
write!(self, "+")?;
83-
}
84-
first = false;
85-
self = p.print(self)?;
86-
}
87-
Ok(self)
79+
self.pretty_print_dyn_existential(predicates)
8880
}
8981

9082
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {

compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ pub trait PrettyPrinter<'tcx>:
11371137
//
11381138
// To avoid causing instabilities in compiletest
11391139
// output, sort the auto-traits alphabetically.
1140-
auto_traits.sort_by_cached_key(|did| self.tcx().def_path_str(*did));
1140+
auto_traits.sort_by_cached_key(|did| with_no_trimmed_paths!(self.tcx().def_path_str(*did)));
11411141

11421142
for def_id in auto_traits {
11431143
if !first {

library/core/tests/any.rs

+18
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ fn distinct_type_names() {
131131
assert_ne!(type_name_of_val(Velocity), type_name_of_val(Velocity(0.0, -9.8)),);
132132
}
133133

134+
#[cfg(not(bootstrap))]
135+
#[test]
136+
fn dyn_type_name() {
137+
trait Foo {
138+
type Bar;
139+
}
140+
141+
assert_eq!(
142+
"dyn core::ops::function::Fn(i32, i32) -> i32",
143+
std::any::type_name::<dyn Fn(i32, i32) -> i32>()
144+
);
145+
assert_eq!(
146+
"dyn coretests::any::dyn_type_name::Foo<Bar = i32> \
147+
+ core::marker::Send + core::marker::Sync",
148+
std::any::type_name::<dyn Foo<Bar = i32> + Send + Sync>()
149+
);
150+
}
151+
134152
// Test the `Provider` API.
135153

136154
struct SomeConcreteType {

src/test/ui/type/issue-94187-verbose-type-name.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,5 @@ fn main() {
1212
struct Wrapper<const VALUE: usize>;
1313
assert_eq!(type_name::<Wrapper<0>>(), "issue_94187_verbose_type_name::main::Wrapper<0>");
1414

15-
assert_eq!(
16-
type_name::<dyn Fn(u32) -> u32>(),
17-
"dyn core::ops::function::Fn<(u32,)>+Output = u32"
18-
);
15+
assert_eq!(type_name::<dyn Fn(u32) -> u32>(), "dyn core::ops::function::Fn(u32) -> u32");
1916
}

0 commit comments

Comments
 (0)