Rollup merge of #74159 - lcnr:const-generic-ty-decl, r=varkor

forbid generic params in the type of const params

implements and closes #74152

fixes #74101, closes #71169, fixes #73491, closes #62878

@eddyb and I talked [on zulip](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/type.20of.20const.20parameters/near/203405696) about this and we probably want to also forbid generic consts in the default
type of a parameter, e.g. `struct Foo<T, U = [u8; std::mem::size_of::<T>()]>`, this is currently still allowed
and I will probably fix that in a followup PR.

r? @varkor @eddyb
This commit is contained in:
Manish Goregaokar 2020-07-16 11:18:46 -07:00 committed by GitHub
commit 6ef0dfa42f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 271 additions and 36 deletions

View file

@ -0,0 +1,15 @@
#![feature(const_generics)]
//~^ WARN the feature `const_generics` is incomplete
// Currently, const parameters cannot depend on other generic parameters,
// as our current implementation can't really support this.
//
// We may want to lift this restriction in the future.
pub struct Dependent<const N: usize, const X: [u8; N]>([(); N]);
//~^ ERROR: the type of const parameters must not depend on other generic parameters
pub struct SelfDependent<const N: [u8; N]>;
//~^ ERROR: the type of const parameters must not depend on other generic parameters
fn main() {}

View file

@ -0,0 +1,24 @@
error[E0770]: the type of const parameters must not depend on other generic parameters
--> $DIR/const-param-type-depends-on-const-param.rs:9:52
|
LL | pub struct Dependent<const N: usize, const X: [u8; N]>([(); N]);
| ^ the type must not depend on the parameter `N`
error[E0770]: the type of const parameters must not depend on other generic parameters
--> $DIR/const-param-type-depends-on-const-param.rs:12:40
|
LL | pub struct SelfDependent<const N: [u8; N]>;
| ^ the type must not depend on the parameter `N`
warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/const-param-type-depends-on-const-param.rs:1:12
|
LL | #![feature(const_generics)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
error: aborting due to 2 previous errors; 1 warning emitted
For more information about this error, try `rustc --explain E0770`.

View file

@ -1,6 +1,6 @@
use std::marker::PhantomData;
struct B<T, const N: T>(PhantomData<[T; N]>); //~ ERROR const generics are unstable
//~^ ERROR `T` is not guaranteed to `#[derive(PartialEq, Eq)]`
//~^ ERROR the type of const parameters must not depend on other generic parameters
fn main() {}

View file

@ -1,3 +1,9 @@
error[E0770]: the type of const parameters must not depend on other generic parameters
--> $DIR/const-param-type-depends-on-type-param-ungated.rs:3:22
|
LL | struct B<T, const N: T>(PhantomData<[T; N]>);
| ^ the type must not depend on the parameter `T`
error[E0658]: const generics are unstable
--> $DIR/const-param-type-depends-on-type-param-ungated.rs:3:19
|
@ -7,15 +13,7 @@ LL | struct B<T, const N: T>(PhantomData<[T; N]>);
= 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[E0741]: `T` is not guaranteed to `#[derive(PartialEq, Eq)]`, so may not be used as the type of a const parameter
--> $DIR/const-param-type-depends-on-type-param-ungated.rs:3:22
|
LL | struct B<T, const N: T>(PhantomData<[T; N]>);
| ^ `T` may not derive both `PartialEq` and `Eq`
|
= note: it is not currently possible to use a type parameter as the type of a const parameter
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0658, E0741.
Some errors have detailed explanations: E0658, E0770.
For more information about an error, try `rustc --explain E0658`.

View file

@ -1,12 +1,13 @@
#![feature(const_generics)]
//~^ WARN the feature `const_generics` is incomplete
// Currently, const parameters cannot depend on type parameters, because there is no way to
// enforce the structural-match property on an arbitrary type parameter. This restriction
// may be relaxed in the future. See https://github.com/rust-lang/rfcs/pull/2000 for more
// details.
// Currently, const parameters cannot depend on other generic parameters,
// as our current implementation can't really support this.
//
// We may want to lift this restriction in the future.
pub struct Dependent<T, const X: T>([(); X]);
//~^ ERROR `T` is not guaranteed to `#[derive(PartialEq, Eq)]`
//~^ ERROR: the type of const parameters must not depend on other generic parameters
//~| ERROR: parameter `T` is never used
fn main() {}

View file

