Skip to content

Commit d74229b

Browse files
committed
Auto merge of #5256 - JohnTitor:try-eval-usize, r=phansch
Use `try_eval_usize` over `eval_usize` Fixes #5223 changelog: Fix ICE in evaluating usizes
2 parents b96c3ca + 46ee6b1 commit d74229b

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

clippy_lints/src/consts.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,13 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
231231
ExprKind::Tup(ref tup) => self.multi(tup).map(Constant::Tuple),
232232
ExprKind::Repeat(ref value, _) => {
233233
let n = match self.tables.expr_ty(e).kind {
234-
ty::Array(_, n) => n.eval_usize(self.lcx.tcx, self.lcx.param_env),
234+
ty::Array(_, n) => {
235+
if let Some(n) = n.try_eval_usize(self.lcx.tcx, self.lcx.param_env) {
236+
n
237+
} else {
238+
return None;
239+
}
240+
},
235241
_ => span_bug!(e.span, "typeck error"),
236242
};
237243
self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))

clippy_lints/src/indexing_slicing.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
9292
if let Some(range) = higher::range(cx, index) {
9393
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
9494
if let ty::Array(_, s) = ty.kind {
95-
let size: u128 = s.eval_usize(cx.tcx, cx.param_env).into();
95+
let size: u128 = if let Some(size) = s.try_eval_usize(cx.tcx, cx.param_env) {
96+
size.into()
97+
} else {
98+
return;
99+
};
96100

97101
let const_range = to_const_range(cx, range, size);
98102

clippy_lints/src/methods/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2324,7 +2324,13 @@ fn derefs_to_slice<'a, 'tcx>(
23242324
ty::Slice(_) => true,
23252325
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
23262326
ty::Adt(..) => is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")),
2327-
ty::Array(_, size) => size.eval_usize(cx.tcx, cx.param_env) < 32,
2327+
ty::Array(_, size) => {
2328+
if let Some(size) = size.try_eval_usize(cx.tcx, cx.param_env) {
2329+
size < 32
2330+
} else {
2331+
false
2332+
}
2333+
},
23282334
ty::Ref(_, inner, _) => may_slice(cx, inner),
23292335
_ => false,
23302336
}

tests/ui/crashes/ice-5223.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Regression test for #5233
2+
3+
#![feature(const_generics)]
4+
#![allow(incomplete_features)]
5+
#![warn(clippy::indexing_slicing, clippy::iter_cloned_collect)]
6+
7+
pub struct KotomineArray<T, const N: usize> {
8+
arr: [T; N],
9+
}
10+
11+
impl<T: std::clone::Clone, const N: usize> KotomineArray<T, N> {
12+
pub fn ice(self) {
13+
let _ = self.arr[..];
14+
let _ = self.arr.iter().cloned().collect::<Vec<_>>();
15+
}
16+
}
17+
18+
fn main() {}

0 commit comments

Comments
 (0)