From 1d39a91e348d0a06804c17a42a3db1a80ab26dee Mon Sep 17 00:00:00 2001 From: biscuitrescue Date: Wed, 11 Feb 2026 06:16:10 +0530 Subject: [PATCH 1/3] require array const params to have correctly typed elems --- compiler/rustc_trait_selection/src/traits/wf.rs | 17 +++++++++++++++++ .../array-const-arg-type-mismatch.rs | 15 +++++++++++++++ .../array-const-arg-type-mismatch.stderr | 17 +++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 tests/ui/const-generics/array-const-arg-type-mismatch.rs create mode 100644 tests/ui/const-generics/array-const-arg-type-mismatch.stderr diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index d383cb9d91dd4..22f6f07a0ac02 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -1094,6 +1094,23 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { }, )); } + ty::Array(elem_ty, _len) => { + let elem_vals = val.to_branch(); + let cause = self.cause(ObligationCauseCode::WellFormed(None)); + + self.out.extend(elem_vals.iter().map(|&elem_val| { + let predicate = ty::PredicateKind::Clause( + ty::ClauseKind::ConstArgHasType(elem_val, *elem_ty), + ); + traits::Obligation::with_depth( + tcx, + cause.clone(), + self.recursion_depth, + self.param_env, + predicate, + ) + })); + } _ => {} } } diff --git a/tests/ui/const-generics/array-const-arg-type-mismatch.rs b/tests/ui/const-generics/array-const-arg-type-mismatch.rs new file mode 100644 index 0000000000000..e54a559f0cba4 --- /dev/null +++ b/tests/ui/const-generics/array-const-arg-type-mismatch.rs @@ -0,0 +1,15 @@ +#![feature(adt_const_params, min_generic_const_args)] +//~^ WARN feature `min_generic_const_args` is incomplete +use std::marker::ConstParamTy; + +#[derive(Eq, PartialEq, ConstParamTy)] +struct Foo; + +struct Bar; + +fn test() {} + +fn main() { + test::<{ [Bar] }>(); + //~^ ERROR constant `Bar` is not of type `Foo` +} diff --git a/tests/ui/const-generics/array-const-arg-type-mismatch.stderr b/tests/ui/const-generics/array-const-arg-type-mismatch.stderr new file mode 100644 index 0000000000000..43a1e72f05bf0 --- /dev/null +++ b/tests/ui/const-generics/array-const-arg-type-mismatch.stderr @@ -0,0 +1,17 @@ +warning: the feature `min_generic_const_args` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/array-const-arg-type-mismatch.rs:1:30 + | +LL | #![feature(adt_const_params, min_generic_const_args)] + | ^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #132980 for more information + = note: `#[warn(incomplete_features)]` on by default + +error: the constant `Bar` is not of type `Foo` + --> $DIR/array-const-arg-type-mismatch.rs:13:14 + | +LL | test::<{ [Bar] }>(); + | ^^^^^ expected `Foo`, found `Bar` + +error: aborting due to 1 previous error; 1 warning emitted + From 68c705d12cecb851f81932c3a94a13291158fb0f Mon Sep 17 00:00:00 2001 From: biscuitrescue Date: Thu, 12 Feb 2026 03:50:58 +0530 Subject: [PATCH 2/3] tests to check if ValTree checks elems recursively + check array expr with tuple arg --- .../array-const-arg-type-mismatch.stderr | 17 -------------- .../array-const-arg-type-mismatch.rs | 2 +- .../mgca/array-const-arg-type-mismatch.stderr | 8 +++++++ .../mgca/array-with-wrong-tuple-type.rs | 18 +++++++++++++++ .../mgca/array-with-wrong-tuple-type.stderr | 14 ++++++++++++ .../mgca/invalid-array-in-tuple.rs | 16 ++++++++++++++ .../mgca/invalid-array-in-tuple.stderr | 8 +++++++ .../mgca/tuple_expr_arg_complex.rs | 5 +++++ .../mgca/tuple_expr_arg_complex.stderr | 22 ++++++++++++++----- 9 files changed, 87 insertions(+), 23 deletions(-) delete mode 100644 tests/ui/const-generics/array-const-arg-type-mismatch.stderr rename tests/ui/const-generics/{ => mgca}/array-const-arg-type-mismatch.rs (82%) create mode 100644 tests/ui/const-generics/mgca/array-const-arg-type-mismatch.stderr create mode 100644 tests/ui/const-generics/mgca/array-with-wrong-tuple-type.rs create mode 100644 tests/ui/const-generics/mgca/array-with-wrong-tuple-type.stderr create mode 100644 tests/ui/const-generics/mgca/invalid-array-in-tuple.rs create mode 100644 tests/ui/const-generics/mgca/invalid-array-in-tuple.stderr diff --git a/tests/ui/const-generics/array-const-arg-type-mismatch.stderr b/tests/ui/const-generics/array-const-arg-type-mismatch.stderr deleted file mode 100644 index 43a1e72f05bf0..0000000000000 --- a/tests/ui/const-generics/array-const-arg-type-mismatch.stderr +++ /dev/null @@ -1,17 +0,0 @@ -warning: the feature `min_generic_const_args` is incomplete and may not be safe to use and/or cause compiler crashes - --> $DIR/array-const-arg-type-mismatch.rs:1:30 - | -LL | #![feature(adt_const_params, min_generic_const_args)] - | ^^^^^^^^^^^^^^^^^^^^^^ - | - = note: see issue #132980 for more information - = note: `#[warn(incomplete_features)]` on by default - -error: the constant `Bar` is not of type `Foo` - --> $DIR/array-const-arg-type-mismatch.rs:13:14 - | -LL | test::<{ [Bar] }>(); - | ^^^^^ expected `Foo`, found `Bar` - -error: aborting due to 1 previous error; 1 warning emitted - diff --git a/tests/ui/const-generics/array-const-arg-type-mismatch.rs b/tests/ui/const-generics/mgca/array-const-arg-type-mismatch.rs similarity index 82% rename from tests/ui/const-generics/array-const-arg-type-mismatch.rs rename to tests/ui/const-generics/mgca/array-const-arg-type-mismatch.rs index e54a559f0cba4..614532d3ec68e 100644 --- a/tests/ui/const-generics/array-const-arg-type-mismatch.rs +++ b/tests/ui/const-generics/mgca/array-const-arg-type-mismatch.rs @@ -1,5 +1,5 @@ +#![expect(incomplete_features)] #![feature(adt_const_params, min_generic_const_args)] -//~^ WARN feature `min_generic_const_args` is incomplete use std::marker::ConstParamTy; #[derive(Eq, PartialEq, ConstParamTy)] diff --git a/tests/ui/const-generics/mgca/array-const-arg-type-mismatch.stderr b/tests/ui/const-generics/mgca/array-const-arg-type-mismatch.stderr new file mode 100644 index 0000000000000..573620366cbce --- /dev/null +++ b/tests/ui/const-generics/mgca/array-const-arg-type-mismatch.stderr @@ -0,0 +1,8 @@ +error: the constant `Bar` is not of type `Foo` + --> $DIR/array-const-arg-type-mismatch.rs:13:14 + | +LL | test::<{ [Bar] }>(); + | ^^^^^ expected `Foo`, found `Bar` + +error: aborting due to 1 previous error + diff --git a/tests/ui/const-generics/mgca/array-with-wrong-tuple-type.rs b/tests/ui/const-generics/mgca/array-with-wrong-tuple-type.rs new file mode 100644 index 0000000000000..d96a626b74226 --- /dev/null +++ b/tests/ui/const-generics/mgca/array-with-wrong-tuple-type.rs @@ -0,0 +1,18 @@ +#![feature(adt_const_params, min_generic_const_args, unsized_const_params)] +#![allow(incomplete_features)] + +use std::marker::ConstParamTy; + +#[derive(Eq, PartialEq, ConstParamTy)] +struct A; + +fn takes_tuple() {} +fn takes_nested_tuple() {} + + +fn main() { + takes_tuple::<{ [A] }>(); + //~^ ERROR the constant `A` is not of type `(u32, u32)` + takes_nested_tuple::<{ [A] }>(); + //~^ ERROR the constant `A` is not of type `(u32, (u32, u32))` +} diff --git a/tests/ui/const-generics/mgca/array-with-wrong-tuple-type.stderr b/tests/ui/const-generics/mgca/array-with-wrong-tuple-type.stderr new file mode 100644 index 0000000000000..d7b1fe096cb42 --- /dev/null +++ b/tests/ui/const-generics/mgca/array-with-wrong-tuple-type.stderr @@ -0,0 +1,14 @@ +error: the constant `A` is not of type `(u32, u32)` + --> $DIR/array-with-wrong-tuple-type.rs:14:21 + | +LL | takes_tuple::<{ [A] }>(); + | ^^^ expected `(u32, u32)`, found `A` + +error: the constant `A` is not of type `(u32, (u32, u32))` + --> $DIR/array-with-wrong-tuple-type.rs:16:28 + | +LL | takes_nested_tuple::<{ [A] }>(); + | ^^^ expected `(u32, (u32, u32))`, found `A` + +error: aborting due to 2 previous errors + diff --git a/tests/ui/const-generics/mgca/invalid-array-in-tuple.rs b/tests/ui/const-generics/mgca/invalid-array-in-tuple.rs new file mode 100644 index 0000000000000..8c84e12e654bc --- /dev/null +++ b/tests/ui/const-generics/mgca/invalid-array-in-tuple.rs @@ -0,0 +1,16 @@ +#![feature(adt_const_params, min_generic_const_args, unsized_const_params)] +#![allow(incomplete_features)] + +use std::marker::ConstParamTy; + +#[derive(Eq, PartialEq, ConstParamTy)] +struct Foo; + +struct Bar; + +fn takes_tuple_with_array() {} + +fn main() { + takes_tuple_with_array::<{ ([Bar], 1) }>(); + //~^ ERROR the constant `Bar` is not of type `Foo` +} diff --git a/tests/ui/const-generics/mgca/invalid-array-in-tuple.stderr b/tests/ui/const-generics/mgca/invalid-array-in-tuple.stderr new file mode 100644 index 0000000000000..1cd46883dcf79 --- /dev/null +++ b/tests/ui/const-generics/mgca/invalid-array-in-tuple.stderr @@ -0,0 +1,8 @@ +error: the constant `Bar` is not of type `Foo` + --> $DIR/invalid-array-in-tuple.rs:14:32 + | +LL | takes_tuple_with_array::<{ ([Bar], 1) }>(); + | ^^^^^^^^^^ expected `Foo`, found `Bar` + +error: aborting due to 1 previous error + diff --git a/tests/ui/const-generics/mgca/tuple_expr_arg_complex.rs b/tests/ui/const-generics/mgca/tuple_expr_arg_complex.rs index 5a40c1b14c2d6..b3aa8c8b18331 100644 --- a/tests/ui/const-generics/mgca/tuple_expr_arg_complex.rs +++ b/tests/ui/const-generics/mgca/tuple_expr_arg_complex.rs @@ -8,6 +8,8 @@ trait Trait { fn takes_tuple() {} fn takes_nested_tuple() {} +fn takes_array() {} +fn takes_tuple_with_array() {} fn generic_caller() { takes_tuple::<{ (N, N + 1) }>(); //~ ERROR complex const arguments must be placed inside of a `const` block @@ -15,6 +17,9 @@ fn generic_caller() { takes_nested_tuple::<{ (N, (N, N + 1)) }>(); //~ ERROR complex const arguments must be placed inside of a `const` block takes_nested_tuple::<{ (N, (N, const { N + 1 })) }>(); //~ ERROR generic parameters may not be used in const operations + + takes_array::<{ [N, N + 1] }>(); //~ ERROR complex const arguments must be placed inside of a `const` block + takes_tuple_with_array::<{ ([N, N + 1], N) }>(); //~ ERROR complex const arguments must be placed inside of a `const` block } fn main() {} diff --git a/tests/ui/const-generics/mgca/tuple_expr_arg_complex.stderr b/tests/ui/const-generics/mgca/tuple_expr_arg_complex.stderr index b4853d3c2e38c..24e29bbbe519d 100644 --- a/tests/ui/const-generics/mgca/tuple_expr_arg_complex.stderr +++ b/tests/ui/const-generics/mgca/tuple_expr_arg_complex.stderr @@ -1,28 +1,40 @@ error: complex const arguments must be placed inside of a `const` block - --> $DIR/tuple_expr_arg_complex.rs:13:25 + --> $DIR/tuple_expr_arg_complex.rs:15:25 | LL | takes_tuple::<{ (N, N + 1) }>(); | ^^^^^ error: complex const arguments must be placed inside of a `const` block - --> $DIR/tuple_expr_arg_complex.rs:14:25 + --> $DIR/tuple_expr_arg_complex.rs:16:25 | LL | takes_tuple::<{ (N, T::ASSOC + 1) }>(); | ^^^^^^^^^^^^ error: complex const arguments must be placed inside of a `const` block - --> $DIR/tuple_expr_arg_complex.rs:16:36 + --> $DIR/tuple_expr_arg_complex.rs:18:36 | LL | takes_nested_tuple::<{ (N, (N, N + 1)) }>(); | ^^^^^ +error: complex const arguments must be placed inside of a `const` block + --> $DIR/tuple_expr_arg_complex.rs:21:25 + | +LL | takes_array::<{ [N, N + 1] }>(); + | ^^^^^ + +error: complex const arguments must be placed inside of a `const` block + --> $DIR/tuple_expr_arg_complex.rs:22:37 + | +LL | takes_tuple_with_array::<{ ([N, N + 1], N) }>(); + | ^^^^^ + error: generic parameters may not be used in const operations - --> $DIR/tuple_expr_arg_complex.rs:17:44 + --> $DIR/tuple_expr_arg_complex.rs:19:44 | LL | takes_nested_tuple::<{ (N, (N, const { N + 1 })) }>(); | ^ | = help: add `#![feature(opaque_generic_const_args)]` to allow generic expressions as the RHS of const items -error: aborting due to 4 previous errors +error: aborting due to 6 previous errors From 7030be68aebccb21ce5eb448e982a8c26dcaf62d Mon Sep 17 00:00:00 2001 From: biscuitrescue Date: Mon, 16 Feb 2026 01:35:50 +0530 Subject: [PATCH 3/3] move array test to diff file --- .../mgca/array_expr_arg_complex.rs | 16 ++++++++++++++ .../mgca/array_expr_arg_complex.stderr | 14 ++++++++++++ .../mgca/tuple_expr_arg_complex.rs | 6 ----- .../mgca/tuple_expr_arg_complex.stderr | 22 +++++-------------- 4 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 tests/ui/const-generics/mgca/array_expr_arg_complex.rs create mode 100644 tests/ui/const-generics/mgca/array_expr_arg_complex.stderr diff --git a/tests/ui/const-generics/mgca/array_expr_arg_complex.rs b/tests/ui/const-generics/mgca/array_expr_arg_complex.rs new file mode 100644 index 0000000000000..6d57e4f4b9686 --- /dev/null +++ b/tests/ui/const-generics/mgca/array_expr_arg_complex.rs @@ -0,0 +1,16 @@ +#![feature(min_generic_const_args, adt_const_params, unsized_const_params)] +#![expect(incomplete_features)] + +trait Trait { + type const ASSOC: usize; +} + +fn takes_array() {} +fn takes_tuple_with_array() {} + +fn generic_caller() { + takes_array::<{ [N, N + 1] }>(); //~ ERROR complex const arguments must be placed inside of a `const` block + takes_tuple_with_array::<{ ([N, N + 1], N) }>(); //~ ERROR complex const arguments must be placed inside of a `const` block +} + +fn main() {} diff --git a/tests/ui/const-generics/mgca/array_expr_arg_complex.stderr b/tests/ui/const-generics/mgca/array_expr_arg_complex.stderr new file mode 100644 index 0000000000000..f40d4b5035d86 --- /dev/null +++ b/tests/ui/const-generics/mgca/array_expr_arg_complex.stderr @@ -0,0 +1,14 @@ +error: complex const arguments must be placed inside of a `const` block + --> $DIR/array_expr_arg_complex.rs:12:25 + | +LL | takes_array::<{ [N, N + 1] }>(); + | ^^^^^ + +error: complex const arguments must be placed inside of a `const` block + --> $DIR/array_expr_arg_complex.rs:13:37 + | +LL | takes_tuple_with_array::<{ ([N, N + 1], N) }>(); + | ^^^^^ + +error: aborting due to 2 previous errors + diff --git a/tests/ui/const-generics/mgca/tuple_expr_arg_complex.rs b/tests/ui/const-generics/mgca/tuple_expr_arg_complex.rs index b3aa8c8b18331..d7cab17bad124 100644 --- a/tests/ui/const-generics/mgca/tuple_expr_arg_complex.rs +++ b/tests/ui/const-generics/mgca/tuple_expr_arg_complex.rs @@ -2,14 +2,11 @@ #![expect(incomplete_features)] trait Trait { - type const ASSOC: usize; } fn takes_tuple() {} fn takes_nested_tuple() {} -fn takes_array() {} -fn takes_tuple_with_array() {} fn generic_caller() { takes_tuple::<{ (N, N + 1) }>(); //~ ERROR complex const arguments must be placed inside of a `const` block @@ -17,9 +14,6 @@ fn generic_caller() { takes_nested_tuple::<{ (N, (N, N + 1)) }>(); //~ ERROR complex const arguments must be placed inside of a `const` block takes_nested_tuple::<{ (N, (N, const { N + 1 })) }>(); //~ ERROR generic parameters may not be used in const operations - - takes_array::<{ [N, N + 1] }>(); //~ ERROR complex const arguments must be placed inside of a `const` block - takes_tuple_with_array::<{ ([N, N + 1], N) }>(); //~ ERROR complex const arguments must be placed inside of a `const` block } fn main() {} diff --git a/tests/ui/const-generics/mgca/tuple_expr_arg_complex.stderr b/tests/ui/const-generics/mgca/tuple_expr_arg_complex.stderr index 24e29bbbe519d..95a98d857b1d4 100644 --- a/tests/ui/const-generics/mgca/tuple_expr_arg_complex.stderr +++ b/tests/ui/const-generics/mgca/tuple_expr_arg_complex.stderr @@ -1,40 +1,28 @@ error: complex const arguments must be placed inside of a `const` block - --> $DIR/tuple_expr_arg_complex.rs:15:25 + --> $DIR/tuple_expr_arg_complex.rs:12:25 | LL | takes_tuple::<{ (N, N + 1) }>(); | ^^^^^ error: complex const arguments must be placed inside of a `const` block - --> $DIR/tuple_expr_arg_complex.rs:16:25 + --> $DIR/tuple_expr_arg_complex.rs:13:25 | LL | takes_tuple::<{ (N, T::ASSOC + 1) }>(); | ^^^^^^^^^^^^ error: complex const arguments must be placed inside of a `const` block - --> $DIR/tuple_expr_arg_complex.rs:18:36 + --> $DIR/tuple_expr_arg_complex.rs:15:36 | LL | takes_nested_tuple::<{ (N, (N, N + 1)) }>(); | ^^^^^ -error: complex const arguments must be placed inside of a `const` block - --> $DIR/tuple_expr_arg_complex.rs:21:25 - | -LL | takes_array::<{ [N, N + 1] }>(); - | ^^^^^ - -error: complex const arguments must be placed inside of a `const` block - --> $DIR/tuple_expr_arg_complex.rs:22:37 - | -LL | takes_tuple_with_array::<{ ([N, N + 1], N) }>(); - | ^^^^^ - error: generic parameters may not be used in const operations - --> $DIR/tuple_expr_arg_complex.rs:19:44 + --> $DIR/tuple_expr_arg_complex.rs:16:44 | LL | takes_nested_tuple::<{ (N, (N, const { N + 1 })) }>(); | ^ | = help: add `#![feature(opaque_generic_const_args)]` to allow generic expressions as the RHS of const items -error: aborting due to 6 previous errors +error: aborting due to 4 previous errors