Auto merge of #68505 - skinny121:canonicalize-const-eval-inputs, r=nikomatsakis

Canonicalize inputs to const eval where needed

Canonicalize inputs to const eval, so that they can contain inference variables. Which enables invoking const eval queries even if the current param env has inference variable within it, which can occur during trait selection.

This is a reattempt of #67717, in a far less invasive way.

Fixes #68477

r? @nikomatsakis
cc @eddyb
diff --git a/src/librustc/mir/interpret/queries.rs b/src/librustc/mir/interpret/queries.rs
index ed57f81..46bf1d9 100644
--- a/src/librustc/mir/interpret/queries.rs
+++ b/src/librustc/mir/interpret/queries.rs
@@ -13,13 +13,13 @@
     pub fn const_eval_poly(self, def_id: DefId) -> ConstEvalResult<'tcx> {
         // In some situations def_id will have substitutions within scope, but they aren't allowed
         // to be used. So we can't use `Instance::mono`, instead we feed unresolved substitutions
-        // into `const_eval` which will return `ErrorHandled::ToGeneric` if any og them are
+        // into `const_eval` which will return `ErrorHandled::ToGeneric` if any of them are
         // encountered.
         let substs = InternalSubsts::identity_for_item(self, def_id);
         let instance = ty::Instance::new(def_id, substs);
         let cid = GlobalId { instance, promoted: None };
         let param_env = self.param_env(def_id).with_reveal_all();
-        self.const_eval_validated(param_env.and(cid))
+        self.const_eval_global_id(param_env, cid, None)
     }
 
     /// Resolves and evaluates a constant.
@@ -41,11 +41,8 @@
     ) -> ConstEvalResult<'tcx> {
         let instance = ty::Instance::resolve(self, param_env, def_id, substs);
         if let Some(instance) = instance {
-            if let Some(promoted) = promoted {
-                self.const_eval_promoted(param_env, instance, promoted)
-            } else {
-                self.const_eval_instance(param_env, instance, span)
-            }
+            let cid = GlobalId { instance, promoted };
+            self.const_eval_global_id(param_env, cid, span)
         } else {
             Err(ErrorHandled::TooGeneric)
         }
@@ -57,22 +54,23 @@
         instance: ty::Instance<'tcx>,
         span: Option<Span>,
     ) -> ConstEvalResult<'tcx> {
-        let cid = GlobalId { instance, promoted: None };
-        if let Some(span) = span {
-            self.at(span).const_eval_validated(param_env.and(cid))
-        } else {
-            self.const_eval_validated(param_env.and(cid))
-        }
+        self.const_eval_global_id(param_env, GlobalId { instance, promoted: None }, span)
     }
 
-    /// Evaluate a promoted constant.
-    pub fn const_eval_promoted(
+    /// Evaluate a constant.
+    pub fn const_eval_global_id(
         self,
         param_env: ty::ParamEnv<'tcx>,
-        instance: ty::Instance<'tcx>,
-        promoted: mir::Promoted,
+        cid: GlobalId<'tcx>,
+        span: Option<Span>,
     ) -> ConstEvalResult<'tcx> {
-        let cid = GlobalId { instance, promoted: Some(promoted) };
-        self.const_eval_validated(param_env.and(cid))
+        // Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
+        // improve caching of queries.
+        let inputs = self.erase_regions(&param_env.and(cid));
+        if let Some(span) = span {
+            self.at(span).const_eval_validated(inputs)
+        } else {
+            self.const_eval_validated(inputs)
+        }
     }
 }
diff --git a/src/librustc/query/mod.rs b/src/librustc/query/mod.rs
index 3a69616..69698ea 100644
--- a/src/librustc/query/mod.rs
+++ b/src/librustc/query/mod.rs
@@ -513,7 +513,7 @@
         /// returns a proper constant that is usable by the rest of the compiler.
         ///
         /// **Do not use this** directly, use one of the following wrappers: `tcx.const_eval_poly`,
