interpret: move type_name implementation to an interpreter-independent helper file

This commit is contained in:
Ralf Jung 2022-10-31 11:04:03 +01:00
parent 4596f4f8b5
commit fa2aa1cedb
3 changed files with 15 additions and 9 deletions

View File

@ -7,7 +7,9 @@
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_middle::mir::{ use rustc_middle::mir::{
self, self,
interpret::{ConstValue, GlobalId, InterpResult, PointerArithmetic, Scalar}, interpret::{
Allocation, ConstAllocation, ConstValue, GlobalId, InterpResult, PointerArithmetic, Scalar,
},
BinOp, NonDivergingIntrinsic, BinOp, NonDivergingIntrinsic,
}; };
use rustc_middle::ty; use rustc_middle::ty;
@ -23,7 +25,6 @@
}; };
mod caller_location; mod caller_location;
mod type_name;
fn numeric_intrinsic<Prov>(name: Symbol, bits: u128, kind: Primitive) -> Scalar<Prov> { fn numeric_intrinsic<Prov>(name: Symbol, bits: u128, kind: Primitive) -> Scalar<Prov> {
let size = match kind { let size = match kind {
@ -42,6 +43,13 @@ fn numeric_intrinsic<Prov>(name: Symbol, bits: u128, kind: Primitive) -> Scalar<
Scalar::from_uint(bits_out, size) Scalar::from_uint(bits_out, size)
} }
/// Directly returns an `Allocation` containing an absolute path representation of the given type.
pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ConstAllocation<'tcx> {
let path = crate::util::type_name(tcx, ty);
let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes());
tcx.intern_const_alloc(alloc)
}
/// The logic for all nullary intrinsics is implemented here. These intrinsics don't get evaluated /// The logic for all nullary intrinsics is implemented here. These intrinsics don't get evaluated
/// inside an `InterpCx` and instead have their value computed directly from rustc internal info. /// inside an `InterpCx` and instead have their value computed directly from rustc internal info.
pub(crate) fn eval_nullary_intrinsic<'tcx>( pub(crate) fn eval_nullary_intrinsic<'tcx>(
@ -55,7 +63,7 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
Ok(match name { Ok(match name {
sym::type_name => { sym::type_name => {
ensure_monomorphic_enough(tcx, tp_ty)?; ensure_monomorphic_enough(tcx, tp_ty)?;
let alloc = type_name::alloc_type_name(tcx, tp_ty); let alloc = alloc_type_name(tcx, tp_ty);
ConstValue::Slice { data: alloc, start: 0, end: alloc.inner().len() } ConstValue::Slice { data: alloc, start: 0, end: alloc.inner().len() }
} }
sym::needs_drop => { sym::needs_drop => {

View File

@ -4,9 +4,11 @@
pub mod collect_writes; pub mod collect_writes;
mod find_self_call; mod find_self_call;
mod might_permit_raw_init; mod might_permit_raw_init;
mod type_name;
pub use self::aggregate::expand_aggregate; pub use self::aggregate::expand_aggregate;
pub use self::alignment::is_disaligned; pub use self::alignment::is_disaligned;
pub use self::call_kind::{call_kind, CallDesugaringKind, CallKind}; pub use self::call_kind::{call_kind, CallDesugaringKind, CallKind};
pub use self::find_self_call::find_self_call; pub use self::find_self_call::find_self_call;
pub use self::might_permit_raw_init::might_permit_raw_init; pub use self::might_permit_raw_init::might_permit_raw_init;
pub use self::type_name::type_name;

View File

@ -1,7 +1,6 @@
use rustc_data_structures::intern::Interned; use rustc_data_structures::intern::Interned;
use rustc_hir::def_id::CrateNum; use rustc_hir::def_id::CrateNum;
use rustc_hir::definitions::DisambiguatedDefPathData; use rustc_hir::definitions::DisambiguatedDefPathData;
use rustc_middle::mir::interpret::{Allocation, ConstAllocation};
use rustc_middle::ty::{ use rustc_middle::ty::{
self, self,
print::{PrettyPrinter, Print, Printer}, print::{PrettyPrinter, Print, Printer},
@ -193,9 +192,6 @@ fn write_str(&mut self, s: &str) -> std::fmt::Result {
} }
} }
/// Directly returns an `Allocation` containing an absolute path representation of the given type. pub fn type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> String {
pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ConstAllocation<'tcx> { AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path
let path = AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path;
let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes());
tcx.intern_const_alloc(alloc)
} }