Skip to content

Commit 7cb1dcd

Browse files
committed
loosen ordering restricts for const_generics_defaults
1 parent 259a368 commit 7cb1dcd

31 files changed

+145
-81
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ fn validate_generic_param_order(
754754
GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident),
755755
GenericParamKind::Const { ref ty, kw_span: _, default: _ } => {
756756
let ty = pprust::ty_to_string(ty);
757-
let unordered = sess.features_untracked().const_generics;
757+
let unordered = sess.features_untracked().unordered_const_ty_params();
758758
(ParamKindOrd::Const { unordered }, Some(format!("const {}: {}", param.ident, ty)))
759759
}
760760
};

compiler/rustc_feature/src/active.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ macro_rules! declare_features {
6363
_ => panic!("`{}` was not listed in `declare_features`", feature),
6464
}
6565
}
66+
67+
pub fn unordered_const_ty_params(&self) -> bool {
68+
self.const_generics || self.const_generics_defaults
69+
}
6670
}
6771
};
6872
}

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,9 @@ impl GenericArg<'_> {
296296
match self {
297297
GenericArg::Lifetime(_) => ast::ParamKindOrd::Lifetime,
298298
GenericArg::Type(_) => ast::ParamKindOrd::Type,
299-
GenericArg::Const(_) => ast::ParamKindOrd::Const { unordered: feats.const_generics },
299+
GenericArg::Const(_) => {
300+
ast::ParamKindOrd::Const { unordered: feats.unordered_const_ty_params() }
301+
}
300302
}
301303
}
302304
}

compiler/rustc_middle/src/ty/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl GenericParamDefKind {
3636
GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime,
3737
GenericParamDefKind::Type { .. } => ast::ParamKindOrd::Type,
3838
GenericParamDefKind::Const { .. } => {
39-
ast::ParamKindOrd::Const { unordered: tcx.features().const_generics }
39+
ast::ParamKindOrd::Const { unordered: tcx.features().unordered_const_ty_params() }
4040
}
4141
}
4242
}

compiler/rustc_typeck/src/astconv/generics.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
286286
ParamKindOrd::Const {
287287
unordered: tcx
288288
.features()
289-
.const_generics,
289+
.unordered_const_ty_params(),
290290
}
291291
}
292292
},
@@ -309,7 +309,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
309309
GenericArg::Lifetime(_) => ParamKindOrd::Lifetime,
310310
GenericArg::Type(_) => ParamKindOrd::Type,
311311
GenericArg::Const(_) => ParamKindOrd::Const {
312-
unordered: tcx.features().const_generics,
312+
unordered: tcx
313+
.features()
314+
.unordered_const_ty_params(),
313315
},
314316
}),
315317
Some(&format!(

src/test/ui/const-generics/defaults/auxiliary/const_defaulty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(const_generics)]
1+
#![cfg_attr(full, feature(const_generics))]
22
#![feature(const_generics_defaults)]
33
#![allow(incomplete_features)]
44

src/test/ui/const-generics/defaults/complex-generic-default-expr.stderr renamed to src/test/ui/const-generics/defaults/complex-generic-default-expr.full.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0277]: the size for values of type `T` cannot be known at compilation time
2-
--> $DIR/complex-generic-default-expr.rs:6:62
2+
--> $DIR/complex-generic-default-expr.rs:9:62
33
|
44
LL | struct Bar<T, const TYPE_SIZE: usize = { std::mem::size_of::<T>() }>(T);
55
| - ^ doesn't have a size known at compile-time
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: generic parameters may not be used in const operations
2+
--> $DIR/complex-generic-default-expr.rs:6:47
3+
|
4+
LL | struct Foo<const N: usize, const M: usize = { N + 1 }>;
5+
| ^ cannot perform const operation using `N`
6+
|
7+
= help: const parameters may only be used as standalone arguments, i.e. `N`
8+
= help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions
9+
10+
error: generic parameters may not be used in const operations
11+
--> $DIR/complex-generic-default-expr.rs:9:62
12+
|
13+
LL | struct Bar<T, const TYPE_SIZE: usize = { std::mem::size_of::<T>() }>(T);
14+
| ^ cannot perform const operation using `T`
15+
|
16+
= note: type parameters may not be used in const expressions
17+
= help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions
18+
19+
error: aborting due to 2 previous errors
20+
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
#![feature(const_generics, const_generics_defaults)]
1+
// revisions: full min
2+
#![cfg_attr(full, feature(const_generics))]
3+
#![feature(const_generics_defaults)]
24
#![allow(incomplete_features)]
35

