about summary refs log tree commit diff
path: root/src/test/ui/consts
diff options
context:
space:
mode:
authorAustin Bonander <austin@launchbadge.com>2021-01-05 13:54:28 -0800
committerAustin Bonander <austin@launchbadge.com>2021-03-01 08:32:15 -0800
commit5a33f531cd87988e95a8e811d71b653a7ff9ffca (patch)
treebce9a1aa9f8927c8ef4b716018277c8300191842 /src/test/ui/consts
parentda305a2b00530aa34dea4e48389204c26fa35dbb (diff)
downloadrust-5a33f531cd87988e95a8e811d71b653a7ff9ffca.tar.gz
rust-5a33f531cd87988e95a8e811d71b653a7ff9ffca.zip
check that first arg to `panic!()` in const is `&str`
Diffstat (limited to 'src/test/ui/consts')
-rw-r--r--src/test/ui/consts/issue-66693-panic-in-array-len.rs17
-rw-r--r--src/test/ui/consts/issue-66693-panic-in-array-len.stderr19
-rw-r--r--src/test/ui/consts/issue-66693.rs24
-rw-r--r--src/test/ui/consts/issue-66693.stderr26
-rw-r--r--src/test/ui/consts/issue-76064.rs4
-rw-r--r--src/test/ui/consts/issue-76064.stderr6
6 files changed, 92 insertions, 4 deletions
diff --git a/src/test/ui/consts/issue-66693-panic-in-array-len.rs b/src/test/ui/consts/issue-66693-panic-in-array-len.rs
new file mode 100644
index 00000000000..718a19067a6
--- /dev/null
+++ b/src/test/ui/consts/issue-66693-panic-in-array-len.rs
@@ -0,0 +1,17 @@
+// This is a separate test from `issue-66693.rs` because array lengths are evaluated
+// in a separate stage before `const`s and `statics` and so the error below is hit and
+// the compiler exits before generating errors for the others.
+
+#![feature(const_panic)]
+
+fn main() {
+    let _ = [0i32; panic!(2f32)];
+    //~^ ERROR: argument to `panic!()` in a const context must have type `&str`
+
+    // ensure that conforming panics are handled correctly
+    let _ = [false; panic!()];
+    //~^ ERROR: evaluation of constant value failed
+
+    // typechecking halts before getting to this one
+    let _ = ['a', panic!("panic in array len")];
+}
diff --git a/src/test/ui/consts/issue-66693-panic-in-array-len.stderr b/src/test/ui/consts/issue-66693-panic-in-array-len.stderr
new file mode 100644
index 00000000000..e0ca9dfde0b
--- /dev/null
+++ b/src/test/ui/consts/issue-66693-panic-in-array-len.stderr
@@ -0,0 +1,19 @@
+error: argument to `panic!()` in a const context must have type `&str`
+  --> $DIR/issue-66693-panic-in-array-len.rs:8:20
+   |
+LL |     let _ = [0i32; panic!(2f32)];
+   |                    ^^^^^^^^^^^^
+   |
+   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error[E0080]: evaluation of constant value failed
+  --> $DIR/issue-66693-panic-in-array-len.rs:12:21
+   |
+LL |     let _ = [false; panic!()];
+   |                     ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/issue-66693-panic-in-array-len.rs:12:21
+   |
+   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0080`.
diff --git a/src/test/ui/consts/issue-66693.rs b/src/test/ui/consts/issue-66693.rs
new file mode 100644
index 00000000000..77fe4417d5b
--- /dev/null
+++ b/src/test/ui/consts/issue-66693.rs
@@ -0,0 +1,24 @@
+// Tests that the compiler does not ICE when const-evaluating a `panic!()` invocation with a
+// non-`&str` argument.
+
+#![feature(const_panic)]
+
+const _: () = panic!(1);
+//~^ ERROR: argument to `panic!()` in a const context must have type `&str`
+
+static _FOO: () = panic!(true);
+//~^ ERROR: argument to `panic!()` in a const context must have type `&str`
+
+const fn _foo() {
+    panic!(&1); //~ ERROR: argument to `panic!()` in a const context must have type `&str`
+}
+
+// ensure that conforming panics don't cause an error
+const _: () = panic!();
+static _BAR: () = panic!("panic in static");
+
+const fn _bar() {
+    panic!("panic in const fn");
+}
+
+fn main() {}
diff --git a/src/test/ui/consts/issue-66693.stderr b/src/test/ui/consts/issue-66693.stderr
new file mode 100644
index 00000000000..6bbde057ead
--- /dev/null
+++ b/src/test/ui/consts/issue-66693.stderr
@@ -0,0 +1,26 @@
+error: argument to `panic!()` in a const context must have type `&str`
+  --> $DIR/issue-66693.rs:13:5
+   |
+LL |     panic!(&1);
+   |     ^^^^^^^^^^^
+   |
+   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: argument to `panic!()` in a const context must have type `&str`
+  --> $DIR/issue-66693.rs:6:15
+   |
+LL | const _: () = panic!(1);
+   |               ^^^^^^^^^
+   |
+   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: argument to `panic!()` in a const context must have type `&str`
+  --> $DIR/issue-66693.rs:9:19
+   |
+LL | static _FOO: () = panic!(true);
+   |                   ^^^^^^^^^^^^
+   |
+   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 3 previous errors
+
diff --git a/src/test/ui/consts/issue-76064.rs b/src/test/ui/consts/issue-76064.rs
index 2da764b47c0..d511f75df81 100644
--- a/src/test/ui/consts/issue-76064.rs
+++ b/src/test/ui/consts/issue-76064.rs
@@ -1,3 +1,5 @@
-struct Bug([u8; panic!(1)]); //~ ERROR panicking in constants is unstable
+// Note: non-`&str` panic arguments gained a separate error in PR #80734
+// which is why this doesn't match the issue
+struct Bug([u8; panic!("panic")]); //~ ERROR panicking in constants is unstable
 
 fn main() {}
diff --git a/src/test/ui/consts/issue-76064.stderr b/src/test/ui/consts/issue-76064.stderr
index f939ff33975..9bda1b7570d 100644
--- a/src/test/ui/consts/issue-76064.stderr
+++ b/src/test/ui/consts/issue-76064.stderr
@@ -1,8 +1,8 @@
 error[E0658]: panicking in constants is unstable
-  --> $DIR/issue-76064.rs:1:17
+  --> $DIR/issue-76064.rs:3:17
    |
-LL | struct Bug([u8; panic!(1)]);
-   |                 ^^^^^^^^^
+LL | struct Bug([u8; panic!("panic")]);
+   |                 ^^^^^^^^^^^^^^^
    |
    = note: see issue #51999 <https://github.com/rust-lang/rust/issues/51999> for more information
    = help: add `#![feature(const_panic)]` to the crate attributes to enable