summary refs log tree commit diff
path: root/src/librustc_codegen_llvm
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-12-22 19:46:07 +0100
committerGitHub <noreply@github.com>2019-12-22 19:46:07 +0100
commit97bee3a793e287162d8f10635d6769e8d28befab (patch)
tree622673b10419d797b82ece48ca3b7f8fb15e022a /src/librustc_codegen_llvm
parent0d2817a439c2ad9fe343f6347eb6d90947893363 (diff)
parentc010d843aacc32ed2bc03d36121aa7f6e08ef045 (diff)
downloadrust-97bee3a793e287162d8f10635d6769e8d28befab.tar.gz
rust-97bee3a793e287162d8f10635d6769e8d28befab.zip
Rollup merge of #66877 - skinny121:const-eval-entry-points, r=oli-obk
Add simpler entry points to const eval for common usages.

I found the `tcx.const_eval` API to be complex/awkward to work with, because of the inherent complexity from all of the different situations it is called from. Though it mainly used in one of the following ways:
- Evaluates the value of a constant without any substitutions, e.g. evaluating a static, discriminant, etc.
- Evaluates the value of a resolved instance of a constant. this happens when evaluating unevaluated constants or normalising trait constants.
- Evaluates a promoted constant.

This PR adds three new functions `const_eval_mono`, `const_eval_resolve`, and `const_eval_promoted` to `TyCtxt`, which each cater to one of the three ways `tcx.const_eval`
 is normally used.
Diffstat (limited to 'src/librustc_codegen_llvm')
-rw-r--r--src/librustc_codegen_llvm/consts.rs10
-rw-r--r--src/librustc_codegen_llvm/intrinsic.rs9
2 files changed, 5 insertions, 14 deletions
diff --git a/src/librustc_codegen_llvm/consts.rs b/src/librustc_codegen_llvm/consts.rs
index e1750887ac4..78a86d33a14 100644
--- a/src/librustc_codegen_llvm/consts.rs
+++ b/src/librustc_codegen_llvm/consts.rs
@@ -8,7 +8,7 @@ use crate::value::Value;
 use libc::c_uint;
 use rustc::hir::def_id::DefId;
 use rustc::mir::interpret::{ConstValue, Allocation, read_target_uint,
-    Pointer, ErrorHandled, GlobalId};
+    Pointer, ErrorHandled};
 use rustc::mir::mono::MonoItem;
 use rustc::hir::Node;
 use rustc_target::abi::HasDataLayout;
@@ -81,13 +81,7 @@ pub fn codegen_static_initializer(
     cx: &CodegenCx<'ll, 'tcx>,
     def_id: DefId,
 ) -> Result<(&'ll Value, &'tcx Allocation), ErrorHandled> {
-    let instance = ty::Instance::mono(cx.tcx, def_id);
-    let cid = GlobalId {
-        instance,
-        promoted: None,
-    };
-    let param_env = ty::ParamEnv::reveal_all();
-    let static_ = cx.tcx.const_eval(param_env.and(cid))?;
+    let static_ = cx.tcx.const_eval_poly(def_id)?;
 
     let alloc = match static_.val {
         ty::ConstKind::Value(ConstValue::ByRef {
diff --git a/src/librustc_codegen_llvm/intrinsic.rs b/src/librustc_codegen_llvm/intrinsic.rs
index e178050b369..e5a7583a8cc 100644
--- a/src/librustc_codegen_llvm/intrinsic.rs
+++ b/src/librustc_codegen_llvm/intrinsic.rs
@@ -14,7 +14,6 @@ use rustc_codegen_ssa::glue;
 use rustc_codegen_ssa::base::{to_immediate, wants_msvc_seh, compare_simd_types};
 use rustc::ty::{self, Ty};
 use rustc::ty::layout::{self, FnAbiExt, LayoutOf, HasTyCtxt, Primitive};
-use rustc::mir::interpret::GlobalId;
 use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
 use rustc::hir;
 use rustc_target::abi::HasDataLayout;
@@ -202,11 +201,9 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
             "needs_drop" |
             "type_id" |
             "type_name" => {
-                let gid = GlobalId {
-                    instance,
-                    promoted: None,
-                };
-                let ty_name = self.tcx.const_eval(ty::ParamEnv::reveal_all().and(gid)).unwrap();
+                let ty_name = self.tcx
+                    .const_eval_instance(ty::ParamEnv::reveal_all(), instance, None)
+                    .unwrap();
                 OperandRef::from_const(self, ty_name).immediate_or_packed_pair(self)
             }
             "init" => {