46
struct Foo<const N: usize, const M: usize = { N + 1 }>;
7+
//[min]~^ ERROR generic parameters may not be used in const operations
58

69
struct Bar<T, const TYPE_SIZE: usize = { std::mem::size_of::<T>() }>(T);
7-
//~^ ERROR the size for values of type `T` cannot be known at compilation time
10+
//[min]~^ ERROR generic parameters may not be used in const operations
11+
//[full]~^^ ERROR the size for values of type `T` cannot be known at compilation time
812

913
fn main() {}

src/test/ui/const-generics/defaults/const-default.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// run-pass
2-
3-
#![feature(const_generics)]
2+
// revisions: full min
3+
#![cfg_attr(full, feature(const_generics))]
44
#![feature(const_generics_defaults)]
55
#![allow(incomplete_features)]
66

src/test/ui/const-generics/defaults/default-on-impl.stderr renamed to src/test/ui/const-generics/defaults/default-on-impl.full.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
2-
--> $DIR/default-on-impl.rs:6:12
2+
--> $DIR/default-on-impl.rs:8:12
33
|
44
LL | impl<const N: usize = 1> Foo<N> {}
55
| ^
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
2+
--> $DIR/default-on-impl.rs:8:12
3+
|
4+
LL | impl<const N: usize = 1> Foo<N> {}
5+
| ^
6+
7+
error: aborting due to previous error
8+

src/test/ui/const-generics/defaults/default-on-impl.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#![feature(const_generics, const_generics_defaults)]
1+
// revisions: full min
2+
#![cfg_attr(full, feature(const_generics))]
3+
#![feature(const_generics_defaults)]
24
#![allow(incomplete_features)]
35

46
struct Foo<const N: usize>;

src/test/ui/const-generics/defaults/external.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// aux-build:const_defaulty.rs
22
// check-pass
3+
// revisions: full min
4+
#![cfg_attr(full, feature(const_generics))]
35
#![feature(const_generics_defaults)]
46
#![allow(incomplete_features)]
57