-        /// `tcx.const_eval_resolve`, `tcx.const_eval_instance`, or `tcx.const_eval_promoted`.
+        /// `tcx.const_eval_resolve`, `tcx.const_eval_instance`, or `tcx.const_eval_global_id`.
         query const_eval_validated(key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>)
             -> ConstEvalResult<'tcx> {
             no_force
diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs
index c3698f4..283333b 100644
--- a/src/librustc/ty/sty.rs
+++ b/src/librustc/ty/sty.rs
@@ -2484,8 +2484,8 @@
                 // HACK(eddyb) when substs contain e.g. inference variables,
                 // attempt using identity substs instead, that will succeed
                 // when the expression doesn't depend on any parameters.
-                // FIXME(eddyb) make `const_eval` a canonical query instead,
-                // that would properly handle inference variables in `substs`.
+                // FIXME(eddyb, skinny121) pass `InferCtxt` into here when it's available, so that
+                // we can call `infcx.const_eval_resolve` which handles inference variables.
                 if substs.has_local_value() {
                     let identity_substs = InternalSubsts::identity_for_item(tcx, did);
                     // The `ParamEnv` needs to match the `identity_substs`.
diff --git a/src/librustc_infer/infer/mod.rs b/src/librustc_infer/infer/mod.rs
index 65f060d..26998f0 100644
--- a/src/librustc_infer/infer/mod.rs
+++ b/src/librustc_infer/infer/mod.rs
@@ -15,6 +15,8 @@
 use rustc::middle::free_region::RegionRelations;
 use rustc::middle::lang_items;
 use rustc::middle::region;
+use rustc::mir;
+use rustc::mir::interpret::ConstEvalResult;
 use rustc::session::config::BorrowckMode;
 use rustc::ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
 use rustc::ty::fold::{TypeFoldable, TypeFolder};
@@ -63,6 +65,7 @@
 mod sub;
 pub mod type_variable;
 
+use crate::infer::canonical::OriginalQueryValues;
 pub use rustc::infer::unify_key;
 
 #[must_use]
@@ -1563,6 +1566,35 @@
         self.universe.set(u);
         u
     }
+
+    /// Resolves and evaluates a constant.
+    ///
+    /// The constant can be located on a trait like `<A as B>::C`, in which case the given
+    /// substitutions and environment are used to resolve the constant. Alternatively if the
+    /// constant has generic parameters in scope the substitutions are used to evaluate the value of
+    /// the constant. For example in `fn foo<T>() { let _ = [0; bar::<T>()]; }` the repeat count
+    /// constant `bar::<T>()` requires a substitution for `T`, if the substitution for `T` is still
+    /// too generic for the constant to be evaluated then `Err(ErrorHandled::TooGeneric)` is
+    /// returned.
+    ///
+    /// This handles inferences variables within both `param_env` and `substs` by
+    /// performing the operation on their respective canonical forms.
+    pub fn const_eval_resolve(
+        &self,
+        param_env: ty::ParamEnv<'tcx>,
+        def_id: DefId,
+        substs: SubstsRef<'tcx>,
+        promoted: Option<mir::Promoted>,
+        span: Option<Span>,
+    ) -> ConstEvalResult<'tcx> {
+        let mut original_values = OriginalQueryValues::default();
+        let canonical = self.canonicalize_query(&(param_env, substs), &mut original_values);
+
+        let (param_env, substs) = canonical.value;
+        // The return value is the evaluated value which doesn't contain any reference to inference
+        // variables, thus we don't need to substitute back the original values.
+        self.tcx.const_eval_resolve(param_env, def_id, substs, promoted, span)
+    }
 }
 
 pub struct ShallowResolver<'a, 'tcx> {
diff --git a/src/librustc_infer/traits/fulfill.rs b/src/librustc_infer/traits/fulfill.rs
index 6055b0e..28d3f26 100644
--- a/src/librustc_infer/traits/fulfill.rs
+++ b/src/librustc_infer/traits/fulfill.rs
@@ -510,27 +510,15 @@
             }
 
             ty::Predicate::ConstEvaluatable(def_id, substs) => {
-                if obligation.param_env.has_local_value() {
-                    ProcessResult::Unchanged
-                } else {
-                    if !substs.has_local_value() {
-                        match self.selcx.tcx().const_eval_resolve(
-                            obligation.param_env,
-                            def_id,
-                            substs,
-                            None,
-                            Some(obligation.cause.span),
-                        ) {
-                            Ok(_) => ProcessResult::Changed(vec![]),
-                            Err(err) => {
-                                ProcessResult::Error(CodeSelectionError(ConstEvalFailure(err)))
-                            }
-                        }
-                    } else {
-                        pending_obligation.stalled_on =
-                            substs.types().map(|ty| infer_ty(ty)).collect();
-                        ProcessResult::Unchanged
-                    }
+                match self.selcx.infcx().const_eval_resolve(
+                    obligation.param_env,
+                    def_id,
+                    substs,
+                    None,
+                    Some(obligation.cause.span),
+                ) {
+                    Ok(_) => ProcessResult::Changed(vec![]),
+                    Err(err) => ProcessResult::Error(CodeSelectionError(ConstEvalFailure(err))),
                 }
             }
         }
