Skip to content

Commit 2b8a395

Browse files
committed
Allow static regions in type_name.
Fixes #146249.
1 parent 55b9b4d commit 2b8a395

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

compiler/rustc_const_eval/src/util/type_name.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ impl<'tcx> Printer<'tcx> for TypeNamePrinter<'tcx> {
1717
self.tcx
1818
}
1919

20-
fn print_region(&mut self, _region: ty::Region<'_>) -> Result<(), PrintError> {
20+
fn print_region(&mut self, region: ty::Region<'_>) -> Result<(), PrintError> {
2121
// FIXME: most regions have been erased by the time this code runs.
22-
// Just printing `'_` is a bit hacky but gives mostly good results, and
23-
// doing better is difficult. See `should_print_optional_region`.
24-
write!(self, "'_")
22+
// Printing `'_` for non-static regions is a bit hacky but gives mostly
23+
// good results, and doing better is difficult. See
24+
// `should_print_optional_region`.
25+
let s = match region.kind() {
26+
ty::ReStatic => "'static",
27+
_ => "'_",
28+
};
29+
write!(self, "{}", s)
2530
}
2631

2732
fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
@@ -168,10 +173,11 @@ impl<'tcx> PrettyPrinter<'tcx> for TypeNamePrinter<'tcx> {
168173
// Bound regions are always printed (as `'_`), which gives some idea that they are special,
169174
// even though the `for` is omitted by the pretty printer.
170175
// E.g. `for<'a, 'b> fn(&'a u32, &'b u32)` is printed as "fn(&'_ u32, &'_ u32)".
176+
let kind = region.kind();
171177
match region.kind() {
172178
ty::ReErased | ty::ReEarlyParam(_) => false,
173-
ty::ReBound(..) => true,
174-
_ => unreachable!(),
179+
ty::ReBound(..) | ty::ReStatic => true,
180+
_ => panic!("type_name unhandled region: {kind:?}"),
175181
}
176182
}
177183

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,19 @@ pub fn main() {
107107
}
108108
let a = Wrap(&()).get();
109109
v!(a, "type_name_basic::main::Wrap<&()>::get::Info");
110+
111+
struct Issue146249<T>(T);
112+
impl Issue146249<Box<dyn FnOnce()>> {
113+
pub fn bar(&self) {
114+
let f = || {};
115+
v!(
116+
f,
117+
"type_name_basic::main::Issue146249<\
118+
alloc::boxed::Box<(dyn core::ops::function::FnOnce() + 'static)>\
119+
>::bar::{{closure}}"
120+
);
121+
}
122+
}
123+
let v: Issue146249<Box<dyn FnOnce()>> = Issue146249(Box::new(|| {}));
124+
v.bar();
110125
}

0 commit comments

Comments
 (0)