about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-03-03 13:38:42 +0000
committerbors <bors@rust-lang.org>2020-03-03 13:38:42 +0000
commitd74229b97dfca686ebd56522340d501496d2d3f2 (patch)
treebd66b38f1813f1831e9766c8847069de0575e4bf
parentb96c3ca811eae28e790c1b200be06a7d35862c6a (diff)
parent46ee6b1840b3ab3c06c9f8ba3bb097f25b5c174a (diff)
downloadrust-d74229b97dfca686ebd56522340d501496d2d3f2.tar.gz
rust-d74229b97dfca686ebd56522340d501496d2d3f2.zip
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
-rw-r--r--clippy_lints/src/consts.rs8
-rw-r--r--clippy_lints/src/indexing_slicing.rs6
-rw-r--r--clippy_lints/src/methods/mod.rs8
-rw-r--r--tests/ui/crashes/ice-5223.rs18
4 files changed, 37 insertions, 3 deletions
diff --git a/clippy_lints/src/consts.rs b/clippy_lints/src/consts.rs
index 9bb7b540d68..ceda5d1a7c3 100644
--- a/clippy_lints/src/consts.rs
+++ b/clippy_lints/src/consts.rs
@@ -231,7 +231,13 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
             ExprKind::Tup(ref tup) => self.multi(tup).map(Constant::Tuple),
             ExprKind::Repeat(ref value, _) => {
                 let n = match self.tables.expr_ty(e).kind {
-                    ty::Array(_, n) => n.eval_usize(self.lcx.tcx, self.lcx.param_env),
+                    ty::Array(_, n) => {
+                        if let Some(n) = n.try_eval_usize(self.lcx.tcx, self.lcx.param_env) {
+                            n
+                        } else {
+                            return None;
+                        }
+                    },
                     _ => span_bug!(e.span, "typeck error"),
                 };
                 self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))
diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs
index 21444ffb23d..65d2dd58106 100644
--- a/clippy_lints/src/indexing_slicing.rs
+++ b/clippy_lints/src/indexing_slicing.rs
@@ -92,7 +92,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
             if let Some(range) = higher::range(cx, index) {
                 // Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
                 if let ty::Array(_, s) = ty.kind {
-                    let size: u128 = s.eval_usize(cx.tcx, cx.param_env).into();
+                    let size: u128 = if let Some(size) = s.try_eval_usize(cx.tcx, cx.param_env) {
+                        size.into()
+                    } else {
+                        return;
+                    };
 
                     let const_range = to_const_range(cx, range, size);
 
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index 06fb9516456..eef51054090 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -2324,7 +2324,13 @@ fn derefs_to_slice<'a, 'tcx>(
             ty::Slice(_) => true,
             ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
             ty::Adt(..) => is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")),
-            ty::Array(_, size) => size.eval_usize(cx.tcx, cx.param_env) < 32,
+            ty::Array(_, size) => {
+                if let Some(size) = size.try_eval_usize(cx.tcx, cx.param_env) {
+                    size < 32
+                } else {
+                    false
+                }
+            },
             ty::Ref(_, inner, _) => may_slice(cx, inner),
             _ => false,
         }
diff --git a/tests/ui/crashes/ice-5223.rs b/tests/ui/crashes/ice-5223.rs
new file mode 100644
index 00000000000..9bb2e227fc1
--- /dev/null
+++ b/tests/ui/crashes/ice-5223.rs
@@ -0,0 +1,18 @@
+// Regression test for #5233
+
+#![feature(const_generics)]
+#![allow(incomplete_features)]
+#![warn(clippy::indexing_slicing, clippy::iter_cloned_collect)]
+
+pub struct KotomineArray<T, const N: usize> {
+    arr: [T; N],
+}
+
+impl<T: std::clone::Clone, const N: usize> KotomineArray<T, N> {
+    pub fn ice(self) {
+        let _ = self.arr[..];
+        let _ = self.arr.iter().cloned().collect::<Vec<_>>();
+    }
+}
+
+fn main() {}