loosen ordering restricts for const_generics_defaults

This commit is contained in:
lcnr 2021-04-18 14:31:00 +02:00
parent 259a368e9e
commit 7cb1dcd488
31 changed files with 145 additions and 81 deletions

View File

@ -754,7 +754,7 @@ fn validate_generic_param_order(
GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident), GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident),
GenericParamKind::Const { ref ty, kw_span: _, default: _ } => { GenericParamKind::Const { ref ty, kw_span: _, default: _ } => {
let ty = pprust::ty_to_string(ty); let ty = pprust::ty_to_string(ty);
let unordered = sess.features_untracked().const_generics; let unordered = sess.features_untracked().unordered_const_ty_params();
(ParamKindOrd::Const { unordered }, Some(format!("const {}: {}", param.ident, ty))) (ParamKindOrd::Const { unordered }, Some(format!("const {}: {}", param.ident, ty)))
} }
}; };

View File

@ -63,6 +63,10 @@ macro_rules! declare_features {
_ => panic!("`{}` was not listed in `declare_features`", feature), _ => panic!("`{}` was not listed in `declare_features`", feature),
} }
} }
pub fn unordered_const_ty_params(&self) -> bool {
self.const_generics || self.const_generics_defaults
}
} }
}; };
} }

View File

@ -296,7 +296,9 @@ impl GenericArg<'_> {
match self { match self {
GenericArg::Lifetime(_) => ast::ParamKindOrd::Lifetime, GenericArg::Lifetime(_) => ast::ParamKindOrd::Lifetime,
GenericArg::Type(_) => ast::ParamKindOrd::Type, GenericArg::Type(_) => ast::ParamKindOrd::Type,
GenericArg::Const(_) => ast::ParamKindOrd::Const { unordered: feats.const_generics }, GenericArg::Const(_) => {
ast::ParamKindOrd::Const { unordered: feats.unordered_const_ty_params() }
}
} }
} }
} }

View File

@ -36,7 +36,7 @@ impl GenericParamDefKind {
GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime, GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime,
GenericParamDefKind::Type { .. } => ast::ParamKindOrd::Type, GenericParamDefKind::Type { .. } => ast::ParamKindOrd::Type,
GenericParamDefKind::Const { .. } => { GenericParamDefKind::Const { .. } => {
ast::ParamKindOrd::Const { unordered: tcx.features().const_generics } ast::ParamKindOrd::Const { unordered: tcx.features().unordered_const_ty_params() }
} }
} }
} }

View File

