about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-08-28 07:29:07 +0000
committerbors <bors@rust-lang.org>2019-08-28 07:29:07 +0000
commitac21131f7859836cd3fcb39231c0162fd892d960 (patch)
tree6ee393b7e16de069560f9407c5b07d7fc6527af6
parentbbd48e6f166739344528a5fff4f02516b77d7916 (diff)
parent181ed55e96e589afe565e5ad4e7f1cd6a8000894 (diff)
downloadrust-ac21131f7859836cd3fcb39231c0162fd892d960.tar.gz
rust-ac21131f7859836cd3fcb39231c0162fd892d960.zip
Auto merge of #63820 - oli-obk:eager_const_eval, r=nikomatsakis
Simplify eager normalization of constants

r? @nikomatsakis
-rw-r--r--src/librustc/traits/project.rs36
-rw-r--r--src/librustc/traits/query/normalize.rs38
-rw-r--r--src/test/ui/consts/const-size_of-cycle.stderr10
-rw-r--r--src/test/ui/issues/issue-44415.stderr10
4 files changed, 13 insertions, 81 deletions
diff --git a/src/librustc/traits/project.rs b/src/librustc/traits/project.rs
index 72df45df923..87a23f655a8 100644
--- a/src/librustc/traits/project.rs
+++ b/src/librustc/traits/project.rs
@@ -15,7 +15,6 @@ use super::util;
 use crate::hir::def_id::DefId;
 use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
 use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
-use crate::mir::interpret::{GlobalId, ConstValue};
 use rustc_data_structures::snapshot_map::{Snapshot, SnapshotMap};
 use rustc_macros::HashStable;
 use syntax::ast::Ident;
@@ -397,40 +396,7 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> {
     }
 
     fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
-        if let ConstValue::Unevaluated(def_id, substs) = constant.val {
-            let tcx = self.selcx.tcx().global_tcx();
-            let param_env = self.param_env;
-            if !param_env.has_local_value() {
-                if substs.needs_infer() || substs.has_placeholders() {
-                    let identity_substs = InternalSubsts::identity_for_item(tcx, def_id);
-                    let instance = ty::Instance::resolve(tcx, param_env, def_id, identity_substs);
-                    if let Some(instance) = instance {
-                        let cid = GlobalId {
-                            instance,
-                            promoted: None
-                        };
-                        if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
-                            let evaluated = evaluated.subst(tcx, substs);
-                            return evaluated;
-                        }
-                    }
-                } else {
-                    if !substs.has_local_value() {
-                        let instance = ty::Instance::resolve(tcx, param_env, def_id, substs);
-                        if let Some(instance) = instance {
-                            let cid = GlobalId {
-                                instance,
-                                promoted: None
-                            };
-                            if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
-                                return evaluated;
-                            }
-                        }
-                    }
-                }
-            }
-        }
-        constant
+        constant.eval(self.selcx.tcx(), self.param_env)
     }
 }
 
diff --git a/src/librustc/traits/query/normalize.rs b/src/librustc/traits/query/normalize.rs
index 2ffcd0fd4d9..c31ff3ab1b5 100644
--- a/src/librustc/traits/query/normalize.rs
+++ b/src/librustc/traits/query/normalize.rs
@@ -5,11 +5,10 @@
 use crate::infer::at::At;
 use crate::infer::canonical::OriginalQueryValues;
 use crate::infer::{InferCtxt, InferOk};
-use crate::mir::interpret::{GlobalId, ConstValue};
 use crate::traits::project::Normalized;
 use crate::traits::{Obligation, ObligationCause, PredicateObligation, Reveal};
 use crate::ty::fold::{TypeFoldable, TypeFolder};
-use crate::ty::subst::{Subst, InternalSubsts};
+use crate::ty::subst::Subst;
 use crate::ty::{self, Ty, TyCtxt};
 
 use super::NoSolution;
@@ -191,40 +190,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
     }
 
     fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
