]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/thir/abstract_const.rs
Rollup merge of #93412 - fee1-dead:improve-rustdoc-const-bounds, r=GuillaumeGomez
[rust.git] / compiler / rustc_middle / src / thir / abstract_const.rs
1 //! A subset of a mir body used for const evaluatability checking.
2 use crate::mir;
3 use crate::ty::{self, Ty, TyCtxt};
4 use rustc_errors::ErrorGuaranteed;
5
6 rustc_index::newtype_index! {
7     /// An index into an `AbstractConst`.
8     pub struct NodeId {
9         derive [HashStable]
10         DEBUG_FORMAT = "n{}",
11     }
12 }
13
14 #[derive(Debug, Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
15 pub enum CastKind {
16     /// thir::ExprKind::As
17     As,
18     /// thir::ExprKind::Use
19     Use,
20 }
21
22 /// A node of an `AbstractConst`.
23 #[derive(Debug, Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
24 pub enum Node<'tcx> {
25     Leaf(ty::Const<'tcx>),
26     Binop(mir::BinOp, NodeId, NodeId),
27     UnaryOp(mir::UnOp, NodeId),
28     FunctionCall(NodeId, &'tcx [NodeId]),
29     Cast(CastKind, NodeId, Ty<'tcx>),
30 }
31
32 #[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
33 pub enum NotConstEvaluatable {
34     Error(ErrorGuaranteed),
35     MentionsInfer,
36     MentionsParam,
37 }
38
39 impl From<ErrorGuaranteed> for NotConstEvaluatable {
40     fn from(e: ErrorGuaranteed) -> NotConstEvaluatable {
41         NotConstEvaluatable::Error(e)
42     }
43 }
44
45 TrivialTypeFoldableAndLiftImpls! {
46     NotConstEvaluatable,
47 }
48
49 impl<'tcx> TyCtxt<'tcx> {
50     #[inline]
51     pub fn thir_abstract_const_opt_const_arg(
52         self,
53         def: ty::WithOptConstParam<rustc_hir::def_id::DefId>,
54     ) -> Result<Option<&'tcx [Node<'tcx>]>, ErrorGuaranteed> {
55         if let Some((did, param_did)) = def.as_const_arg() {
56             self.thir_abstract_const_of_const_arg((did, param_did))
57         } else {
58             self.thir_abstract_const(def.did)
59         }
60     }
61 }