|
1 | 1 | //! See docs in build/expr/mod.rs
|
2 | 2 |
|
3 | 3 | use crate::build::Builder;
|
4 |
| -use rustc_middle::mir::interpret::{ConstValue, Scalar}; |
| 4 | +use crate::thir::constant::parse_float; |
| 5 | +use rustc_ast::ast; |
| 6 | +use rustc_hir::def_id::DefId; |
| 7 | +use rustc_middle::mir::interpret::{ |
| 8 | + Allocation, ConstValue, LitToConstError, LitToConstInput, Scalar, |
| 9 | +}; |
5 | 10 | use rustc_middle::mir::*;
|
6 | 11 | use rustc_middle::thir::*;
|
7 |
| -use rustc_middle::ty::CanonicalUserTypeAnnotation; |
| 12 | +use rustc_middle::ty::subst::SubstsRef; |
| 13 | +use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt}; |
| 14 | +use rustc_target::abi::Size; |
8 | 15 |
|
9 | 16 | impl<'a, 'tcx> Builder<'a, 'tcx> {
|
10 | 17 | /// Compile `expr`, yielding a compile-time constant. Assumes that
|
11 | 18 | /// `expr` is a valid compile-time constant!
|
12 | 19 | crate fn as_constant(&mut self, expr: &Expr<'tcx>) -> Constant<'tcx> {
|
| 20 | + let create_uneval_from_def_id = |
| 21 | + |tcx: TyCtxt<'tcx>, def_id: DefId, ty: Ty<'tcx>, substs: SubstsRef<'tcx>| { |
| 22 | + let uneval = ty::Unevaluated::new(ty::WithOptConstParam::unknown(def_id), substs); |
| 23 | + tcx.mk_const(ty::ConstS { val: ty::ConstKind::Unevaluated(uneval), ty }) |
| 24 | + }; |
| 25 | + |
13 | 26 | let this = self;
|
| 27 | + let tcx = this.tcx; |
14 | 28 | let Expr { ty, temp_lifetime: _, span, ref kind } = *expr;
|
15 | 29 | match *kind {
|
16 | 30 | ExprKind::Scope { region_scope: _, lint_level: _, value } => {
|
17 | 31 | this.as_constant(&this.thir[value])
|
18 | 32 | }
|
19 |
| - ExprKind::Literal { literal, user_ty, const_id: _ } => { |
| 33 | + ExprKind::Literal { lit, neg } => { |
| 34 | + let literal = |
| 35 | + match lit_to_constant(tcx, LitToConstInput { lit: &lit.node, ty, neg }) { |
| 36 | + Ok(c) => c, |
| 37 | + Err(LitToConstError::Reported) => ConstantKind::Ty(tcx.const_error(ty)), |
| 38 | + Err(LitToConstError::TypeError) => { |
| 39 | + bug!("encountered type error in `lit_to_constant") |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + Constant { span, user_ty: None, literal: literal.into() } |
| 44 | + } |
| 45 | + ExprKind::NonHirLiteral { lit, user_ty } => { |
20 | 46 | let user_ty = user_ty.map(|user_ty| {
|
21 | 47 | this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
|
22 | 48 | span,
|
23 | 49 | user_ty,
|
24 | 50 | inferred_ty: ty,
|
25 | 51 | })
|
26 | 52 | });
|
27 |
| - assert_eq!(literal.ty(), ty); |
28 |
| - Constant { span, user_ty, literal: literal.into() } |
| 53 | + |
| 54 | + let literal = ConstantKind::Val(ConstValue::Scalar(Scalar::Int(lit)), ty); |
| 55 | + |
| 56 | + Constant { span, user_ty: user_ty, literal } |
| 57 | + } |
| 58 | + ExprKind::NamedConst { def_id, substs, user_ty } => { |
| 59 | + let user_ty = user_ty.map(|user_ty| { |
| 60 | + this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { |
| 61 | + span, |
| 62 | + user_ty, |
| 63 | + inferred_ty: ty, |
| 64 | + }) |
| 65 | + }); |
| 66 | + let literal = ConstantKind::Ty(create_uneval_from_def_id(tcx, def_id, ty, substs)); |
| 67 | + |
| 68 | + Constant { user_ty, span, literal } |
| 69 | + } |
| 70 | + ExprKind::ConstParam { param, def_id: _ } => { |
| 71 | + let const_param = |
| 72 | + tcx.mk_const(ty::ConstS { val: ty::ConstKind::Param(param), ty: expr.ty }); |
| 73 | + let literal = ConstantKind::Ty(const_param); |
| 74 | + |
| 75 | + Constant { user_ty: None, span, literal } |
| 76 | + } |
| 77 | + ExprKind::ConstBlock { did: def_id, substs } => { |
| 78 | + let literal = ConstantKind::Ty(create_uneval_from_def_id(tcx, def_id, ty, substs)); |
| 79 | + |
| 80 | + Constant { user_ty: None, span, literal } |
29 | 81 | }
|
30 | 82 | ExprKind::StaticRef { alloc_id, ty, .. } => {
|
31 |
| - let const_val = |
32 |
| - ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &this.tcx)); |
| 83 | + let const_val = ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &tcx)); |
33 | 84 | let literal = ConstantKind::Val(const_val, ty);
|
34 | 85 |
|
35 | 86 | Constant { span, user_ty: None, literal }
|
36 | 87 | }
|
37 |
| - ExprKind::ConstBlock { value } => { |
38 |
| - Constant { span: span, user_ty: None, literal: value.into() } |
39 |
| - } |
40 | 88 | _ => span_bug!(span, "expression is not a valid constant {:?}", kind),
|
41 | 89 | }
|
42 | 90 | }
|
43 | 91 | }
|
| 92 | + |
| 93 | +crate fn lit_to_constant<'tcx>( |
| 94 | + tcx: TyCtxt<'tcx>, |
| 95 | + lit_input: LitToConstInput<'tcx>, |
| 96 | +) -> Result<ConstantKind<'tcx>, LitToConstError> { |
| 97 | + let LitToConstInput { lit, ty, neg } = lit_input; |
| 98 | + let trunc = |n| { |
| 99 | + let param_ty = ty::ParamEnv::reveal_all().and(ty); |
| 100 | + let width = tcx.layout_of(param_ty).map_err(|_| LitToConstError::Reported)?.size; |
| 101 | + trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits()); |
| 102 | + let result = width.truncate(n); |
| 103 | + trace!("trunc result: {}", result); |
| 104 | + Ok(ConstValue::Scalar(Scalar::from_uint(result, width))) |
| 105 | + }; |
| 106 | + |
| 107 | + let value = match (lit, &ty.kind()) { |
| 108 | + (ast::LitKind::Str(s, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => { |
| 109 | + let s = s.as_str(); |
| 110 | + let allocation = Allocation::from_bytes_byte_aligned_immutable(s.as_bytes()); |
| 111 | + let allocation = tcx.intern_const_alloc(allocation); |
| 112 | + ConstValue::Slice { data: allocation, start: 0, end: s.len() } |
| 113 | + } |
| 114 | + (ast::LitKind::ByteStr(data), ty::Ref(_, inner_ty, _)) |
| 115 | + if matches!(inner_ty.kind(), ty::Slice(_)) => |
| 116 | + { |
| 117 | + let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]); |
| 118 | + let allocation = tcx.intern_const_alloc(allocation); |
| 119 | + ConstValue::Slice { data: allocation, start: 0, end: data.len() } |
| 120 | + } |
| 121 | + (ast::LitKind::ByteStr(data), ty::Ref(_, inner_ty, _)) if inner_ty.is_array() => { |
| 122 | + let id = tcx.allocate_bytes(data); |
| 123 | + ConstValue::Scalar(Scalar::from_pointer(id.into(), &tcx)) |
| 124 | + } |
| 125 | + (ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => { |
| 126 | + ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1))) |
| 127 | + } |
| 128 | + (ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => { |
| 129 | + trunc(if neg { (*n as i128).overflowing_neg().0 as u128 } else { *n })? |
| 130 | + } |
| 131 | + (ast::LitKind::Float(n, _), ty::Float(fty)) => { |
| 132 | + parse_float(*n, *fty, neg).ok_or(LitToConstError::Reported)? |
| 133 | + } |
| 134 | + (ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)), |
| 135 | + (ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)), |
| 136 | + (ast::LitKind::Err(_), _) => return Err(LitToConstError::Reported), |
| 137 | + _ => return Err(LitToConstError::TypeError), |
| 138 | + }; |
| 139 | + |
| 140 | + Ok(ConstantKind::Val(value, ty)) |
| 141 | +} |
0 commit comments