]>
Commit | Line | Data |
---|---|---|
e9174d1e SL |
1 | //! See docs in build/expr/mod.rs |
2 | ||
9fa01778 | 3 | use crate::build::Builder; |
ba9703b0 | 4 | use rustc_middle::mir::*; |
17df50a5 | 5 | use rustc_middle::thir::*; |
ba9703b0 | 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! | |
17df50a5 | 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 { | |
17df50a5 XL |
15 | ExprKind::Scope { region_scope: _, lint_level: _, value } => { |
16 | this.as_constant(&this.thir[value]) | |
17 | } | |
1b1a35ee | 18 | ExprKind::Literal { literal, user_ty, const_id: _ } => { |
9fa01778 XL |
19 | let user_ty = user_ty.map(|user_ty| { |
20 | this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { | |
21 | span, | |
22 | user_ty, | |
23 | inferred_ty: ty, | |
24 | }) | |
0731742a | 25 | }); |
e1599b0c | 26 | assert_eq!(literal.ty, ty); |
6a06907d XL |
27 | Constant { span, user_ty, literal: literal.into() } |
28 | } | |
29 | ExprKind::StaticRef { literal, .. } => { | |
30 | Constant { span, user_ty: None, literal: literal.into() } | |
31 | } | |
32 | ExprKind::ConstBlock { value } => { | |
33 | Constant { span: span, user_ty: None, literal: value.into() } | |
60c5eb7d | 34 | } |
b7449926 | 35 | _ => span_bug!(span, "expression is not a valid constant {:?}", kind), |
b039eaaf | 36 | } |
e9174d1e SL |
37 | } |
38 | } |