about summary refs log tree commit diff
path: root/src/librustc_codegen_ssa
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_ssa
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_ssa')
-rw-r--r--src/librustc_codegen_ssa/mir/block.rs7
-rw-r--r--src/librustc_codegen_ssa/mir/constant.rs19
-rw-r--r--src/librustc_codegen_ssa/mir/place.rs7
3 files changed, 10 insertions, 23 deletions
diff --git a/src/librustc_codegen_ssa/mir/block.rs b/src/librustc_codegen_ssa/mir/block.rs
index dabd097b000..a532c23a6e0 100644
--- a/src/librustc_codegen_ssa/mir/block.rs
+++ b/src/librustc_codegen_ssa/mir/block.rs
@@ -638,12 +638,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
                                 projection: &[],
                             } = place.as_ref()
                             {
-                                let param_env = ty::ParamEnv::reveal_all();
-                                let cid = mir::interpret::GlobalId {
-                                    instance: self.instance,
-                                    promoted: Some(promoted),
-                                };
-                                let c = bx.tcx().const_eval(param_env.and(cid));
+                                let c = bx.tcx().const_eval_promoted(self.instance, promoted);
                                 let (llval, ty) = self.simd_shuffle_indices(
                                     &bx,
                                     terminator.source_info.span,
diff --git a/src/librustc_codegen_ssa/mir/constant.rs b/src/librustc_codegen_ssa/mir/constant.rs
index fb8f504d04b..fc17e2c0c71 100644
--- a/src/librustc_codegen_ssa/mir/constant.rs
+++ b/src/librustc_codegen_ssa/mir/constant.rs
@@ -43,17 +43,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
         match constant.literal.val {
             ty::ConstKind::Unevaluated(def_id, substs) => {
                 let substs = self.monomorphize(&substs);
-                let instance = ty::Instance::resolve(
-                    self.cx.tcx(), ty::ParamEnv::reveal_all(), def_id, substs,
-                ).unwrap();
-                let cid = mir::interpret::GlobalId {
-                    instance,
-                    promoted: None,
-                };
-                self.cx.tcx().const_eval(ty::ParamEnv::reveal_all().and(cid)).map_err(|err| {
-                    self.cx.tcx().sess.span_err(constant.span, "erroneous constant encountered");
-                    err
-                })
+                self.cx.tcx()
+                    .const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, None)
+                    .map_err(|err| {
+                        self.cx.tcx().sess.span_err(
+                            constant.span,
+                            "erroneous constant encountered");
+                        err
+                    })
             },
             _ => Ok(self.monomorphize(&constant.literal)),
         }
diff --git a/src/librustc_codegen_ssa/mir/place.rs b/src/librustc_codegen_ssa/mir/place.rs
index 5b21dfbdf1c..98d239d353d 100644
--- a/src/librustc_codegen_ssa/mir/place.rs
+++ b/src/librustc_codegen_ssa/mir/place.rs
@@ -473,14 +473,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
                 }),
                 projection: [],
             } => {
-                let param_env = ty::ParamEnv::reveal_all();
                 let instance = Instance::new(*def_id, self.monomorphize(substs));
-                let cid = mir::interpret::GlobalId {
-                    instance: instance,
-                    promoted: Some(*promoted),
-                };
                 let layout = cx.layout_of(self.monomorphize(&ty));
-                match bx.tcx().const_eval(param_env.and(cid)) {
+                match bx.tcx().const_eval_promoted(instance, *promoted) {
                     Ok(val) => match val.val {
                         ty::ConstKind::Value(mir::interpret::ConstValue::ByRef {
                             alloc, offset