forbid generic params in complex consts

This commit is contained in:
Bastian Kauschke 2020-07-28 15:55:42 +02:00
parent 375bccb8b3
commit 289e5fca7e
8 changed files with 194 additions and 18 deletions

View file

@ -0,0 +1,37 @@
#![feature(min_const_generics)]
fn test<const N: usize>() {}
fn ok<const M: usize>() -> [u8; M] {
[0; { M }]
}
struct Break0<const N: usize>([u8; { N + 1 }]);
//~^ ERROR generic parameters must not be used inside of non trivial constant values
struct Break1<const N: usize>([u8; { { N } }]);
//~^ ERROR generic parameters must not be used inside of non trivial constant values
fn break2<const N: usize>() {
let _: [u8; N + 1];
//~^ ERROR generic parameters must not be used inside of non trivial constant values
}
fn break3<const N: usize>() {
let _ = [0; N + 1];
//~^ ERROR generic parameters must not be used inside of non trivial constant values
}
trait Foo {
const ASSOC: usize;
}
impl<const N: usize> Foo for [u8; N] {
const ASSOC: usize = N + 1;
//~^ ERROR generic parameters must not be used inside of non trivial constant values
// FIXME(min_const_generics): We probably have to allow this as we can
// already allow referencing type parameters here on stable.
}
fn main() {}

View file

@ -0,0 +1,42 @@
error: generic parameters must not be used inside of non trivial constant values
--> $DIR/complex-expression.rs:9:38
|
LL | struct Break0<const N: usize>([u8; { N + 1 }]);
| ^ non-trivial anonymous constants must not depend on the parameter `N`
|
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
error: generic parameters must not be used inside of non trivial constant values
--> $DIR/complex-expression.rs:12:40
|
LL | struct Break1<const N: usize>([u8; { { N } }]);
| ^ non-trivial anonymous constants must not depend on the parameter `N`
|
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
error: generic parameters must not be used inside of non trivial constant values
--> $DIR/complex-expression.rs:16:17
|
LL | let _: [u8; N + 1];
| ^ non-trivial anonymous constants must not depend on the parameter `N`
|
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
error: generic parameters must not be used inside of non trivial constant values
--> $DIR/complex-expression.rs:21:17
|
LL | let _ = [0; N + 1];
| ^ non-trivial anonymous constants must not depend on the parameter `N`
|
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
error: generic parameters must not be used inside of non trivial constant values
--> $DIR/complex-expression.rs:30:26
|
LL | const ASSOC: usize = N + 1;
| ^ non-trivial anonymous constants must not depend on the parameter `N`
|
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
error: aborting due to 5 previous errors

View file

@ -0,0 +1,4 @@
fn test<const N: usize>() {}
//~^ ERROR const generics are unstable
fn main() {}

View file

@ -0,0 +1,12 @@
error[E0658]: const generics are unstable
--> $DIR/feature-gate-min_const_generics.rs:1:15
|
LL | fn test<const N: usize>() {}
| ^
|
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
= help: add `#![feature(const_generics)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.