@ -286,7 +286,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
ParamKindOrd::Const { ParamKindOrd::Const {
unordered: tcx unordered: tcx
.features() .features()
.const_generics, .unordered_const_ty_params(),
} }
} }
}, },
@ -309,7 +309,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
GenericArg::Lifetime(_) => ParamKindOrd::Lifetime, GenericArg::Lifetime(_) => ParamKindOrd::Lifetime,
GenericArg::Type(_) => ParamKindOrd::Type, GenericArg::Type(_) => ParamKindOrd::Type,
GenericArg::Const(_) => ParamKindOrd::Const { GenericArg::Const(_) => ParamKindOrd::Const {
unordered: tcx.features().const_generics, unordered: tcx
.features()
.unordered_const_ty_params(),
}, },
}), }),
Some(&format!( Some(&format!(

View File

@ -1,4 +1,4 @@
#![feature(const_generics)] #![cfg_attr(full, feature(const_generics))]
#![feature(const_generics_defaults)] #![feature(const_generics_defaults)]
#![allow(incomplete_features)] #![allow(incomplete_features)]

View File

@ -1,5 +1,5 @@
error[E0277]: the size for values of type `T` cannot be known at compilation time error[E0277]: the size for values of type `T` cannot be known at compilation time
--> $DIR/complex-generic-default-expr.rs:6:62 --> $DIR/complex-generic-default-expr.rs:9:62
| |
LL | struct Bar<T, const TYPE_SIZE: usize = { std::mem::size_of::<T>() }>(T); LL | struct Bar<T, const TYPE_SIZE: usize = { std::mem::size_of::<T>() }>(T);
| - ^ doesn't have a size known at compile-time | - ^ doesn't have a size known at compile-time

View File

@ -0,0 +1,20 @@
error: generic parameters may not be used in const operations
--> $DIR/complex-generic-default-expr.rs:6:47
|
LL | struct Foo<const N: usize, const M: usize = { N + 1 }>;
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments, i.e. `N`
= help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions
error: generic parameters may not be used in const operations
--> $DIR/complex-generic-default-expr.rs:9:62
|
LL | struct Bar<T, const TYPE_SIZE: usize = { std::mem::size_of::<T>() }>(T);
| ^ cannot perform const operation using `T`
|
= note: type parameters may not be used in const expressions
= help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions
error: aborting due to 2 previous errors

View File

@ -1,9 +1,13 @@
#![feature(const_generics, const_generics_defaults)] // revisions: full min
#![cfg_attr(full, feature(const_generics))]
#![feature(const_generics_defaults)]
#![allow(incomplete_features)] #![allow(incomplete_features)]
struct Foo<const N: usize, const M: usize = { N + 1 }>; struct Foo<const N: usize, const M: usize = { N + 1 }>;
//[min]~^ ERROR generic parameters may not be used in const operations
struct Bar<T, const TYPE_SIZE: usize = { std::mem::size_of::<T>() }>(T); struct Bar<T, const TYPE_SIZE: usize = { std::mem::size_of::<T>() }>(T);
//~^ ERROR the size for values of type `T` cannot be known at compilation time //[min]~^ ERROR generic parameters may not be used in const operations
//[full]~^^ ERROR the size for values of type `T` cannot be known at compilation time
fn main() {} fn main() {}

View File

@ -1,6 +1,6 @@
// run-pass // run-pass
// revisions: full min
#![feature(const_generics)] #![cfg_attr(full, feature(const_generics))]
#![feature(const_generics_defaults)] #![feature(const_generics_defaults)]
#![allow(incomplete_features)] #![allow(incomplete_features)]

View File

@ -1,5 +1,5 @@
error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
--> $DIR/default-on-impl.rs:6:12 --> $DIR/default-on-impl.rs:8:12
| |
LL | impl<const N: usize = 1> Foo<N> {} LL | impl<const N: usize = 1> Foo<N> {}
| ^ | ^

View File

@ -0,0 +1,8 @@
error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
--> $DIR/default-on-impl.rs:8:12
|
LL | impl<const N: usize = 1> Foo<N> {}
| ^
error: aborting due to previous error

View File

@ -1,4 +1,6 @@
#![feature(const_generics, const_generics_defaults)] // revisions: full min
#![cfg_attr(full, feature(const_generics))]
#![feature(const_generics_defaults)]
#![allow(incomplete_features)] #![allow(incomplete_features)]
struct Foo<const N: usize>; struct Foo<const N: usize>;

View File

@ -1,5 +1,7 @@
// aux-build:const_defaulty.rs // aux-build:const_defaulty.rs
// check-pass // check-pass
// revisions: full min
#![cfg_attr(full, feature(const_generics))]
#![feature(const_generics_defaults)] #![feature(const_generics_defaults)]
#![allow(incomplete_features)] #![allow(incomplete_features)]

View File

@ -1,5 +1,5 @@
error: lifetime parameters must be declared prior to const parameters error: lifetime parameters must be declared prior to const parameters
--> $DIR/intermixed-lifetime.rs:6:28 --> $DIR/intermixed-lifetime.rs:7:28
| |
LL | struct Foo<const N: usize, 'a, T = u32>(&'a (), T); LL | struct Foo<const N: usize, 'a, T = u32>(&'a (), T);
| -----------------^^---------- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>` | -----------------^^---------- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>`

View File

@ -1,26 +1,14 @@
error: lifetime parameters must be declared prior to const parameters error: lifetime parameters must be declared prior to const parameters
--> $DIR/intermixed-lifetime.rs:6:28 --> $DIR/intermixed-lifetime.rs:7:28
| |
LL | struct Foo<const N: usize, 'a, T = u32>(&'a (), T); LL | struct Foo<const N: usize, 'a, T = u32>(&'a (), T);
| -----------------^^---------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = u32, const N: usize>` | -----------------^^---------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, const N: usize, T = u32>`
error: type parameters must be declared prior to const parameters error: lifetime parameters must be declared prior to type parameters
--> $DIR/intermixed-lifetime.rs:6:32
|
LL | struct Foo<const N: usize, 'a, T = u32>(&'a (), T);
| ---------------------^------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = u32, const N: usize>`
error: lifetime parameters must be declared prior to const parameters
--> $DIR/intermixed-lifetime.rs:10:37 --> $DIR/intermixed-lifetime.rs:10:37
| |
LL | struct Bar<const N: usize, T = u32, 'a>(&'a (), T); LL | struct Bar<const N: usize, T = u32, 'a>(&'a (), T);
| --------------------------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = u32, const N: usize>` | --------------------------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, const N: usize, T = u32>`
error: type parameters must be declared prior to const parameters error: aborting due to 2 previous errors
--> $DIR/intermixed-lifetime.rs:10:28
|
LL | struct Bar<const N: usize, T = u32, 'a>(&'a (), T);
| -----------------^----------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = u32, const N: usize>`
error: aborting due to 4 previous errors

