Skip to content

Commit 5eac9c0

Browse files
committed
Excercise const trait interaction with default fields
Add a test case for using the result of a fn call of an associated function of a `const` trait in a struct default field. ```rust struct X; trait Trait { fn value() -> Self; } impl const Trait for X { fn value() -> Self { X } } struct S<T: const Trait> { a: T = T::value(), } ```
1 parent 33c245b commit 5eac9c0

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

tests/ui/structs/default-field-values-support.rs

+30-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ run-pass
22
//@ aux-build:struct_field_default.rs
3-
#![feature(default_field_values, generic_const_exprs)]
3+
#![feature(const_trait_impl, default_field_values, generic_const_exprs)]
44
#![allow(unused_variables, dead_code, incomplete_features)]
55

66
extern crate struct_field_default as xc;
@@ -22,25 +22,35 @@ pub enum Bar {
2222
}
2323
}
2424

25-
#[derive(Default)]
26-
pub struct Qux<A, const C: i32> {
27-
bar: S = Qux::<A, C>::S,
25+
#[const_trait] pub trait ConstDefault {
26+
fn value() -> Self;
27+
}
28+
29+
impl const ConstDefault for i32 {
30+
fn value() -> i32 {
31+
101
32+
}
33+
}
34+
35+
pub struct Qux<A, const C: i32, X: const ConstDefault> {
36+
bar: S = Qux::<A, C, X>::S,
2837
baz: i32 = foo(),
29-
bat: i32 = <Qux<A, C> as T>::K,
38+
bat: i32 = <Qux<A, C, X> as T>::K,
3039
baq: i32 = Self::K,
3140
bay: i32 = C,
3241
bak: Vec<A> = Vec::new(),
42+
ban: X = X::value(),
3343
}
3444

35-
impl<A, const C: i32> Qux<A, C> {
45+
impl<A, const C: i32, X: const ConstDefault> Qux<A, C, X> {
3646
const S: S = S;
3747
}
3848

3949
trait T {
4050
const K: i32;
4151
}
4252

43-
impl<A, const C: i32> T for Qux<A, C> {
53+
impl<A, const C: i32, X: const ConstDefault> T for Qux<A, C, X> {
4454
const K: i32 = 2;
4555
}
4656

@@ -65,8 +75,19 @@ fn main () {
6575
assert!(matches!(Bar::Foo { bar: S, baz: 45 }, y));
6676
assert!(matches!(Bar::Foo { bar: S, baz: 1 }, z));
6777

68-
let x = Qux::<i32, 4> { .. };
69-
assert!(matches!(Qux::<i32, 4> { bar: S, baz: 42, bat: 2, baq: 2, bay: 4, .. }, x));
78+
let x = Qux::<i32, 4, i32> { .. };
79+
assert!(matches!(
80+
Qux::<i32, 4, i32> {
81+
bar: S,
82+
baz: 42,
83+
bat: 2,
84+
baq: 2,
85+
bay: 4,
86+
ban: 101,
87+
..
88+
},
89+
x,
90+
));
7091
assert!(x.bak.is_empty());
7192

7293
let x = xc::A { .. };

0 commit comments

Comments
 (0)