2015-08-18 23:59:21 +02:00
|
|
|
//! See docs in build/expr/mod.rs
|
|
|
|
|
2015-10-07 14:37:42 +02:00
|
|
|
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
|
|
|
|
2016-05-11 03:14:41 +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!
|
2015-10-05 18:31:48 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-10-05 18:31:48 +02:00
|
|
|
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;
|
2015-09-10 21:44:44 +02:00
|
|
|
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-09-10 21:44:44 +02:00
|
|
|
}
|
2015-08-18 23:59:21 +02:00
|
|
|
}
|
|
|
|
}
|