Skip to content

Commit 327e380

Browse files
committed
ScalarInt: size mismatches are a bug, do not delay the panic
1 parent 655600c commit 327e380

File tree

46 files changed

+227
-291
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+227
-291
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub(crate) fn codegen_const_value<'tcx>(
110110
if fx.clif_type(layout.ty).is_some() {
111111
return CValue::const_val(fx, layout, int);
112112
} else {
113-
let raw_val = int.size().truncate(int.assert_bits(int.size()));
113+
let raw_val = int.size().truncate(int.to_bits(int.size()));
114114
let val = match int.size().bytes() {
115115
1 => fx.bcx.ins().iconst(types::I8, raw_val as i64),
116116
2 => fx.bcx.ins().iconst(types::I16, raw_val as i64),
@@ -501,12 +501,12 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
501501
Ordering::Equal => scalar_int,
502502
Ordering::Less => match ty.kind() {
503503
ty::Uint(_) => ScalarInt::try_from_uint(
504-
scalar_int.assert_uint(scalar_int.size()),
504+
scalar_int.to_uint(scalar_int.size()),
505505
fx.layout_of(*ty).size,
506506
)
507507
.unwrap(),
508508
ty::Int(_) => ScalarInt::try_from_int(
509-
scalar_int.assert_int(scalar_int.size()),
509+
scalar_int.to_int(scalar_int.size()),
510510
fx.layout_of(*ty).size,
511511
)
512512
.unwrap(),

compiler/rustc_codegen_cranelift/src/intrinsics/llvm_x86.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
902902
.span_fatal(span, "Index argument for `_mm_cmpestri` is not a constant");
903903
};
904904

905-
let imm8 = imm8.try_to_u8().unwrap_or_else(|_| panic!("kind not scalar: {:?}", imm8));
905+
let imm8 = imm8.to_u8();
906906

907907
codegen_inline_asm_inner(
908908
fx,
@@ -955,7 +955,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
955955
.span_fatal(span, "Index argument for `_mm_cmpestrm` is not a constant");
956956
};
957957

958-
let imm8 = imm8.try_to_u8().unwrap_or_else(|_| panic!("kind not scalar: {:?}", imm8));
958+
let imm8 = imm8.to_u8();
959959

960960
codegen_inline_asm_inner(
961961
fx,
@@ -1003,7 +1003,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
10031003
);
10041004
};
10051005

1006-
let imm8 = imm8.try_to_u8().unwrap_or_else(|_| panic!("kind not scalar: {:?}", imm8));
1006+
let imm8 = imm8.to_u8();
10071007

10081008
codegen_inline_asm_inner(
10091009
fx,
@@ -1040,7 +1040,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
10401040
);
10411041
};
10421042

1043-
let imm8 = imm8.try_to_u8().unwrap_or_else(|_| panic!("kind not scalar: {:?}", imm8));
1043+
let imm8 = imm8.to_u8();
10441044

10451045
codegen_inline_asm_inner(
10461046
fx,
@@ -1195,7 +1195,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
11951195
.span_fatal(span, "Func argument for `_mm_sha1rnds4_epu32` is not a constant");
11961196
};
11971197

1198-
let func = func.try_to_u8().unwrap_or_else(|_| panic!("kind not scalar: {:?}", func));
1198+
let func = func.to_u8();
11991199

