1
1
use rustc:: mir:: interpret:: {
2
2
truncate, Allocation , ConstValue , LitToConstError , LitToConstInput , Scalar ,
3
3
} ;
4
- use rustc:: ty:: { self , layout:: Size , ParamEnv , TyCtxt } ;
4
+ use rustc:: ty:: { self , layout:: Size , ParamEnv , TyCtxt , TyS } ;
5
5
use rustc_span:: symbol:: Symbol ;
6
6
use syntax:: ast;
7
7
@@ -20,50 +20,35 @@ crate fn lit_to_const<'tcx>(
20
20
Ok ( ConstValue :: Scalar ( Scalar :: from_uint ( result, width) ) )
21
21
} ;
22
22
23
- let lit = match * lit {
24
- ast:: LitKind :: Str ( ref s, _) => {
23
+ let lit = match ( lit, & ty . kind ) {
24
+ ( ast:: LitKind :: Str ( s, _) , ty :: Ref ( _ , TyS { kind : ty :: Str , .. } , _ ) ) => {
25
25
let s = s. as_str ( ) ;
26
26
let allocation = Allocation :: from_byte_aligned_bytes ( s. as_bytes ( ) ) ;
27
27
let allocation = tcx. intern_const_alloc ( allocation) ;
28
28
ConstValue :: Slice { data : allocation, start : 0 , end : s. len ( ) }
29
29
}
30
- ast:: LitKind :: ByteStr ( ref data) => {
31
- if let ty:: Ref ( _, ref_ty, _) = ty. kind {
32
- match ref_ty. kind {
33
- ty:: Slice ( _) => {
34
- let allocation = Allocation :: from_byte_aligned_bytes ( data as & Vec < u8 > ) ;
35
- let allocation = tcx. intern_const_alloc ( allocation) ;
36
- ConstValue :: Slice { data : allocation, start : 0 , end : data. len ( ) }
37
- }
38
- ty:: Array ( _, _) => {
39
- let id = tcx. allocate_bytes ( data) ;
40
- ConstValue :: Scalar ( Scalar :: Ptr ( id. into ( ) ) )
41
- }
42
- _ => {
43
- bug ! ( "bytestring should have type of either &[u8] or &[u8; _], not {}" , ty)
44
- }
45
- }
46
- } else {
47
- bug ! ( "bytestring should have type of either &[u8] or &[u8; _], not {}" , ty)
48
- }
30
+ ( ast:: LitKind :: ByteStr ( data) , ty:: Ref ( _, TyS { kind : ty:: Slice ( _) , .. } , _) ) => {
31
+ let allocation = Allocation :: from_byte_aligned_bytes ( data as & Vec < u8 > ) ;
32
+ let allocation = tcx. intern_const_alloc ( allocation) ;
33
+ ConstValue :: Slice { data : allocation, start : 0 , end : data. len ( ) }
34
+ }
35
+ ( ast:: LitKind :: ByteStr ( data) , ty:: Ref ( _, TyS { kind : ty:: Array ( _, _) , .. } , _) ) => {
36
+ let id = tcx. allocate_bytes ( data) ;
37
+ ConstValue :: Scalar ( Scalar :: Ptr ( id. into ( ) ) )
38
+ }
39
+ ( ast:: LitKind :: Byte ( n) , ty:: Uint ( ast:: UintTy :: U8 ) ) => {
40
+ ConstValue :: Scalar ( Scalar :: from_uint ( * n, Size :: from_bytes ( 1 ) ) )
49
41
}
50
- ast:: LitKind :: Byte ( n) => ConstValue :: Scalar ( Scalar :: from_uint ( n, Size :: from_bytes ( 1 ) ) ) ,
51
- ast:: LitKind :: Int ( n, _) if neg => {
52
- let n = n as i128 ;
53
- let n = n. overflowing_neg ( ) . 0 ;
54
- trunc ( n as u128 ) ?
42
+ ( ast:: LitKind :: Int ( n, _) , ty:: Uint ( _) ) | ( ast:: LitKind :: Int ( n, _) , ty:: Int ( _) ) => {
43
+ trunc ( if neg { ( * n as i128 ) . overflowing_neg ( ) . 0 as u128 } else { * n } ) ?
55
44
}
56
- ast:: LitKind :: Int ( n, _) => trunc ( n) ?,
57
- ast:: LitKind :: Float ( n, _) => {
58
- let fty = match ty. kind {
59
- ty:: Float ( fty) => fty,
60
- _ => bug ! ( ) ,
61
- } ;
62
- parse_float ( n, fty, neg) . map_err ( |_| LitToConstError :: UnparseableFloat ) ?
45
+ ( ast:: LitKind :: Float ( n, _) , ty:: Float ( fty) ) => {
46
+ parse_float ( * n, * fty, neg) . map_err ( |_| LitToConstError :: UnparseableFloat ) ?
63
47
}
64
- ast:: LitKind :: Bool ( b) => ConstValue :: Scalar ( Scalar :: from_bool ( b) ) ,
65
- ast:: LitKind :: Char ( c) => ConstValue :: Scalar ( Scalar :: from_char ( c) ) ,
66
- ast:: LitKind :: Err ( _) => return Err ( LitToConstError :: Reported ) ,
48
+ ( ast:: LitKind :: Bool ( b) , ty:: Bool ) => ConstValue :: Scalar ( Scalar :: from_bool ( * b) ) ,
49
+ ( ast:: LitKind :: Char ( c) , ty:: Char ) => ConstValue :: Scalar ( Scalar :: from_char ( * c) ) ,
50
+ ( ast:: LitKind :: Err ( _) , _) => return Err ( LitToConstError :: Reported ) ,
51
+ _ => return Err ( LitToConstError :: TypeError ) ,
67
52
} ;
68
53
Ok ( tcx. mk_const ( ty:: Const { val : ty:: ConstKind :: Value ( lit) , ty } ) )
69
54
}
0 commit comments