View File

@ -1,15 +1,13 @@
// revisions: full min
// Checks that lifetimes cannot be interspersed between consts and types. // Checks that lifetimes cannot be interspersed between consts and types.
// revisions: full min
#![cfg_attr(full, feature(const_generics))] #![cfg_attr(full, feature(const_generics))]
#![cfg_attr(full, allow(incomplete_features))] #![feature(const_generics_defaults)]
#![allow(incomplete_features)]
struct Foo<const N: usize, 'a, T = u32>(&'a (), T); struct Foo<const N: usize, 'a, T = u32>(&'a (), T);
//~^ Error lifetime parameters must be declared prior to const parameters //~^ Error lifetime parameters must be declared prior to const parameters
//[min]~^^ Error type parameters must be declared prior to const parameters
struct Bar<const N: usize, T = u32, 'a>(&'a (), T); struct Bar<const N: usize, T = u32, 'a>(&'a (), T);
//[full]~^ Error lifetime parameters must be declared prior to type parameters //~^ Error lifetime parameters must be declared prior to type parameters
//[min]~^^ Error type parameters must be declared prior to const parameters
//[min]~| Error lifetime parameters must be declared prior to const parameters
fn main() {} fn main() {}

View File

@ -1,5 +1,5 @@
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/mismatch.rs:11:28 --> $DIR/mismatch.rs:12:28
| |
LL | let e: Example::<13> = (); LL | let e: Example::<13> = ();
| ------------- ^^ expected struct `Example`, found `()` | ------------- ^^ expected struct `Example`, found `()`
@ -7,7 +7,7 @@ LL | let e: Example::<13> = ();
| expected due to this | expected due to this
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/mismatch.rs:13:34 --> $DIR/mismatch.rs:14:34
| |
LL | let e: Example2::<u32, 13> = (); LL | let e: Example2::<u32, 13> = ();
| ------------------- ^^ expected struct `Example2`, found `()` | ------------------- ^^ expected struct `Example2`, found `()`
@ -18,7 +18,7 @@ LL | let e: Example2::<u32, 13> = ();
found unit type `()` found unit type `()`
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/mismatch.rs:15:34 --> $DIR/mismatch.rs:16:34
| |
LL | let e: Example3::<13, u32> = (); LL | let e: Example3::<13, u32> = ();
| ------------------- ^^ expected struct `Example3`, found `()` | ------------------- ^^ expected struct `Example3`, found `()`
@ -29,7 +29,7 @@ LL | let e: Example3::<13, u32> = ();
found unit type `()` found unit type `()`
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/mismatch.rs:17:28 --> $DIR/mismatch.rs:18:28
| |
LL | let e: Example3::<7> = (); LL | let e: Example3::<7> = ();
| ------------- ^^ expected struct `Example3`, found `()` | ------------- ^^ expected struct `Example3`, found `()`
@ -40,7 +40,7 @@ LL | let e: Example3::<7> = ();
found unit type `()` found unit type `()`
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/mismatch.rs:21:28 --> $DIR/mismatch.rs:22:28
| |
LL | let e: Example4::<7> = (); LL | let e: Example4::<7> = ();
| ------------- ^^ expected struct `Example4`, found `()` | ------------- ^^ expected struct `Example4`, found `()`

View File