12001200
codegen_inline_asm_inner(
12011201
fx,

compiler/rustc_codegen_cranelift/src/intrinsics/simd.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
147147

148148
let total_len = lane_count * 2;
149149

150-
let indexes =
151-
idx.iter().map(|idx| idx.unwrap_leaf().try_to_u32().unwrap()).collect::<Vec<u32>>();
150+
let indexes = idx.iter().map(|idx| idx.unwrap_leaf().to_u32()).collect::<Vec<u32>>();
152151

153152
for &idx in &indexes {
154153
assert!(u64::from(idx) < total_len, "idx {} out of range 0..{}", idx, total_len);
@@ -282,9 +281,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
282281
fx.tcx.dcx().span_fatal(span, "Index argument for `simd_insert` is not a constant");
283282
};
284283

285-
let idx: u32 = idx_const
286-
.try_to_u32()
287-
.unwrap_or_else(|_| panic!("kind not scalar: {:?}", idx_const));
284+
let idx: u32 = idx_const.to_u32();
288285
let (lane_count, _lane_ty) = base.layout().ty.simd_size_and_type(fx.tcx);
289286
if u64::from(idx) >= lane_count {
290287
fx.tcx.dcx().span_fatal(
@@ -330,9 +327,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
330327
return;
331328
};
332329

333-
let idx = idx_const
334-
.try_to_u32()
335-
.unwrap_or_else(|_| panic!("kind not scalar: {:?}", idx_const));
330+
let idx = idx_const.to_u32();
336331
let (lane_count, _lane_ty) = v.layout().ty.simd_size_and_type(fx.tcx);
337332
if u64::from(idx) >= lane_count {
338333
fx.tcx.dcx().span_fatal(

compiler/rustc_codegen_cranelift/src/value_and_place.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ impl<'tcx> CValue<'tcx> {
327327

328328
let val = match layout.ty.kind() {
329329
ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
330-
let const_val = const_val.assert_bits(layout.size);
330+
let const_val = const_val.to_bits(layout.size);
331331
let lsb = fx.bcx.ins().iconst(types::I64, const_val as u64 as i64);
332332
let msb = fx.bcx.ins().iconst(types::I64, (const_val >> 64) as u64 as i64);
333333
fx.bcx.ins().iconcat(lsb, msb)
@@ -339,7 +339,7 @@ impl<'tcx> CValue<'tcx> {
339339
| ty::Ref(..)
340340
| ty::RawPtr(..)
341341
| ty::FnPtr(..) => {
342-
let raw_val = const_val.size().truncate(const_val.assert_bits(layout.size));
342+
let raw_val = const_val.size().truncate(const_val.to_bits(layout.size));
343343
fx.bcx.ins().iconst(clif_ty, raw_val as i64)
344344
}
345345
ty::Float(FloatTy::F32) => {

compiler/rustc_codegen_gcc/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
166166
let bitsize = if layout.is_bool() { 1 } else { layout.size(self).bits() };
167167
match cv {
168168
Scalar::Int(int) => {
169-
let data = int.assert_bits(layout.size(self));
169+
let data = int.to_bits(layout.size(self));
170170

171171
// FIXME(antoyo): there's some issues with using the u128 code that follows, so hard-code
172172
// the paths for floating-point values.

compiler/rustc_codegen_llvm/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
244244
let bitsize = if layout.is_bool() { 1 } else { layout.size(self).bits() };
245245
match cv {
246246
Scalar::Int(int) => {
247-
let data = int.assert_bits(layout.size(self));
247+
let data = int.to_bits(layout.size(self));
248248
let llval = self.const_uint_big(self.type_ix(bitsize), data);
249249
if matches!(layout.primitive(), Pointer(_)) {
250250
unsafe { llvm::LLVMConstIntToPtr(llval, llty) }

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
12211221
.iter()
12221222
.enumerate()
12231223
.map(|(arg_idx, val)| {
1224-
let idx = val.unwrap_leaf().try_to_i32().unwrap();
1224+
let idx = val.unwrap_leaf().to_i32();
12251225
if idx >= i32::try_from(total_len).unwrap() {
12261226
bx.sess().dcx().emit_err(InvalidMonomorphization::SimdIndexOutOfBounds {
12271227
span,

compiler/rustc_codegen_ssa/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub fn asm_const_to_str<'tcx>(
163163
let mir::ConstValue::Scalar(scalar) = const_value else {
164164
span_bug!(sp, "expected Scalar for promoted asm const, but got {:#?}", const_value)
165165
};
166-
let value = scalar.assert_bits(ty_and_layout.size);
166+
let value = scalar.assert_scalar_int().to_bits(ty_and_layout.size);
167167
match ty_and_layout.ty.kind() {
168168
ty::Uint(_) => value.to_string(),
169169
ty::Int(int_ty) => match int_ty.normalize(tcx.sess.target.pointer_width) {

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeInterpreter<'tcx> {
549549
use rustc_middle::mir::AssertKind::*;
550550
// Convert `AssertKind<Operand>` to `AssertKind<Scalar>`.
551551
let eval_to_int =
552-
|op| ecx.read_immediate(&ecx.eval_operand(op, None)?).map(|x| x.to_const_int());
552+
|op| ecx.read_immediate(&ecx.eval_operand(op, None)?).and_then(|x| x.to_const_int());
553553
let err = match msg {
554554
BoundsCheck { len, index } => {
555555
let len = eval_to_int(len)?;

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ fn const_to_valtree_inner<'tcx>(
9595
}
9696
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => {
9797
let val = ecx.read_immediate(place)?;
98-
let val = val.to_scalar();
98+
let val = val.to_scalar_int().unwrap();
9999
*num_nodes += 1;
100100

101-
Ok(ty::ValTree::Leaf(val.assert_int()))
101+
Ok(ty::ValTree::Leaf(val))
102102
}
103103

104104
ty::Pat(base, ..) => {
@@ -125,7 +125,7 @@ fn const_to_valtree_inner<'tcx>(
125125
let val = val.to_scalar();
126126
// We are in the CTFE machine, so ptr-to-int casts will fail.
127127
// This can only be `Ok` if `val` already is an integer.
128-
let Ok(val) = val.try_to_int() else {
128+
let Ok(val) = val.try_to_scalar_int() else {
129129
return Err(ValTreeCreationError::NonSupportedType);
130130
};
131131
// It's just a ScalarInt!
@@ -411,7 +411,7 @@ fn valtree_into_mplace<'tcx>(
411411
ty::Adt(def, _) if def.is_enum() => {
412412
// First element of valtree corresponds to variant
413413
let scalar_int = branches[0].unwrap_leaf();
414-
let variant_idx = VariantIdx::from_u32(scalar_int.try_to_u32().unwrap());
414+
let variant_idx = VariantIdx::from_u32(scalar_int.to_u32());
415415
let variant = def.variant(variant_idx);
416416
debug!(?variant);
417417

0 commit comments

Comments
 (0)