Skip to content

Commit bd9b93d

Browse files
committed
Get rid of special const intrinsic query in favour of const_eval
1 parent 9cb052a commit bd9b93d

File tree

9 files changed

+52
-37
lines changed

9 files changed

+52
-37
lines changed

src/librustc/query/mod.rs

-9
Original file line numberDiff line numberDiff line change
@@ -441,15 +441,6 @@ rustc_queries! {
441441
no_force
442442
desc { "extract field of const" }
443443
}
444-
445-
/// Produces an absolute path representation of the given type. See also the documentation
446-
/// on `std::any::type_name`.
447-
query type_name(key: Ty<'tcx>) -> &'tcx ty::Const<'tcx> {
448-
eval_always
449-
no_force
450-
desc { "get absolute path of type" }
451-
}
452-
453444
}
454445

455446
TypeChecking {

src/librustc_codegen_llvm/intrinsic.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_codegen_ssa::glue;
1717
use rustc_codegen_ssa::base::{to_immediate, wants_msvc_seh, compare_simd_types};
1818
use rustc::ty::{self, Ty};
1919
use rustc::ty::layout::{self, LayoutOf, HasTyCtxt, Primitive};
20+
use rustc::mir::interpret::GlobalId;
2021
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
2122
use rustc::hir;
2223
use syntax::ast::{self, FloatTy};
@@ -83,13 +84,14 @@ fn get_simple_intrinsic(cx: &CodegenCx<'ll, '_>, name: &str) -> Option<&'ll Valu
8384
impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
8485
fn codegen_intrinsic_call(
8586
&mut self,
86-
callee_ty: Ty<'tcx>,
87+
instance: ty::Instance<'tcx>,
8788
fn_ty: &FnType<'tcx, Ty<'tcx>>,
8889
args: &[OperandRef<'tcx, &'ll Value>],
8990
llresult: &'ll Value,
9091
span: Span,
9192
) {
9293
let tcx = self.tcx;
94+
let callee_ty = instance.ty(tcx);
9395

9496
let (def_id, substs) = match callee_ty.sty {
9597
ty::FnDef(def_id, substs) => (def_id, substs),
@@ -208,8 +210,11 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
208210
self.const_usize(self.layout_of(tp_ty).align.pref.bytes())
209211
}
210212
"type_name" => {
211-
let tp_ty = substs.type_at(0);
212-
let ty_name = self.tcx.type_name(tp_ty);
213+
let gid = GlobalId {
214+
instance,
215+
promoted: None,
216+
};
217+
let ty_name = self.tcx.const_eval(ty::ParamEnv::reveal_all().and(gid)).unwrap();
213218
OperandRef::from_const(self, ty_name).immediate_or_packed_pair(self)
214219
}
215220
"type_id" => {

src/librustc_codegen_ssa/mir/block.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
662662
}).collect();
663663

664664

665-
let callee_ty = instance.as_ref().unwrap().ty(bx.tcx());
666-
bx.codegen_intrinsic_call(callee_ty, &fn_ty, &args, dest,
665+
bx.codegen_intrinsic_call(*instance.as_ref().unwrap(), &fn_ty, &args, dest,
667666
terminator.source_info.span);
668667

669668
if let ReturnDest::IndirectOperand(dst, _) = ret_dest {

src/librustc_codegen_ssa/traits/intrinsic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::BackendTypes;
22
use crate::mir::operand::OperandRef;
3-
use rustc::ty::Ty;
3+
use rustc::ty::{self, Ty};
44
use rustc_target::abi::call::FnType;
55
use syntax_pos::Span;
66

@@ -10,7 +10,7 @@ pub trait IntrinsicCallMethods<'tcx>: BackendTypes {
1010
/// add them to librustc_codegen_llvm/context.rs
1111
fn codegen_intrinsic_call(
1212
&mut self,
13-
callee_ty: Ty<'tcx>,
13+
instance: ty::Instance<'tcx>,
1414
fn_ty: &FnType<'tcx, Ty<'tcx>>,
1515
args: &[OperandRef<'tcx, Self::Value>],
1616
llresult: Self::Value,

src/librustc_mir/const_eval.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ use rustc::mir::interpret::{ConstEvalErr, ErrorHandled, ScalarMaybeUndef};
1313
use rustc::mir;
1414
use rustc::ty::{self, TyCtxt, query::TyCtxtAt};
1515
use rustc::ty::layout::{self, LayoutOf, VariantIdx};
16-
use rustc::ty::subst::Subst;
16+
use rustc::ty::subst::{Subst, SubstsRef};
1717
use rustc::traits::Reveal;
1818
use rustc::util::common::ErrorReported;
1919
use rustc_data_structures::fx::FxHashMap;
20+
use crate::interpret::alloc_type_name;
2021

2122
use syntax::source_map::{Span, DUMMY_SP};
2223

@@ -588,11 +589,42 @@ pub fn const_eval_provider<'tcx>(
588589
other => return other,
589590
}
590591
}
592+
593+
if let ty::InstanceDef::Intrinsic(def_id) = key.value.instance.def {
594+
let ty = key.value.instance.ty(tcx);
595+
let substs = match ty.sty {
596+
ty::FnDef(_, substs) => substs,
597+
_ => bug!("intrinsic with type {:?}", ty),
598+
};
599+
return Ok(eval_intrinsic(tcx, def_id, substs));
600+
}
601+
591602
tcx.const_eval_raw(key).and_then(|val| {
592603
validate_and_turn_into_const(tcx, val, key)
593604
})
594605
}
595606

607+
fn eval_intrinsic<'tcx>(
608+
tcx: TyCtxt<'tcx>,
609+
def_id: DefId,
610+
substs: SubstsRef<'tcx>,
611+
) -> &'tcx ty::Const<'tcx> {
612+
match &*tcx.item_name(def_id).as_str() {
613+
"type_name" => {
614+
let alloc = alloc_type_name(tcx, substs.type_at(0));
615+
tcx.mk_const(ty::Const {
616+
val: ConstValue::Slice {
617+
data: alloc,
618+
start: 0,
619+
end: alloc.bytes.len(),
620+
},
621+
ty: tcx.mk_static_str(),
622+
})
623+
},
624+
other => bug!("`{}` is not a zero arg intrinsic", other),
625+
}
626+
}
627+
596628
pub fn const_eval_raw_provider<'tcx>(
597629
tcx: TyCtxt<'tcx>,
598630
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,

src/librustc_mir/interpret/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use super::{
1616

1717
mod type_name;
1818

19-
pub use type_name::*;
19+
pub(crate) use type_name::alloc_type_name;
2020

2121
fn numeric_intrinsic<'tcx, Tag>(
2222
name: &str,

src/librustc_mir/interpret/intrinsics/type_name.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc::ty::{
77
use rustc::hir::map::{DefPathData, DisambiguatedDefPathData};
88
use rustc::hir::def_id::CrateNum;
99
use std::fmt::Write;
10-
use rustc::mir::interpret::{Allocation, ConstValue};
10+
use rustc::mir::interpret::Allocation;
1111

1212
struct AbsolutePathPrinter<'tcx> {
1313
tcx: TyCtxt<'tcx>,
@@ -208,22 +208,11 @@ impl Write for AbsolutePathPrinter<'_> {
208208
}
209209
}
210210

211-
/// Produces an absolute path representation of the given type. See also the documentation on
212-
/// `std::any::type_name`
213-
pub fn type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> &'tcx ty::Const<'tcx> {
214-
let alloc = alloc_type_name(tcx, ty);
215-
tcx.mk_const(ty::Const {
216-
val: ConstValue::Slice {
217-
data: alloc,
218-
start: 0,
219-
end: alloc.bytes.len(),
220-
},
221-
ty: tcx.mk_static_str(),
222-
})
223-
}
224-
225211
/// Directly returns an `Allocation` containing an absolute path representation of the given type.
226-
pub(super) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> &'tcx Allocation {
212+
pub(crate) fn alloc_type_name<'tcx>(
213+
tcx: TyCtxt<'tcx>,
214+
ty: Ty<'tcx>
215+
) -> &'tcx Allocation {
227216
let path = AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path;
228217
let alloc = Allocation::from_byte_aligned_bytes(path.into_bytes());
229218
tcx.intern_const_alloc(alloc)

src/librustc_mir/interpret/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ pub use self::visitor::{ValueVisitor, MutValueVisitor};
3434

3535
pub use self::validity::RefTracking;
3636

37-
pub(super) use self::intrinsics::type_name;
38-
3937
pub use self::intern::intern_const_alloc_recursive;
38+
39+
pub(crate) use self::intrinsics::alloc_type_name;

src/librustc_mir/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ pub fn provide(providers: &mut Providers<'_>) {
6969
let (param_env, (value, field)) = param_env_and_value.into_parts();
7070
const_eval::const_field(tcx, param_env, None, field, value)
7171
};
72-
providers.type_name = interpret::type_name;
7372
}
7473

7574
__build_diagnostic_array! { librustc_mir, DIAGNOSTICS }

0 commit comments

Comments
 (0)