rust/src/librustc_mir/build/expr/as_constant.rs

42 lines
1.1 KiB
Rust
Raw Normal View History

2015-08-18 23:59:21 +02:00
//! See docs in build/expr/mod.rs
use build::Builder;
2015-08-18 23:59:21 +02:00
use hair::*;
2016-09-19 22:50:00 +02:00
use rustc::mir::*;
2015-08-18 23:59:21 +02:00
impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
2015-08-18 23:59:21 +02:00
/// Compile `expr`, yielding a compile-time constant. Assumes that
/// `expr` is a valid compile-time constant!
pub fn as_constant<M>(&mut self, expr: M) -> Constant<'tcx>
2018-09-06 23:34:26 +02:00
where
M: Mirror<'tcx, Output = Expr<'tcx>>,
2015-08-18 23:59:21 +02:00
{
let expr = self.hir.mirror(expr);
self.expr_as_constant(expr)
}
fn expr_as_constant(&mut self, expr: Expr<'tcx>) -> Constant<'tcx> {
2015-08-18 23:59:21 +02:00
let this = self;
2018-09-06 23:34:26 +02:00
let Expr {
ty,
temp_lifetime: _,
span,
kind,
} = expr;
match kind {
2018-09-06 23:34:26 +02:00
ExprKind::Scope {
region_scope: _,
lint_level: _,
value,
} => this.as_constant(value),
ExprKind::Literal { literal, user_ty } => Constant {
span,
ty,
user_ty,
literal,
},
_ => span_bug!(span, "expression is not a valid constant {:?}", kind),
}
2015-08-18 23:59:21 +02:00
}
}