Skip to content

Commit 0f40e95

Browse files
author
Yuki Okushi
authored
Rollup merge of rust-lang#103798 - RalfJung:type_name, r=oli-obk
interpret: move type_name implementation to an interpreter-independent helper file This should avoid pinging rust-lang/miri each time that file changes, which is really not necessary. r? `@oli-obk`
2 parents 2c7f137 + fa2aa1c commit 0f40e95

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use std::convert::TryFrom;
77
use rustc_hir::def_id::DefId;
88
use rustc_middle::mir::{
99
self,
10-
interpret::{ConstValue, GlobalId, InterpResult, PointerArithmetic, Scalar},
10+
interpret::{
11+
Allocation, ConstAllocation, ConstValue, GlobalId, InterpResult, PointerArithmetic, Scalar,
12+
},
1113
BinOp, NonDivergingIntrinsic,
1214
};
1315
use rustc_middle::ty;
@@ -23,7 +25,6 @@ use super::{
2325
};
2426

2527
mod caller_location;
26-
mod type_name;
2728

2829
fn numeric_intrinsic<Prov>(name: Symbol, bits: u128, kind: Primitive) -> Scalar<Prov> {
2930
let size = match kind {
@@ -42,6 +43,13 @@ fn numeric_intrinsic<Prov>(name: Symbol, bits: u128, kind: Primitive) -> Scalar<
4243
Scalar::from_uint(bits_out, size)
4344
}
4445

46+
/// Directly returns an `Allocation` containing an absolute path representation of the given type.
47+
pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ConstAllocation<'tcx> {
48+
let path = crate::util::type_name(tcx, ty);
49+
let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes());
50+
tcx.intern_const_alloc(alloc)
51+
}
52+
4553
/// The logic for all nullary intrinsics is implemented here. These intrinsics don't get evaluated
4654
/// inside an `InterpCx` and instead have their value computed directly from rustc internal info.
4755
pub(crate) fn eval_nullary_intrinsic<'tcx>(
@@ -55,7 +63,7 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
5563
Ok(match name {
5664
sym::type_name => {
5765
ensure_monomorphic_enough(tcx, tp_ty)?;
58-
let alloc = type_name::alloc_type_name(tcx, tp_ty);
66+
let alloc = alloc_type_name(tcx, tp_ty);
5967
ConstValue::Slice { data: alloc, start: 0, end: alloc.inner().len() }
6068
}
6169
sym::needs_drop => {

compiler/rustc_const_eval/src/util/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ mod call_kind;
44
pub mod collect_writes;
55
mod find_self_call;
66
mod might_permit_raw_init;
7+
mod type_name;
78

89
pub use self::aggregate::expand_aggregate;
910
pub use self::alignment::is_disaligned;
1011
pub use self::call_kind::{call_kind, CallDesugaringKind, CallKind};
1112
pub use self::find_self_call::find_self_call;
1213
pub use self::might_permit_raw_init::might_permit_raw_init;
14+
pub use self::type_name::type_name;

compiler/rustc_const_eval/src/interpret/intrinsics/type_name.rs renamed to compiler/rustc_const_eval/src/util/type_name.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use rustc_data_structures::intern::Interned;
22
use rustc_hir::def_id::CrateNum;
33
use rustc_hir::definitions::DisambiguatedDefPathData;
4-
use rustc_middle::mir::interpret::{Allocation, ConstAllocation};
54
use rustc_middle::ty::{
65
self,
76
print::{PrettyPrinter, Print, Printer},
@@ -193,9 +192,6 @@ impl Write for AbsolutePathPrinter<'_> {
193192
}
194193
}
195194

196-
/// Directly returns an `Allocation` containing an absolute path representation of the given type.
197-
pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ConstAllocation<'tcx> {
198-
let path = AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path;
199-
let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes());
200-
tcx.intern_const_alloc(alloc)
195+
pub fn type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> String {
196+
AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path
201197
}

0 commit comments

Comments
 (0)