Skip to content

Commit 6f055c8

Browse files
committed
Move nulary intrinsic logic to the other intrinsic logic
1 parent 9310a64 commit 6f055c8

File tree

3 files changed

+50
-48
lines changed

3 files changed

+50
-48
lines changed

src/librustc_mir/const_eval.rs

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use rustc::mir::interpret::{ConstEvalErr, ErrorHandled, ScalarMaybeUndef};
1313
use rustc::mir;
1414
use rustc::ty::{self, Ty, TyCtxt, subst::Subst};
1515
use rustc::ty::layout::{self, LayoutOf, VariantIdx};
16-
use rustc::ty::subst::{Subst, SubstsRef};
16+
use rustc::ty::subst::Subst;
1717
use rustc::traits::Reveal;
1818
use rustc_data_structures::fx::FxHashMap;
19-
use crate::interpret::alloc_type_name;
19+
use crate::interpret::eval_nulary_intrinsic;
2020

2121
use syntax::source_map::{Span, DUMMY_SP};
2222

@@ -621,48 +621,6 @@ pub fn const_eval_provider<'tcx>(
621621
})
622622
}
623623

624-
fn eval_nulary_intrinsic<'tcx>(
625-
tcx: TyCtxt<'tcx>,
626-
param_env: ty::ParamEnv<'tcx>,
627-
def_id: DefId,
628-
substs: SubstsRef<'tcx>,
629-
) -> &'tcx ty::Const<'tcx> {
630-
let tp_ty = substs.type_at(0);
631-
let name = &*tcx.item_name(def_id).as_str();
632-
match name {
633-
"type_name" => {
634-
let alloc = alloc_type_name(tcx, tp_ty);
635-
tcx.mk_const(ty::Const {
636-
val: ConstValue::Slice {
637-
data: alloc,
638-
start: 0,
639-
end: alloc.bytes.len(),
640-
},
641-
ty: tcx.mk_static_str(),
642-
})
643-
},
644-
"needs_drop" => ty::Const::from_bool(tcx, tp_ty.needs_drop(tcx, param_env)),
645-
"size_of" |
646-
"min_align_of" |
647-
"pref_align_of" => {
648-
let layout = tcx.layout_of(param_env.and(tp_ty)).unwrap();
649-
let n = match name {
650-
"pref_align_of" => layout.align.pref.bytes(),
651-
"min_align_of" => layout.align.abi.bytes(),
652-
"size_of" => layout.size.bytes(),
653-
_ => bug!(),
654-
};
655-
ty::Const::from_usize(tcx, n)
656-
},
657-
"type_id" => ty::Const::from_bits(
658-
tcx,
659-
tcx.type_id_hash(tp_ty).into(),
660-
param_env.and(tcx.types.u64),
661-
),
662-
other => bug!("`{}` is not a zero arg intrinsic", other),
663-
}
664-
}
665-
666624
pub fn const_eval_raw_provider<'tcx>(
667625
tcx: TyCtxt<'tcx>,
668626
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,

src/librustc_mir/interpret/intrinsics.rs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
use syntax::symbol::Symbol;
66
use rustc::ty;
77
use rustc::ty::layout::{LayoutOf, Primitive, Size};
8+
use rustc::ty::subst::SubstsRef;
9+
use rustc::hir::def_id::DefId;
10+
use rustc::ty::TyCtxt;
811
use rustc::mir::BinOp;
9-
use rustc::mir::interpret::{InterpResult, Scalar, GlobalId};
12+
use rustc::mir::interpret::{InterpResult, Scalar, GlobalId, ConstValue};
1013

1114
use super::{
1215
Machine, PlaceTy, OpTy, InterpCx,
1316
};
1417

1518
mod type_name;
1619

17-
pub(crate) use type_name::alloc_type_name;
18-
1920
fn numeric_intrinsic<'tcx, Tag>(
2021
name: &str,
2122
bits: u128,
@@ -37,6 +38,49 @@ fn numeric_intrinsic<'tcx, Tag>(
3738
Ok(Scalar::from_uint(bits_out, size))
3839
}
3940

41+
42+
crate fn eval_nulary_intrinsic<'tcx>(
43+
tcx: TyCtxt<'tcx>,
44+
param_env: ty::ParamEnv<'tcx>,
45+
def_id: DefId,
46+
substs: SubstsRef<'tcx>,
47+
) -> &'tcx ty::Const<'tcx> {
48+
let tp_ty = substs.type_at(0);
49+
let name = &*tcx.item_name(def_id).as_str();
50+
match name {
51+
"type_name" => {
52+
let alloc = type_name::alloc_type_name(tcx, tp_ty);
53+
tcx.mk_const(ty::Const {
54+
val: ConstValue::Slice {
55+
data: alloc,
56+
start: 0,
57+
end: alloc.bytes.len(),
58+
},
59+
ty: tcx.mk_static_str(),
60+
})
61+
},
62+
"needs_drop" => ty::Const::from_bool(tcx, tp_ty.needs_drop(tcx, param_env)),
63+
"size_of" |
64+
"min_align_of" |
65+
"pref_align_of" => {
66+
let layout = tcx.layout_of(param_env.and(tp_ty)).unwrap();
67+
let n = match name {
68+
"pref_align_of" => layout.align.pref.bytes(),
69+
"min_align_of" => layout.align.abi.bytes(),
70+
"size_of" => layout.size.bytes(),
71+
_ => bug!(),
72+
};
73+
ty::Const::from_usize(tcx, n)
74+
},
75+
"type_id" => ty::Const::from_bits(
76+
tcx,
77+
tcx.type_id_hash(tp_ty).into(),
78+
param_env.and(tcx.types.u64),
79+
),
80+
other => bug!("`{}` is not a zero arg intrinsic", other),
81+
}
82+
}
83+
4084
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
4185
/// Returns `true` if emulation happened.
4286
pub fn emulate_intrinsic(

src/librustc_mir/interpret/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ pub use self::validity::RefTracking;
3636

3737
pub use self::intern::intern_const_alloc_recursive;
3838

39-
pub(crate) use self::intrinsics::alloc_type_name;
39+
pub(crate) use self::intrinsics::eval_nulary_intrinsic;

0 commit comments

Comments
 (0)