abstract_const.rs 1.4 KB
Newer Older
B
Bastian Kauschke 已提交
1
//! A subset of a mir body used for const evaluatability checking.
E
Ellen 已提交
2
use crate::mir;
E
nits  
Ellen 已提交
3 4
use crate::ty::{self, Ty, TyCtxt};
use rustc_errors::ErrorReported;
B
Bastian Kauschke 已提交
5

6 7 8 9 10 11 12
rustc_index::newtype_index! {
    /// An index into an `AbstractConst`.
    pub struct NodeId {
        derive [HashStable]
        DEBUG_FORMAT = "n{}",
    }
}
B
Bastian Kauschke 已提交
13 14

/// A node of an `AbstractConst`.
15
#[derive(Debug, Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
B
Bastian Kauschke 已提交
16 17 18 19 20
pub enum Node<'tcx> {
    Leaf(&'tcx ty::Const<'tcx>),
    Binop(mir::BinOp, NodeId, NodeId),
    UnaryOp(mir::UnOp, NodeId),
    FunctionCall(NodeId, &'tcx [NodeId]),
E
Ellen 已提交
21
    Cast(NodeId, Ty<'tcx>),
B
Bastian Kauschke 已提交
22
}
E
Ellen 已提交
23 24 25

#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
pub enum NotConstEvaluatable {
E
nits  
Ellen 已提交
26
    Error(ErrorReported),
E
Ellen 已提交
27 28 29 30
    MentionsInfer,
    MentionsParam,
}

E
nits  
Ellen 已提交
31 32
impl From<ErrorReported> for NotConstEvaluatable {
    fn from(e: ErrorReported) -> NotConstEvaluatable {
E
Ellen 已提交
33 34 35 36 37 38 39
        NotConstEvaluatable::Error(e)
    }
}

TrivialTypeFoldableAndLiftImpls! {
    NotConstEvaluatable,
}
E
nits  
Ellen 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53

impl<'tcx> TyCtxt<'tcx> {
    #[inline]
    pub fn thir_abstract_const_opt_const_arg(
        self,
        def: ty::WithOptConstParam<rustc_hir::def_id::DefId>,
    ) -> Result<Option<&'tcx [Node<'tcx>]>, ErrorReported> {
        if let Some((did, param_did)) = def.as_const_arg() {
            self.thir_abstract_const_of_const_arg((did, param_did))
        } else {
            self.thir_abstract_const(def.did)
        }
    }
}