diff --git a/src/librustc_infer/traits/select.rs b/src/librustc_infer/traits/select.rs
index 4eac891..c6878fa 100644
--- a/src/librustc_infer/traits/select.rs
+++ b/src/librustc_infer/traits/select.rs
@@ -532,20 +532,15 @@
             }
 
             ty::Predicate::ConstEvaluatable(def_id, substs) => {
-                if !(obligation.param_env, substs).has_local_value() {
-                    match self.tcx().const_eval_resolve(
-                        obligation.param_env,
-                        def_id,
-                        substs,
-                        None,
-                        None,
-                    ) {
-                        Ok(_) => Ok(EvaluatedToOk),
-                        Err(_) => Ok(EvaluatedToErr),
-                    }
-                } else {
-                    // Inference variables still left in param_env or substs.
-                    Ok(EvaluatedToAmbig)
+                match self.tcx().const_eval_resolve(
+                    obligation.param_env,
+                    def_id,
+                    substs,
+                    None,
+                    None,
+                ) {
+                    Ok(_) => Ok(EvaluatedToOk),
+                    Err(_) => Ok(EvaluatedToErr),
                 }
             }
         }
diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs
index fc4ba4d..cce4b90 100644
--- a/src/librustc_mir/interpret/eval_context.rs
+++ b/src/librustc_mir/interpret/eval_context.rs
@@ -768,11 +768,7 @@
         } else {
             self.param_env
         };
-        let val = if let Some(promoted) = gid.promoted {
-            self.tcx.const_eval_promoted(param_env, gid.instance, promoted)?
-        } else {
-            self.tcx.const_eval_instance(param_env, gid.instance, Some(self.tcx.span))?
-        };
+        let val = self.tcx.const_eval_global_id(param_env, gid, Some(self.tcx.span))?;
 
         // Even though `ecx.const_eval` is called from `eval_const_to_op` we can never have a
         // recursion deeper than one level, because the `tcx.const_eval` above is guaranteed to not
diff --git a/src/test/incremental/const-generics/issue-68477.rs b/src/test/incremental/const-generics/issue-68477.rs
new file mode 100644
index 0000000..925931b
--- /dev/null
+++ b/src/test/incremental/const-generics/issue-68477.rs
@@ -0,0 +1,23 @@
+// edition:2018
+// revisions:rpass1
+#![feature(const_generics)]
+
+const FOO: usize = 1;
+
+struct Container<T> {
+    val: std::marker::PhantomData<T>,
+    blah: [(); FOO]
+}
+
+async fn dummy() {}
+
+async fn foo() {
+    let a: Container<&'static ()>;
+    dummy().await;
+}
+
+fn is_send<T: Send>(_: T) {}
+
+fn main() {
+    is_send(foo());
+}