Skip to content

Commit 743c0b5

Browse files
Use the official implementation of type_name.
This makes the `type_name` intrinsic call the implementation in `librustc_mir::interpret::type_name` instead of `Ty::to_string()`.
1 parent e1a0f66 commit 743c0b5

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/intrinsic.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc::mir;
2-
use rustc::mir::interpret::{EvalResult, PointerArithmetic};
2+
use rustc::mir::interpret::{ConstValue, EvalResult, PointerArithmetic};
33
use rustc::ty::layout::{self, LayoutOf, Size};
44
use rustc::ty;
55

@@ -400,10 +400,17 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
400400

401401
"type_name" => {
402402
let ty = substs.type_at(0);
403-
let ty_name = ty.to_string();
404-
let ptr = this.memory_mut().allocate_static_bytes(ty_name.as_bytes(), MiriMemoryKind::Static.into());
405-
let value = Immediate::new_slice(Scalar::Ptr(ptr), ty_name.len() as u64, this);
406-
this.write_immediate(value, dest)?;
403+
let ty_name = rustc_mir::interpret::type_name(*tcx, ty);
404+
405+
if let ConstValue::Slice { data, start, end } = ty_name.val {
406+
let ptr = this.memory_mut().allocate_with(data.clone(), MiriMemoryKind::Static.into());
407+
let ptr = ptr.offset(Size::from_bytes(start as u64), this)?;
408+
let len = end - start;
409+
let value = Immediate::new_slice(Scalar::Ptr(ptr), len as u64, this);
410+
this.write_immediate(value, dest)?;
411+
} else {
412+
unreachable!("Result of `type_name` must be a `ConstValue::Slice`");
413+
}
407414
}
408415

409416
"unchecked_div" => {

tests/run-pass/intrinsics.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#![feature(core_intrinsics)]
2+
3+
use std::intrinsics::type_name;
14
use std::mem::{size_of, size_of_val};
25

36
fn main() {
@@ -7,4 +10,6 @@ fn main() {
710
assert_eq!(size_of_val(&[] as &[i32]), 0);
811
assert_eq!(size_of_val(&[1, 2, 3] as &[i32]), 12);
912
assert_eq!(size_of_val("foobar"), 6);
13+
14+
assert_eq!(unsafe { type_name::<Option<i32>>() }, "core::option::Option<i32>");
1015
}

0 commit comments

Comments
 (0)