Skip to content

Commit 33f1862

Browse files
authored
Rollup merge of #145033 - nnethercote:fix-144994, r=fmease
Reimplement `print_region` in `type_name.rs`. Broken by #144776; this is reachable after all. Fixes #144994. The commit also adds a lot more cases to the `type-name-basic.rs`, because it's currently very anaemic. This includes some cases where region omission does very badly; these are marked with FIXME. r? `@fmease`
2 parents 3af6bb1 + 8074e67 commit 33f1862

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed

compiler/rustc_const_eval/src/util/type_name.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
1818
}
1919

2020
fn print_region(&mut self, _region: ty::Region<'_>) -> Result<(), PrintError> {
21-
unreachable!(); // because `<Self As PrettyPrinter>::should_print_region` returns false
21+
// This is reachable (via `pretty_print_dyn_existential`) even though
22+
// `<Self As PrettyPrinter>::should_print_region` returns false. See #144994.
23+
Ok(())
2224
}
2325

2426
fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {

compiler/rustc_symbol_mangling/src/legacy.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ impl<'tcx> Printer<'tcx> for SymbolPrinter<'tcx> {
234234
}
235235

236236
fn print_region(&mut self, _region: ty::Region<'_>) -> Result<(), PrintError> {
237-
unreachable!(); // because `<Self As PrettyPrinter>::should_print_region` returns false
237+
// This might be reachable (via `pretty_print_dyn_existential`) even though
238+
// `<Self As PrettyPrinter>::should_print_region` returns false. See #144994.
239+
Ok(())
238240
}
239241

240242
fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {

tests/ui/type/type-name-basic.rs

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,85 @@
66
#![allow(dead_code)]
77

88
use std::any::type_name;
9+
use std::borrow::Cow;
910

10-
struct Foo<T> {
11-
x: T,
11+
struct Foo<T>(T);
12+
13+
struct Bar<'a>(&'a u32);
14+
15+
struct Baz<'a, T>(&'a T);
16+
17+
trait TrL<'a> {}
18+
trait TrLA<'a> {
19+
type A;
20+
}
21+
trait TrLT<'a, T> {}
22+
trait TrLTA<'a, T> {
23+
type A;
24+
}
25+
26+
macro_rules! t {
27+
($ty:ty, $str:literal) => {
28+
assert_eq!(type_name::<$ty>(), $str);
29+
}
1230
}
1331

1432
pub fn main() {
15-
assert_eq!(type_name::<isize>(), "isize");
16-
assert_eq!(type_name::<Foo<usize>>(), "type_name_basic::Foo<usize>");
33+
t!(bool, "bool");
34+
t!(char, "char");
35+
36+
t!(u8, "u8");
37+
t!(u16, "u16");
38+
t!(u32, "u32");
39+
t!(u64, "u64");
40+
t!(u128, "u128");
41+
t!(usize, "usize");
42+
43+
t!(i8, "i8");
44+
t!(i16, "i16");
45+
t!(i32, "i32");
46+
t!(i64, "i64");
47+
t!(i128, "i128");
48+
t!(isize, "isize");
49+
50+
t!(String, "alloc::string::String");
51+
t!(str, "str");
52+
t!(&str, "&str");
53+
t!(&'static str, "&str");
54+
55+
t!((u16, u32, u64), "(u16, u32, u64)");
56+
t!([usize; 4], "[usize; 4]");
57+
t!([usize], "[usize]");
58+
t!(&[usize], "&[usize]");
59+
60+
t!(*const bool, "*const bool");
61+
t!(*mut u64, "*mut u64");
62+
63+
t!(Vec<Vec<u32>>, "alloc::vec::Vec<alloc::vec::Vec<u32>>");
64+
t!(Foo<usize>, "type_name_basic::Foo<usize>");
65+
t!(Bar<'static>, "type_name_basic::Bar");
66+
t!(Baz<'static, u32>, "type_name_basic::Baz<u32>");
67+
68+
// FIXME: lifetime omission means these all print badly.
69+
t!(dyn TrL<'static>, "dyn type_name_basic::TrL<>");
70+
t!(dyn TrLA<'static, A = u32>, "dyn type_name_basic::TrLA<, A = u32>");
71+
t!(
72+
dyn TrLT<'static, Cow<'static, ()>>,
73+
"dyn type_name_basic::TrLT<, alloc::borrow::Cow<()>>"
74+
);
75+
t!(
76+
dyn TrLTA<'static, u32, A = Cow<'static, ()>>,
77+
"dyn type_name_basic::TrLTA<, u32, A = alloc::borrow::Cow<()>>"
78+
);
79+
80+
t!(fn(i32) -> i32, "fn(i32) -> i32");
81+
t!(dyn for<'a> Fn(&'a u32), "dyn core::ops::function::Fn(&u32)");
82+
83+
struct S<'a, T>(&'a T);
84+
impl<'a, T: Clone> S<'a, T> {
85+
fn test() {
86+
t!(Cow<'a, T>, "alloc::borrow::Cow<u32>");
87+
}
88+
}
89+
S::<u32>::test();
1790
}

0 commit comments

Comments
 (0)