Skip to content

Commit 82dd3cb

Browse files
committed
Auto merge of #150380 - Kivooeo:add-match-for-constkidn, r=BoxyUwU
avoid recreating field_const vec r? BoxyUwU fixes #150354 fixes #150355
2 parents 8da80d3 + 517411f commit 82dd3cb

3 files changed

Lines changed: 56 additions & 12 deletions

File tree

compiler/rustc_ty_utils/src/consts.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,16 @@ fn destructure_const<'tcx>(
3939
(field_consts, None)
4040
}
4141
ty::Adt(def, _) if def.variants().is_empty() => bug!("unreachable"),
42-
ty::Adt(def, args) => {
43-
let (variant_idx, branches) = if def.is_enum() {
42+
ty::Adt(def, _) => {
43+
let (variant_idx, field_consts) = if def.is_enum() {
4444
let (head, rest) = branches.split_first().unwrap();
4545
(VariantIdx::from_u32(head.to_leaf().to_u32()), rest)
4646
} else {
4747
(FIRST_VARIANT, branches)
4848
};
49-
let fields = &def.variant(variant_idx).fields;
50-
let mut field_consts = Vec::with_capacity(fields.len());
51-
52-
for (field, field_valtree) in iter::zip(fields, branches) {
53-
let field_ty = field.ty(tcx, args);
54-
let field_const =
55-
ty::Const::new_value(tcx, field_valtree.to_value().valtree, field_ty);
56-
field_consts.push(field_const);
57-
}
5849
debug!(?field_consts);
5950

60-
(field_consts, Some(variant_idx))
51+
(field_consts.to_vec(), Some(variant_idx))
6152
}
6253
ty::Tuple(elem_tys) => {
6354
let fields = iter::zip(*elem_tys, branches)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! Regression test for <https://github.com/rust-lang/rust/issues/150354>
2+
//@ edition 2024
3+
4+
#![allow(incomplete_features)]
5+
#![feature(min_generic_const_args, adt_const_params)]
6+
7+
#[derive(Eq, PartialEq, core::marker::ConstParamTy)]
8+
struct Foo;
9+
10+
trait Trait {
11+
#[type_const]
12+
const ASSOC: usize;
13+
}
14+
15+
fn foo<const N: Foo>() {}
16+
17+
fn bar<T, const N: u32>() {
18+
foo::<{ Option::Some::<u32> { 0: N } }>;
19+
//~^ ERROR the constant `Option::<u32>::Some(N)` is not of type `Foo`
20+
}
21+
22+
fn baz<T: Trait>() {
23+
foo::<{ Option::Some::<u32> { 0: <T as Trait>::ASSOC } }>();
24+
//~^ ERROR the constant `Option::<u32>::Some(<T as Trait>::ASSOC)` is not of type `Foo`
25+
}
26+
27+
fn main() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: the constant `Option::<u32>::Some(N)` is not of type `Foo`
2+
--> $DIR/printing_valtrees_supports_non_values.rs:18:13
3+
|
4+
LL | foo::<{ Option::Some::<u32> { 0: N } }>;
5+
| ^^^^^^^^^^^^^^^^^^^ expected `Foo`, found `Option<u32>`
6+
|
7+
note: required by a const generic parameter in `foo`
8+
--> $DIR/printing_valtrees_supports_non_values.rs:15:8
9+
|
10+
LL | fn foo<const N: Foo>() {}
11+
| ^^^^^^^^^^^^ required by this const generic parameter in `foo`
12+
13+
error: the constant `Option::<u32>::Some(<T as Trait>::ASSOC)` is not of type `Foo`
14+
--> $DIR/printing_valtrees_supports_non_values.rs:23:13
15+
|
16+
LL | foo::<{ Option::Some::<u32> { 0: <T as Trait>::ASSOC } }>();
17+
| ^^^^^^^^^^^^^^^^^^^ expected `Foo`, found `Option<u32>`
18+
|
19+
note: required by a const generic parameter in `foo`
20+
--> $DIR/printing_valtrees_supports_non_values.rs:15:8
21+
|
22+
LL | fn foo<const N: Foo>() {}
23+
| ^^^^^^^^^^^^ required by this const generic parameter in `foo`
24+
25+
error: aborting due to 2 previous errors
26+

0 commit comments

Comments
 (0)