@@ -855,10 +855,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &TyCtxt<'tcx>,
855
855
debug ! ( "const call({:?})" , call_args) ;
856
856
try!( eval_const_expr_partial ( tcx, & result, ty_hint, Some ( & call_args) ) )
857
857
} ,
858
- hir:: ExprLit ( ref lit) => match lit_to_const ( & lit. node , tcx, ety, lit. span ) {
859
- Ok ( val) => val,
860
- Err ( err) => signal ! ( e, Math ( err) ) ,
861
- } ,
858
+ hir:: ExprLit ( ref lit) => try!( lit_to_const ( & lit. node , tcx, ety, lit. span ) ) ,
862
859
hir:: ExprBlock ( ref block) => {
863
860
match block. expr {
864
861
Some ( ref expr) => try!( eval_const_expr_partial ( tcx, & expr, ty_hint, fn_args) ) ,
@@ -926,7 +923,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &TyCtxt<'tcx>,
926
923
if let Tuple ( tup_id) = c {
927
924
if let hir:: ExprTup ( ref fields) = tcx. map . expect_expr ( tup_id) . node {
928
925
if index. node < fields. len ( ) {
929
- return eval_const_expr_partial ( tcx, & fields[ index. node ] , ty_hint, fn_args)
926
+ try! ( eval_const_expr_partial ( tcx, & fields[ index. node ] , ty_hint, fn_args) )
930
927
} else {
931
928
signal ! ( e, TupleIndexOutOfBounds ) ;
932
929
}
@@ -947,7 +944,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &TyCtxt<'tcx>,
947
944
// if the idents are compared run-pass/issue-19244 fails
948
945
if let Some ( f) = fields. iter ( ) . find ( |f| f. name . node
949
946
== field_name. node ) {
950
- return eval_const_expr_partial ( tcx, & f. expr , ty_hint, fn_args)
947
+ try! ( eval_const_expr_partial ( tcx, & f. expr , ty_hint, fn_args) )
951
948
} else {
952
949
signal ! ( e, MissingStructField ) ;
953
950
}
@@ -974,22 +971,6 @@ fn infer<'tcx>(
974
971
span : Span
975
972
) -> Result < ConstInt , ConstEvalErr > {
976
973
use syntax:: ast:: * ;
977
- const I8MAX : u64 = :: std:: i8:: MAX as u64 ;
978
- const I16MAX : u64 = :: std:: i16:: MAX as u64 ;
979
- const I32MAX : u64 = :: std:: i32:: MAX as u64 ;
980
- const I64MAX : u64 = :: std:: i64:: MAX as u64 ;
981
-
982
- const U8MAX : u64 = :: std:: u8:: MAX as u64 ;
983
- const U16MAX : u64 = :: std:: u16:: MAX as u64 ;
984
- const U32MAX : u64 = :: std:: u32:: MAX as u64 ;
985
-
986
- const I8MAXI : i64 = :: std:: i8:: MAX as i64 ;
987
- const I16MAXI : i64 = :: std:: i16:: MAX as i64 ;
988
- const I32MAXI : i64 = :: std:: i32:: MAX as i64 ;
989
-
990
- const I8MINI : i64 = :: std:: i8:: MIN as i64 ;
991
- const I16MINI : i64 = :: std:: i16:: MIN as i64 ;
992
- const I32MINI : i64 = :: std:: i32:: MIN as i64 ;
993
974
994
975
let err = |e| ConstEvalErr {
995
976
span : span,
@@ -1009,41 +990,38 @@ fn infer<'tcx>(
1009
990
( & ty:: TyUint ( UintTy :: U64 ) , result @ U64 ( _) ) => Ok ( result) ,
1010
991
( & ty:: TyUint ( UintTy :: Us ) , result @ Usize ( _) ) => Ok ( result) ,
1011
992
1012
- ( & ty:: TyInt ( IntTy :: I8 ) , Infer ( i @ 0 ... I8MAX ) ) => Ok ( I8 ( i as i8 ) ) ,
1013
- ( & ty:: TyInt ( IntTy :: I16 ) , Infer ( i @ 0 ... I16MAX ) ) => Ok ( I16 ( i as i16 ) ) ,
1014
- ( & ty:: TyInt ( IntTy :: I32 ) , Infer ( i @ 0 ... I32MAX ) ) => Ok ( I32 ( i as i32 ) ) ,
1015
- ( & ty:: TyInt ( IntTy :: I64 ) , Infer ( i @ 0 ... I64MAX ) ) => Ok ( I64 ( i as i64 ) ) ,
1016
- ( & ty:: TyInt ( IntTy :: Is ) , Infer ( i @ 0 ... I64MAX ) ) => {
993
+ ( & ty:: TyInt ( IntTy :: I8 ) , Infer ( i) ) => Ok ( I8 ( i as i64 as i8 ) ) ,
994
+ ( & ty:: TyInt ( IntTy :: I16 ) , Infer ( i) ) => Ok ( I16 ( i as i64 as i16 ) ) ,
995
+ ( & ty:: TyInt ( IntTy :: I32 ) , Infer ( i) ) => Ok ( I32 ( i as i64 as i32 ) ) ,
996
+ ( & ty:: TyInt ( IntTy :: I64 ) , Infer ( i) ) => Ok ( I64 ( i as i64 ) ) ,
997
+ ( & ty:: TyInt ( IntTy :: Is ) , Infer ( i) ) => {
1017
998
match ConstIsize :: new ( i as i64 , tcx. sess . target . int_type ) {
1018
999
Ok ( val) => Ok ( Isize ( val) ) ,
1019
- Err ( e ) => Err ( err ( e . into ( ) ) ) ,
1000
+ Err ( _ ) => Ok ( Isize ( ConstIsize :: Is32 ( i as i64 as i32 ) ) ) ,
1020
1001
}
1021
1002
} ,
1022
- ( & ty:: TyInt ( _) , Infer ( _) ) => Err ( err ( Math ( ConstMathErr :: NotInRange ) ) ) ,
1023
1003
1024
- ( & ty:: TyInt ( IntTy :: I8 ) , InferSigned ( i @ I8MINI ... I8MAXI ) ) => Ok ( I8 ( i as i8 ) ) ,
1025
- ( & ty:: TyInt ( IntTy :: I16 ) , InferSigned ( i @ I16MINI ... I16MAXI ) ) => Ok ( I16 ( i as i16 ) ) ,
1026
- ( & ty:: TyInt ( IntTy :: I32 ) , InferSigned ( i @ I32MINI ... I32MAXI ) ) => Ok ( I32 ( i as i32 ) ) ,
1004
+ ( & ty:: TyInt ( IntTy :: I8 ) , InferSigned ( i) ) => Ok ( I8 ( i as i8 ) ) ,
1005
+ ( & ty:: TyInt ( IntTy :: I16 ) , InferSigned ( i) ) => Ok ( I16 ( i as i16 ) ) ,
1006
+ ( & ty:: TyInt ( IntTy :: I32 ) , InferSigned ( i) ) => Ok ( I32 ( i as i32 ) ) ,
1027
1007
( & ty:: TyInt ( IntTy :: I64 ) , InferSigned ( i) ) => Ok ( I64 ( i) ) ,
1028
1008
( & ty:: TyInt ( IntTy :: Is ) , InferSigned ( i) ) => {
1029
1009
match ConstIsize :: new ( i, tcx. sess . target . int_type ) {
1030
1010
Ok ( val) => Ok ( Isize ( val) ) ,
1031
- Err ( e ) => Err ( err ( e . into ( ) ) ) ,
1011
+ Err ( _ ) => Ok ( Isize ( ConstIsize :: Is32 ( i as i32 ) ) ) ,
1032
1012
}
1033
1013
} ,
1034
- ( & ty:: TyInt ( _) , InferSigned ( _) ) => Err ( err ( Math ( ConstMathErr :: NotInRange ) ) ) ,
1035
1014
1036
- ( & ty:: TyUint ( UintTy :: U8 ) , Infer ( i @ 0 ... U8MAX ) ) => Ok ( U8 ( i as u8 ) ) ,
1037
- ( & ty:: TyUint ( UintTy :: U16 ) , Infer ( i @ 0 ... U16MAX ) ) => Ok ( U16 ( i as u16 ) ) ,
1038
- ( & ty:: TyUint ( UintTy :: U32 ) , Infer ( i @ 0 ... U32MAX ) ) => Ok ( U32 ( i as u32 ) ) ,
1015
+ ( & ty:: TyUint ( UintTy :: U8 ) , Infer ( i) ) => Ok ( U8 ( i as u8 ) ) ,
1016
+ ( & ty:: TyUint ( UintTy :: U16 ) , Infer ( i) ) => Ok ( U16 ( i as u16 ) ) ,
1017
+ ( & ty:: TyUint ( UintTy :: U32 ) , Infer ( i) ) => Ok ( U32 ( i as u32 ) ) ,
1039
1018
( & ty:: TyUint ( UintTy :: U64 ) , Infer ( i) ) => Ok ( U64 ( i) ) ,
1040
1019
( & ty:: TyUint ( UintTy :: Us ) , Infer ( i) ) => {
1041
1020
match ConstUsize :: new ( i, tcx. sess . target . uint_type ) {
1042
1021
Ok ( val) => Ok ( Usize ( val) ) ,
1043
- Err ( e ) => Err ( err ( e . into ( ) ) ) ,
1022
+ Err ( _ ) => Ok ( Usize ( ConstUsize :: Us32 ( i as u32 ) ) ) ,
1044
1023
}
1045
1024
} ,
1046
- ( & ty:: TyUint ( _) , Infer ( _) ) => Err ( err ( Math ( ConstMathErr :: NotInRange ) ) ) ,
1047
1025
( & ty:: TyUint ( _) , InferSigned ( _) ) => Err ( err ( IntermediateUnsignedNegative ) ) ,
1048
1026
1049
1027
( & ty:: TyInt ( ity) , i) => Err ( err ( TypeMismatch ( ity. to_string ( ) , i) ) ) ,
@@ -1115,19 +1093,25 @@ fn cast_const_int<'tcx>(tcx: &TyCtxt<'tcx>, val: ConstInt, ty: ty::Ty) -> CastRe
1115
1093
match ty. sty {
1116
1094
ty:: TyBool if v == 0 => Ok ( Bool ( false ) ) ,
1117
1095
ty:: TyBool if v == 1 => Ok ( Bool ( true ) ) ,
1118
- ty:: TyInt ( ast:: IntTy :: I8 ) => Ok ( Integral ( I8 ( v as i8 ) ) ) ,
1119
- ty:: TyInt ( ast:: IntTy :: I16 ) => Ok ( Integral ( I16 ( v as i16 ) ) ) ,
1120
- ty:: TyInt ( ast:: IntTy :: I32 ) => Ok ( Integral ( I32 ( v as i32 ) ) ) ,
1096
+ ty:: TyInt ( ast:: IntTy :: I8 ) => Ok ( Integral ( I8 ( v as i64 as i8 ) ) ) ,
1097
+ ty:: TyInt ( ast:: IntTy :: I16 ) => Ok ( Integral ( I16 ( v as i64 as i16 ) ) ) ,
1098
+ ty:: TyInt ( ast:: IntTy :: I32 ) => Ok ( Integral ( I32 ( v as i64 as i32 ) ) ) ,
1121
1099
ty:: TyInt ( ast:: IntTy :: I64 ) => Ok ( Integral ( I64 ( v as i64 ) ) ) ,
1122
1100
ty:: TyInt ( ast:: IntTy :: Is ) => {
1123
- Ok ( Integral ( Isize ( try!( ConstIsize :: new ( v as i64 , tcx. sess . target . int_type ) ) ) ) )
1101
+ match ConstIsize :: new ( v as i64 , tcx. sess . target . int_type ) {
1102
+ Ok ( val) => Ok ( Integral ( Isize ( val) ) ) ,
1103
+ Err ( _) => Ok ( Integral ( Isize ( ConstIsize :: Is32 ( v as i64 as i32 ) ) ) ) ,
1104
+ }
1124
1105
} ,
1125
1106
ty:: TyUint ( ast:: UintTy :: U8 ) => Ok ( Integral ( U8 ( v as u8 ) ) ) ,
1126
1107
ty:: TyUint ( ast:: UintTy :: U16 ) => Ok ( Integral ( U16 ( v as u16 ) ) ) ,
1127
1108
ty:: TyUint ( ast:: UintTy :: U32 ) => Ok ( Integral ( U32 ( v as u32 ) ) ) ,
1128
- ty:: TyUint ( ast:: UintTy :: U64 ) => Ok ( Integral ( U64 ( v as u64 ) ) ) ,
1109
+ ty:: TyUint ( ast:: UintTy :: U64 ) => Ok ( Integral ( U64 ( v) ) ) ,
1129
1110
ty:: TyUint ( ast:: UintTy :: Us ) => {
1130
- Ok ( Integral ( Usize ( try!( ConstUsize :: new ( v, tcx. sess . target . uint_type ) ) ) ) )
1111
+ match ConstUsize :: new ( v, tcx. sess . target . uint_type ) {
1112
+ Ok ( val) => Ok ( Integral ( Usize ( val) ) ) ,
1113
+ Err ( _) => Ok ( Integral ( Usize ( ConstUsize :: Us32 ( v as u32 ) ) ) ) ,
1114
+ }
1131
1115
} ,
1132
1116
ty:: TyFloat ( ast:: FloatTy :: F64 ) if val. is_negative ( ) => {
1133
1117
// FIXME: this could probably be prettier
@@ -1174,57 +1158,37 @@ fn lit_to_const<'tcx>(lit: &ast::LitKind,
1174
1158
tcx : & TyCtxt < ' tcx > ,
1175
1159
ty_hint : Option < Ty < ' tcx > > ,
1176
1160
span : Span ,
1177
- ) -> Result < ConstVal , ConstMathErr > {
1161
+ ) -> Result < ConstVal , ConstEvalErr > {
1178
1162
use syntax:: ast:: * ;
1179
1163
use syntax:: ast:: LitIntType :: * ;
1180
- const I8MAX : u64 = :: std:: i8:: MAX as u64 ;
1181
- const I16MAX : u64 = :: std:: i16:: MAX as u64 ;
1182
- const I32MAX : u64 = :: std:: i32:: MAX as u64 ;
1183
- const I64MAX : u64 = :: std:: i64:: MAX as u64 ;
1184
- const U8MAX : u64 = :: std:: u8:: MAX as u64 ;
1185
- const U16MAX : u64 = :: std:: u16:: MAX as u64 ;
1186
- const U32MAX : u64 = :: std:: u32:: MAX as u64 ;
1187
- const U64MAX : u64 = :: std:: u64:: MAX as u64 ;
1188
1164
match * lit {
1189
1165
LitKind :: Str ( ref s, _) => Ok ( Str ( ( * s) . clone ( ) ) ) ,
1190
1166
LitKind :: ByteStr ( ref data) => Ok ( ByteStr ( data. clone ( ) ) ) ,
1191
1167
LitKind :: Byte ( n) => Ok ( Integral ( U8 ( n) ) ) ,
1192
- LitKind :: Int ( n @ 0 ...I8MAX , Signed ( IntTy :: I8 ) ) => Ok ( Integral ( I8 ( n as i8 ) ) ) ,
1193
- LitKind :: Int ( n @ 0 ...I16MAX , Signed ( IntTy :: I16 ) ) => Ok ( Integral ( I16 ( n as i16 ) ) ) ,
1194
- LitKind :: Int ( n @ 0 ...I32MAX , Signed ( IntTy :: I32 ) ) => Ok ( Integral ( I32 ( n as i32 ) ) ) ,
1195
- LitKind :: Int ( n @ 0 ...I64MAX , Signed ( IntTy :: I64 ) ) => Ok ( Integral ( I64 ( n as i64 ) ) ) ,
1196
- LitKind :: Int ( n, Signed ( IntTy :: Is ) ) => {
1197
- Ok ( Integral ( Isize ( try!( ConstIsize :: new ( n as i64 , tcx. sess . target . int_type ) ) ) ) )
1168
+ LitKind :: Int ( n, Signed ( ity) ) => {
1169
+ infer ( InferSigned ( n as i64 ) , tcx, & ty:: TyInt ( ity) , span) . map ( Integral )
1198
1170
} ,
1199
1171
1200
- LitKind :: Int ( _, Signed ( ty) ) => Err ( ConstMathErr :: LitOutOfRange ( ty) ) ,
1201
-
1202
1172
LitKind :: Int ( n, Unsuffixed ) => {
1203
1173
match ty_hint. map ( |t| & t. sty ) {
1204
1174
Some ( & ty:: TyInt ( ity) ) => {
1205
- lit_to_const ( & LitKind :: Int ( n , Signed ( ity ) ) , tcx, ty_hint , span)
1175
+ infer ( InferSigned ( n as i64 ) , tcx, & ty :: TyInt ( ity ) , span) . map ( Integral )
1206
1176
} ,
1207
1177
Some ( & ty:: TyUint ( uty) ) => {
1208
- lit_to_const ( & LitKind :: Int ( n , Unsigned ( uty ) ) , tcx, ty_hint , span)
1178
+ infer ( Infer ( n ) , tcx, & ty :: TyUint ( uty ) , span) . map ( Integral )
1209
1179
} ,
1210
1180
None => Ok ( Integral ( Infer ( n) ) ) ,
1211
1181
Some ( & ty:: TyEnum ( ref adt, _) ) => {
1212
1182
let hints = tcx. lookup_repr_hints ( adt. did ) ;
1213
1183
let int_ty = tcx. enum_repr_type ( hints. iter ( ) . next ( ) ) ;
1214
- lit_to_const ( lit , tcx, Some ( int_ty. to_ty ( tcx) ) , span)
1184
+ infer ( Infer ( n ) , tcx, & int_ty. to_ty ( tcx) . sty , span) . map ( Integral )
1215
1185
} ,
1216
1186
Some ( ty_hint) => panic ! ( "bad ty_hint: {:?}, {:?}" , ty_hint, lit) ,
1217
1187
}
1218
1188
} ,
1219
- LitKind :: Int ( n @ 0 ...U8MAX , Unsigned ( UintTy :: U8 ) ) => Ok ( Integral ( U8 ( n as u8 ) ) ) ,
1220
- LitKind :: Int ( n @ 0 ...U16MAX , Unsigned ( UintTy :: U16 ) ) => Ok ( Integral ( U16 ( n as u16 ) ) ) ,
1221
- LitKind :: Int ( n @ 0 ...U32MAX , Unsigned ( UintTy :: U32 ) ) => Ok ( Integral ( U32 ( n as u32 ) ) ) ,
1222
- LitKind :: Int ( n @ 0 ...U64MAX , Unsigned ( UintTy :: U64 ) ) => Ok ( Integral ( U64 ( n as u64 ) ) ) ,
1223
-
1224
- LitKind :: Int ( n, Unsigned ( UintTy :: Us ) ) => {
1225
- Ok ( Integral ( Usize ( try!( ConstUsize :: new ( n as u64 , tcx. sess . target . uint_type ) ) ) ) )
1189
+ LitKind :: Int ( n, Unsigned ( ity) ) => {
1190
+ infer ( Infer ( n) , tcx, & ty:: TyUint ( ity) , span) . map ( Integral )
1226
1191
} ,
1227
- LitKind :: Int ( _, Unsigned ( ty) ) => Err ( ConstMathErr :: ULitOutOfRange ( ty) ) ,
1228
1192
1229
1193
LitKind :: Float ( ref n, _) |
1230
1194
LitKind :: FloatUnsuffixed ( ref n) => {
0 commit comments