about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOliver Schneider <github35764891676564198441@oli-obk.de>2018-05-18 09:41:24 +0200
committerOliver Schneider <github35764891676564198441@oli-obk.de>2018-05-19 13:10:51 +0200
commit27e710f55744534d6b0d54f507eea87bdbfe1916 (patch)
tree5c6aa5581c556400dbb6d6521f1c9453adf6e542
parentd81651e8e9af5c8c40a4bd9b6b7becf12486bdad (diff)
Add a test showing the erroneous promoted bug
-rw-r--r--src/librustc_codegen_llvm/mir/operand.rs4
-rw-r--r--src/test/ui/const-eval/promoted_const_fn_fail.rs38
-rw-r--r--src/test/ui/const-eval/promoted_const_fn_fail.stderr31
3 files changed, 71 insertions, 2 deletions
diff --git a/src/librustc_codegen_llvm/mir/operand.rs b/src/librustc_codegen_llvm/mir/operand.rs
index 62ef58f8255..caa67ef01c2 100644
--- a/src/librustc_codegen_llvm/mir/operand.rs
+++ b/src/librustc_codegen_llvm/mir/operand.rs
@@ -407,10 +407,10 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
                     .unwrap_or_else(|err| {
                         match constant.literal {
                             mir::Literal::Promoted { .. } => {
-                                // don't report errors inside promoteds, just warnings.
+                                // FIXME: generate a panic here
                             },
                             mir::Literal::Value { .. } => {
-                                err.report(bx.tcx(), constant.span, "const operand")
+                                err.report(bx.tcx(), constant.span, "const operand");
                             },
                         }
                         // We've errored, so we don't have to produce working code.
diff --git a/src/test/ui/const-eval/promoted_const_fn_fail.rs b/src/test/ui/const-eval/promoted_const_fn_fail.rs
new file mode 100644
index 00000000000..5ced2c9dd8f
--- /dev/null
+++ b/src/test/ui/const-eval/promoted_const_fn_fail.rs
@@ -0,0 +1,38 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(const_fn)]
+
+#![deny(const_err)]
+
+union Bar {
+    a: &'static u8,
+    b: usize,
+}
+
+const fn bar() -> u8 {
+    unsafe {
+        // this will error as long as this test
+        // is run on a system whose pointers need more
+        // than 8 bits
+        Bar { a: &42 }.b as u8
+        //~^ constant evaluation error
+        //~| constant evaluation error
+    }
+}
+
+fn main() {
+    // FIXME(oli-obk): this should compile but panic at runtime
+    // if we change the `const_err` lint to allow this will actually compile, but then
+    // continue with undefined values.
+    let x: &'static u8 = &(bar() + 1);
+    let y = *x;
+    unreachable!();
+}
diff --git a/src/test/ui/const-eval/promoted_const_fn_fail.stderr b/src/test/ui/const-eval/promoted_const_fn_fail.stderr
new file mode 100644
index 00000000000..f910705bb7b
--- /dev/null
+++ b/src/test/ui/const-eval/promoted_const_fn_fail.stderr
@@ -0,0 +1,31 @@
+error: constant evaluation error
+  --> $DIR/promoted_const_fn_fail.rs:25:9
+   |
+LL |         Bar { a: &42 }.b as u8
+   |         ^^^^^^^^^^^^^^^^^^^^^^ a raw memory access tried to access part of a pointer value as raw bytes
+   |
+note: lint level defined here
+  --> $DIR/promoted_const_fn_fail.rs:13:9
+   |
+LL | #![deny(const_err)]
+   |         ^^^^^^^^^
+note: inside call to `bar`
+  --> $DIR/promoted_const_fn_fail.rs:35:28
+   |
+LL |     let x: &'static u8 = &(bar() + 1);
+   |                            ^^^^^
+
+error: constant evaluation error
+  --> $DIR/promoted_const_fn_fail.rs:25:9
+   |
+LL |         Bar { a: &42 }.b as u8
+   |         ^^^^^^^^^^^^^^^^^^^^^^ a raw memory access tried to access part of a pointer value as raw bytes
+   |
+note: inside call to `bar`
+  --> $DIR/promoted_const_fn_fail.rs:35:28
+   |
+LL |     let x: &'static u8 = &(bar() + 1);
+   |                            ^^^^^
+
+error: aborting due to 2 previous errors
+