]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_middle/src/thir/abstract_const.rs
New upstream version 1.57.0+dfsg1
[rustc.git] / compiler / rustc_middle / src / thir / abstract_const.rs
CommitLineData
c295e0f8
XL
1//! A subset of a mir body used for const evaluatability checking.
2use crate::mir;
3use crate::ty::{self, Ty, TyCtxt};
4use rustc_errors::ErrorReported;
5
6rustc_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)]
15pub 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)]
24pub enum Node<'tcx> {
25 Leaf(&'tcx 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)]
33pub enum NotConstEvaluatable {
34 Error(ErrorReported),
35 MentionsInfer,
36 MentionsParam,
37}
38
39impl From<ErrorReported> for NotConstEvaluatable {
40 fn from(e: ErrorReported) -> NotConstEvaluatable {
41 NotConstEvaluatable::Error(e)
42 }
43}
44
45TrivialTypeFoldableAndLiftImpls! {
46 NotConstEvaluatable,
47}
48
49impl<'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>]>, ErrorReported> {
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}