Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions compiler/rustc_trait_selection/src/traits/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,23 @@ impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> 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,
)
}));
}
_ => {}
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/const-generics/mgca/array-const-arg-type-mismatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![expect(incomplete_features)]
#![feature(adt_const_params, min_generic_const_args)]
use std::marker::ConstParamTy;

#[derive(Eq, PartialEq, ConstParamTy)]
struct Foo;

struct Bar;

fn test<const N: [Foo; 1]>() {}

fn main() {
test::<{ [Bar] }>();
//~^ ERROR constant `Bar` is not of type `Foo`
}
Original file line number Diff line number Diff line change
@@ -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

18 changes: 18 additions & 0 deletions tests/ui/const-generics/mgca/array-with-wrong-tuple-type.rs
Original file line number Diff line number Diff line change
@@ -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<const N: [(u32, u32); 1]>() {}
fn takes_nested_tuple<const N: [(u32, (u32, u32)); 1]>() {}


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))`
}
14 changes: 14 additions & 0 deletions tests/ui/const-generics/mgca/array-with-wrong-tuple-type.stderr
Original file line number Diff line number Diff line change
@@ -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

16 changes: 16 additions & 0 deletions tests/ui/const-generics/mgca/array_expr_arg_complex.rs
Original file line number Diff line number Diff line change
@@ -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<const A: [u32; 2]>() {}
fn takes_tuple_with_array<const A: ([u32; 2], u32)>() {}

fn generic_caller<T: Trait, const N: u32, const N2: u32>() {
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() {}
14 changes: 14 additions & 0 deletions tests/ui/const-generics/mgca/array_expr_arg_complex.stderr
Original file line number Diff line number Diff line change
@@ -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

16 changes: 16 additions & 0 deletions tests/ui/const-generics/mgca/invalid-array-in-tuple.rs
Original file line number Diff line number Diff line change
@@ -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<const A: ([Foo; 1], u32)>() {}

fn main() {
takes_tuple_with_array::<{ ([Bar], 1) }>();
//~^ ERROR the constant `Bar` is not of type `Foo`
}
8 changes: 8 additions & 0 deletions tests/ui/const-generics/mgca/invalid-array-in-tuple.stderr
Original file line number Diff line number Diff line change
@@ -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

1 change: 0 additions & 1 deletion tests/ui/const-generics/mgca/tuple_expr_arg_complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#![expect(incomplete_features)]

trait Trait {

type const ASSOC: usize;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/const-generics/mgca/tuple_expr_arg_complex.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
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: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:14: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:16:36
--> $DIR/tuple_expr_arg_complex.rs:15:36
|
LL | takes_nested_tuple::<{ (N, (N, N + 1)) }>();
| ^^^^^

error: generic parameters may not be used in const operations
--> $DIR/tuple_expr_arg_complex.rs:17:44
--> $DIR/tuple_expr_arg_complex.rs:16:44
|
LL | takes_nested_tuple::<{ (N, (N, const { N + 1 })) }>();
| ^
Expand Down
Loading