Auto merge of #80547 - lqd:const_generics_defaults, r=varkor

In which we start to parse const generics defaults

As discussed in this [zulip topic](https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/const.20generic.20defaults), this PR extracts the parsing parts from `@JulianKnodt's` PR #75384 for a better user-experience using the newly stabilized `min_const_generics` (albeit temporary) as shown in #80507: trying to use default values on const generics currently results in parse errors, as if the user didn't use the correct syntax (which is somewhat true but also misleading).

This PR extracts (and slightly modifies in a couple places) `@JulianKnodt's` parsing code (with attribution if I've done everything correctly), AST and HIR changes, and feature gate setup.

This feature is now marked as "incomplete" and thus will also print out the expected "const generics default values are unstable" error instead of a syntax error. Note that, as I've only extracted the parsing part, the actual feature will not work at all if enabled. There will be ICEs, and inference errors on the const generics default values themselves.

Fixes #80507.

Once this merges, I'll:
- modify the const generics tracking issue to refer to the `const_generics_defaults` gate rather than the older temporary name it uses there.
- create the GH `F-const_generics_defaults` label

r? `@varkor`
This commit is contained in:
bors 2021-01-01 13:17:42 +00:00
commit a609fb45ef
37 changed files with 160 additions and 53 deletions

View file

@ -1,4 +1,4 @@
fn foo<const SIZE: usize = 5>() {}
//~^ ERROR expected one of `!`, `(`, `+`, `,`, `::`, `<`, or `>`, found `=`
//~^ ERROR default values for const generic parameters are experimental
fn main() {}

View file

@ -1,8 +1,12 @@
error: expected one of `!`, `(`, `+`, `,`, `::`, `<`, or `>`, found `=`
error[E0658]: default values for const generic parameters are experimental
--> $DIR/default_function_param.rs:1:26
|
LL | fn foo<const SIZE: usize = 5>() {}
| ^ expected one of 7 possible tokens
| ^^^
|
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
= help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.

View file

@ -1,4 +1,4 @@
trait Foo<const KIND: bool = true> {}
//~^ ERROR expected one of `!`, `(`, `+`, `,`, `::`, `<`, or `>`, found `=`
//~^ ERROR default values for const generic parameters are experimental
fn main() {}

View file

@ -1,8 +1,12 @@
error: expected one of `!`, `(`, `+`, `,`, `::`, `<`, or `>`, found `=`
error[E0658]: default values for const generic parameters are experimental
--> $DIR/default_trait_param.rs:1:28
|
LL | trait Foo<const KIND: bool = true> {}
| ^ expected one of 7 possible tokens
| ^^^^^^
|
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
= help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.

View file

@ -7,7 +7,7 @@ struct Foo<T, U = [u8; std::mem::size_of::<T>()]>(T, U);
//[full]~^ ERROR constant values inside of type parameter defaults
//[min]~^^ ERROR generic parameters may not be used in const operations
// FIXME(const_generics:defaults): We still don't know how to we deal with type defaults.
// FIXME(const_generics_defaults): We still don't know how to deal with type defaults.
struct Bar<T = [u8; N], const N: usize>(T);
//~^ ERROR constant values inside of type parameter defaults
//~| ERROR type parameters with a default

View file

@ -0,0 +1,9 @@
#[cfg(FALSE)]
struct A<const N: usize = 3>;
//~^ ERROR default values for const generic parameters are experimental
#[cfg(FALSE)]
fn foo<const B: bool = false>() {}
//~^ ERROR default values for const generic parameters are experimental
fn main() {}

View file

@ -0,0 +1,21 @@
error[E0658]: default values for const generic parameters are experimental
--> $DIR/feature-gate-const_generics_defaults.rs:2:25
|
LL | struct A<const N: usize = 3>;
| ^^^
|
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
= help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
error[E0658]: default values for const generic parameters are experimental
--> $DIR/feature-gate-const_generics_defaults.rs:6:22
|
LL | fn foo<const B: bool = false>() {}
| ^^^^^^^
|
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
= help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0658`.