Skip to content

Commit 35f54fd

Browse files
authored
Rollup merge of rust-lang#126159 - RalfJung:scalarint-size-mismatch, r=oli-obk
ScalarInt: size mismatches are a bug, do not delay the panic Cc [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Why.20are.20ScalarInt.20to.20iN.2FuN.20methods.20fallible.3F) r? ``@oli-obk``
2 parents 5acc25e + 8c1f953 commit 35f54fd

File tree

6 files changed

+9
-24
lines changed

6 files changed

+9
-24
lines changed

clippy_lints/src/large_const_arrays.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeConstArrays {
5555
&& let ty = cx.tcx.type_of(item.owner_id).instantiate_identity()
5656
&& let ty::Array(element_type, cst) = ty.kind()
5757
&& let ConstKind::Value(_, ty::ValTree::Leaf(element_count)) = cst.kind()
58-
&& let Ok(element_count) = element_count.try_to_target_usize(cx.tcx)
58+
&& let element_count = element_count.to_target_usize(cx.tcx)
5959
&& let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes())
6060
&& self.maximum_allowed_size < u128::from(element_count) * u128::from(element_size)
6161
{

clippy_lints/src/large_stack_arrays.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
6565
&& !self.is_from_vec_macro(cx, expr.span)
6666
&& let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind()
6767
&& let ConstKind::Value(_, ty::ValTree::Leaf(element_count)) = cst.kind()
68-
&& let Ok(element_count) = element_count.try_to_target_usize(cx.tcx)
68+
&& let element_count = element_count.to_target_usize(cx.tcx)
6969
&& let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes())
7070
&& !cx.tcx.hir().parent_iter(expr.hir_id).any(|(_, node)| {
7171
matches!(

clippy_lints/src/non_copy_const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<'tcx> NonCopyConst<'tcx> {
199199
.any(|field| Self::is_value_unfrozen_raw_inner(cx, *field, ty)),
200200
ty::Adt(def, args) if def.is_enum() => {
201201
let (&variant_index, fields) = val.unwrap_branch().split_first().unwrap();
202-
let variant_index = VariantIdx::from_u32(variant_index.unwrap_leaf().try_to_u32().ok().unwrap());
202+
let variant_index = VariantIdx::from_u32(variant_index.unwrap_leaf().to_u32());
203203
fields
204204
.iter()
205205
.copied()

clippy_lints/src/zero_repeat_side_effects.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ impl LateLintPass<'_> for ZeroRepeatSideEffects {
5656
} else if let ExprKind::Repeat(inner_expr, _) = expr.kind
5757
&& let ty::Array(_, cst) = cx.typeck_results().expr_ty(expr).kind()
5858
&& let ConstKind::Value(_, ty::ValTree::Leaf(element_count)) = cst.kind()
59-
&& let Ok(element_count) = element_count.try_to_target_usize(cx.tcx)
60-
&& element_count == 0
59+
&& element_count.to_target_usize(cx.tcx) == 0
6160
{
6261
inner_check(cx, expr, inner_expr, false);
6362
}

clippy_utils/src/consts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -810,14 +810,14 @@ pub fn mir_to_const<'tcx>(lcx: &LateContext<'tcx>, result: mir::Const<'tcx>) ->
810810
(ConstValue::Scalar(Scalar::Int(int)), _) => match result.ty().kind() {
811811
ty::Adt(adt_def, _) if adt_def.is_struct() => Some(Constant::Adt(result)),
812812
ty::Bool => Some(Constant::Bool(int == ScalarInt::TRUE)),
813-
ty::Uint(_) | ty::Int(_) => Some(Constant::Int(int.assert_bits(int.size()))),
813+
ty::Uint(_) | ty::Int(_) => Some(Constant::Int(int.to_bits(int.size()))),
814814
ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(
815815
int.try_into().expect("invalid f32 bit representation"),
816816
))),
817817
ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(
818818
int.try_into().expect("invalid f64 bit representation"),
819819
))),
820-
ty::RawPtr(_, _) => Some(Constant::RawPtr(int.assert_bits(int.size()))),
820+
ty::RawPtr(_, _) => Some(Constant::RawPtr(int.to_bits(int.size()))),
821821
_ => None,
822822
},
823823
(_, ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Str) => {

clippy_utils/src/ty.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_middle::ty::{
2323
};
2424
use rustc_span::symbol::Ident;
2525
use rustc_span::{sym, Span, Symbol, DUMMY_SP};
26-
use rustc_target::abi::{Size, VariantIdx};
26+
use rustc_target::abi::VariantIdx;
2727
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
2828
use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
2929
use rustc_trait_selection::traits::{Obligation, ObligationCause};
@@ -865,22 +865,8 @@ impl core::ops::Add<u32> for EnumValue {
865865
pub fn read_explicit_enum_value(tcx: TyCtxt<'_>, id: DefId) -> Option<EnumValue> {
866866
if let Ok(ConstValue::Scalar(Scalar::Int(value))) = tcx.const_eval_poly(id) {
867867
match tcx.type_of(id).instantiate_identity().kind() {
868-
ty::Int(_) => Some(EnumValue::Signed(match value.size().bytes() {
869-
1 => i128::from(value.assert_bits(Size::from_bytes(1)) as u8 as i8),
870-
2 => i128::from(value.assert_bits(Size::from_bytes(2)) as u16 as i16),
871-
4 => i128::from(value.assert_bits(Size::from_bytes(4)) as u32 as i32),
872-
8 => i128::from(value.assert_bits(Size::from_bytes(8)) as u64 as i64),
873-
16 => value.assert_bits(Size::from_bytes(16)) as i128,
874-
_ => return None,
875-
})),
876-
ty::Uint(_) => Some(EnumValue::Unsigned(match value.size().bytes() {
877-
1 => value.assert_bits(Size::from_bytes(1)),
878-
2 => value.assert_bits(Size::from_bytes(2)),
879-
4 => value.assert_bits(Size::from_bytes(4)),
880-
8 => value.assert_bits(Size::from_bytes(8)),
881-
16 => value.assert_bits(Size::from_bytes(16)),
882-
_ => return None,
883-
})),
868+
ty::Int(_) => Some(EnumValue::Signed(value.to_int(value.size()))),
869+
ty::Uint(_) => Some(EnumValue::Unsigned(value.to_uint(value.size()))),
884870
_ => None,
885871
}
886872
} else {

0 commit comments

Comments
 (0)