@ -0,0 +1,52 @@
error[E0308]: mismatched types
--> $DIR/mismatch.rs:12:28
|
LL | let e: Example::<13> = ();
| ------------- ^^ expected struct `Example`, found `()`
| |
| expected due to this
error[E0308]: mismatched types
--> $DIR/mismatch.rs:14:34
|
LL | let e: Example2::<u32, 13> = ();
| ------------------- ^^ expected struct `Example2`, found `()`
| |
| expected due to this
|
= note: expected struct `Example2`
found unit type `()`
error[E0308]: mismatched types
--> $DIR/mismatch.rs:16:34
|
LL | let e: Example3::<13, u32> = ();
| ------------------- ^^ expected struct `Example3`, found `()`
| |
| expected due to this
|
= note: expected struct `Example3`
found unit type `()`
error[E0308]: mismatched types
--> $DIR/mismatch.rs:18:28
|
LL | let e: Example3::<7> = ();
| ------------- ^^ expected struct `Example3`, found `()`
| |
| expected due to this
|
= note: expected struct `Example3<7_usize>`
found unit type `()`
error[E0308]: mismatched types
--> $DIR/mismatch.rs:22:28
|
LL | let e: Example4::<7> = ();
| ------------- ^^ expected struct `Example4`, found `()`
| |
| expected due to this
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0308`.

View File

@ -1,4 +1,5 @@
#![feature(const_generics)] // revisions: full min
#![cfg_attr(full, feature(const_generics))]
#![feature(const_generics_defaults)] #![feature(const_generics_defaults)]
#![allow(incomplete_features)] #![allow(incomplete_features)]

View File

@ -18,4 +18,3 @@ fn foo<const SIZE : usize = 5>() { }
struct Range<const FROM : usize = 0, const LEN : usize = 0, const TO : usize = struct Range<const FROM : usize = 0, const LEN : usize = 0, const TO : usize =
FROM>; FROM>;

View File

@ -6,7 +6,7 @@
#![allow(incomplete_features)] #![allow(incomplete_features)]
#[repr(C)] #[repr(C)]
pub struct Loaf<T: Sized, const N: usize = 1usize> { pub struct Loaf<T: Sized, const N: usize = 1> {
head: [T; N], head: [T; N],
slice: [T], slice: [T],
} }

View File

@ -1,8 +0,0 @@
error: type parameters must be declared prior to const parameters
--> $DIR/simple-defaults.rs:8:40
|
LL | struct FixedOutput<'a, const N: usize, T=u32> {
| ---------------------^----- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = u32, const N: usize>`
error: aborting due to previous error

View File

@ -1,12 +1,12 @@
// [full] run-pass // run-pass
// revisions: min full // Checks that type param defaults are allowed after const params.
// Checks some basic test cases for defaults. // revisions: full min
#![cfg_attr(full, feature(const_generics))] #![cfg_attr(full, feature(const_generics))]
#![cfg_attr(full, allow(incomplete_features))] #![feature(const_generics_defaults)]
#![allow(incomplete_features)]
#![allow(dead_code)] #![allow(dead_code)]
struct FixedOutput<'a, const N: usize, T=u32> { struct FixedOutput<'a, const N: usize, T=u32> {
//[min]~^ ERROR type parameters must be declared prior to const parameters
out: &'a [T; N], out: &'a [T; N],
} }

View File

@ -1,5 +1,7 @@
// check-pass // check-pass
#![feature(const_generics, const_generics_defaults)] // revisions: full min
#![cfg_attr(full, feature(const_generics))]
#![feature(const_generics_defaults)]
#![allow(incomplete_features)] #![allow(incomplete_features)]
struct N; struct N;

View File

