From 2d67edd32c71ac32c8dafb7106e7187993750629 Mon Sep 17 00:00:00 2001
From: Oliver Scherer <github35764891676564198441@oli-obk.de>
Date: Wed, 25 Dec 2019 01:06:51 +0100
Subject: [PATCH] Move const eval query components into their own module

---
 src/librustc_mir/const_eval.rs       | 230 +--------------------------
 src/librustc_mir/const_eval/error.rs |  16 +-
 src/librustc_mir/const_eval/query.rs | 216 +++++++++++++++++++++++++
 3 files changed, 234 insertions(+), 228 deletions(-)
 create mode 100644 src/librustc_mir/const_eval/query.rs

diff --git a/src/librustc_mir/const_eval.rs b/src/librustc_mir/const_eval.rs
index 833547d4d05..c308bf8a46c 100644
--- a/src/librustc_mir/const_eval.rs
+++ b/src/librustc_mir/const_eval.rs
@@ -5,12 +5,8 @@ use std::error::Error;
 use std::fmt;
 use std::hash::Hash;
 
-use crate::interpret::eval_nullary_intrinsic;
-use crate::interpret::eval_nullary_intrinsic;
-use rustc::hir::def::DefKind;
 use rustc::mir;
-use rustc::mir::interpret::{ConstEvalErr, ErrorHandled, ScalarMaybeUndef};
-use rustc::traits::Reveal;
+use rustc::mir::interpret::ScalarMaybeUndef;
 use rustc::ty::layout::{self, LayoutOf, VariantIdx};
 use rustc::ty::{self, subst::Subst, TyCtxt};
 
@@ -21,15 +17,14 @@ use syntax::{
 
 use crate::interpret::{
     intern_const_alloc_recursive, Allocation, ConstValue, GlobalId, ImmTy, Immediate, InterpCx,
-    InterpErrorInfo, InterpResult, MPlaceTy, Machine, MemoryKind, OpTy, RawConst, RefTracking,
-    Scalar, StackPopCleanup,
+    InterpResult, MPlaceTy, MemoryKind, OpTy, Scalar, StackPopCleanup,
 };
 
 mod error;
-mod machine;
+mod query;
 
 pub use error::*;
-pub use machine::*;
+pub use query::*;
 
 /// The `InterpCx` is only meant to be used to do field and index projections into constants for
 /// `simd_shuffle` and const patterns in match arms.
@@ -225,220 +220,3 @@ pub fn const_variant_index<'tcx>(
     let op = ecx.eval_const_to_op(val, None).unwrap();
     ecx.read_discriminant(op).unwrap().1
 }