src/test/ui/const-generics/defaults/intermixed-lifetime.full.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: lifetime parameters must be declared prior to const parameters
2-
--> $DIR/intermixed-lifetime.rs:6:28
2+
--> $DIR/intermixed-lifetime.rs:7:28
33
|
44
LL | struct Foo<const N: usize, 'a, T = u32>(&'a (), T);
55
| -----------------^^---------- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>`
Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
error: lifetime parameters must be declared prior to const parameters
2-
--> $DIR/intermixed-lifetime.rs:6:28
2+
--> $DIR/intermixed-lifetime.rs:7:28
33
|
44
LL | struct Foo<const N: usize, 'a, T = u32>(&'a (), T);
5-
| -----------------^^---------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = u32, const N: usize>`
5+
| -----------------^^---------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, const N: usize, T = u32>`
66

7-
error: type parameters must be declared prior to const parameters
8-
--> $DIR/intermixed-lifetime.rs:6:32
9-
|
10-
LL | struct Foo<const N: usize, 'a, T = u32>(&'a (), T);
11-
| ---------------------^------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = u32, const N: usize>`
12-
13-
error: lifetime parameters must be declared prior to const parameters
7+
error: lifetime parameters must be declared prior to type parameters
148
--> $DIR/intermixed-lifetime.rs:10:37
159
|
1610
LL | struct Bar<const N: usize, T = u32, 'a>(&'a (), T);
17-
| --------------------------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = u32, const N: usize>`
18-
19-
error: type parameters must be declared prior to const parameters
20-
--> $DIR/intermixed-lifetime.rs:10:28
21-
|
22-
LL | struct Bar<const N: usize, T = u32, 'a>(&'a (), T);
23-
| -----------------^----------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = u32, const N: usize>`
11+
| --------------------------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, const N: usize, T = u32>`
2412

25-
error: aborting due to 4 previous errors
13+
error: aborting due to 2 previous errors
2614

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
// revisions: full min
21
// Checks that lifetimes cannot be interspersed between consts and types.
2+
// revisions: full min
33
#![cfg_attr(full, feature(const_generics))]
4-
#![cfg_attr(full, allow(incomplete_features))]
4+
#![feature(const_generics_defaults)]
5+
#![allow(incomplete_features)]
56

67
struct Foo<const N: usize, 'a, T = u32>(&'a (), T);
78
//~^ Error lifetime parameters must be declared prior to const parameters
8-
//[min]~^^ Error type parameters must be declared prior to const parameters
99

1010
struct Bar<const N: usize, T = u32, 'a>(&'a (), T);
11-
//[full]~^ Error lifetime parameters must be declared prior to type parameters
12-
//[min]~^^ Error type parameters must be declared prior to const parameters
13-
//[min]~| Error lifetime parameters must be declared prior to const parameters
11+
//~^ Error lifetime parameters must be declared prior to type parameters
1412

1513
fn main() {}

src/test/ui/const-generics/defaults/mismatch.stderr renamed to src/test/ui/const-generics/defaults/mismatch.full.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error[E0308]: mismatched types
2-
--> $DIR/mismatch.rs:11:28
2+
--> $DIR/mismatch.rs:12:28
33
|
44
LL | let e: Example::<13> = ();
55
| ------------- ^^ expected struct `Example`, found `()`
66
| |
77
| expected due to this
88

99
error[E0308]: mismatched types
10-
--> $DIR/mismatch.rs:13:34
10+
--> $DIR/mismatch.rs:14:34
1111
|
1212
LL | let e: Example2::<u32, 13> = ();
1313
| ------------------- ^^ expected struct `Example2`, found `()`
@@ -18,7 +18,7 @@ LL | let e: Example2::<u32, 13> = ();
1818
found unit type `()`
1919

2020
error[E0308]: mismatched types
21-
--> $DIR/mismatch.rs:15:34
21+
--> $DIR/mismatch.rs:16:34
2222
|
2323
LL | let e: Example3::<13, u32> = ();
2424
| ------------------- ^^ expected struct `Example3`, found `()`
@@ -29,7 +29,7 @@ LL | let e: Example3::<13, u32> = ();
2929
found unit type `()`
3030

3131
error[E0308]: mismatched types
32-
--> $DIR/mismatch.rs:17:28
32+
--> $DIR/mismatch.rs:18:28
3333
|
3434
LL | let e: Example3::<7> = ();
3535
| ------------- ^^ expected struct `Example3`, found `()`
@@ -40,7 +40,7 @@ LL | let e: Example3::<7> = ();
4040
found unit type `()`
4141

4242
error[E0308]: mismatched types
43-
--> $DIR/mismatch.rs:21:28
43+
--> $DIR/mismatch.rs:22:28
4444
|
4545
LL | let e: Example4::<7> = ();
4646
| ------------- ^^ expected struct `Example4`, found `()`
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/mismatch.rs:12:28
3+
|
4+
LL | let e: Example::<13> = ();
5+
| ------------- ^^ expected struct `Example`, found `()`
6+
| |
7+
| expected due to this
8+
9+
error[E0308]: mismatched types
10+
--> $DIR/mismatch.rs:14:34
11+
|
12+
LL | let e: Example2::<u32, 13> = ();
13+
| ------------------- ^^ expected struct `Example2`, found `()`
14+
| |
15+
| expected due to this
16+
|
17+
= note: expected struct `Example2`
18+
found unit type `()`
19+
20+
error[E0308]: mismatched types
21+
--> $DIR/mismatch.rs:16:34
22+
|
23+
LL | let e: Example3::<13, u32> = ();
24+
| ------------------- ^^ expected struct `Example3`, found `()`
25+
| |
26+
| expected due to this
27+
|
28+
= note: expected struct `Example3`
29+
found unit type `()`
30+
31+
error[E0308]: mismatched types
32+
--> $DIR/mismatch.rs:18:28
33+
|
34+
LL | let e: Example3::<7> = ();
35+
| ------------- ^^ expected struct `Example3`, found `()`
36+
| |
37+
| expected due to this
38+
|
39+
= note: expected struct `Example3<7_usize>`
40+
found unit type `()`
41+
42+
error[E0308]: mismatched types
43+
--> $DIR/mismatch.rs:22:28
44+
|
45+
LL | let e: Example4::<7> = ();
46+
| ------------- ^^ expected struct `Example4`, found `()`
47+
| |
48+
| expected due to this
49+
50+
error: aborting due to 5 previous errors
51+
52+
For more information about this error, try `rustc --explain E0308`.

src/test/ui/const-generics/defaults/mismatch.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#![feature(const_generics)]
1+
// revisions: full min
2+
#![cfg_attr(full, feature(const_generics))]
23
#![feature(const_generics_defaults)]
34
#![allow(incomplete_features)]
45

src/test/ui/const-generics/defaults/pretty-printing-ast.stdout

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ fn foo<const SIZE : usize = 5>() { }
1818

1919
struct Range<const FROM : usize = 0, const LEN : usize = 0, const TO : usize =
2020
FROM>;
21-

src/test/ui/const-generics/defaults/repr-c-issue-82792.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#![allow(incomplete_features)]
77

88
#[repr(C)]
9-
pub struct Loaf<T: Sized, const N: usize = 1usize> {
9+
pub struct Loaf<T: Sized, const N: usize = 1> {
1010
head: [T; N],
1111
slice: [T],
1212
}

src/test/ui/const-generics/defaults/simple-defaults.min.stderr

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/test/ui/const-generics/defaults/simple-defaults.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// [full] run-pass
2-
// revisions: min full
3-
// Checks some basic test cases for defaults.
1+
// run-pass
2+
// Checks that type param defaults are allowed after const params.
3+
// revisions: full min
44
#![cfg_attr(full, feature(const_generics))]
5-
#![cfg_attr(full, allow(incomplete_features))]
5+
#![feature(const_generics_defaults)]
6+
#![allow(incomplete_features)]
67
#![allow(dead_code)]
78

89
struct FixedOutput<'a, const N: usize, T=u32> {
9-
//[min]~^ ERROR type parameters must be declared prior to const parameters
1010
out: &'a [T; N],
1111
}
1212

src/test/ui/const-generics/defaults/type-default-const-param-name.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// check-pass
2-
#![feature(const_generics, const_generics_defaults)]
2+
// revisions: full min
3+
#![cfg_attr(full, feature(const_generics))]
4+
#![feature(const_generics_defaults)]
35
#![allow(incomplete_features)]
46

57
struct N;
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
error: generic parameters with a default must be trailing
2-
--> $DIR/wrong-order.rs:4:10
2+
--> $DIR/wrong-order.rs:6:10
33
|
44
LL | struct A<T = u32, const N: usize> {
55
| ^
6-
|
7-
= note: using type defaults and const parameters in the same parameter list is currently not permitted
8-
9-
warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
10-
--> $DIR/wrong-order.rs:2:27
11-
|
12-
LL | #![cfg_attr(full, feature(const_generics))]
13-
| ^^^^^^^^^^^^^^
14-
|
15-
= note: `#[warn(incomplete_features)]` on by default
16-
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
176

18-
error: aborting due to previous error; 1 warning emitted
7+
error: aborting due to previous error
198

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error: generic parameters with a default must be trailing
2-
--> $DIR/wrong-order.rs:4:10
2+
--> $DIR/wrong-order.rs:6:10
33
|
44
LL | struct A<T = u32, const N: usize> {
55
| ^
6-
|
7-
= note: using type defaults and const parameters in the same parameter list is currently not permitted
86

97
error: aborting due to previous error
108

0 commit comments

Comments
 (0)