Skip to content

Commit 1858cfb

Browse files
committed
Allow truncating constants to 16-bit u/isize
1 parent e51958b commit 1858cfb

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

src/librustc_const_eval/eval.rs

+5-20
Original file line numberDiff line numberDiff line change
@@ -956,32 +956,23 @@ fn infer<'a, 'tcx>(i: ConstInt,
956956
(&ty::TyInt(IntTy::I32), Infer(i)) => Ok(I32(i as i64 as i32)),
957957
(&ty::TyInt(IntTy::I64), Infer(i)) => Ok(I64(i as i64)),
958958
(&ty::TyInt(IntTy::Is), Infer(i)) => {
959-
match ConstIsize::new(i as i64, tcx.sess.target.int_type) {
960-
Ok(val) => Ok(Isize(val)),
961-
Err(_) => Ok(Isize(ConstIsize::Is32(i as i64 as i32))),
962-
}
959+
Ok(Isize(ConstIsize::new_truncating(i as i64, tcx.sess.target.int_type)))
963960
},
964961

965962
(&ty::TyInt(IntTy::I8), InferSigned(i)) => Ok(I8(i as i8)),
966963
(&ty::TyInt(IntTy::I16), InferSigned(i)) => Ok(I16(i as i16)),
967964
(&ty::TyInt(IntTy::I32), InferSigned(i)) => Ok(I32(i as i32)),
968965
(&ty::TyInt(IntTy::I64), InferSigned(i)) => Ok(I64(i)),
969966
(&ty::TyInt(IntTy::Is), InferSigned(i)) => {
970-
match ConstIsize::new(i, tcx.sess.target.int_type) {
971-
Ok(val) => Ok(Isize(val)),
972-
Err(_) => Ok(Isize(ConstIsize::Is32(i as i32))),
973-
}
967+
Ok(Isize(ConstIsize::new_truncating(i, tcx.sess.target.int_type)))
974968
},
975969

976970
(&ty::TyUint(UintTy::U8), Infer(i)) => Ok(U8(i as u8)),
977971
(&ty::TyUint(UintTy::U16), Infer(i)) => Ok(U16(i as u16)),
978972
(&ty::TyUint(UintTy::U32), Infer(i)) => Ok(U32(i as u32)),
979973
(&ty::TyUint(UintTy::U64), Infer(i)) => Ok(U64(i)),
980974
(&ty::TyUint(UintTy::Us), Infer(i)) => {
981-
match ConstUsize::new(i, tcx.sess.target.uint_type) {
982-
Ok(val) => Ok(Usize(val)),
983-
Err(_) => Ok(Usize(ConstUsize::Us32(i as u32))),
984-
}
975+
Ok(Usize(ConstUsize::new_truncating(i, tcx.sess.target.uint_type)))
985976
},
986977
(&ty::TyUint(_), InferSigned(_)) => Err(IntermediateUnsignedNegative),
987978

@@ -1063,20 +1054,14 @@ fn cast_const_int<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, val: ConstInt, ty: ty::
10631054
ty::TyInt(ast::IntTy::I32) => Ok(Integral(I32(v as i64 as i32))),
10641055
ty::TyInt(ast::IntTy::I64) => Ok(Integral(I64(v as i64))),
10651056
ty::TyInt(ast::IntTy::Is) => {
1066-
match ConstIsize::new(v as i64, tcx.sess.target.int_type) {
1067-
Ok(val) => Ok(Integral(Isize(val))),
1068-
Err(_) => Ok(Integral(Isize(ConstIsize::Is32(v as i64 as i32)))),
1069-
}
1057+
Ok(Integral(Isize(ConstIsize::new_truncating(v as i64, tcx.sess.target.int_type))))
10701058
},
10711059
ty::TyUint(ast::UintTy::U8) => Ok(Integral(U8(v as u8))),
10721060
ty::TyUint(ast::UintTy::U16) => Ok(Integral(U16(v as u16))),
10731061
ty::TyUint(ast::UintTy::U32) => Ok(Integral(U32(v as u32))),
10741062
ty::TyUint(ast::UintTy::U64) => Ok(Integral(U64(v))),
10751063
ty::TyUint(ast::UintTy::Us) => {
1076-
match ConstUsize::new(v, tcx.sess.target.uint_type) {
1077-
Ok(val) => Ok(Integral(Usize(val))),
1078-
Err(_) => Ok(Integral(Usize(ConstUsize::Us32(v as u32)))),
1079-
}
1064+
Ok(Integral(Usize(ConstUsize::new_truncating(v, tcx.sess.target.uint_type))))
10801065
},
10811066
ty::TyFloat(ast::FloatTy::F64) => match val.erase_type() {
10821067
Infer(u) => Ok(Float(F64(u as f64))),

src/librustc_const_math/is.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ impl ConstIsize {
2727
(Is16(i), ast::IntTy::I16) => i as i64,
2828
(Is32(i), ast::IntTy::I32) => i as i64,
2929
(Is64(i), ast::IntTy::I64) => i,
30-
_ => panic!("got invalid isize size for target"),
30+
_ => panic!("unable to convert self ({:?}) to target isize ({:?})",
31+
self, target_int_ty),
3132
}
3233
}
3334
pub fn new(i: i64, target_int_ty: ast::IntTy) -> Result<Self, ConstMathErr> {
@@ -40,4 +41,12 @@ impl ConstIsize {
4041
_ => unreachable!(),
4142
}
4243
}
44+
pub fn new_truncating(i: i64, target_int_ty: ast::IntTy) -> Self {
45+
match target_int_ty {
46+
ast::IntTy::I16 => Is16(i as i16),
47+
ast::IntTy::I32 => Is32(i as i32),
48+
ast::IntTy::I64 => Is64(i),
49+
_ => unreachable!(),
50+
}
51+
}
4352
}

src/librustc_const_math/us.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ impl ConstUsize {
2727
(Us16(i), ast::UintTy::U16) => i as u64,
2828
(Us32(i), ast::UintTy::U32) => i as u64,
2929
(Us64(i), ast::UintTy::U64) => i,
30-
_ => panic!("got invalid usize size for target"),
30+
_ => panic!("unable to convert self ({:?}) to target usize ({:?})",
31+
self, target_uint_ty),
3132
}
3233
}
3334
pub fn new(i: u64, target_uint_ty: ast::UintTy) -> Result<Self, ConstMathErr> {
@@ -40,4 +41,12 @@ impl ConstUsize {
4041
_ => unreachable!(),
4142
}
4243
}
44+
pub fn new_truncating(i: u64, target_uint_ty: ast::UintTy) -> Self {
45+
match target_uint_ty {
46+
ast::UintTy::U16 => Us16(i as u16),
47+
ast::UintTy::U32 => Us32(i as u32),
48+
ast::UintTy::U64 => Us64(i),
49+
_ => unreachable!(),
50+
}
51+
}
4352
}

0 commit comments

Comments
 (0)