From 95b3b61b8c2ecf0b5073d5779cea2825c726bf4b Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 21 Aug 2025 14:09:53 +1000 Subject: [PATCH] Handle `ReEarlyParam` in `type_name`. Fixes #145696. --- .../rustc_const_eval/src/util/type_name.rs | 6 +++--- tests/ui/type/type-name-basic.rs | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_const_eval/src/util/type_name.rs b/compiler/rustc_const_eval/src/util/type_name.rs index 13cc607135a02..10bfd5aad9210 100644 --- a/compiler/rustc_const_eval/src/util/type_name.rs +++ b/compiler/rustc_const_eval/src/util/type_name.rs @@ -135,12 +135,12 @@ impl<'tcx> Printer<'tcx> for TypeNamePrinter<'tcx> { } impl<'tcx> PrettyPrinter<'tcx> for TypeNamePrinter<'tcx> { - fn should_print_optional_region(&self, _region: ty::Region<'_>) -> bool { + fn should_print_optional_region(&self, region: ty::Region<'_>) -> bool { // Bound regions are always printed (as `'_`), which gives some idea that they are special, // even though the `for` is omitted by the pretty printer. // E.g. `for<'a, 'b> fn(&'a u32, &'b u32)` is printed as "fn(&'_ u32, &'_ u32)". - match _region.kind() { - ty::ReErased => false, + match region.kind() { + ty::ReErased | ty::ReEarlyParam(_) => false, ty::ReBound(..) => true, _ => unreachable!(), } diff --git a/tests/ui/type/type-name-basic.rs b/tests/ui/type/type-name-basic.rs index e1310e1f36599..343bcae175a04 100644 --- a/tests/ui/type/type-name-basic.rs +++ b/tests/ui/type/type-name-basic.rs @@ -5,7 +5,7 @@ #![allow(dead_code)] -use std::any::type_name; +use std::any::{Any, type_name, type_name_of_val}; use std::borrow::Cow; struct Foo(T); @@ -29,6 +29,12 @@ macro_rules! t { } } +macro_rules! v { + ($v:expr, $str:literal) => { + assert_eq!(type_name_of_val(&$v), $str); + } +} + pub fn main() { t!(bool, "bool"); t!(char, "char"); @@ -91,4 +97,14 @@ pub fn main() { } } S::::test(); + + struct Wrap(T); + impl Wrap<&()> { + fn get(&self) -> impl Any { + struct Info; + Info + } + } + let a = Wrap(&()).get(); + v!(a, "type_name_basic::main::Wrap<&()>::get::Info"); }