]> git.lizzy.rs Git - rust.git/commitdiff
Add tests for lint on type dependent on consts
authorkadmin <julianknodt@gmail.com>
Tue, 17 May 2022 05:50:16 +0000 (05:50 +0000)
committerkadmin <julianknodt@gmail.com>
Tue, 17 May 2022 06:40:15 +0000 (06:40 +0000)
compiler/rustc_trait_selection/src/traits/const_evaluatable.rs
src/test/ui/const-generics/generic_const_exprs/dependence_lint.full.stderr [new file with mode: 0644]
src/test/ui/const-generics/generic_const_exprs/dependence_lint.gce.stderr [new file with mode: 0644]
src/test/ui/const-generics/generic_const_exprs/dependence_lint.rs [new file with mode: 0644]
src/test/ui/const-generics/generic_const_exprs/no_dependence.rs [new file with mode: 0644]

index 27ce08ea0453f184e7b84d9d1b6c8b32d47d5835..c1b77c67c1fe2d96109c04aca47e040dccac29ec 100644 (file)
@@ -108,6 +108,9 @@ enum FailureKind {
     }
 
     let future_compat_lint = || {
     }
 
     let future_compat_lint = || {
+        if tcx.features().generic_const_exprs {
+            return;
+        }
         if let Some(local_def_id) = uv.def.did.as_local() {
             infcx.tcx.struct_span_lint_hir(
                 lint::builtin::CONST_EVALUATABLE_UNCHECKED,
         if let Some(local_def_id) = uv.def.did.as_local() {
             infcx.tcx.struct_span_lint_hir(
                 lint::builtin::CONST_EVALUATABLE_UNCHECKED,
diff --git a/src/test/ui/const-generics/generic_const_exprs/dependence_lint.full.stderr b/src/test/ui/const-generics/generic_const_exprs/dependence_lint.full.stderr
new file mode 100644 (file)
index 0000000..4cd86fe
--- /dev/null
@@ -0,0 +1,39 @@
+error: generic parameters may not be used in const operations
+  --> $DIR/dependence_lint.rs:13:32
+   |
+LL |     let _: [u8; size_of::<*mut T>()]; // error on stable, error with gce
+   |                                ^ cannot perform const operation using `T`
+   |
+   = note: type parameters may not be used in const expressions
+   = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
+
+error: generic parameters may not be used in const operations
+  --> $DIR/dependence_lint.rs:20:37
+   |
+LL |     let _: [u8; if true { size_of::<T>() } else { 3 }]; // error on stable, error with gce
+   |                                     ^ cannot perform const operation using `T`
+   |
+   = note: type parameters may not be used in const expressions
+   = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
+
+warning: cannot use constants which depend on generic parameters in types
+  --> $DIR/dependence_lint.rs:9:9
+   |
+LL |     [0; size_of::<*mut T>()]; // lint on stable, error with `generic_const_exprs`
+   |         ^^^^^^^^^^^^^^^^^^^
+   |
+   = note: `#[warn(const_evaluatable_unchecked)]` on by default
+   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
+   = note: for more information, see issue #76200 <https://github.com/rust-lang/rust/issues/76200>
+
+warning: cannot use constants which depend on generic parameters in types
+  --> $DIR/dependence_lint.rs:16:9
+   |
+LL |     [0; if false { size_of::<T>() } else { 3 }]; // lint on stable, error with gce
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
+   = note: for more information, see issue #76200 <https://github.com/rust-lang/rust/issues/76200>
+
+error: aborting due to 2 previous errors; 2 warnings emitted
+
diff --git a/src/test/ui/const-generics/generic_const_exprs/dependence_lint.gce.stderr b/src/test/ui/const-generics/generic_const_exprs/dependence_lint.gce.stderr
new file mode 100644 (file)
index 0000000..b13bcdb
--- /dev/null
@@ -0,0 +1,34 @@
+error: overly complex generic constant
+  --> $DIR/dependence_lint.rs:16:9
+   |
+LL |     [0; if false { size_of::<T>() } else { 3 }]; // lint on stable, error with gce
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ control flow is not supported in generic constants
+   |
+   = help: consider moving this anonymous constant into a `const` function
+
+error: overly complex generic constant
+  --> $DIR/dependence_lint.rs:20:17
+   |
+LL |     let _: [u8; if true { size_of::<T>() } else { 3 }]; // error on stable, error with gce
+   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ control flow is not supported in generic constants
+   |
+   = help: consider moving this anonymous constant into a `const` function
+
+error: unconstrained generic constant
+  --> $DIR/dependence_lint.rs:13:12
+   |
+LL |     let _: [u8; size_of::<*mut T>()]; // error on stable, error with gce
+   |            ^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: try adding a `where` bound using this expression: `where [(); size_of::<*mut T>()]:`
+
+error: unconstrained generic constant
+  --> $DIR/dependence_lint.rs:9:9
+   |
+LL |     [0; size_of::<*mut T>()]; // lint on stable, error with `generic_const_exprs`
+   |         ^^^^^^^^^^^^^^^^^^^
+   |
+   = help: try adding a `where` bound using this expression: `where [(); size_of::<*mut T>()]:`
+
+error: aborting due to 4 previous errors
+
diff --git a/src/test/ui/const-generics/generic_const_exprs/dependence_lint.rs b/src/test/ui/const-generics/generic_const_exprs/dependence_lint.rs
new file mode 100644 (file)
index 0000000..dcdfd75
--- /dev/null
@@ -0,0 +1,25 @@
+// revisions: full gce
+
+#![cfg_attr(gce, feature(generic_const_exprs))]
+#![allow(incomplete_features)]
+
+use std::mem::size_of;
+
+fn foo<T>() {
+    [0; size_of::<*mut T>()]; // lint on stable, error with `generic_const_exprs`
+    //[gce]~^ ERROR unconstrained
+    //[full]~^^ WARNING cannot use constants
+    //[full]~| WARNING this was previously accepted
+    let _: [u8; size_of::<*mut T>()]; // error on stable, error with gce
+    //[full]~^ ERROR generic parameters may not be used
+    //[gce]~^^ ERROR unconstrained generic
+    [0; if false { size_of::<T>() } else { 3 }]; // lint on stable, error with gce
+    //[gce]~^ ERROR overly complex
+    //[full]~^^ WARNING cannot use constants
+    //[full]~| WARNING this was previously accepted
+    let _: [u8; if true { size_of::<T>() } else { 3 }]; // error on stable, error with gce
+    //[full]~^ ERROR generic parameters may not be used
+    //[gce]~^^ ERROR overly complex
+}
+
+fn main() {}
diff --git a/src/test/ui/const-generics/generic_const_exprs/no_dependence.rs b/src/test/ui/const-generics/generic_const_exprs/no_dependence.rs
new file mode 100644 (file)
index 0000000..db8dc6e
--- /dev/null
@@ -0,0 +1,13 @@
+// check-pass
+#![feature(generic_const_exprs)]
+#![allow(incomplete_features)]
+
+fn two_args<const N: usize, const M: usize>() -> [u8; M + 2] {
+    [0; M + 2]
+}
+
+fn yay<const N: usize>() -> [u8; 4] {
+     two_args::<N, 2>() // no lint
+}
+
+fn main() {}