2015-08-18 23:59:21 +02:00
|
|
|
//! See docs in build/expr/mod.rs
|
|
|
|
|
2019-02-07 22:28:15 +01:00
|
|
|
use crate::build::Builder;
|
|
|
|
use crate::hair::*;
|
2016-09-19 22:50:00 +02:00
|
|
|
use rustc::mir::*;
|
2019-01-06 18:10:53 +01:00
|
|
|
use rustc::ty::CanonicalUserTypeAnnotation;
|
2015-08-18 23:59:21 +02:00
|
|
|
|
2019-06-01 13:38:36 +02:00
|
|
|
impl<'a, 'tcx> Builder<'a, '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),
|
2018-11-16 22:56:18 +01:00
|
|
|
ExprKind::Literal { literal, user_ty } => {
|
2019-01-12 15:55:23 +01:00
|
|
|
let user_ty = user_ty.map(|user_ty| {
|
2019-01-06 18:10:53 +01:00
|
|
|
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
|
|
|
|
span,
|
2019-01-12 15:55:23 +01:00
|
|
|
user_ty,
|
|
|
|
inferred_ty: ty,
|
2019-01-06 18:10:53 +01:00
|
|
|
})
|
2018-11-16 22:56:18 +01:00
|
|
|
});
|
2019-08-12 17:15:45 +02:00
|
|
|
assert_eq!(literal.ty, ty);
|
2018-11-16 22:56:18 +01:00
|
|
|
Constant {
|
|
|
|
span,
|
|
|
|
user_ty,
|
|
|
|
literal,
|
|
|
|
}
|
2018-09-06 23:34:26 +02:00
|
|
|
},
|
|
|
|
_ => 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
|
|
|
}
|
|
|
|
}
|