-
-/// Turn an interpreter error into something to report to the user.
-/// As a side-effect, if RUSTC_CTFE_BACKTRACE is set, this prints the backtrace.
-/// Should be called only if the error is actually going to to be reported!
-pub fn error_to_const_error<'mir, 'tcx, M: Machine<'mir, 'tcx>>(
-    ecx: &InterpCx<'mir, 'tcx, M>,
-    mut error: InterpErrorInfo<'tcx>,
-) -> ConstEvalErr<'tcx> {
-    error.print_backtrace();
-    let stacktrace = ecx.generate_stacktrace(None);
-    ConstEvalErr { error: error.kind, stacktrace, span: ecx.tcx.span }
-}
-
-pub fn note_on_undefined_behavior_error() -> &'static str {
-    "The rules on what exactly is undefined behavior aren't clear, \
-     so this check might be overzealous. Please open an issue on the rustc \
-     repository if you believe it should not be considered undefined behavior."
-}
-
-fn validate_and_turn_into_const<'tcx>(
-    tcx: TyCtxt<'tcx>,
-    constant: RawConst<'tcx>,
-    key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
-) -> ::rustc::mir::interpret::ConstEvalResult<'tcx> {
-    let cid = key.value;
-    let def_id = cid.instance.def.def_id();
-    let is_static = tcx.is_static(def_id);
-    let ecx = mk_eval_cx(tcx, tcx.def_span(key.value.instance.def_id()), key.param_env, is_static);
-    let val = (|| {
-        let mplace = ecx.raw_const_to_mplace(constant)?;
-        let mut ref_tracking = RefTracking::new(mplace);
-        while let Some((mplace, path)) = ref_tracking.todo.pop() {
-            ecx.validate_operand(mplace.into(), path, Some(&mut ref_tracking))?;
-        }
-        // Now that we validated, turn this into a proper constant.
-        // Statics/promoteds are always `ByRef`, for the rest `op_to_const` decides
-        // whether they become immediates.
-        if is_static || cid.promoted.is_some() {
-            let ptr = mplace.ptr.to_ptr()?;
-            Ok(tcx.mk_const(ty::Const {
-                val: ty::ConstKind::Value(ConstValue::ByRef {
-                    alloc: ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id),
-                    offset: ptr.offset,
-                }),
-                ty: mplace.layout.ty,
-            }))
-        } else {
-            Ok(op_to_const(&ecx, mplace.into()))
-        }
-    })();
-
-    val.map_err(|error| {
-        let err = error_to_const_error(&ecx, error);
-        match err.struct_error(ecx.tcx, "it is undefined behavior to use this value") {
-            Ok(mut diag) => {
-                diag.note(note_on_undefined_behavior_error());
-                diag.emit();
-                ErrorHandled::Reported
-            }
-            Err(err) => err,
-        }
-    })
-}
-
-pub fn const_eval_validated_provider<'tcx>(
-    tcx: TyCtxt<'tcx>,
-    key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
-) -> ::rustc::mir::interpret::ConstEvalResult<'tcx> {
-    // see comment in const_eval_raw_provider for what we're doing here
-    if key.param_env.reveal == Reveal::All {
-        let mut key = key.clone();
-        key.param_env.reveal = Reveal::UserFacing;
-        match tcx.const_eval_validated(key) {
-            // try again with reveal all as requested
-            Err(ErrorHandled::TooGeneric) => {
-                // Promoteds should never be "too generic" when getting evaluated.
-                // They either don't get evaluated, or we are in a monomorphic context
-                assert!(key.value.promoted.is_none());
-            }
-            // dedupliate calls
-            other => return other,
-        }
-    }
-
-    // We call `const_eval` for zero arg intrinsics, too, in order to cache their value.
-    // Catch such calls and evaluate them instead of trying to load a constant's MIR.
-    if let ty::InstanceDef::Intrinsic(def_id) = key.value.instance.def {
-        let ty = key.value.instance.ty(tcx);
-        let substs = match ty.kind {
-            ty::FnDef(_, substs) => substs,
-            _ => bug!("intrinsic with type {:?}", ty),
-        };
-        return eval_nullary_intrinsic(tcx, key.param_env, def_id, substs).map_err(|error| {
-            let span = tcx.def_span(def_id);
-            let error = ConstEvalErr { error: error.kind, stacktrace: vec![], span };
-            error.report_as_error(tcx.at(span), "could not evaluate nullary intrinsic")
-        });
-    }
-
-    tcx.const_eval_raw(key).and_then(|val| validate_and_turn_into_const(tcx, val, key))
-}
-
-pub fn const_eval_raw_provider<'tcx>(
-    tcx: TyCtxt<'tcx>,
-    key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
-) -> ::rustc::mir::interpret::ConstEvalRawResult<'tcx> {
-    // Because the constant is computed twice (once per value of `Reveal`), we are at risk of
-    // reporting the same error twice here. To resolve this, we check whether we can evaluate the
-    // constant in the more restrictive `Reveal::UserFacing`, which most likely already was
-    // computed. For a large percentage of constants that will already have succeeded. Only
-    // associated constants of generic functions will fail due to not enough monomorphization
-    // information being available.
-
-    // In case we fail in the `UserFacing` variant, we just do the real computation.
-    if key.param_env.reveal == Reveal::All {
-        let mut key = key.clone();
-        key.param_env.reveal = Reveal::UserFacing;
-        match tcx.const_eval_raw(key) {
-            // try again with reveal all as requested
-            Err(ErrorHandled::TooGeneric) => {}
-            // dedupliate calls
-            other => return other,
-        }
-    }
-    if cfg!(debug_assertions) {
-        // Make sure we format the instance even if we do not print it.
-        // This serves as a regression test against an ICE on printing.
-        // The next two lines concatenated contain some discussion:
-        // https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/
-        // subject/anon_const_instance_printing/near/135980032
-        let instance = key.value.instance.to_string();
-        trace!("const eval: {:?} ({})", key, instance);
-    }
-
-    let cid = key.value;
-    let def_id = cid.instance.def.def_id();
-
-    if def_id.is_local() && tcx.typeck_tables_of(def_id).tainted_by_errors {
-        return Err(ErrorHandled::Reported);
-    }
-
-    let is_static = tcx.is_static(def_id);
-
-    let span = tcx.def_span(cid.instance.def_id());
-    let mut ecx = InterpCx::new(
-        tcx.at(span),
-        key.param_env,
-        CompileTimeInterpreter::new(),
-        MemoryExtra { can_access_statics: is_static },
-    );
-
-    let res = ecx.load_mir(cid.instance.def, cid.promoted);
-    res.and_then(|body| eval_body_using_ecx(&mut ecx, cid, *body))
-        .and_then(|place| {
-            Ok(RawConst { alloc_id: place.ptr.assert_ptr().alloc_id, ty: place.layout.ty })
-        })
-        .map_err(|error| {
-            let err = error_to_const_error(&ecx, error);
-            // errors in statics are always emitted as fatal errors
-            if is_static {
-                // Ensure that if the above error was either `TooGeneric` or `Reported`
-                // an error must be reported.
-                let v = err.report_as_error(ecx.tcx, "could not evaluate static initializer");
-                tcx.sess.delay_span_bug(
-                    err.span,
-                    &format!("static eval failure did not emit an error: {:#?}", v),
-                );
-                v
-            } else if def_id.is_local() {
-                // constant defined in this crate, we can figure out a lint level!
-                match tcx.def_kind(def_id) {
-                    // constants never produce a hard error at the definition site. Anything else is
-                    // a backwards compatibility hazard (and will break old versions of winapi for sure)
-                    //
-                    // note that validation may still cause a hard error on this very same constant,
-                    // because any code that existed before validation could not have failed validation
-                    // thus preventing such a hard error from being a backwards compatibility hazard
-                    Some(DefKind::Const) | Some(DefKind::AssocConst) => {
-                        let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
-                        err.report_as_lint(
-                            tcx.at(tcx.def_span(def_id)),
-                            "any use of this value will cause an error",
-                            hir_id,
-                            Some(err.span),
-                        )
-                    }
-                    // promoting runtime code is only allowed to error if it references broken constants
-                    // any other kind of error will be reported to the user as a deny-by-default lint
-                    _ => {
-                        if let Some(p) = cid.promoted {
-                            let span = tcx.promoted_mir(def_id)[p].span;
-                            if let err_inval!(ReferencedConstant) = err.error {
-                                err.report_as_error(
-                                    tcx.at(span),
-                                    "evaluation of constant expression failed",
-                                )
-                            } else {
-                                err.report_as_lint(
-                                    tcx.at(span),
-                                    "reaching this expression at runtime will panic or abort",
-                                    tcx.hir().as_local_hir_id(def_id).unwrap(),
-                                    Some(err.span),
-                                )
-                            }
-                        // anything else (array lengths, enum initializers, constant patterns) are reported
-                        // as hard errors
-                        } else {
-                            err.report_as_error(ecx.tcx, "evaluation of constant value failed")
-                        }
-                    }
-                }
-            } else {
-                // use of broken constant from other crate
-                err.report_as_error(ecx.tcx, "could not evaluate constant")
-            }
-        })
-}
diff --git a/src/librustc_mir/const_eval/error.rs b/src/librustc_mir/const_eval/error.rs
index 8948cc0fc3e..c2db3c31f85 100644
--- a/src/librustc_mir/const_eval/error.rs
+++ b/src/librustc_mir/const_eval/error.rs
@@ -1,8 +1,8 @@
 use std::error::Error;
 use std::fmt;
 
