Skip to content

mGCA: Enforce WF element types for array valtrees#152492

Open
biscuitrescue wants to merge 2 commits intorust-lang:mainfrom
biscuitrescue:fix-array-valtree-wf
Open

mGCA: Enforce WF element types for array valtrees#152492
biscuitrescue wants to merge 2 commits intorust-lang:mainfrom
biscuitrescue:fix-array-valtree-wf

Conversation

@biscuitrescue
Copy link

@biscuitrescue biscuitrescue commented Feb 11, 2026

Fixes #152125

Extends WellFormedness checking for const generics to validate that array element types in ValTrees match the declared element type.

Problem

Before commit, this would have incorrectly compiled

#![feature(adt_const_params, min_generic_const_args)]

use std::marker::ConstParamTy;

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

struct Bar;

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

fn main() {
    foo::<{ [Bar] }>();
}

Solution

Added a ty::Array arm to checking WellFormedness (similar to the existing ty::Tuple arm from #150713) which creates ConstArgHasType obligations for each array element, ensuring they match the array's declared element type.

I attempted to combine the Tuple and Array arms into a single arm, but this proved difficult due to pattern matching limitations, and the separate arm is more readable anyway.

Tests

Added UI test which verifies that the type mismatch is now properly caught, and also returns an appropriate test message

r? @BoxyUwU

@rustbot
Copy link
Collaborator

rustbot commented Feb 11, 2026

changes to the core type system

cc @lcnr

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 11, 2026
@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 11, 2026
@biscuitrescue biscuitrescue changed the title Enforce WF element types for array valtrees mGCA: Enforce WF element types for array valtrees Feb 11, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 11, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 11, 2026
@@ -0,0 +1,15 @@
#![feature(adt_const_params, min_generic_const_args)]
//~^ WARN feature `min_generic_const_args` is incomplete
Copy link
Contributor

@asder8215 asder8215 Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also do #![expect(incomplete_features)] instead of the warning here. I personally would go for that route because I don't think the warning should be mentioned in the .stderr file.

@asder8215
Copy link
Contributor

asder8215 commented Feb 11, 2026

I would also recommend adding test files that check what happens when an array expression contains tuple argument. Kinda like this:

#![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))`
}

One other thing to test would probably be something you add into tuple_exp_arg_complex.rs, which you want to try out what happens when you take a tuple of an array containing const structs. Normally, it should say:

error: complex const arguments must be placed inside of a `const` block
...

^This is if you don't put the const block around the whole tuple. But I think now that you added WF element type check for array valtrees, this should emit an error if there's an invalid array inside tuple?

@biscuitrescue
Copy link
Author

Thankyou for the feedback. I'll add the test-cases and update the PR shortly.

As for the question, I'll have to test this to see its behaviour. I believe it should, because the WF check walks through the valtree structure recursively so that when it checks the tuple, it should also check the array elements inside it.
I'll write the test case and verify what error message actually gets produced.

@biscuitrescue
Copy link
Author

The WellFormed elems check does catch type errors recursively so invalid array elements inside tuples are also caught.
I have also added the requested tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mGCA: type check arguments to array expressions

4 participants