1
1
//! See docs in build/expr/mod.rs
2
2
3
3
use crate :: build:: Builder ;
4
- use crate :: thir:: constant:: parse_float;
5
- use rustc_ast:: ast;
6
4
use rustc_hir:: def_id:: DefId ;
7
- use rustc_middle:: mir:: interpret:: {
8
- Allocation , ConstValue , LitToConstError , LitToConstInput , Scalar ,
9
- } ;
5
+ use rustc_middle:: mir:: interpret:: { ConstValue , LitToConstError , LitToConstInput , Scalar } ;
10
6
use rustc_middle:: mir:: * ;
11
7
use rustc_middle:: thir:: * ;
12
8
use rustc_middle:: ty:: subst:: SubstsRef ;
13
9
use rustc_middle:: ty:: { self , CanonicalUserTypeAnnotation , Ty , TyCtxt } ;
14
- use rustc_target:: abi:: Size ;
15
10
16
11
impl < ' a , ' tcx > Builder < ' a , ' tcx > {
17
12
/// Compile `expr`, yielding a compile-time constant. Assumes that
@@ -32,11 +27,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
32
27
}
33
28
ExprKind :: Literal { lit, neg } => {
34
29
let literal =
35
- match lit_to_constant ( tcx, LitToConstInput { lit : & lit. node , ty, neg } ) {
30
+ match tcx. lit_to_mir_constant ( LitToConstInput { lit : & lit. node , ty, neg } ) {
36
31
Ok ( c) => c,
37
32
Err ( LitToConstError :: Reported ) => ConstantKind :: Ty ( tcx. const_error ( ty) ) ,
38
33
Err ( LitToConstError :: TypeError ) => {
39
- bug ! ( "encountered type error in `lit_to_constant " )
34
+ bug ! ( "encountered type error in `lit_to_mir_constant " )
40
35
}
41
36
} ;
42
37
@@ -85,60 +80,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
85
80
86
81
Constant { span, user_ty : None , literal }
87
82
}
88
- ExprKind :: ConstBlock { value } => {
89
- Constant { span : span, user_ty : None , literal : value }
90
- }
91
83
_ => span_bug ! ( span, "expression is not a valid constant {:?}" , kind) ,
92
84
}
93
85
}
94
86
}
95
-
96
- crate fn lit_to_constant < ' tcx > (
97
- tcx : TyCtxt < ' tcx > ,
98
- lit_input : LitToConstInput < ' tcx > ,
99
- ) -> Result < ConstantKind < ' tcx > , LitToConstError > {
100
- let LitToConstInput { lit, ty, neg } = lit_input;
101
- let trunc = |n| {
102
- let param_ty = ty:: ParamEnv :: reveal_all ( ) . and ( ty) ;
103
- let width = tcx. layout_of ( param_ty) . map_err ( |_| LitToConstError :: Reported ) ?. size ;
104
- trace ! ( "trunc {} with size {} and shift {}" , n, width. bits( ) , 128 - width. bits( ) ) ;
105
- let result = width. truncate ( n) ;
106
- trace ! ( "trunc result: {}" , result) ;
107
- Ok ( ConstValue :: Scalar ( Scalar :: from_uint ( result, width) ) )
108
- } ;
109
-
110
- let value = match ( lit, & ty. kind ( ) ) {
111
- ( ast:: LitKind :: Str ( s, _) , ty:: Ref ( _, inner_ty, _) ) if inner_ty. is_str ( ) => {
112
- let s = s. as_str ( ) ;
113
- let allocation = Allocation :: from_bytes_byte_aligned_immutable ( s. as_bytes ( ) ) ;
114
- let allocation = tcx. intern_const_alloc ( allocation) ;
115
- ConstValue :: Slice { data : allocation, start : 0 , end : s. len ( ) }
116
- }
117
- ( ast:: LitKind :: ByteStr ( data) , ty:: Ref ( _, inner_ty, _) )
118
- if matches ! ( inner_ty. kind( ) , ty:: Slice ( _) ) =>
119
- {
120
- let allocation = Allocation :: from_bytes_byte_aligned_immutable ( data as & [ u8 ] ) ;
121
- let allocation = tcx. intern_const_alloc ( allocation) ;
122
- ConstValue :: Slice { data : allocation, start : 0 , end : data. len ( ) }
123
- }
124
- ( ast:: LitKind :: ByteStr ( data) , ty:: Ref ( _, inner_ty, _) ) if inner_ty. is_array ( ) => {
125
- let id = tcx. allocate_bytes ( data) ;
126
- ConstValue :: Scalar ( Scalar :: from_pointer ( id. into ( ) , & tcx) )
127
- }
128
- ( ast:: LitKind :: Byte ( n) , ty:: Uint ( ty:: UintTy :: U8 ) ) => {
129
- ConstValue :: Scalar ( Scalar :: from_uint ( * n, Size :: from_bytes ( 1 ) ) )
130
- }
131
- ( ast:: LitKind :: Int ( n, _) , ty:: Uint ( _) ) | ( ast:: LitKind :: Int ( n, _) , ty:: Int ( _) ) => {
132
- trunc ( if neg { ( * n as i128 ) . overflowing_neg ( ) . 0 as u128 } else { * n } ) ?
133
- }
134
- ( ast:: LitKind :: Float ( n, _) , ty:: Float ( fty) ) => {
135
- parse_float ( * n, * fty, neg) . ok_or ( LitToConstError :: Reported ) ?
136
- }
137
- ( ast:: LitKind :: Bool ( b) , ty:: Bool ) => ConstValue :: Scalar ( Scalar :: from_bool ( * b) ) ,
138
- ( ast:: LitKind :: Char ( c) , ty:: Char ) => ConstValue :: Scalar ( Scalar :: from_char ( * c) ) ,
139
- ( ast:: LitKind :: Err ( _) , _) => return Err ( LitToConstError :: Reported ) ,
140
- _ => return Err ( LitToConstError :: TypeError ) ,
141
- } ;
142
-
143
- Ok ( ConstantKind :: Val ( value, ty) )
144
- }
0 commit comments