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

3 files changed

Lines changed: 83 additions & 6 deletions

File tree

‎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
fnprint_region(&mutself,_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
fnprint_type(&mutself,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
fnprint_region(&mutself,_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
fnprint_type(&mutself,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-
structFoo<T>{
11-
x:T,
11+
structFoo<T>(T);
12+
13+
structBar<'a>(&'au32);
14+
15+
structBaz<'a,T>(&'aT);
16+
17+
traitTrL<'a>{}
18+
traitTrLA<'a>{
19+
typeA;
20+
}
21+
traitTrLT<'a,T>{}
22+
traitTrLTA<'a,T>{
23+
typeA;
24+
}
25+
26+
macro_rules! t {
27+
($ty:ty, $str:literal) => {
28+
assert_eq!(type_name::<$ty>(), $str);
29+
}
1230
}
1331

1432
pubfnmain(){
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!(&'staticstr,"&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!(*constbool,"*const bool");
61+
t!(*mutu64,"*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+
structS<'a,T>(&'aT);
84+
impl<'a,T:Clone>S<'a,T>{
85+
fntest(){
86+
t!(Cow<'a,T>,"alloc::borrow::Cow<u32>");
87+
}
88+
}
89+
S::<u32>::test();
1790
}

0 commit comments

Comments
 (0)