add const kind match

This commit is contained in:
Kivooeo 2025-12-25 19:18:32 +00:00
parent a47e39ea20
commit 517411f143
3 changed files with 56 additions and 12 deletions

View file

@ -39,25 +39,16 @@ fn destructure_const<'tcx>(
(field_consts, None)
}
ty::Adt(def, _) if def.variants().is_empty() => bug!("unreachable"),
ty::Adt(def, args) => {
let (variant_idx, branches) = if def.is_enum() {
ty::Adt(def, _) => {
let (variant_idx, field_consts) = if def.is_enum() {
let (head, rest) = branches.split_first().unwrap();
(VariantIdx::from_u32(head.to_leaf().to_u32()), rest)
} else {
(FIRST_VARIANT, branches)
};
let fields = &def.variant(variant_idx).fields;
let mut field_consts = Vec::with_capacity(fields.len());
for (field, field_valtree) in iter::zip(fields, branches) {
let field_ty = field.ty(tcx, args);
let field_const =
ty::Const::new_value(tcx, field_valtree.to_value().valtree, field_ty);
field_consts.push(field_const);
}
debug!(?field_consts);
(field_consts, Some(variant_idx))
(field_consts.to_vec(), Some(variant_idx))
}
ty::Tuple(elem_tys) => {
let fields = iter::zip(*elem_tys, branches)

View file

@ -0,0 +1,27 @@
//! Regression test for <https://github.com/rust-lang/rust/issues/150354>
//@ edition 2024
#![allow(incomplete_features)]
#![feature(min_generic_const_args, adt_const_params)]
#[derive(Eq, PartialEq, core::marker::ConstParamTy)]
struct Foo;
trait Trait {
#[type_const]
const ASSOC: usize;
}
fn foo<const N: Foo>() {}
fn bar<T, const N: u32>() {
foo::<{ Option::Some::<u32> { 0: N } }>;
//~^ ERROR the constant `Option::<u32>::Some(N)` is not of type `Foo`
}
fn baz<T: Trait>() {
foo::<{ Option::Some::<u32> { 0: <T as Trait>::ASSOC } }>();
//~^ ERROR the constant `Option::<u32>::Some(<T as Trait>::ASSOC)` is not of type `Foo`
}
fn main() {}

View file

@ -0,0 +1,26 @@
error: the constant `Option::<u32>::Some(N)` is not of type `Foo`
--> $DIR/printing_valtrees_supports_non_values.rs:18:13
|
LL | foo::<{ Option::Some::<u32> { 0: N } }>;
| ^^^^^^^^^^^^^^^^^^^ expected `Foo`, found `Option<u32>`
|
note: required by a const generic parameter in `foo`
--> $DIR/printing_valtrees_supports_non_values.rs:15:8
|
LL | fn foo<const N: Foo>() {}
| ^^^^^^^^^^^^ required by this const generic parameter in `foo`
error: the constant `Option::<u32>::Some(<T as Trait>::ASSOC)` is not of type `Foo`
--> $DIR/printing_valtrees_supports_non_values.rs:23:13
|
LL | foo::<{ Option::Some::<u32> { 0: <T as Trait>::ASSOC } }>();
| ^^^^^^^^^^^^^^^^^^^ expected `Foo`, found `Option<u32>`
|
note: required by a const generic parameter in `foo`
--> $DIR/printing_valtrees_supports_non_values.rs:15:8
|
LL | fn foo<const N: Foo>() {}
| ^^^^^^^^^^^^ required by this const generic parameter in `foo`
error: aborting due to 2 previous errors