Skip to content

Commit 30782e6

Browse files
committed
Get rid of special const intrinsic query in favour of const_eval
1 parent 72b2abf commit 30782e6

File tree

9 files changed

+52
-36
lines changed

9 files changed

+52
-36
lines changed

src/librustc/query/mod.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -463,15 +463,6 @@ rustc_queries! {
463463
no_force
464464
desc { "extract field of const" }
465465
}
466-
467-
/// Produces an absolute path representation of the given type. See also the documentation
468-
/// on `std::any::type_name`.
469-
query type_name(key: Ty<'tcx>) -> &'tcx ty::Const<'tcx> {
470-
eval_always
471-
no_force
472-
desc { "get absolute path of type" }
473-
}
474-
475466
}
476467

477468
TypeChecking {

src/librustc_codegen_llvm/intrinsic.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_codegen_ssa::glue;
1515
use rustc_codegen_ssa::base::{to_immediate, wants_msvc_seh, compare_simd_types};
1616
use rustc::ty::{self, Ty};
1717
use rustc::ty::layout::{self, LayoutOf, HasTyCtxt, Primitive};
18+
use rustc::mir::interpret::GlobalId;
1819
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
1920
use rustc::hir;
2021
use syntax::ast::{self, FloatTy};
@@ -81,13 +82,14 @@ fn get_simple_intrinsic(cx: &CodegenCx<'ll, '_>, name: &str) -> Option<&'ll Valu
8182
impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
8283
fn codegen_intrinsic_call(
8384
&mut self,
84-
callee_ty: Ty<'tcx>,
85+
instance: ty::Instance<'tcx>,
8586
fn_ty: &FnType<'tcx, Ty<'tcx>>,
8687
args: &[OperandRef<'tcx, &'ll Value>],
8788
llresult: &'ll Value,
8889
span: Span,
8990
) {
9091
let tcx = self.tcx;
92+
let callee_ty = instance.ty(tcx);
9193

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

src/librustc_codegen_ssa/mir/block.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
668668
}).collect();
669669

670670

671-
let callee_ty = instance.as_ref().unwrap().ty(bx.tcx());
672-
bx.codegen_intrinsic_call(callee_ty, &fn_ty, &args, dest,
671+
bx.codegen_intrinsic_call(*instance.as_ref().unwrap(), &fn_ty, &args, dest,
673672
terminator.source_info.span);
674673

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

src/librustc_codegen_ssa/traits/intrinsic.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +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};
1617
use rustc::traits::Reveal;
1718
use rustc_data_structures::fx::FxHashMap;
19+
use crate::interpret::alloc_type_name;
1820

1921
use syntax::source_map::{Span, DUMMY_SP};
2022

@@ -604,11 +606,42 @@ pub fn const_eval_provider<'tcx>(
604606
other => return other,
605607
}
606608
}
609+
610+
if let ty::InstanceDef::Intrinsic(def_id) = key.value.instance.def {
611+
let ty = key.value.instance.ty(tcx);
612+
let substs = match ty.sty {
613+
ty::FnDef(_, substs) => substs,
614+
_ => bug!("intrinsic with type {:?}", ty),
615+
};
616+
return Ok(eval_intrinsic(tcx, def_id, substs));
617+
}
618+
607619
tcx.const_eval_raw(key).and_then(|val| {
608620
validate_and_turn_into_const(tcx, val, key)
609621
})
610622
}
611623

624+
fn eval_intrinsic<'tcx>(
625+
tcx: TyCtxt<'tcx>,
626+
def_id: DefId,
627+
substs: SubstsRef<'tcx>,
628+
) -> &'tcx ty::Const<'tcx> {
629+
match &*tcx.item_name(def_id).as_str() {
630+
"type_name" => {
631+
let alloc = alloc_type_name(tcx, substs.type_at(0));
632+
tcx.mk_const(ty::Const {
633+
val: ConstValue::Slice {
634+
data: alloc,
635+
start: 0,
636+
end: alloc.bytes.len(),
637+
},
638+
ty: tcx.mk_static_str(),
639+
})
640+
},
641+
other => bug!("`{}` is not a zero arg intrinsic", other),
642+
}
643+
}
644+
612645
pub fn const_eval_raw_provider<'tcx>(
613646
tcx: TyCtxt<'tcx>,
614647
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,

src/librustc_mir/interpret/intrinsics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use super::{
1414

1515
mod type_name;
1616

17-
pub use type_name::*;
17+
pub(crate) use type_name::alloc_type_name;
1818

1919
fn numeric_intrinsic<'tcx, Tag>(
2020
name: &str,

src/librustc_mir/interpret/intrinsics/type_name.rs

Lines changed: 5 additions & 16 deletions
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>,
@@ -213,22 +213,11 @@ impl Write for AbsolutePathPrinter<'_> {
213213
}
214214
}
215215

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

src/librustc_mir/interpret/mod.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ pub fn provide(providers: &mut Providers<'_>) {
6060
let (param_env, (value, field)) = param_env_and_value.into_parts();
6161
const_eval::const_field(tcx, param_env, None, field, value)
6262
};
63-
providers.type_name = interpret::type_name;
6463
}
6564

6665
__build_diagnostic_array! { librustc_mir, DIAGNOSTICS }

0 commit comments

Comments
 (0)