]> git.lizzy.rs Git - rust.git/commitdiff
Emit the enum range assumption if the range only contains one element
authorhi-rustin <rustin.liu@gmail.com>
Thu, 11 Mar 2021 15:30:39 +0000 (23:30 +0800)
committerhi-rustin <rustin.liu@gmail.com>
Fri, 12 Mar 2021 04:06:10 +0000 (12:06 +0800)
test: add test case

make tidy happy

compiler/rustc_codegen_ssa/src/mir/rvalue.rs
src/test/codegen/enum-bounds-check-issue-82871.rs [new file with mode: 0644]

index 1795710ff533886917bac2c2b4eb0d43ce391ef5..629cb64d43ee1c55f01a5a9bce4421df09464120 100644 (file)
@@ -325,7 +325,7 @@ pub fn codegen_rvalue_operand(
 
                                 let er = scalar.valid_range_exclusive(bx.cx());
                                 if er.end != er.start
-                                    && scalar.valid_range.end() > scalar.valid_range.start()
+                                    && scalar.valid_range.end() >= scalar.valid_range.start()
                                 {
                                     // We want `table[e as usize ± k]` to not
                                     // have bound checks, and this is the most
diff --git a/src/test/codegen/enum-bounds-check-issue-82871.rs b/src/test/codegen/enum-bounds-check-issue-82871.rs
new file mode 100644 (file)
index 0000000..e779e2e
--- /dev/null
@@ -0,0 +1,16 @@
+// compile-flags: -O
+// min-llvm-version: 11.0
+
+#![crate_type = "lib"]
+
+#[repr(C)]
+pub enum E {
+    A,
+}
+
+// CHECK-LABEL: @index
+#[no_mangle]
+pub fn index(x: &[u32; 3], ind: E) -> u32{
+    // CHECK-NOT: panic_bounds_check
+    x[ind as usize]
+}