-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
22 changed files
with
271 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
The type of a const parameter references other generic parameters. | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,E0770 | ||
#![feature(const_generics)] | ||
fn foo<T, const N: T>() {} // error! | ||
``` | ||
|
||
To fix this error, use a concrete type for the const parameter: | ||
|
||
``` | ||
#![feature(const_generics)] | ||
fn foo<T, const N: usize>() {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/test/ui/const-generics/const-param-type-depends-on-const-param.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {} |
24 changes: 24 additions & 0 deletions
24
src/test/ui/const-generics/const-param-type-depends-on-const-param.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`. |
2 changes: 1 addition & 1 deletion
2
src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 6 additions & 5 deletions
11
src/test/ui/const-generics/const-param-type-depends-on-type-param.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.