Skip to main content

rustc_const_eval/
lib.rs

1// tidy-alphabetical-start
2#![feature(array_try_map)]
3#![feature(decl_macro)]
4#![feature(deref_patterns)]
5#![feature(never_type)]
6#![feature(slice_ptr_get)]
7#![feature(trait_alias)]
8#![feature(unqualified_local_imports)]
9#![feature(yeet_expr)]
10#![warn(unqualified_local_imports)]
11// tidy-alphabetical-end
12
13pub mod check_consts;
14pub mod const_eval;
15mod errors;
16pub mod interpret;
17pub mod util;
18
19use std::sync::atomic::AtomicBool;
20
21use rustc_middle::util::Providers;
22use rustc_middle::{bug, ty};
23
24/// Const eval always happens in post analysis mode in order to be able to use the hidden types of
25/// opaque types. This is needed for trivial things like `size_of`, but also for using associated
26/// types that are not specified in the opaque type. We also use MIR bodies whose opaque types have
27/// already been revealed, so we'd be able to at least partially observe the hidden types anyways.
28fn assert_typing_mode(typing_mode: ty::TypingMode<'_>) {
29    if truecfg!(debug_assertions) {
30        match typing_mode.assert_not_erased() {
31            ty::TypingMode::PostAnalysis | ty::TypingMode::Codegen => {}
32            // Const eval always happens in PostAnalysis or Codegen mode. See the comment in
33            // `InterpCx::new` for more details.
34            ty::TypingMode::Coherence
35            | ty::TypingMode::Typeck { .. }
36            | ty::TypingMode::PostTypeckUntilBorrowck { .. }
37            | ty::TypingMode::PostBorrowck { .. } => ::rustc_middle::util::bug::bug_fmt(format_args!("Const eval should always happens in PostAnalysis or Codegen mode. See the comment on `assert_typing_mode` for more details."))bug!(
38                "Const eval should always happens in PostAnalysis or Codegen mode. See the comment on `assert_typing_mode` for more details."
39            ),
40        }
41    }
42}
43
44pub fn provide(providers: &mut Providers) {
45    const_eval::provide(&mut providers.queries);
46    providers.queries.tag_for_variant = const_eval::tag_for_variant_provider;
47    providers.queries.eval_to_const_value_raw = const_eval::eval_to_const_value_raw_provider;
48    providers.queries.eval_to_allocation_raw = const_eval::eval_to_allocation_raw_provider;
49    providers.queries.eval_static_initializer = const_eval::eval_static_initializer_provider;
50    providers.hooks.const_caller_location = util::caller_location::const_caller_location_provider;
51    providers.queries.eval_to_valtree = |tcx, ty::PseudoCanonicalInput { typing_env, value }| {
52        const_eval::eval_to_valtree(tcx, typing_env, value)
53    };
54    providers.hooks.try_destructure_mir_constant_for_user_output =
55        const_eval::try_destructure_mir_constant_for_user_output;
56    providers.queries.valtree_to_const_val =
57        |tcx, cv| const_eval::valtree_to_const_value(tcx, ty::TypingEnv::fully_monomorphized(), cv);
58    providers.queries.check_validity_requirement = |tcx, (init_kind, param_env_and_ty)| {
59        util::check_validity_requirement(tcx, init_kind, param_env_and_ty)
60    };
61    providers.hooks.validate_scalar_in_layout =
62        |tcx, scalar, layout| util::validate_scalar_in_layout(tcx, scalar, layout);
63}
64
65/// `rustc_driver::main` installs a handler that will set this to `true` if
66/// the compiler has been sent a request to shut down, such as by a Ctrl-C.
67/// This static lives here because it is only read by the interpreter.
68pub static CTRL_C_RECEIVED: AtomicBool = AtomicBool::new(false);