]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/const_evaluatable.rs
fdb87c085b54ef377ba7306c1d4e44a548812087
[rust.git] / compiler / rustc_trait_selection / src / traits / const_evaluatable.rs
1 use rustc_hir::def::DefKind;
2 use rustc_infer::infer::InferCtxt;
3 use rustc_middle::mir::interpret::ErrorHandled;
4 use rustc_middle::ty::subst::SubstsRef;
5 use rustc_middle::ty::{self, TypeFoldable};
6 use rustc_session::lint;
7 use rustc_span::def_id::DefId;
8 use rustc_span::Span;
9
10 pub fn is_const_evaluatable<'cx, 'tcx>(
11     infcx: &InferCtxt<'cx, 'tcx>,
12     def: ty::WithOptConstParam<DefId>,
13     substs: SubstsRef<'tcx>,
14     param_env: ty::ParamEnv<'tcx>,
15     span: Span,
16 ) -> Result<(), ErrorHandled> {
17     debug!("is_const_evaluatable({:?}, {:?})", def, substs);
18     if infcx.tcx.features().const_evaluatable_checked {
19         // FIXME(const_evaluatable_checked): Actually look into generic constants to
20         // implement const equality.
21         for pred in param_env.caller_bounds() {
22             match pred.skip_binders() {
23                 ty::PredicateAtom::ConstEvaluatable(b_def, b_substs) => {
24                     debug!("is_const_evaluatable: caller_bound={:?}, {:?}", b_def, b_substs);
25                     if b_def == def && b_substs == substs {
26                         debug!("is_const_evaluatable: caller_bound ~~> ok");
27                         return Ok(());
28                     }
29                 }
30                 _ => {} // don't care
31             }
32         }
33     }
34
35     let future_compat_lint = || {
36         if let Some(local_def_id) = def.did.as_local() {
37             infcx.tcx.struct_span_lint_hir(
38                 lint::builtin::CONST_EVALUATABLE_UNCHECKED,
39                 infcx.tcx.hir().local_def_id_to_hir_id(local_def_id),
40                 span,
41                 |err| {
42                     err.build("cannot use constants which depend on generic parameters in types")
43                         .emit();
44                 },
45             );
46         }
47     };
48
49     // FIXME: We should only try to evaluate a given constant here if it is fully concrete
50     // as we don't want to allow things like `[u8; std::mem::size_of::<*mut T>()]`.
51     //
52     // We previously did not check this, so we only emit a future compat warning if
53     // const evaluation succeeds and the given constant is still polymorphic for now
54     // and hopefully soon change this to an error.
55     //
56     // See #74595 for more details about this.
57     let concrete = infcx.const_eval_resolve(param_env, def, substs, None, Some(span));
58
59     if concrete.is_ok() && substs.has_param_types_or_consts() {
60         match infcx.tcx.def_kind(def.did) {
61             DefKind::AnonConst => {
62                 let mir_body = if let Some(def) = def.as_const_arg() {
63                     infcx.tcx.optimized_mir_of_const_arg(def)
64                 } else {
65                     infcx.tcx.optimized_mir(def.did)
66                 };
67
68                 if mir_body.is_polymorphic {
69                     future_compat_lint();
70                 }
71             }
72             _ => future_compat_lint(),
73         }
74     }
75
76     debug!(?concrete, "is_const_evaluatable");
77     concrete.map(drop)
78 }