Skip to main content

rustc_const_eval/
lib.rs

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