]>
Commit | Line | Data |
---|---|---|
e9174d1e SL |
1 | //! See docs in build/expr/mod.rs |
2 | ||
9fa01778 | 3 | use crate::build::Builder; |
3dfed10e | 4 | use crate::thir::*; |
ba9703b0 XL |
5 | use rustc_middle::mir::*; |
6 | use rustc_middle::ty::CanonicalUserTypeAnnotation; | |
e9174d1e | 7 | |
dc9dc135 | 8 | impl<'a, 'tcx> Builder<'a, 'tcx> { |
e9174d1e SL |
9 | /// Compile `expr`, yielding a compile-time constant. Assumes that |
10 | /// `expr` is a valid compile-time constant! | |
6a06907d | 11 | crate fn as_constant(&mut self, expr: &Expr<'_, 'tcx>) -> Constant<'tcx> { |
e9174d1e | 12 | let this = self; |
6a06907d XL |
13 | let Expr { ty, temp_lifetime: _, span, ref kind } = *expr; |
14 | match *kind { | |
dfeec247 | 15 | ExprKind::Scope { region_scope: _, lint_level: _, value } => this.as_constant(value), |
1b1a35ee | 16 | ExprKind::Literal { literal, user_ty, const_id: _ } => { |
9fa01778 XL |
17 | let user_ty = user_ty.map(|user_ty| { |
18 | this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { | |
19 | span, | |
20 | user_ty, | |
21 | inferred_ty: ty, | |
22 | }) | |
0731742a | 23 | }); |
e1599b0c | 24 | assert_eq!(literal.ty, ty); |
6a06907d XL |
25 | Constant { span, user_ty, literal: literal.into() } |
26 | } | |
27 | ExprKind::StaticRef { literal, .. } => { | |
28 | Constant { span, user_ty: None, literal: literal.into() } | |
29 | } | |
30 | ExprKind::ConstBlock { value } => { | |
31 | Constant { span: span, user_ty: None, literal: value.into() } | |
60c5eb7d | 32 | } |
b7449926 | 33 | _ => span_bug!(span, "expression is not a valid constant {:?}", kind), |
b039eaaf | 34 | } |
e9174d1e SL |
35 | } |
36 | } |