Skip to content

Commit f0c2bdf

Browse files
committed
Auto merge of #61998 - eddyb:type-name-params, r=oli-obk
rustc_mir: support type parameters by printing them as `_`. Fixes #61894. r? @oli-obk
2 parents c1a5edd + 3606003 commit f0c2bdf

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

src/librustc_mir/interpret/intrinsics/type_name.rs

+26-21
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
3131
Ok(self)
3232
}
3333

34-
fn print_type(self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
34+
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
3535
match ty.sty {
36-
// types without identity
36+
// Types without identity.
3737
| ty::Bool
3838
| ty::Char
3939
| ty::Int(_)
@@ -48,28 +48,33 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
4848
| ty::Never
4949
| ty::Tuple(_)
5050
| ty::Dynamic(_, _)
51-
| ty::Adt(..)
52-
| ty::Foreign(_)
53-
// should be unreachable, but there's no hurt in printing it (and better than ICEing)
54-
| ty::Error
5551
=> self.pretty_print_type(ty),
56-
| ty::Infer(_)
57-
| ty::Bound(_, _)
52+
53+
// Placeholders (all printed as `_` to uniformize them).
5854
| ty::Param(_)
55+
| ty::Bound(..)
5956
| ty::Placeholder(_)
60-
| ty::Projection(_)
61-
| ty::UnnormalizedProjection(_)
62-
| ty::GeneratorWitness(_)
63-
=> bug!(
64-
"{:#?} in `type_name` should not happen because we are always monomorphized",
65-
ty,
66-
),
67-
// types with identity (print the module path instead)
68-
| ty::FnDef(did, substs)
69-
| ty::Opaque(did, substs)
70-
=> self.print_def_path(did, substs),
71-
ty::Closure(did, substs) => self.print_def_path(did, substs.substs),
72-
ty::Generator(did, substs, _) => self.print_def_path(did, substs.substs),
57+
| ty::Infer(_)
58+
| ty::Error
59+
=> {
60+
write!(self, "_")?;
61+
Ok(self)
62+
}
63+
64+
// Types with identity (print the module path).
65+
| ty::Adt(&ty::AdtDef { did: def_id, .. }, substs)
66+
| ty::FnDef(def_id, substs)
67+
| ty::Opaque(def_id, substs)
68+
| ty::Projection(ty::ProjectionTy { item_def_id: def_id, substs })
69+
| ty::UnnormalizedProjection(ty::ProjectionTy { item_def_id: def_id, substs })
70+
| ty::Closure(def_id, ty::ClosureSubsts { substs })
71+
| ty::Generator(def_id, ty::GeneratorSubsts { substs }, _)
72+
=> self.print_def_path(def_id, substs),
73+
ty::Foreign(def_id) => self.print_def_path(def_id, &[]),
74+
75+
ty::GeneratorWitness(_) => {
76+
bug!("type_name: unexpected `GeneratorWitness`")
77+
}
7378
}
7479
}
7580

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(core_intrinsics)]
2+
3+
use std::intrinsics::type_name;
4+
5+
struct Bar<M>(M);
6+
7+
impl<M> Bar<M> {
8+
fn foo(&self) -> &'static str {
9+
fn f() {}
10+
fn type_name_of<T>(_: T) -> &'static str {
11+
unsafe { type_name::<T>() }
12+
}
13+
type_name_of(f)
14+
}
15+
}
16+
17+
fn main() {
18+
assert_eq!(Bar(()).foo(), "issue_61894::Bar<_>::foo::f");
19+
}

0 commit comments

Comments
 (0)