Skip to content

Commit d7d2f7e

Browse files
committed
mgca: Type-check fields of ADT constructor expr const args
1 parent 6c2dc40 commit d7d2f7e

6 files changed

Lines changed: 106 additions & 16 deletions

File tree

compiler/rustc_trait_selection/src/traits/wf.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,31 @@ impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
10511051
| ty::ConstKind::Placeholder(..) => {
10521052
// These variants are trivially WF, so nothing to do here.
10531053
}
1054-
ty::ConstKind::Value(..) => {
1054+
ty::ConstKind::Value(val) => {
1055+
// FIXME(mgca): no need to feature-gate once valtree lifetimes are not erased
1056+
if tcx.features().min_generic_const_args()
1057+
&& let ty::Adt(adt_def, args) = val.ty.kind()
1058+
{
1059+
let adt_val = val.destructure_adt_const();
1060+
let variant_def = adt_def.variant(adt_val.variant);
1061+
let cause = self.cause(ObligationCauseCode::WellFormed(None));
1062+
self.out.extend(variant_def.fields.iter().zip(adt_val.fields).map(
1063+
|(field_def, &field_val)| {
1064+
let field_ty = tcx.type_of(field_def.did).instantiate(tcx, args);
1065+
let predicate = ty::PredicateKind::Clause(
1066+
ty::ClauseKind::ConstArgHasType(field_val, field_ty),
1067+
);
1068+
traits::Obligation::with_depth(
1069+
tcx,
1070+
cause.clone(),
1071+
self.recursion_depth,
1072+
self.param_env,
1073+
predicate,
1074+
)
1075+
},
1076+
));
1077+
}
1078+
10551079
// FIXME: Enforce that values are structurally-matchable.
10561080
}
10571081
}

tests/ui/const-generics/mgca/adt_expr_arg_simple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn foo<const N: Option<u32>>() {}
1212

1313
trait Trait {
1414
#[type_const]
15-
const ASSOC: usize;
15+
const ASSOC: u32;
1616
}
1717

