about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorTyler Mandry <tmandry@gmail.com>2020-09-10 12:20:07 -0700
committerGitHub <noreply@github.com>2020-09-10 12:20:07 -0700
commitac85a4d71eaf73ad0b729dbdbbf3623695c2dd6f (patch)
tree7ea5a425cd5b65d9f735a8c670d0d662b8cd3ad6 /src
parentf9df658aad43b3a0328052b5347cd4a8cae8328f (diff)
parent300b0acb85e41a19b518715147968f177679ebc1 (diff)
downloadrust-ac85a4d71eaf73ad0b729dbdbbf3623695c2dd6f.tar.gz
rust-ac85a4d71eaf73ad0b729dbdbbf3623695c2dd6f.zip
Rollup merge of #76559 - lcnr:const-evaluatable, r=oli-obk
add the `const_evaluatable_checked` feature

Implements a rather small subset of https://github.com/rust-lang/compiler-team/issues/340

Unlike the MCP, this does not try to compare different constant, but instead only adds the constants found in where clauses
to the predicates of a function. This PR adds the feature gate `const_evaluatable_checked`, without which nothing should change.

r? @oli-obk @eddyb
Diffstat (limited to 'src')
-rw-r--r--src/test/ui/const-generics/const_evaluatable_checked/feature-gate-const_evaluatable_checked.rs14
-rw-r--r--src/test/ui/const-generics/const_evaluatable_checked/feature-gate-const_evaluatable_checked.stderr10
-rw-r--r--src/test/ui/const-generics/const_evaluatable_checked/simple.rs14
-rw-r--r--src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs12
-rw-r--r--src/test/ui/const-generics/const_evaluatable_checked/simple_fail.stderr9
5 files changed, 59 insertions, 0 deletions
diff --git a/src/test/ui/const-generics/const_evaluatable_checked/feature-gate-const_evaluatable_checked.rs b/src/test/ui/const-generics/const_evaluatable_checked/feature-gate-const_evaluatable_checked.rs
new file mode 100644
index 00000000000..941bd5e9e5d
--- /dev/null
+++ b/src/test/ui/const-generics/const_evaluatable_checked/feature-gate-const_evaluatable_checked.rs
@@ -0,0 +1,14 @@
+#![feature(const_generics)]
+#![allow(incomplete_features)]
+
+type Arr<const N: usize> = [u8; N - 1];
+
+fn test<const N: usize>() -> Arr<N> where Arr<N>: Default {
+    //~^ ERROR constant expression depends
+    Default::default()
+}
+
+fn main() {
+    let x = test::<33>();
+    assert_eq!(x, [0; 32]);
+}
diff --git a/src/test/ui/const-generics/const_evaluatable_checked/feature-gate-const_evaluatable_checked.stderr b/src/test/ui/const-generics/const_evaluatable_checked/feature-gate-const_evaluatable_checked.stderr
new file mode 100644
index 00000000000..6e4a22a38b1
--- /dev/null
+++ b/src/test/ui/const-generics/const_evaluatable_checked/feature-gate-const_evaluatable_checked.stderr
@@ -0,0 +1,10 @@
+error: constant expression depends on a generic parameter
+  --> $DIR/feature-gate-const_evaluatable_checked.rs:6:30
+   |
+LL | fn test<const N: usize>() -> Arr<N> where Arr<N>: Default {
+   |                              ^^^^^^
+   |
+   = note: this may fail depending on what value the parameter takes
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/const-generics/const_evaluatable_checked/simple.rs b/src/test/ui/const-generics/const_evaluatable_checked/simple.rs
new file mode 100644
index 00000000000..a7ead78b97b
--- /dev/null
+++ b/src/test/ui/const-generics/const_evaluatable_checked/simple.rs
@@ -0,0 +1,14 @@
+// run-pass
+#![feature(const_generics, const_evaluatable_checked)]
+#![allow(incomplete_features)]
+
+type Arr<const N: usize> = [u8; N - 1];
+
+fn test<const N: usize>() -> Arr<N> where Arr<N>: Default {
+    Default::default()
+}
+
+fn main() {
+    let x = test::<33>();
+    assert_eq!(x, [0; 32]);
+}
diff --git a/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs
new file mode 100644
index 00000000000..1edf1885dd2
--- /dev/null
+++ b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs
@@ -0,0 +1,12 @@
+#![feature(const_generics, const_evaluatable_checked)]
+#![allow(incomplete_features)]
+
+type Arr<const N: usize> = [u8; N - 1]; //~ ERROR evaluation of constant
+
+fn test<const N: usize>() -> Arr<N> where Arr<N>: Sized {
+    todo!()
+}
+
+fn main() {
+    test::<0>();
+}
diff --git a/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.stderr b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.stderr
new file mode 100644
index 00000000000..1ac5e1d9553
--- /dev/null
+++ b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.stderr
@@ -0,0 +1,9 @@
+error[E0080]: evaluation of constant value failed
+  --> $DIR/simple_fail.rs:4:33
+   |
+LL | type Arr<const N: usize> = [u8; N - 1];
+   |                                 ^^^^^ attempt to compute `0_usize - 1_usize` which would overflow
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0080`.