-        if let ConstValue::Unevaluated(def_id, substs) = constant.val {
-            let tcx = self.infcx.tcx.global_tcx();
-            let param_env = self.param_env;
-            if !param_env.has_local_value() {
-                if substs.needs_infer() || substs.has_placeholders() {
-                    let identity_substs = InternalSubsts::identity_for_item(tcx, def_id);
-                    let instance = ty::Instance::resolve(tcx, param_env, def_id, identity_substs);
-                    if let Some(instance) = instance {
-                        let cid = GlobalId {
-                            instance,
-                            promoted: None,
-                        };
-                        if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
-                            let evaluated = evaluated.subst(tcx, substs);
-                            return evaluated;
-                        }
-                    }
-                } else {
-                    if !substs.has_local_value() {
-                        let instance = ty::Instance::resolve(tcx, param_env, def_id, substs);
-                        if let Some(instance) = instance {
-                            let cid = GlobalId {
-                                instance,
-                                promoted: None,
-                            };
-                            if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
-                                return evaluated;
-                            }
-                        }
-                    }
-                }
-            }
-        }
-        constant
+        constant.eval(self.infcx.tcx, self.param_env)
     }
 }
 
diff --git a/src/test/ui/consts/const-size_of-cycle.stderr b/src/test/ui/consts/const-size_of-cycle.stderr
index 113ec292396..fdba359e746 100644
--- a/src/test/ui/consts/const-size_of-cycle.stderr
+++ b/src/test/ui/consts/const-size_of-cycle.stderr
@@ -4,6 +4,11 @@ error[E0391]: cycle detected when const-evaluating + checking `Foo::bytes::{{con
 LL |     bytes: [u8; std::mem::size_of::<Foo>()]
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
+note: ...which requires const-evaluating + checking `Foo::bytes::{{constant}}#0`...
+  --> $DIR/const-size_of-cycle.rs:6:17
+   |
+LL |     bytes: [u8; std::mem::size_of::<Foo>()]
+   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 note: ...which requires const-evaluating `Foo::bytes::{{constant}}#0`...
   --> $SRC_DIR/libcore/mem/mod.rs:LL:COL
    |
@@ -11,11 +16,6 @@ LL |     intrinsics::size_of::<T>()
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: ...which requires computing layout of `Foo`...
    = note: ...which requires normalizing `ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: All, def_id: None }, value: [u8; _] }`...
-note: ...which requires const-evaluating + checking `Foo::bytes::{{constant}}#0`...
-  --> $DIR/const-size_of-cycle.rs:6:17
-   |
-LL |     bytes: [u8; std::mem::size_of::<Foo>()]
-   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: ...which again requires const-evaluating + checking `Foo::bytes::{{constant}}#0`, completing the cycle
 note: cycle used when processing `Foo`
   --> $DIR/const-size_of-cycle.rs:5:1
diff --git a/src/test/ui/issues/issue-44415.stderr b/src/test/ui/issues/issue-44415.stderr
index df8e804c87a..8008e53f65f 100644
--- a/src/test/ui/issues/issue-44415.stderr
+++ b/src/test/ui/issues/issue-44415.stderr
@@ -4,6 +4,11 @@ error[E0391]: cycle detected when const-evaluating + checking `Foo::bytes::{{con
 LL |     bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
    |                 ^^^^^^
    |
+note: ...which requires const-evaluating + checking `Foo::bytes::{{constant}}#0`...
+  --> $DIR/issue-44415.rs:6:17
+   |
+LL |     bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
+   |                 ^^^^^^
 note: ...which requires const-evaluating `Foo::bytes::{{constant}}#0`...
   --> $DIR/issue-44415.rs:6:26
    |
@@ -11,11 +16,6 @@ LL |     bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
    |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: ...which requires computing layout of `Foo`...
    = note: ...which requires normalizing `ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: All, def_id: None }, value: [u8; _] }`...
-note: ...which requires const-evaluating + checking `Foo::bytes::{{constant}}#0`...
-  --> $DIR/issue-44415.rs:6:17
-   |
-LL |     bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
-   |                 ^^^^^^
    = note: ...which again requires const-evaluating + checking `Foo::bytes::{{constant}}#0`, completing the cycle
 note: cycle used when processing `Foo`
   --> $DIR/issue-44415.rs:5:1