1818
fn bar<T: Trait, const N: u32>() {
Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
1-
//@ check-pass
2-
// FIXME(mgca): This should error
3-
41
#![feature(min_generic_const_args, adt_const_params)]
52
#![expect(incomplete_features)]
63

74
#[derive(Eq, PartialEq, std::marker::ConstParamTy)]
8-
struct Foo<T> { field: T }
5+
struct S1<T> {
6+
f1: T,
7+
f2: isize,
8+
}
9+
10+
#[derive(Eq, PartialEq, std::marker::ConstParamTy)]
11+
struct S2<T>(T, isize);
12+
13+
#[derive(Eq, PartialEq, std::marker::ConstParamTy)]
14+
enum En<T> {
15+
Var1(bool, T),
16+
Var2 { field: i64 },
17+
}
918

10-
fn accepts<const N: Foo<u8>>() {}
19+
fn accepts_1<const N: S1<u8>>() {}
20+
fn accepts_2<const N: S2<u8>>() {}
21+
fn accepts_3<const N: En<u8>>() {}
1122

1223
fn bar<const N: bool>() {
13-
// `N` is not of type `u8` but we don't actually check this anywhere yet
14-
accepts::<{ Foo::<u8> { field: N }}>();
24+
accepts_1::<{ S1::<u8> { f1: N, f2: N } }>();
25+
//~^ ERROR the constant `N` is not of type `u8`
26+
//~| ERROR the constant `N` is not of type `isize`
27+
accepts_2::<{ S2::<u8>(N, N) }>();
28+
//~^ ERROR the constant `N` is not of type `u8`
29+
//~| ERROR the constant `N` is not of type `isize`
30+
accepts_3::<{ En::Var1::<u8>(N, N) }>();
31+
//~^ ERROR the constant `N` is not of type `u8`
32+
accepts_3::<{ En::Var2::<u8> { field: N } }>();
33+
//~^ ERROR the constant `N` is not of type `i64`
34+
accepts_3::<{ En::Var2::<u8> { field: const { false } } }>();
35+
//~^ ERROR mismatched types
1536
}
1637

1738
fn main() {}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error: the constant `N` is not of type `u8`
2+
--> $DIR/adt_expr_fields_type_check.rs:24:19
3+
|
4+
LL | accepts_1::<{ S1::<u8> { f1: N, f2: N } }>();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `bool`
6+
7+
error: the constant `N` is not of type `isize`
8+
--> $DIR/adt_expr_fields_type_check.rs:24:19
9+
|
10+
LL | accepts_1::<{ S1::<u8> { f1: N, f2: N } }>();
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `bool`
12+
13+
error: the constant `N` is not of type `u8`
14+
--> $DIR/adt_expr_fields_type_check.rs:27:19
15+
|
16+
LL | accepts_2::<{ S2::<u8>(N, N) }>();
17+
| ^^^^^^^^^^^^^^ expected `u8`, found `bool`
18+
19+
error: the constant `N` is not of type `isize`
20+
--> $DIR/adt_expr_fields_type_check.rs:27:19
21+
|
22+
LL | accepts_2::<{ S2::<u8>(N, N) }>();
23+
| ^^^^^^^^^^^^^^ expected `isize`, found `bool`
24+
25+
error: the constant `N` is not of type `u8`
26+
--> $DIR/adt_expr_fields_type_check.rs:30:19
27+
|
28+
LL | accepts_3::<{ En::Var1::<u8>(N, N) }>();
29+
| ^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `bool`
30+
31+
error: the constant `N` is not of type `i64`
32+
--> $DIR/adt_expr_fields_type_check.rs:32:19
33+
|
34+
LL | accepts_3::<{ En::Var2::<u8> { field: N } }>();
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `i64`, found `bool`
36+
37+
error[E0308]: mismatched types
38+
--> $DIR/adt_expr_fields_type_check.rs:34:51
39+
|
40+
LL | accepts_3::<{ En::Var2::<u8> { field: const { false } } }>();
41+
| ^^^^^ expected `i64`, found `bool`
42+
43+
error: aborting due to 7 previous errors
44+
45+
For more information about this error, try `rustc --explain E0308`.

tests/ui/const-generics/mgca/printing_valtrees_supports_non_values.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct Foo;
99

1010
trait Trait {
1111
#[type_const]
12-
const ASSOC: usize;
12+
const ASSOC: u32;
1313
}
1414

1515
fn foo<const N: Foo>() {}
@@ -27,7 +27,7 @@ fn baz<T: Trait>() {
2727
fn main() {}
2828

2929
fn test_ice_missing_bound<T>() {
30-
foo::<{Option::Some::<u32>{0: <T as Trait>::ASSOC}}>();
30+
foo::<{ Option::Some::<u32> { 0: <T as Trait>::ASSOC } }>();
3131
//~^ ERROR the trait bound `T: Trait` is not satisfied
3232
//~| ERROR the constant `Option::<u32>::Some(_)` is not of type `Foo`
3333
}

tests/ui/const-generics/mgca/printing_valtrees_supports_non_values.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ LL | fn foo<const N: Foo>() {}
2525
error[E0277]: the trait bound `T: Trait` is not satisfied
2626
--> $DIR/printing_valtrees_supports_non_values.rs:30:5
2727
|
28-
LL | foo::<{Option::Some::<u32>{0: <T as Trait>::ASSOC}}>();
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `T`
28+
LL | foo::<{ Option::Some::<u32> { 0: <T as Trait>::ASSOC } }>();
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `T`
3030
|
3131
help: consider restricting type parameter `T` with trait `Trait`
3232
|
3333
LL | fn test_ice_missing_bound<T: Trait>() {
3434
| +++++++
3535

3636
error: the constant `Option::<u32>::Some(_)` is not of type `Foo`
37-
--> $DIR/printing_valtrees_supports_non_values.rs:30:12
37+
--> $DIR/printing_valtrees_supports_non_values.rs:30:13
3838
|
39-
LL | foo::<{Option::Some::<u32>{0: <T as Trait>::ASSOC}}>();
40-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Foo`, found `Option<u32>`
39+
LL | foo::<{ Option::Some::<u32> { 0: <T as Trait>::ASSOC } }>();
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Foo`, found `Option<u32>`
4141
|
4242
note: required by a const generic parameter in `foo`
4343
--> $DIR/printing_valtrees_supports_non_values.rs:15:8

0 commit comments

Comments
 (0)