Skip to content

Commit e2843b7

Browse files
authored
Re-add type_name.rs changes
1 parent 99f66f4 commit e2843b7

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/librustc_mir/interpret/intrinsics/type_name.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,23 @@ impl Write for AbsolutePathPrinter<'_, '_> {
213213
/// Produces an absolute path representation of the given type. See also the documentation on
214214
/// `std::any::type_name`
215215
pub fn type_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> &'tcx ty::Const<'tcx> {
216-
let path = AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path;
217-
let len = path.len();
218-
let alloc = Allocation::from_byte_aligned_bytes(path.into_bytes());
219-
let alloc = tcx.intern_const_alloc(alloc);
216+
let alloc = alloc_type_name(tcx, ty);
220217
tcx.mk_const(ty::Const {
221218
val: ConstValue::Slice {
222219
data: alloc,
223220
start: 0,
224-
end: len,
221+
end: alloc.bytes.len(),
225222
},
226223
ty: tcx.mk_static_str(),
227224
})
228225
}
226+
227+
/// Directly returns an `Allocation` containing an absolute path representation of the given type.
228+
pub(super) fn alloc_type_name<'a, 'tcx>(
229+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
230+
ty: Ty<'tcx>
231+
) -> &'tcx Allocation {
232+
let path = AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path;
233+
let alloc = Allocation::from_byte_aligned_bytes(path.into_bytes());
234+
tcx.intern_const_alloc(alloc)
235+
}

0 commit comments

Comments
 (0)