2015-08-18 17:59:21 -04:00
|
|
|
//! See docs in build/expr/mod.rs
|
|
|
|
|
2019-02-08 06:28:15 +09:00
|
|
|
use crate::build::Builder;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::*;
|
2021-04-04 18:42:17 +02:00
|
|
|
use rustc_middle::thir::*;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::CanonicalUserTypeAnnotation;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2019-06-01 13:38:36 +02:00
|
|
|
impl<'a, 'tcx> Builder<'a, 'tcx> {
|
2015-08-18 17:59:21 -04:00
|
|
|
/// Compile `expr`, yielding a compile-time constant. Assumes that
|
|
|
|
/// `expr` is a valid compile-time constant!
|
2021-04-03 19:58:46 +02:00
|
|
|
crate fn as_constant(&mut self, expr: &Expr<'tcx>) -> Constant<'tcx> {
|
2015-08-18 17:59:21 -04:00
|
|
|
let this = self;
|
2021-03-06 22:24:04 +01:00
|
|
|
let Expr { ty, temp_lifetime: _, span, ref kind } = *expr;
|
2021-03-08 16:18:03 +00:00
|
|
|
match *kind {
|
2021-04-03 19:58:46 +02:00
|
|
|
ExprKind::Scope { region_scope: _, lint_level: _, value } => {
|
|
|
|
this.as_constant(&this.thir[value])
|
|
|
|
}
|
2020-08-10 07:16:30 -04:00
|
|
|
ExprKind::Literal { literal, user_ty, const_id: _ } => {
|
2019-01-12 14:55:23 +00:00
|
|
|
let user_ty = user_ty.map(|user_ty| {
|
2019-01-06 17:10:53 +00:00
|
|
|
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
|
2021-03-06 22:24:04 +01:00
|
|
|
span,
|
2019-01-12 14:55:23 +00:00
|
|
|
user_ty,
|
|
|
|
inferred_ty: ty,
|
2019-01-06 17:10:53 +00:00
|
|
|
})
|
2018-11-16 22:56:18 +01:00
|
|
|
});
|
2022-02-02 14:24:45 +11:00
|
|
|
assert_eq!(literal.ty(), ty);
|
2021-03-08 16:18:03 +00:00
|
|
|
Constant { span, user_ty, literal: literal.into() }
|
2021-02-24 21:29:09 +01:00
|
|
|
}
|
2022-02-20 21:56:20 -05:00
|
|
|
ExprKind::StaticRef { literal, .. } => {
|
|
|
|
Constant { span, user_ty: None, literal: literal.into() }
|
2021-03-12 13:46:39 +00:00
|
|
|
}
|
2021-02-24 21:29:09 +01:00
|
|
|
ExprKind::ConstBlock { value } => {
|
2021-03-08 16:18:03 +00:00
|
|
|
Constant { span: span, user_ty: None, literal: value.into() }
|
2019-11-18 23:04:06 +00:00
|
|
|
}
|
2021-03-06 22:24:04 +01:00
|
|
|
_ => span_bug!(span, "expression is not a valid constant {:?}", kind),
|
2015-09-10 15:44:44 -04:00
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
}
|