-use crate::interpret::InterpErrorInfo;
-
+use super::InterpCx;
+use crate::interpret::{ConstEvalErr, InterpErrorInfo, Machine};
 #[derive(Clone, Debug)]
 pub enum ConstEvalError {
     NeedsRfc(String),
@@ -28,3 +28,15 @@ impl fmt::Display for ConstEvalError {
 }
 
 impl Error for ConstEvalError {}
+
+/// Turn an interpreter error into something to report to the user.
+/// As a side-effect, if RUSTC_CTFE_BACKTRACE is set, this prints the backtrace.
+/// Should be called only if the error is actually going to to be reported!
+pub fn error_to_const_error<'mir, 'tcx, M: Machine<'mir, 'tcx>>(
+    ecx: &InterpCx<'mir, 'tcx, M>,
+    mut error: InterpErrorInfo<'tcx>,
+) -> ConstEvalErr<'tcx> {
+    error.print_backtrace();
+    let stacktrace = ecx.generate_stacktrace(None);
+    ConstEvalErr { error: error.kind, stacktrace, span: ecx.tcx.span }
+}
diff --git a/src/librustc_mir/const_eval/query.rs b/src/librustc_mir/const_eval/query.rs
new file mode 100644
index 00000000000..0a33143e8c6
--- /dev/null
+++ b/src/librustc_mir/const_eval/query.rs
@@ -0,0 +1,216 @@
+use crate::interpret::eval_nullary_intrinsic;
+use rustc::hir::def::DefKind;
+use rustc::mir::interpret::{ConstEvalErr, ErrorHandled};
+use rustc::traits::Reveal;
+use rustc::ty::{self, TyCtxt};
+
+use crate::interpret::{ConstValue, GlobalId, InterpCx, RawConst, RefTracking};
+
+use super::{
+    error_to_const_error, eval_body_using_ecx, mk_eval_cx, op_to_const, CompileTimeInterpreter,
+};
+
+pub fn note_on_undefined_behavior_error() -> &'static str {
+    "The rules on what exactly is undefined behavior aren't clear, \
+     so this check might be overzealous. Please open an issue on the rustc \
+     repository if you believe it should not be considered undefined behavior."
+}
+
+fn validate_and_turn_into_const<'tcx>(
+    tcx: TyCtxt<'tcx>,
+    constant: RawConst<'tcx>,
+    key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
+) -> ::rustc::mir::interpret::ConstEvalResult<'tcx> {
+    let cid = key.value;
+    let def_id = cid.instance.def.def_id();
+    let is_static = tcx.is_static(def_id);
+    let ecx = mk_eval_cx(tcx, tcx.def_span(key.value.instance.def_id()), key.param_env, is_static);
+    let val = (|| {
+        let mplace = ecx.raw_const_to_mplace(constant)?;
+        let mut ref_tracking = RefTracking::new(mplace);
+        while let Some((mplace, path)) = ref_tracking.todo.pop() {
+            ecx.validate_operand(mplace.into(), path, Some(&mut ref_tracking))?;
+        }
+        // Now that we validated, turn this into a proper constant.
+        // Statics/promoteds are always `ByRef`, for the rest `op_to_const` decides
+        // whether they become immediates.
+        if is_static || cid.promoted.is_some() {
+            let ptr = mplace.ptr.to_ptr()?;
+            Ok(tcx.mk_const(ty::Const {
+                val: ty::ConstKind::Value(ConstValue::ByRef {
+                    alloc: ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id),
+                    offset: ptr.offset,
+                }),
+                ty: mplace.layout.ty,
+            }))
+        } else {
+            Ok(op_to_const(&ecx, mplace.into()))
+        }
+    })();
+
+    val.map_err(|error| {
+        let err = error_to_const_error(&ecx, error);
+        match err.struct_error(ecx.tcx, "it is undefined behavior to use this value") {
+            Ok(mut diag) => {
+                diag.note(note_on_undefined_behavior_error());
+                diag.emit();
+                ErrorHandled::Reported
+            }
+            Err(err) => err,
+        }
+    })
+}
+
+pub fn const_eval_validated_provider<'tcx>(
+    tcx: TyCtxt<'tcx>,
+    key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
+) -> ::rustc::mir::interpret::ConstEvalResult<'tcx> {
+    // see comment in const_eval_raw_provider for what we're doing here
+    if key.param_env.reveal == Reveal::All {
+        let mut key = key.clone();
+        key.param_env.reveal = Reveal::UserFacing;
+        match tcx.const_eval_validated(key) {
+            // try again with reveal all as requested
+            Err(ErrorHandled::TooGeneric) => {
+                // Promoteds should never be "too generic" when getting evaluated.
+                // They either don't get evaluated, or we are in a monomorphic context
+                assert!(key.value.promoted.is_none());
+            }
+            // dedupliate calls
+            other => return other,
+        }
+    }
+
+    // We call `const_eval` for zero arg intrinsics, too, in order to cache their value.
+    // Catch such calls and evaluate them instead of trying to load a constant's MIR.
+    if let ty::InstanceDef::Intrinsic(def_id) = key.value.instance.def {
+        let ty = key.value.instance.ty(tcx);
+        let substs = match ty.kind {
+            ty::FnDef(_, substs) => substs,
+            _ => bug!("intrinsic with type {:?}", ty),
+        };
+        return eval_nullary_intrinsic(tcx, key.param_env, def_id, substs).map_err(|error| {
+            let span = tcx.def_span(def_id);
+            let error = ConstEvalErr { error: error.kind, stacktrace: vec![], span };
+            error.report_as_error(tcx.at(span), "could not evaluate nullary intrinsic")
+        });
+    }
+
+    tcx.const_eval_raw(key).and_then(|val| validate_and_turn_into_const(tcx, val, key))
+}
+
+pub fn const_eval_raw_provider<'tcx>(
+    tcx: TyCtxt<'tcx>,
+    key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
+) -> ::rustc::mir::interpret::ConstEvalRawResult<'tcx> {
+    // Because the constant is computed twice (once per value of `Reveal`), we are at risk of
+    // reporting the same error twice here. To resolve this, we check whether we can evaluate the
+    // constant in the more restrictive `Reveal::UserFacing`, which most likely already was
+    // computed. For a large percentage of constants that will already have succeeded. Only
+    // associated constants of generic functions will fail due to not enough monomorphization
+    // information being available.
+
+    // In case we fail in the `UserFacing` variant, we just do the real computation.
+    if key.param_env.reveal == Reveal::All {
+        let mut key = key.clone();
+        key.param_env.reveal = Reveal::UserFacing;
+        match tcx.const_eval_raw(key) {
+            // try again with reveal all as requested
+            Err(ErrorHandled::TooGeneric) => {}
+            // dedupliate calls
+            other => return other,
+        }
+    }
+    if cfg!(debug_assertions) {
+        // Make sure we format the instance even if we do not print it.
+        // This serves as a regression test against an ICE on printing.
+        // The next two lines concatenated contain some discussion:
+        // https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/
+        // subject/anon_const_instance_printing/near/135980032
+        let instance = key.value.instance.to_string();
+        trace!("const eval: {:?} ({})", key, instance);
+    }
+
+    let cid = key.value;
+    let def_id = cid.instance.def.def_id();
+
+    if def_id.is_local() && tcx.typeck_tables_of(def_id).tainted_by_errors {
+        return Err(ErrorHandled::Reported);
+    }
+
+    let is_static = tcx.is_static(def_id);
+
+    let span = tcx.def_span(cid.instance.def_id());
+    let mut ecx = InterpCx::new(
+        tcx.at(span),
+        key.param_env,
+        CompileTimeInterpreter::new(),
+        MemoryExtra { can_access_statics: is_static },
+    );
+
+    let res = ecx.load_mir(cid.instance.def, cid.promoted);
+    res.and_then(|body| eval_body_using_ecx(&mut ecx, cid, *body))
+        .and_then(|place| {
+            Ok(RawConst { alloc_id: place.ptr.assert_ptr().alloc_id, ty: place.layout.ty })
+        })
+        .map_err(|error| {
+            let err = error_to_const_error(&ecx, error);
+            // errors in statics are always emitted as fatal errors
+            if is_static {
+                // Ensure that if the above error was either `TooGeneric` or `Reported`
+                // an error must be reported.
+                let v = err.report_as_error(ecx.tcx, "could not evaluate static initializer");
+                tcx.sess.delay_span_bug(
+                    err.span,
+                    &format!("static eval failure did not emit an error: {:#?}", v),
+                );
+                v
+            } else if def_id.is_local() {
+                // constant defined in this crate, we can figure out a lint level!
+                match tcx.def_kind(def_id) {
+                    // constants never produce a hard error at the definition site. Anything else is
+                    // a backwards compatibility hazard (and will break old versions of winapi for sure)
+                    //
+                    // note that validation may still cause a hard error on this very same constant,
+                    // because any code that existed before validation could not have failed validation
+                    // thus preventing such a hard error from being a backwards compatibility hazard
+                    Some(DefKind::Const) | Some(DefKind::AssocConst) => {
+                        let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
+                        err.report_as_lint(
+                            tcx.at(tcx.def_span(def_id)),
+                            "any use of this value will cause an error",
+                            hir_id,
+                            Some(err.span),
+                        )
+                    }
+                    // promoting runtime code is only allowed to error if it references broken constants
+                    // any other kind of error will be reported to the user as a deny-by-default lint
+                    _ => {
+                        if let Some(p) = cid.promoted {
+                            let span = tcx.promoted_mir(def_id)[p].span;
+                            if let err_inval!(ReferencedConstant) = err.error {
+                                err.report_as_error(
+                                    tcx.at(span),
+                                    "evaluation of constant expression failed",
+                                )
+                            } else {
+                                err.report_as_lint(
+                                    tcx.at(span),
+                                    "reaching this expression at runtime will panic or abort",
+                                    tcx.hir().as_local_hir_id(def_id).unwrap(),
+                                    Some(err.span),
+                                )
+                            }
+                        // anything else (array lengths, enum initializers, constant patterns) are reported
+                        // as hard errors
+                        } else {
+                            err.report_as_error(ecx.tcx, "evaluation of constant value failed")
+                        }
+                    }
+                }
+            } else {
+                // use of broken constant from other crate
+                err.report_as_error(ecx.tcx, "could not evaluate constant")
+            }
+        })
+}