@ -1,19 +1,8 @@
error: generic parameters with a default must be trailing error: generic parameters with a default must be trailing
--> $DIR/wrong-order.rs:4:10 --> $DIR/wrong-order.rs:6:10
| |
LL | struct A<T = u32, const N: usize> { LL | struct A<T = u32, const N: usize> {
| ^ | ^
|
= note: using type defaults and const parameters in the same parameter list is currently not permitted
warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes error: aborting due to previous error
--> $DIR/wrong-order.rs:2:27
|
LL | #![cfg_attr(full, 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 previous error; 1 warning emitted

View File

@ -1,10 +1,8 @@
error: generic parameters with a default must be trailing error: generic parameters with a default must be trailing
--> $DIR/wrong-order.rs:4:10 --> $DIR/wrong-order.rs:6:10
| |
LL | struct A<T = u32, const N: usize> { LL | struct A<T = u32, const N: usize> {
| ^ | ^
|
= note: using type defaults and const parameters in the same parameter list is currently not permitted
error: aborting due to previous error error: aborting due to previous error

View File

@ -1,5 +1,7 @@
// revisions: full min // revisions: full min
#![cfg_attr(full, feature(const_generics))] //[full]~WARN the feature `const_generics` is incomplete #![cfg_attr(full, feature(const_generics))]
#![feature(const_generics_defaults)]
#![allow(incomplete_features)]
struct A<T = u32, const N: usize> { struct A<T = u32, const N: usize> {
//~^ ERROR generic parameters with a default must be trailing //~^ ERROR generic parameters with a default must be trailing

View File

@ -1,5 +1,5 @@
error: generic parameters with a default must be trailing error: generic parameters with a default must be trailing
--> $DIR/params-in-ct-in-ty-param-lazy-norm.rs:11:12 --> $DIR/params-in-ct-in-ty-param-lazy-norm.rs:10:12
| |
LL | struct Bar<T = [u8; N], const N: usize>(T); LL | struct Bar<T = [u8; N], const N: usize>(T);
| ^ | ^
@ -7,7 +7,7 @@ LL | struct Bar<T = [u8; N], const N: usize>(T);
= note: using type defaults and const parameters in the same parameter list is currently not permitted = note: using type defaults and const parameters in the same parameter list is currently not permitted
error[E0128]: generic parameters with a default cannot use forward declared identifiers error[E0128]: generic parameters with a default cannot use forward declared identifiers
--> $DIR/params-in-ct-in-ty-param-lazy-norm.rs:11:21 --> $DIR/params-in-ct-in-ty-param-lazy-norm.rs:10:21
| |
LL | struct Bar<T = [u8; N], const N: usize>(T); LL | struct Bar<T = [u8; N], const N: usize>(T);
| ^ defaulted generic parameters cannot be forward declared | ^ defaulted generic parameters cannot be forward declared

View File

@ -1,5 +1,5 @@
error: generic parameters with a default must be trailing error: generic parameters with a default must be trailing
--> $DIR/params-in-ct-in-ty-param-lazy-norm.rs:11:12 --> $DIR/params-in-ct-in-ty-param-lazy-norm.rs:10:12
| |
LL | struct Bar<T = [u8; N], const N: usize>(T); LL | struct Bar<T = [u8; N], const N: usize>(T);
| ^ | ^
@ -16,7 +16,7 @@ LL | struct Foo<T, U = [u8; std::mem::size_of::<T>()]>(T, U);
= help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions = help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions
error[E0128]: generic parameters with a default cannot use forward declared identifiers error[E0128]: generic parameters with a default cannot use forward declared identifiers
--> $DIR/params-in-ct-in-ty-param-lazy-norm.rs:11:21 --> $DIR/params-in-ct-in-ty-param-lazy-norm.rs:10:21
| |
LL | struct Bar<T = [u8; N], const N: usize>(T); LL | struct Bar<T = [u8; N], const N: usize>(T);
| ^ defaulted generic parameters cannot be forward declared | ^ defaulted generic parameters cannot be forward declared

View File

@ -7,7 +7,6 @@ struct Foo<T, U = [u8; std::mem::size_of::<T>()]>(T, U);
//[full]~^ ERROR the size for values of type `T` cannot be known at compilation time //[full]~^ ERROR the size for values of type `T` cannot be known at compilation time
//[min]~^^ ERROR generic parameters may not be used in const operations //[min]~^^ ERROR generic parameters may not be used in const operations
// FIXME(const_generics_defaults): We still don't know how to deal with type defaults.
struct Bar<T = [u8; N], const N: usize>(T); struct Bar<T = [u8; N], const N: usize>(T);
//~^ ERROR generic parameters with a default cannot use forward declared identifiers //~^ ERROR generic parameters with a default cannot use forward declared identifiers
//~| ERROR generic parameters with a default //~| ERROR generic parameters with a default