From f53ac185acbf85a781f3601695fb83359290f890 Mon Sep 17 00:00:00 2001 From: TankhouseAle <51239665+TankhouseAle@users.noreply.github.com> Date: Mon, 3 Jun 2019 14:36:22 -0400 Subject: [PATCH 1/4] Re-add test file --- src/test/run-pass/ctfe/const-fn-type-name.rs | 37 ++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/test/run-pass/ctfe/const-fn-type-name.rs diff --git a/src/test/run-pass/ctfe/const-fn-type-name.rs b/src/test/run-pass/ctfe/const-fn-type-name.rs new file mode 100644 index 00000000000..2ee6415aa68 --- /dev/null +++ b/src/test/run-pass/ctfe/const-fn-type-name.rs @@ -0,0 +1,37 @@ +// run-pass + +#![feature(core_intrinsics)] +#![feature(const_fn)] +#![allow(dead_code)] + +const fn type_name_wrapper(_: &T) -> &'static str { + unsafe { core::intrinsics::type_name::() } +} + +struct Struct { + a: TA, + b: TB, + c: TC, +} + +type StructInstantiation = Struct; + +const CONST_STRUCT: StructInstantiation = StructInstantiation { + a: 12, + b: 13.7, + c: false, +}; + +const CONST_STRUCT_NAME: &'static str = type_name_wrapper(&CONST_STRUCT); + +fn main() { + let non_const_struct = StructInstantiation { + a: 87, + b: 65.99, + c: true, + }; + + let non_const_struct_name = type_name_wrapper(&non_const_struct); + + assert_eq!(CONST_STRUCT_NAME, non_const_struct_name); +} From 99f66f4045d405e306b6459b6807372a88279875 Mon Sep 17 00:00:00 2001 From: TankhouseAle <51239665+TankhouseAle@users.noreply.github.com> Date: Mon, 3 Jun 2019 14:38:39 -0400 Subject: [PATCH 2/4] Re-add intrinsics.rs changes --- src/librustc_mir/interpret/intrinsics.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/librustc_mir/interpret/intrinsics.rs b/src/librustc_mir/interpret/intrinsics.rs index 34e9b4972a1..dd882bf7a4d 100644 --- a/src/librustc_mir/interpret/intrinsics.rs +++ b/src/librustc_mir/interpret/intrinsics.rs @@ -78,6 +78,16 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M> let id_val = Scalar::from_uint(type_id, dest.layout.size); self.write_scalar(id_val, dest)?; } + + "type_name" => { + let alloc = alloc_type_name(self.tcx.tcx, substs.type_at(0)); + let name_id = self.tcx.alloc_map.lock().create_memory_alloc(alloc); + let id_ptr = self.memory.tag_static_base_pointer(name_id.into()); + let alloc_len = alloc.bytes.len() as u64; + let name_val = Immediate::new_slice(Scalar::Ptr(id_ptr), alloc_len, self); + self.write_immediate(name_val, dest)?; + } + | "ctpop" | "cttz" | "cttz_nonzero" From e2843b792a605f8166515b9f8b9e47794101980d Mon Sep 17 00:00:00 2001 From: TankhouseAle <51239665+TankhouseAle@users.noreply.github.com> Date: Mon, 3 Jun 2019 14:40:08 -0400 Subject: [PATCH 3/4] Re-add type_name.rs changes --- .../interpret/intrinsics/type_name.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/librustc_mir/interpret/intrinsics/type_name.rs b/src/librustc_mir/interpret/intrinsics/type_name.rs index 456fc70fc0d..1270b35ebb9 100644 --- a/src/librustc_mir/interpret/intrinsics/type_name.rs +++ b/src/librustc_mir/interpret/intrinsics/type_name.rs @@ -213,16 +213,23 @@ impl Write for AbsolutePathPrinter<'_, '_> { /// 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); + 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<'a, 'tcx>( + tcx: TyCtxt<'a, '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) +} From 70aeb2233fb43a4bb87795e97ea55032ce1164be Mon Sep 17 00:00:00 2001 From: TankhouseAle <51239665+TankhouseAle@users.noreply.github.com> Date: Mon, 3 Jun 2019 14:55:36 -0400 Subject: [PATCH 4/4] Re-add needed Immediate dependency --- src/librustc_mir/interpret/intrinsics.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_mir/interpret/intrinsics.rs b/src/librustc_mir/interpret/intrinsics.rs index dd882bf7a4d..61375d3bab5 100644 --- a/src/librustc_mir/interpret/intrinsics.rs +++ b/src/librustc_mir/interpret/intrinsics.rs @@ -11,7 +11,7 @@ use rustc::mir::interpret::{ }; use super::{ - Machine, PlaceTy, OpTy, InterpretCx, + Machine, PlaceTy, OpTy, InterpretCx, Immediate, }; mod type_name;