use std::fmt::Write;
use rustc::mir::interpret::{Allocation, ConstValue};
-struct AbsolutePathPrinter<'a, 'tcx> {
- tcx: TyCtxt<'a, 'tcx, 'tcx>,
+struct AbsolutePathPrinter<'tcx> {
+ tcx: TyCtxt<'tcx, 'tcx>,
path: String,
}
-impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> {
+impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'tcx> {
type Error = std::fmt::Error;
type Path = Self;
type DynExistential = Self;
type Const = Self;
- fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
+ fn tcx(&self) -> TyCtxt<'tcx, 'tcx> {
self.tcx
}
}
}
}
-impl PrettyPrinter<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> {
+impl PrettyPrinter<'tcx, 'tcx> for AbsolutePathPrinter<'tcx> {
fn region_should_not_be_omitted(
&self,
_region: ty::Region<'_>,
}
}
-impl Write for AbsolutePathPrinter<'_, '_> {
+impl Write for AbsolutePathPrinter<'_> {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
Ok(self.path.push_str(s))
}
/// Produces an absolute path representation of the given type. See also the documentation on
/// `std::any::type_name`
-pub fn type_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> &'tcx ty::Const<'tcx> {
- let path = AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path;
- let len = path.len();
- let alloc = Allocation::from_byte_aligned_bytes(path.into_bytes());
- let alloc = tcx.intern_const_alloc(alloc);
+pub fn type_name<'tcx>(tcx: TyCtxt<'tcx, 'tcx>, ty: Ty<'tcx>) -> &'tcx ty::Const<'tcx> {
+ let alloc = alloc_type_name(tcx, ty);
tcx.mk_const(ty::Const {
val: ConstValue::Slice {
data: alloc,
start: 0,
- end: len,
+ end: alloc.bytes.len(),
},
ty: tcx.mk_static_str(),
})
}
+
+/// Directly returns an `Allocation` containing an absolute path representation of the given type.
+pub(super) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx, 'tcx>, ty: Ty<'tcx>) -> &'tcx Allocation {
+ let path = AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path;
+ let alloc = Allocation::from_byte_aligned_bytes(path.into_bytes());
+ tcx.intern_const_alloc(alloc)
+}