X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibrustc_mir%2Finterpret%2Fintrinsics%2Ftype_name.rs;h=a44a20adbca0e6b2700533361507fa24b5f152a7;hb=fff08cb04389497d254fb40948674cbbee402908;hp=48b02d8e11b6057dc1f91ab9842e8a494a650b2b;hpb=7d3a0dd2fb3e7d5ab08f4e95df91decbf1b9b74d;p=rust.git diff --git a/src/librustc_mir/interpret/intrinsics/type_name.rs b/src/librustc_mir/interpret/intrinsics/type_name.rs index 48b02d8e11b..a44a20adbca 100644 --- a/src/librustc_mir/interpret/intrinsics/type_name.rs +++ b/src/librustc_mir/interpret/intrinsics/type_name.rs @@ -9,12 +9,12 @@ 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; @@ -23,7 +23,7 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { type DynExistential = Self; type Const = Self; - fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> { + fn tcx(&self) -> TyCtxt<'tcx, 'tcx> { self.tcx } @@ -167,7 +167,7 @@ fn path_generic_args( } } } -impl PrettyPrinter<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { +impl PrettyPrinter<'tcx, 'tcx> for AbsolutePathPrinter<'tcx> { fn region_should_not_be_omitted( &self, _region: ty::Region<'_>, @@ -204,7 +204,7 @@ fn generic_delimiters( } } -impl Write for AbsolutePathPrinter<'_, '_> { +impl Write for AbsolutePathPrinter<'_> { fn write_str(&mut self, s: &str) -> std::fmt::Result { Ok(self.path.push_str(s)) } @@ -212,17 +212,21 @@ fn write_str(&mut self, s: &str) -> std::fmt::Result { /// 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) +}