1 //! See docs in build/expr/mod.rs
3 use crate::build
::Builder
;
6 use rustc
::ty
::CanonicalUserTypeAnnotation
;
8 impl<'a
, 'tcx
> Builder
<'a
, 'tcx
> {
9 /// Compile `expr`, yielding a compile-time constant. Assumes that
10 /// `expr` is a valid compile-time constant!
11 pub fn as_constant
<M
>(&mut self, expr
: M
) -> Constant
<'tcx
>
13 M
: Mirror
<'tcx
, Output
= Expr
<'tcx
>>,
15 let expr
= self.hir
.mirror(expr
);
16 self.expr_as_constant(expr
)
19 fn expr_as_constant(&mut self, expr
: Expr
<'tcx
>) -> Constant
<'tcx
> {
32 } => this
.as_constant(value
),
33 ExprKind
::Literal { literal, user_ty }
=> {
34 let user_ty
= user_ty
.map(|user_ty
| {
35 this
.canonical_user_type_annotations
.push(CanonicalUserTypeAnnotation
{
41 assert_eq
!(literal
.ty
, ty
);
48 _
=> span_bug
!(span
, "expression is not a valid constant {:?}", kind
),