@ -1,3 +1,9 @@
error[E0770]: the type of const parameters must not depend on other generic parameters
--> $DIR/const-param-type-depends-on-type-param.rs:9:34
|
LL | pub struct Dependent<T, const X: T>([(); X]);
| ^ the type must not depend on the parameter `T`
warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/const-param-type-depends-on-type-param.rs:1:12
|
@ -7,14 +13,15 @@ LL | #![feature(const_generics)]
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
error[E0741]: `T` is not guaranteed to `#[derive(PartialEq, Eq)]`, so may not be used as the type of a const parameter
--> $DIR/const-param-type-depends-on-type-param.rs:9:34
error[E0392]: parameter `T` is never used
--> $DIR/const-param-type-depends-on-type-param.rs:9:22
|
LL | pub struct Dependent<T, const X: T>([(); X]);
| ^ `T` may not derive both `PartialEq` and `Eq`
| ^ unused parameter
|
= note: it is not currently possible to use a type parameter as the type of a const parameter
= help: consider removing `T`, referring to it in a field, or using a marker such as `std::marker::PhantomData`
error: aborting due to previous error; 1 warning emitted
error: aborting due to 2 previous errors; 1 warning emitted
For more information about this error, try `rustc --explain E0741`.
Some errors have detailed explanations: E0392, E0770.
For more information about an error, try `rustc --explain E0392`.

View file

@ -0,0 +1,10 @@
#![feature(const_generics)]
#![allow(incomplete_features)]
fn foo<const LEN: usize, const DATA: [u8; LEN]>() {}
//~^ ERROR the type of const parameters must not
fn main() {
const DATA: [u8; 4] = *b"ABCD";
foo::<4, DATA>();
//~^ ERROR constant expression depends on
}

View file

@ -0,0 +1,17 @@
error[E0770]: the type of const parameters must not depend on other generic parameters
--> $DIR/issue-71169.rs:4:43
|
LL | fn foo<const LEN: usize, const DATA: [u8; LEN]>() {}
| ^^^ the type must not depend on the parameter `LEN`
error: constant expression depends on a generic parameter
--> $DIR/issue-71169.rs:8:14
|
LL | foo::<4, DATA>();
| ^^^^
|
= note: this may fail depending on what value the parameter takes
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0770`.

View file

@ -12,6 +12,7 @@ unsafe extern "C" fn pass(args: PassArg) {
impl Test {
pub fn call_me<Args: Sized, const IDX: usize, const FN: unsafe extern "C" fn(Args)>(&self) {
//~^ ERROR: using function pointers as const generic parameters is forbidden
//~| ERROR: the type of const parameters must not depend on other generic parameters
self.0 = Self::trampiline::<Args, IDX, FN> as _
}
@ -20,6 +21,7 @@ impl Test {
const IDX: usize,
const FN: unsafe extern "C" fn(Args),
//~^ ERROR: using function pointers as const generic parameters is forbidden
//~| ERROR: the type of const parameters must not depend on other generic parameters
>(
args: Args,
) {

View file

@ -1,3 +1,15 @@
error[E0770]: the type of const parameters must not depend on other generic parameters
--> $DIR/issue-71381.rs:13:82
|
LL | pub fn call_me<Args: Sized, const IDX: usize, const FN: unsafe extern "C" fn(Args)>(&self) {
| ^^^^ the type must not depend on the parameter `Args`
error[E0770]: the type of const parameters must not depend on other generic parameters
--> $DIR/issue-71381.rs:22:40
|
LL | const FN: unsafe extern "C" fn(Args),
| ^^^^ the type must not depend on the parameter `Args`
error: using function pointers as const generic parameters is forbidden
--> $DIR/issue-71381.rs:13:61
|
@ -5,10 +17,11 @@ LL | pub fn call_me<Args: Sized, const IDX: usize, const FN: unsafe extern "
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: using function pointers as const generic parameters is forbidden
--> $DIR/issue-71381.rs:21:19
--> $DIR/issue-71381.rs:22:19
|
LL | const FN: unsafe extern "C" fn(Args),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0770`.

View file

@ -3,6 +3,7 @@
fn func<A, const F: fn(inner: A)>(outer: A) {
//~^ ERROR: using function pointers as const generic parameters is forbidden
//~| ERROR: the type of const parameters must not depend on other generic parameters
F(outer);
}

View file

@ -1,8 +1,15 @@
error[E0770]: the type of const parameters must not depend on other generic parameters
--> $DIR/issue-71611.rs:4:31
|
LL | fn func<A, const F: fn(inner: A)>(outer: A) {
| ^ the type must not depend on the parameter `A`
error: using function pointers as const generic parameters is forbidden
--> $DIR/issue-71611.rs:4:21
|
LL | fn func<A, const F: fn(inner: A)>(outer: A) {
| ^^^^^^^^^^^^
error: aborting due to previous error
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0770`.

View file

@ -0,0 +1,9 @@
// check-pass
#![feature(const_generics)]
#![allow(incomplete_features)]
const LEN: usize = 1024;
fn hoge<const IN: [u32; LEN]>() {}
fn main() {}

View file

@ -0,0 +1,9 @@
// check-pass
#![feature(const_generics)]
#![allow(incomplete_features)]
fn test<const N: [u8; 1 + 2]>() {}
struct Foo<const N: [u8; 1 + 2]>;
fn main() {}