Skip to content

Commit e032bb7

Browse files
authored
Rollup merge of rust-lang#77439 - varkor:min_const_generics-tests, r=lcnr,estebank
Fix missing diagnostic span for `impl Trait` with const generics, and add various tests for `min_const_generics` and `const_generics` Closes rust-lang#61410. Adds `min_const_generics` tests for: - rust-lang#73727 - rust-lang#72293 - rust-lang#67375 - rust-lang#75153 - rust-lang#71922 - rust-lang#69913 - rust-lang#67945 - rust-lang#69239 Adds `const_generics` tests for: - rust-lang#67375 - rust-lang#75153 - rust-lang#71922 - rust-lang#69913 - rust-lang#67945 - rust-lang#69239 (I only added separate `min_const_generics` and `const_generics` tests if they were handled differently by the two features.) We need to figure out how to deduplicate when `const_generics` is stabilised, but we can discuss that later. For now, we should be checking neither feature breaks, so require regression tests for both. I've given them identical names when I've added both, which should make it easier to spot them later. r? @lcnr
2 parents f1afed5 + 6647eee commit e032bb7

File tree

74 files changed

+628
-66
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+628
-66
lines changed

compiler/rustc_feature/src/active.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ declare_features! (
581581
/// Allows `if let` guard in match arms.
582582
(active, if_let_guard, "1.47.0", Some(51114), None),
583583

584-
/// Allows non trivial generic constants which have to be manually propageted upwards.
584+
/// Allows non-trivial generic constants which have to be manually propageted upwards.
585585
(active, const_evaluatable_checked, "1.48.0", Some(76560), None),
586586

587587
/// Allows basic arithmetic on floating point types in a `const fn`.

compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ impl<'a> Resolver<'a> {
469469
ResolutionError::ParamInNonTrivialAnonConst { name, is_type } => {
470470
let mut err = self.session.struct_span_err(
471471
span,
472-
"generic parameters must not be used inside of non trivial constant values",
472+
"generic parameters must not be used inside of non-trivial constant values",
473473
);
474474
err.span_label(
475475
span,

compiler/rustc_resolve/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ enum ResolutionError<'a> {
218218
ParamInTyOfConstParam(Symbol),
219219
/// constant values inside of type parameter defaults must not depend on generic parameters.
220220
ParamInAnonConstInTyDefault(Symbol),
221-
/// generic parameters must not be used inside of non trivial constant values.
221+
/// generic parameters must not be used inside of non-trivial constant values.
222222
///
223223
/// This error is only emitted when using `min_const_generics`.
224224
ParamInNonTrivialAnonConst { name: Symbol, is_type: bool },

compiler/rustc_typeck/src/astconv/generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
562562
.args
563563
.iter()
564564
.filter_map(|arg| match arg {
565-
GenericArg::Type(_) => Some(arg.span()),
565+
GenericArg::Type(_) | GenericArg::Const(_) => Some(arg.span()),
566566
_ => None,
567567
})
568568
.collect::<Vec<_>>();

compiler/rustc_typeck/src/collect.rs

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
222222
hir::GenericParamKind::Const { .. } => {
223223
let def_id = self.tcx.hir().local_def_id(param.hir_id);
224224
self.tcx.ensure().type_of(def_id);
225+
// FIXME(const_generics:defaults)
225226
}
226227
}
227228
}

src/test/ui/const-generics/array-size-in-generic-struct-param.min.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error: generic parameters must not be used inside of non trivial constant values
1+
error: generic parameters must not be used inside of non-trivial constant values
22
--> $DIR/array-size-in-generic-struct-param.rs:9:48
33
|
44
LL | struct ArithArrayLen<const N: usize>([u32; 0 + N]);
55
| ^ non-trivial anonymous constants must not depend on the parameter `N`
66
|
77
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
88

9-
error: generic parameters must not be used inside of non trivial constant values
9+
error: generic parameters must not be used inside of non-trivial constant values
1010
--> $DIR/array-size-in-generic-struct-param.rs:20:15
1111
|
1212
LL | arr: [u8; CFG.arr_size],

src/test/ui/const-generics/array-size-in-generic-struct-param.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#[allow(dead_code)]
99
struct ArithArrayLen<const N: usize>([u32; 0 + N]);
1010
//[full]~^ ERROR constant expression depends on a generic parameter
11-
//[min]~^^ ERROR generic parameters must not be used inside of non trivial constant values
11+
//[min]~^^ ERROR generic parameters must not be used inside of non-trivial constant values
1212

1313
#[derive(PartialEq, Eq)]
1414
struct Config {
@@ -19,7 +19,7 @@ struct B<const CFG: Config> {
1919
//[min]~^ ERROR `Config` is forbidden
2020
arr: [u8; CFG.arr_size],
2121
//[full]~^ ERROR constant expression depends on a generic parameter
22-
//[min]~^^ ERROR generic parameters must not be used inside of non trivial
22+
//[min]~^^ ERROR generic parameters must not be used inside of non-trivial
2323
}
2424

2525
const C: Config = Config { arr_size: 5 };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
error[E0277]: the size for values of type `T` cannot be known at compilation time
2+
--> $DIR/const-argument-if-length.rs:8:28
3+
|
4+
LL | pub const fn is_zst<T: ?Sized>() -> usize {
5+
| - this type parameter needs to be `Sized`
6+
LL | if std::mem::size_of::<T>() == 0 {
7+
| ^ doesn't have a size known at compile-time
8+
|
9+
::: $SRC_DIR/core/src/mem/mod.rs:LL:COL
10+
|
11+
LL | pub const fn size_of<T>() -> usize {
12+
| - required by this bound in `std::mem::size_of`
13+
14+
error[E0080]: evaluation of constant value failed
15+
--> $DIR/const-argument-if-length.rs:19:15
16+
|
17+
LL | pad: [u8; is_zst::<T>()],
18+
| ^^^^^^^^^^^^^ referenced constant has errors
19+
20+
error[E0277]: the size for values of type `T` cannot be known at compilation time
21+
--> $DIR/const-argument-if-length.rs:17:12
22+
|
23+
LL | pub struct AtLeastByte<T: ?Sized> {
24+
| - this type parameter needs to be `Sized`
25+
LL | value: T,
26+
| ^ doesn't have a size known at compile-time
27+
|
28+
= note: only the last field of a struct may have a dynamically sized type
29+
= help: change the field's type to have a statically known size
30+
help: borrowed types always have a statically known size
31+
|
32+
LL | value: &T,
33+
| ^
34+
help: the `Box` type always has a statically known size and allocates its contents in the heap
35+
|
36+
LL | value: Box<T>,
37+
| ^^^^ ^
38+
39+
error: aborting due to 3 previous errors
40+
41+
Some errors have detailed explanations: E0080, E0277.
42+
For more information about an error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error: generic parameters must not be used inside of non-trivial constant values
2+
--> $DIR/const-argument-if-length.rs:19:24
3+
|
4+
LL | pad: [u8; is_zst::<T>()],
5+
| ^ non-trivial anonymous constants must not depend on the parameter `T`
6+
|
7+
= note: type parameters are currently not permitted in anonymous constants
8+
9+
error[E0277]: the size for values of type `T` cannot be known at compilation time
10+
--> $DIR/const-argument-if-length.rs:17:12
11+
|
12+
LL | pub struct AtLeastByte<T: ?Sized> {
13+
| - this type parameter needs to be `Sized`
14+
LL | value: T,
15+
| ^ doesn't have a size known at compile-time
16+
|
17+
= note: only the last field of a struct may have a dynamically sized type
18+
= help: change the field's type to have a statically known size
19+
help: borrowed types always have a statically known size
20+
|
21+
LL | value: &T,
22+
| ^
23+
help: the `Box` type always has a statically known size and allocates its contents in the heap
24+
|
25+
LL | value: Box<T>,
26+
| ^^^^ ^
27+
28+
error: aborting due to 2 previous errors
29+
30+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// revisions: full min
2+
3+
#![cfg_attr(full, allow(incomplete_features))]
4+
#![cfg_attr(full, feature(const_generics))]
5+
#![cfg_attr(min, feature(min_const_generics))]
6+
7+
pub const fn is_zst<T: ?Sized>() -> usize {
8+
if std::mem::size_of::<T>() == 0 {
9+
//[full]~^ ERROR the size for values of type `T` cannot be known at compilation time
10+
1
11+
} else {
12+
0
13+
}
14+
}
15+
16+
pub struct AtLeastByte<T: ?Sized> {
17+
value: T,
18+
//~^ ERROR the size for values of type `T` cannot be known at compilation time
19+
pad: [u8; is_zst::<T>()],
20+
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
21+
//[full]~^^ ERROR evaluation of constant value failed
22+
}
23+
24+
fn main() {}

src/test/ui/const-generics/const_evaluatable_checked/feature-gate-const_evaluatable_checked.min.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: generic parameters must not be used inside of non trivial constant values
1+
error: generic parameters must not be used inside of non-trivial constant values
22
--> $DIR/feature-gate-const_evaluatable_checked.rs:6:33
33
|
44
LL | type Arr<const N: usize> = [u8; N - 1];

src/test/ui/const-generics/const_evaluatable_checked/feature-gate-const_evaluatable_checked.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![cfg_attr(min, feature(min_const_generics))]
55

66
type Arr<const N: usize> = [u8; N - 1];
7-
//[min]~^ ERROR generic parameters must not be used inside of non trivial constant values
7+
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
88

99
fn test<const N: usize>() -> Arr<N> where Arr<N>: Default {
1010
//[full]~^ ERROR constant expression depends

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error: generic parameters must not be used inside of non trivial constant values
1+
error: generic parameters must not be used inside of non-trivial constant values
22
--> $DIR/simple.rs:8:53
33
|
44
LL | fn test<const N: usize>() -> [u8; N - 1] where [u8; N - 1]: Default {
55
| ^ non-trivial anonymous constants must not depend on the parameter `N`
66
|
77
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
88

9-
error: generic parameters must not be used inside of non trivial constant values
9+
error: generic parameters must not be used inside of non-trivial constant values
1010
--> $DIR/simple.rs:8:35
1111
|
1212
LL | fn test<const N: usize>() -> [u8; N - 1] where [u8; N - 1]: Default {

src/test/ui/const-generics/const_evaluatable_checked/simple_fail.min.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: generic parameters must not be used inside of non trivial constant values
1+
error: generic parameters must not be used inside of non-trivial constant values
22
--> $DIR/simple_fail.rs:7:33
33
|
44
LL | type Arr<const N: usize> = [u8; N - 1];

src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![allow(incomplete_features)]
66

77
type Arr<const N: usize> = [u8; N - 1]; //[full]~ ERROR evaluation of constant
8-
//[min]~^ ERROR generic parameters must not be used inside of non trivial constant values
8+
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
99

1010
fn test<const N: usize>() -> Arr<N> where Arr<N>: Sized {
1111
todo!()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/generic-function-call-in-array-length.rs:9:29
3+
|
4+
LL | fn bar<const N: usize>() -> [u32; foo(N)] {
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: aborting due to previous error
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: generic parameters must not be used inside of non-trivial constant values
2+
--> $DIR/generic-function-call-in-array-length.rs:9:39
3+
|
4+
LL | fn bar<const N: usize>() -> [u32; foo(N)] {
5+
| ^ non-trivial anonymous constants must not depend on the parameter `N`
6+
|
7+
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
8+
9+
error: generic parameters must not be used inside of non-trivial constant values
10+
--> $DIR/generic-function-call-in-array-length.rs:12:13
11+
|
12+
LL | [0; foo(N)]
13+
| ^ non-trivial anonymous constants must not depend on the parameter `N`
14+
|
15+
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
16+
17+
error: aborting due to 2 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// revisions: full min
2+
3+
#![cfg_attr(full, allow(incomplete_features))]
4+
#![cfg_attr(full, feature(const_generics))]
5+
#![cfg_attr(min, feature(min_const_generics))]
6+
7+
const fn foo(n: usize) -> usize { n * 2 }
8+
9+
fn bar<const N: usize>() -> [u32; foo(N)] {
10+
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
11+
//[full]~^^ ERROR constant expression depends on a generic parameter
12+
[0; foo(N)]
13+
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/generic-sum-in-array-length.rs:7:45
3+
|
4+
LL | fn foo<const A: usize, const B: usize>(bar: [usize; A + B]) {}
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: aborting due to previous error
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: generic parameters must not be used inside of non-trivial constant values
2+
--> $DIR/generic-sum-in-array-length.rs:7:53
3+
|
4+
LL | fn foo<const A: usize, const B: usize>(bar: [usize; A + B]) {}
5+
| ^ non-trivial anonymous constants must not depend on the parameter `A`
6+
|
7+
= help: it is currently only allowed to use either `A` or `{ A }` as generic constants
8+
9+
error: generic parameters must not be used inside of non-trivial constant values
10+
--> $DIR/generic-sum-in-array-length.rs:7:57
11+
|
12+
LL | fn foo<const A: usize, const B: usize>(bar: [usize; A + B]) {}
13+
| ^ non-trivial anonymous constants must not depend on the parameter `B`
14+
|
15+
= help: it is currently only allowed to use either `B` or `{ B }` as generic constants
16+
17+
error: aborting due to 2 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// revisions: full min
2+
3+
#![cfg_attr(full, allow(incomplete_features))]
4+
#![cfg_attr(full, feature(const_generics))]
5+
#![cfg_attr(min, feature(min_const_generics))]
6+
7+
fn foo<const A: usize, const B: usize>(bar: [usize; A + B]) {}
8+
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
9+
//[min]~| ERROR generic parameters must not be used inside of non-trivial constant values
10+
//[full]~^^^ ERROR constant expression depends on a generic parameter
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error[E0632]: cannot provide explicit generic arguments when `impl Trait` is used in argument position
2+
--> $DIR/impl-trait-with-const-arguments.rs:24:20
3+
|
4+
LL | assert_eq!(f::<4usize>(Usizable), 20usize);
5+
| ^^^^^^ explicit generic argument not allowed
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error[E0632]: cannot provide explicit generic arguments when `impl Trait` is used in argument position
2+
--> $DIR/impl-trait-with-const-arguments.rs:24:20
3+
|
4+
LL | assert_eq!(f::<4usize>(Usizable), 20usize);
5+
| ^^^^^^ explicit generic argument not allowed
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// revisions: full min
2+
3+
#![cfg_attr(full, allow(incomplete_features))]
4+
#![cfg_attr(full, feature(const_generics))]
5+
#![cfg_attr(min, feature(min_const_generics))]
6+
7+
trait Usizer {
8+
fn m(self) -> usize;
9+
}
10+
11+
fn f<const N: usize>(u: impl Usizer) -> usize {
12+
N + u.m()
13+
}
14+
15+
struct Usizable;
16+
17+
impl Usizer for Usizable {
18+
fn m(self) -> usize {
19+
16
20+
}
21+
}
22+
23+
fn main() {
24+
assert_eq!(f::<4usize>(Usizable), 20usize);
25+
//~^ ERROR cannot provide explicit generic arguments when `impl Trait` is used in argument position
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/intrinsics-type_name-as-const-argument.rs:15:8
3+
|
4+
LL | T: Trait<{std::intrinsics::type_name::<T>()}>
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: aborting due to previous error
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: generic parameters must not be used inside of non-trivial constant values
2+
--> $DIR/intrinsics-type_name-as-const-argument.rs:15:44
3+
|
4+
LL | T: Trait<{std::intrinsics::type_name::<T>()}>
5+
| ^ non-trivial anonymous constants must not depend on the parameter `T`
6+
|
7+
= note: type parameters are currently not permitted in anonymous constants
8+
9+
error: `&'static str` is forbidden as the type of a const generic parameter
10+
--> $DIR/intrinsics-type_name-as-const-argument.rs:10:22
11+
|
12+
LL | trait Trait<const S: &'static str> {}
13+
| ^^^^^^^^^^^^
14+
|
15+
= note: the only supported types are integers, `bool` and `char`
16+
= note: more complex types are supported with `#[feature(const_generics)]`
17+
18+
error: aborting due to 2 previous errors
19+

0 commit comments

Comments
 (0)